/ Check-in [6723add4]
Login

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

Overview
Comment:Merge all the latest trunk changes into the apple-osx branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 6723add4f492c30b20db8de6ed818c191dedea40
User & Date: drh 2012-08-25 01:38:39
Context
2012-08-31
14:10
Merge in latest trunk changes. check-in: bc9b9cd0 user: dan tags: apple-osx
2012-08-25
01:38
Merge all the latest trunk changes into the apple-osx branch. check-in: 6723add4 user: drh tags: apple-osx
2012-08-24
23:56
When the same index is used for all OR-terms in a WHERE clause, then try to use that index as a covering index. check-in: 62678be3 user: drh tags: trunk
2012-07-17
17:46
Cherrypick [8c9ee1d78f] and [e416359633] from trunk: Ensure that there is always at least one aReadMark slot usable by an unprivileged reader while a checkpoint is running. Also, if one or more transactions are recovered from a log file, initialize one of the aReadMark slots to contain mxFrame as part of the recovery process. check-in: 65035912 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   168    168            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
   169    169            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   170    170            fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   171    171            func.lo global.lo hash.lo \
   172    172            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
   173    173            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   174    174            memjournal.lo \
   175         -         mutex.lo mutex_noop.lo mutex_os2.lo mutex_unix.lo mutex_w32.lo \
   176         -         notify.lo opcodes.lo os.lo os_os2.lo os_unix.lo os_win.lo \
          175  +         mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
          176  +         notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
   177    177            pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   178    178            random.lo resolve.lo rowset.lo rtree.lo select.lo status.lo \
   179    179            table.lo tokenize.lo trigger.lo \
   180    180            update.lo util.lo vacuum.lo \
   181    181            vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
   182    182            vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo
   183    183   
................................................................................
   228    228     $(TOP)/src/mem2.c \
   229    229     $(TOP)/src/mem3.c \
   230    230     $(TOP)/src/mem5.c \
   231    231     $(TOP)/src/memjournal.c \
   232    232     $(TOP)/src/mutex.c \
   233    233     $(TOP)/src/mutex.h \
   234    234     $(TOP)/src/mutex_noop.c \
   235         -  $(TOP)/src/mutex_os2.c \
   236    235     $(TOP)/src/mutex_unix.c \
   237    236     $(TOP)/src/mutex_w32.c \
   238    237     $(TOP)/src/notify.c \
   239    238     $(TOP)/src/os.c \
   240    239     $(TOP)/src/os.h \
   241    240     $(TOP)/src/os_common.h \
   242         -  $(TOP)/src/os_os2.c \
   243    241     $(TOP)/src/os_unix.c \
   244    242     $(TOP)/src/os_win.c \
   245    243     $(TOP)/src/pager.c \
   246    244     $(TOP)/src/pager.h \
   247    245     $(TOP)/src/parse.y \
   248    246     $(TOP)/src/pcache.c \
   249    247     $(TOP)/src/pcache.h \
................................................................................
   401    399     $(TOP)/src/date.c \
   402    400     $(TOP)/src/expr.c \
   403    401     $(TOP)/src/func.c \
   404    402     $(TOP)/src/insert.c \
   405    403     $(TOP)/src/wal.c \
   406    404     $(TOP)/src/mem5.c \
   407    405     $(TOP)/src/os.c \
   408         -  $(TOP)/src/os_os2.c \
   409    406     $(TOP)/src/os_unix.c \
   410    407     $(TOP)/src/os_win.c \
   411    408     $(TOP)/src/pager.c \
   412    409     $(TOP)/src/pragma.c \
   413    410     $(TOP)/src/prepare.c \
   414    411     $(TOP)/src/printf.c \
   415    412     $(TOP)/src/random.c \
................................................................................
   666    663   
   667    664   mutex.lo:	$(TOP)/src/mutex.c $(HDR)
   668    665   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex.c
   669    666   
   670    667   mutex_noop.lo:	$(TOP)/src/mutex_noop.c $(HDR)
   671    668   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex_noop.c
   672    669   
   673         -mutex_os2.lo:	$(TOP)/src/mutex_os2.c $(HDR)
   674         -	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex_os2.c
   675         -
   676    670   mutex_unix.lo:	$(TOP)/src/mutex_unix.c $(HDR)
   677    671   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex_unix.c
   678    672   
   679    673   mutex_w32.lo:	$(TOP)/src/mutex_w32.c $(HDR)
   680    674   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex_w32.c
   681    675   
   682    676   notify.lo:	$(TOP)/src/notify.c $(HDR)
................................................................................
   696    690   
   697    691   os_unix.lo:	$(TOP)/src/os_unix.c $(HDR)
   698    692   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os_unix.c
   699    693   
   700    694   os_win.lo:	$(TOP)/src/os_win.c $(HDR)
   701    695   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os_win.c
   702    696   
   703         -os_os2.lo:	$(TOP)/src/os_os2.c $(HDR)
   704         -	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os_os2.c
   705         -
   706    697   pragma.lo:	$(TOP)/src/pragma.c $(HDR)
   707    698   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pragma.c
   708    699   
   709    700   prepare.lo:	$(TOP)/src/prepare.c $(HDR)
   710    701   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/prepare.c
   711    702   
   712    703   printf.lo:	$(TOP)/src/printf.c $(HDR)
................................................................................
   868    859   
   869    860   fts3_tokenizer.lo:	$(TOP)/ext/fts3/fts3_tokenizer.c $(HDR) $(EXTHDR)
   870    861   	$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenizer.c
   871    862   
   872    863   fts3_tokenizer1.lo:	$(TOP)/ext/fts3/fts3_tokenizer1.c $(HDR) $(EXTHDR)
   873    864   	$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenizer1.c
   874    865   
          866  +fts3_unicode.lo:	$(TOP)/ext/fts3/fts3_unicode.c $(HDR) $(EXTHDR)
          867  +	$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode.c
          868  +
          869  +fts3_unicode2.lo:	$(TOP)/ext/fts3/fts3_unicode2.c $(HDR) $(EXTHDR)
          870  +	$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode2.c
          871  +
   875    872   fts3_write.lo:	$(TOP)/ext/fts3/fts3_write.c $(HDR) $(EXTHDR)
   876    873   	$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_write.c
   877    874   
   878    875   rtree.lo:	$(TOP)/ext/rtree/rtree.c $(HDR) $(EXTHDR)
   879    876   	$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/rtree/rtree.c
   880    877   
   881    878   
................................................................................
   953    950   	rm -rf quota2a quota2b quota2c
   954    951   	rm -rf tsrc .target_source
   955    952   	rm -f tclsqlite3$(TEXE)
   956    953   	rm -f testfixture$(TEXE) test.db
   957    954   	rm -f sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
   958    955   	rm -f sqlite3.c
   959    956   	rm -f sqlite3_analyzer$(TEXE) sqlite3_analyzer.c
          957  +	rm -f sqlite-output.vsix
   960    958   
   961    959   distclean:	clean
   962    960   	rm -f config.log config.status libtool Makefile sqlite3.pc
   963    961   
   964    962   #
   965    963   # Windows section
   966    964   #

Changes to Makefile.msc.

    10     10   # Set this non-0 to create and use the SQLite amalgamation file.
    11     11   #
    12     12   USE_AMALGAMATION = 1
    13     13   
    14     14   # Set this non-0 to use the International Components for Unicode (ICU).
    15     15   #
    16     16   USE_ICU = 0
           17  +
           18  +# Set this non-0 to dynamically link to the MSVC runtime library.
           19  +#
           20  +USE_CRT_DLL = 0
           21  +
           22  +# Set this non-0 to use the native libraries paths for cross-compiling
           23  +# the command line tools needed during the compilation process.
           24  +#
           25  +USE_NATIVE_LIBPATHS = 0
    17     26   
    18     27   # Set this non-0 to compile binaries suitable for the WinRT environment.
    19     28   # This setting does not apply to any binaries that require Tcl to operate
    20     29   # properly (i.e. the text fixture, etc).
    21     30   #
    22     31   FOR_WINRT = 0
           32  +
           33  +# Set this non-0 to skip attempting to look for and/or link with the Tcl
           34  +# runtime library.
           35  +#
           36  +NO_TCL = 0
    23     37   
    24     38   # Set this to non-0 to create and use PDBs.
    25     39   #
    26     40   SYMBOLS = 1
           41  +
           42  +# Set this to non-0 to use the SQLite debugging heap subsystem.
           43  +#
           44  +MEMDEBUG = 0
           45  +
           46  +# Set this to non-0 to use the Win32 native heap subsystem.
           47  +#
           48  +WIN32HEAP = 0
    27     49   
    28     50   # Set this to one of the following values to enable various debugging
    29     51   # features.  Each level includes the debugging options from the previous
    30     52   # levels.  Currently, the recognized values for DEBUG are:
    31     53   #
    32     54   # 0 == NDEBUG: Disables assert() and other runtime diagnostics.
    33     55   # 1 == Disables NDEBUG and all optimizations and then enables PDBs.
................................................................................
    34     56   # 2 == SQLITE_DEBUG: Enables various diagnostics messages and code.
    35     57   # 3 == SQLITE_WIN32_MALLOC_VALIDATE: Validate the Win32 native heap per call.
    36     58   # 4 == SQLITE_DEBUG_OS_TRACE: Enables output from the OSTRACE() macros.
    37     59   # 5 == SQLITE_ENABLE_IOTRACE: Enables output from the IOTRACE() macros.
    38     60   #
    39     61   DEBUG = 0
    40     62   
    41         -# C Compiler and options for use in building executables that
           63  +# Check for the predefined command macro CC.  This should point to the compiler
           64  +# binary for the target platform.  If it is not defined, simply define it to
           65  +# the legacy default value 'cl.exe'.
           66  +#
           67  +!IFNDEF CC
           68  +CC = cl.exe
           69  +!ENDIF
           70  +
           71  +# Check for the command macro LD.  This should point to the linker binary for
           72  +# the target platform.  If it is not defined, simply define it to the legacy
           73  +# default value 'link.exe'.
           74  +#
           75  +!IFNDEF LD
           76  +LD = link.exe
           77  +!ENDIF
           78  +
           79  +# Check for the command macro NCC.  This should point to the compiler binary
           80  +# for the platform the compilation process is taking place on.  If it is not
           81  +# defined, simply define it to have the same value as the CC macro.  When
           82  +# cross-compiling, it is suggested that this macro be modified via the command
           83  +# line (since nmake itself does not provide a built-in method to guess it).
           84  +# For example, to use the x86 compiler when cross-compiling for x64, a command
           85  +# line similar to the following could be used (all on one line):
           86  +#
           87  +#     nmake /f Makefile.msc sqlite3.dll
           88  +#           "NCC=""%VCINSTALLDIR%\bin\cl.exe"""
           89  +#           USE_NATIVE_LIBPATHS=1
           90  +#
           91  +!IFDEF NCC
           92  +NCC = $(NCC:\\=\)
           93  +!ELSE
           94  +NCC = $(CC)
           95  +!ENDIF
           96  +
           97  +# Check for the MSVC runtime library path macro.  Othertise, this
           98  +# value will default to the 'lib' directory underneath the MSVC
           99  +# installation directory.
          100  +#
          101  +!IFNDEF NCRTLIBPATH
          102  +NCRTLIBPATH = $(VCINSTALLDIR)\lib
          103  +!ENDIF
          104  +
          105  +NCRTLIBPATH = $(NCRTLIBPATH:\\=\)
          106  +
          107  +# Check for the Platform SDK library path macro.  Othertise, this
          108  +# value will default to the 'lib' directory underneath the Windows
          109  +# SDK installation directory (the environment variable used appears
          110  +# to be available when using Visual C++ 2008 or later via the
          111  +# command line).
          112  +#
          113  +!IFNDEF NSDKLIBPATH
          114  +NSDKLIBPATH = $(WINDOWSSDKDIR)\lib
          115  +!ENDIF
          116  +
          117  +NSDKLIBPATH = $(NSDKLIBPATH:\\=\)
          118  +
          119  +# C compiler and options for use in building executables that
    42    120   # will run on the platform that is doing the build.
    43    121   #
    44         -BCC = cl.exe -W3
          122  +BCC = $(NCC) -W3
    45    123   
    46         -# C Compile and options for use in building executables that
          124  +# Check if the native library paths should be used when compiling
          125  +# the command line tools used during the compilation process.  If
          126  +# so, set the necessary macro now.
          127  +#
          128  +!IF $(USE_NATIVE_LIBPATHS)!=0
          129  +NLTLIBPATHS = "/LIBPATH:$(NCRTLIBPATH)" "/LIBPATH:$(NSDKLIBPATH)"
          130  +!ENDIF
          131  +
          132  +# C compiler and options for use in building executables that
    47    133   # will run on the target platform.  (BCC and TCC are usually the
    48    134   # same unless your are cross-compiling.)
    49    135   #
    50         -TCC = cl.exe -W3 -DSQLITE_OS_WIN=1 -I. -I$(TOP)\src -fp:precise
          136  +TCC = $(CC) -W3 -DSQLITE_OS_WIN=1 -I. -I$(TOP)\src -fp:precise
    51    137   
    52    138   # When compiling the library for use in the WinRT environment,
    53    139   # the following compile-time options must be used as well to
    54    140   # disable use of Win32 APIs that are not available and to enable
    55    141   # use of Win32 APIs that are specific to Windows 8 and/or WinRT.
    56         -# Also, we need to dynamically link to the MSVC runtime when
    57         -# compiling for WinRT.
    58    142   #
    59    143   !IF $(FOR_WINRT)!=0
    60         -TCC = $(TCC) -DSQLITE_OS_WINRT=1 -MD
          144  +TCC = $(TCC) -DSQLITE_OS_WINRT=1
          145  +TCC = $(TCC) -DWINAPI_FAMILY=WINAPI_PARTITION_APP
          146  +!ENDIF
          147  +
          148  +# Also, we need to dynamically link to the correct MSVC runtime
          149  +# when compiling for WinRT (e.g. debug or release) OR if the
          150  +# USE_CRT_DLL option is set to force dynamically linking to the
          151  +# MSVC runtime library.
          152  +#
          153  +!IF $(FOR_WINRT)!=0 || $(USE_CRT_DLL)!=0
          154  +!IF $(DEBUG)>0
          155  +TCC = $(TCC) -MDd
          156  +!ELSE
          157  +TCC = $(TCC) -MD
          158  +!ENDIF
          159  +!ELSE
          160  +!IF $(DEBUG)>0
          161  +TCC = $(TCC) -MTd
          162  +!ELSE
          163  +TCC = $(TCC) -MT
          164  +!ENDIF
    61    165   !ENDIF
    62    166   
    63    167   # The mksqlite3c.tcl and mksqlite3h.tcl scripts will pull in
    64    168   # any extension header files by default.  For non-amalgamation
    65    169   # builds, we need to make sure the compiler can find these.
    66    170   #
    67    171   !IF $(USE_AMALGAMATION)==0
................................................................................
    87    191   !ENDIF
    88    192   
    89    193   !IF $(DEBUG)>4
    90    194   TCC = $(TCC) -DSQLITE_ENABLE_IOTRACE
    91    195   !ENDIF
    92    196   
    93    197   #
    94         -# Prevent warnings about "insecure" runtime library functions being used.
          198  +# Prevent warnings about "insecure" MSVC runtime library functions
          199  +# being used.
    95    200   #
    96    201   TCC = $(TCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS
    97    202   BCC = $(BCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS
    98    203   
    99    204   #
   100         -# Use native Win32 heap instead of malloc/free?
          205  +# Prevent warnings about "deprecated" POSIX functions being used.
   101    206   #
   102         -# TCC = $(TCC) -DSQLITE_WIN32_MALLOC=1
          207  +TCC = $(TCC) -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS
          208  +BCC = $(BCC) -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS
          209  +
          210  +#
          211  +# Use the SQLite debugging heap subsystem?
          212  +#
          213  +!IF $(MEMDEBUG)!=0
          214  +TCC = $(TCC) -DSQLITE_MEMDEBUG=1
          215  +
          216  +#
          217  +# Use native Win32 heap subsystem instead of malloc/free?
          218  +#
          219  +!ELSEIF $(WIN32HEAP)!=0
          220  +TCC = $(TCC) -DSQLITE_WIN32_MALLOC=1
   103    221   
   104    222   #
   105    223   # Validate the heap on every call into the native Win32 heap subsystem?
   106    224   #
   107    225   !IF $(DEBUG)>2
   108    226   TCC = $(TCC) -DSQLITE_WIN32_MALLOC_VALIDATE=1
          227  +!ENDIF
   109    228   !ENDIF
   110    229   
   111    230   # The locations of the Tcl header and library files.  Also, the library that
   112    231   # non-stubs enabled programs using Tcl must link against.  These variables
   113    232   # (TCLINCDIR, TCLLIBDIR, and LIBTCL) may be overridden via the environment
   114    233   # prior to running nmake in order to match the actual installed location and
   115    234   # version on this machine.
   116    235   #
   117         -!if "$(TCLINCDIR)" == ""
          236  +!IFNDEF TCLINCDIR
   118    237   TCLINCDIR = c:\tcl\include
   119         -!endif
          238  +!ENDIF
   120    239   
   121         -!if "$(TCLLIBDIR)" == ""
          240  +!IFNDEF TCLLIBDIR
   122    241   TCLLIBDIR = c:\tcl\lib
   123         -!endif
          242  +!ENDIF
   124    243   
   125         -!if "$(LIBTCL)" == ""
          244  +!IFNDEF LIBTCL
   126    245   LIBTCL = tcl85.lib
   127         -!endif
          246  +!ENDIF
   128    247   
   129    248   # The locations of the ICU header and library files.  These variables
   130    249   # (ICUINCDIR, ICULIBDIR, and LIBICU) may be overridden via the environment
   131    250   # prior to running nmake in order to match the actual installed location on
   132    251   # this machine.
   133    252   #
   134         -!if "$(ICUINCDIR)" == ""
          253  +!IFNDEF ICUINCDIR
   135    254   ICUINCDIR = c:\icu\include
   136         -!endif
          255  +!ENDIF
   137    256   
   138         -!if "$(ICULIBDIR)" == ""
          257  +!IFNDEF ICULIBDIR
   139    258   ICULIBDIR = c:\icu\lib
   140         -!endif
          259  +!ENDIF
   141    260   
   142         -!if "$(LIBICU)" == ""
          261  +!IFNDEF LIBICU
   143    262   LIBICU = icuuc.lib icuin.lib
   144         -!endif
          263  +!ENDIF
   145    264   
   146    265   # This is the command to use for tclsh - normally just "tclsh", but we may
   147    266   # know the specific version we want to use.  This variable (TCLSH_CMD) may be
   148    267   # overridden via the environment prior to running nmake in order to select a
   149    268   # specific Tcl shell to use.
   150    269   #
   151         -!if "$(TCLSH_CMD)" == ""
          270  +!IFNDEF TCLSH_CMD
   152    271   TCLSH_CMD = tclsh85
   153         -!endif
          272  +!ENDIF
   154    273   
   155    274   # Compiler options needed for programs that use the readline() library.
   156    275   #
   157    276   READLINE_FLAGS = -DHAVE_READLINE=0
   158    277   
   159    278   # The library that programs using readline() must link against.
   160    279   #
................................................................................
   166    285   
   167    286   # Do threads override each others locks by default (1), or do we test (-1)
   168    287   #
   169    288   TCC = $(TCC) -DSQLITE_THREAD_OVERRIDE_LOCK=-1
   170    289   
   171    290   # Any target libraries which libsqlite must be linked against
   172    291   #
   173         -!if "$(TLIBS)" == ""
          292  +!IFNDEF TLIBS
   174    293   TLIBS =
   175         -!endif
          294  +!ENDIF
   176    295   
   177    296   # Flags controlling use of the in memory btree implementation
   178    297   #
   179    298   # SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to
   180    299   # default to file, 2 to default to memory, and 3 to force temporary
   181    300   # tables to always be in memory.
   182    301   #
................................................................................
   230    349   LTLIB = lib.exe
   231    350   LTLINK = $(TCC) -Fe$@
   232    351   
   233    352   # If a platform was set, force the linker to target that.
   234    353   # Note that the vcvars*.bat family of batch files typically
   235    354   # set this for you.  Otherwise, the linker will attempt
   236    355   # to deduce the binary type based on the object files.
   237         -!IF "$(PLATFORM)"!=""
          356  +!IFDEF PLATFORM
   238    357   LTLINKOPTS = /MACHINE:$(PLATFORM)
   239    358   LTLIBOPTS = /MACHINE:$(PLATFORM)
   240    359   !ENDIF
   241    360   
   242    361   # When compiling for use in the WinRT environment, the following
   243    362   # linker option must be used to mark the executable as runnable
   244    363   # only in the context of an application container.
................................................................................
   245    364   #
   246    365   !IF $(FOR_WINRT)!=0
   247    366   LTLINKOPTS = $(LTLINKOPTS) /APPCONTAINER
   248    367   !ENDIF
   249    368   
   250    369   # If either debugging or symbols are enabled, enable PDBs.
   251    370   !IF $(DEBUG)>0 || $(SYMBOLS)!=0
   252         -LTLINKOPTS = $(LTLINKOPTS) /DEBUG
   253         -BCC = $(BCC) /DEBUG
          371  +LDFLAGS = /DEBUG
   254    372   !ENDIF
   255    373   
   256    374   # Start with the Tcl related linker options.
          375  +!IF $(NO_TCL)==0
   257    376   LTLIBPATHS = /LIBPATH:$(TCLLIBDIR)
   258    377   LTLIBS = $(LIBTCL)
          378  +!ENDIF
   259    379   
   260    380   # If ICU support is enabled, add the linker options for it.
   261    381   !IF $(USE_ICU)!=0
   262    382   LTLIBPATHS = $(LTLIBPATHS) /LIBPATH:$(ICULIBDIR)
   263    383   LTLIBS = $(LTLIBS) $(LIBICU)
   264    384   !ENDIF
   265    385   
................................................................................
   278    398            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
   279    399            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   280    400            fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   281    401            func.lo global.lo hash.lo \
   282    402            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
   283    403            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   284    404            memjournal.lo \
   285         -         mutex.lo mutex_noop.lo mutex_os2.lo mutex_unix.lo mutex_w32.lo \
   286         -         notify.lo opcodes.lo os.lo os_os2.lo os_unix.lo os_win.lo \
          405  +         mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
          406  +         notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
   287    407            pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   288    408            random.lo resolve.lo rowset.lo rtree.lo select.lo status.lo \
   289    409            table.lo tokenize.lo trigger.lo \
   290    410            update.lo util.lo vacuum.lo \
   291    411            vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
   292    412            vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo
   293    413   
................................................................................
   341    461     $(TOP)\src\mem2.c \
   342    462     $(TOP)\src\mem3.c \
   343    463     $(TOP)\src\mem5.c \
   344    464     $(TOP)\src\memjournal.c \
   345    465     $(TOP)\src\mutex.c \
   346    466     $(TOP)\src\mutex.h \
   347    467     $(TOP)\src\mutex_noop.c \
   348         -  $(TOP)\src\mutex_os2.c \
   349    468     $(TOP)\src\mutex_unix.c \
   350    469     $(TOP)\src\mutex_w32.c \
   351    470     $(TOP)\src\notify.c \
   352    471     $(TOP)\src\os.c \
   353    472     $(TOP)\src\os.h \
   354    473     $(TOP)\src\os_common.h \
   355         -  $(TOP)\src\os_os2.c \
   356    474     $(TOP)\src\os_unix.c \
   357    475     $(TOP)\src\os_win.c \
   358    476     $(TOP)\src\pager.c \
   359    477     $(TOP)\src\pager.h \
   360    478     $(TOP)\src\parse.y \
   361    479     $(TOP)\src\pcache.c \
   362    480     $(TOP)\src\pcache.h \
................................................................................
   510    628     $(TOP)\src\date.c \
   511    629     $(TOP)\src\expr.c \
   512    630     $(TOP)\src\func.c \
   513    631     $(TOP)\src\insert.c \
   514    632     $(TOP)\src\wal.c \
   515    633     $(TOP)\src\mem5.c \
   516    634     $(TOP)\src\os.c \
   517         -  $(TOP)\src\os_os2.c \
   518    635     $(TOP)\src\os_unix.c \
   519    636     $(TOP)\src\os_win.c \
   520    637     $(TOP)\src\pager.c \
   521    638     $(TOP)\src\pragma.c \
   522    639     $(TOP)\src\prepare.c \
   523    640     $(TOP)\src\printf.c \
   524    641     $(TOP)\src\random.c \
................................................................................
   633    750   
   634    751   # Rules to build the LEMON compiler generator
   635    752   #
   636    753   lempar.c:	$(TOP)\src\lempar.c
   637    754   	copy $(TOP)\src\lempar.c .
   638    755   
   639    756   lemon.exe:	$(TOP)\tool\lemon.c lempar.c
   640         -	$(BCC) -Fe$@ $(TOP)\tool\lemon.c
          757  +	$(BCC) -Daccess=_access -Fe$@ $(TOP)\tool\lemon.c /link $(NLTLIBPATHS)
   641    758   
   642    759   # Rules to build individual *.lo files from generated *.c files. This
   643    760   # applies to:
   644    761   #
   645    762   #     parse.lo
   646    763   #     opcodes.lo
   647    764   #
................................................................................
   751    868   
   752    869   mutex.lo:	$(TOP)\src\mutex.c $(HDR)
   753    870   	$(LTCOMPILE) -c $(TOP)\src\mutex.c
   754    871   
   755    872   mutex_noop.lo:	$(TOP)\src\mutex_noop.c $(HDR)
   756    873   	$(LTCOMPILE) -c $(TOP)\src\mutex_noop.c
   757    874   
   758         -mutex_os2.lo:	$(TOP)\src\mutex_os2.c $(HDR)
   759         -	$(LTCOMPILE) -c $(TOP)\src\mutex_os2.c
   760         -
   761    875   mutex_unix.lo:	$(TOP)\src\mutex_unix.c $(HDR)
   762    876   	$(LTCOMPILE) -c $(TOP)\src\mutex_unix.c
   763    877   
   764    878   mutex_w32.lo:	$(TOP)\src\mutex_w32.c $(HDR)
   765    879   	$(LTCOMPILE) -c $(TOP)\src\mutex_w32.c
   766    880   
   767    881   notify.lo:	$(TOP)\src\notify.c $(HDR)
................................................................................
   781    895   
   782    896   os_unix.lo:	$(TOP)\src\os_unix.c $(HDR)
   783    897   	$(LTCOMPILE) -c $(TOP)\src\os_unix.c
   784    898   
   785    899   os_win.lo:	$(TOP)\src\os_win.c $(HDR)
   786    900   	$(LTCOMPILE) -c $(TOP)\src\os_win.c
   787    901   
   788         -os_os2.lo:	$(TOP)\src\os_os2.c $(HDR)
   789         -	$(LTCOMPILE) -c $(TOP)\src\os_os2.c
   790         -
   791    902   pragma.lo:	$(TOP)\src\pragma.c $(HDR)
   792    903   	$(LTCOMPILE) -c $(TOP)\src\pragma.c
   793    904   
   794    905   prepare.lo:	$(TOP)\src\prepare.c $(HDR)
   795    906   	$(LTCOMPILE) -c $(TOP)\src\prepare.c
   796    907   
   797    908   printf.lo:	$(TOP)\src\printf.c $(HDR)
................................................................................
   895   1006   	move parse.h parse.h.temp
   896   1007   	$(NAWK) -f $(TOP)\addopcodes.awk parse.h.temp > parse.h
   897   1008   
   898   1009   sqlite3.h:	$(TOP)\src\sqlite.h.in $(TOP)\manifest.uuid $(TOP)\VERSION
   899   1010   	$(TCLSH_CMD) $(TOP)\tool\mksqlite3h.tcl $(TOP) > sqlite3.h
   900   1011   
   901   1012   mkkeywordhash.exe:	$(TOP)\tool\mkkeywordhash.c
   902         -	$(BCC) -Femkkeywordhash.exe $(OPT_FEATURE_FLAGS) $(OPTS) $(TOP)\tool\mkkeywordhash.c
         1013  +	$(BCC) -Fe$@ $(OPT_FEATURE_FLAGS) $(OPTS) $(TOP)\tool\mkkeywordhash.c /link $(NLTLIBPATHS)
   903   1014   
   904   1015   keywordhash.h:	$(TOP)\tool\mkkeywordhash.c mkkeywordhash.exe
   905   1016   	.\mkkeywordhash.exe > keywordhash.h
   906   1017   
   907   1018   
   908   1019   
   909   1020   # Rules to build the extension objects.
................................................................................
  1027   1138   	-rmdir /Q/S tsrc
  1028   1139   	del /Q .target_source
  1029   1140   	del /Q tclsqlite3.exe
  1030   1141   	del /Q testfixture.exe testfixture.exp test.db
  1031   1142   	del /Q sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
  1032   1143   	del /Q sqlite3.c
  1033   1144   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.exp sqlite3_analyzer.c
         1145  +	del /Q sqlite-output.vsix
  1034   1146   
  1035   1147   #
  1036   1148   # Windows section
  1037   1149   #
  1038   1150   dll: sqlite3.dll
  1039   1151   
  1040   1152   sqlite3.def: libsqlite3.lib
  1041   1153   	echo EXPORTS > sqlite3.def
  1042   1154   	dumpbin /all libsqlite3.lib \
  1043   1155   		| $(NAWK) "/ 1 _?sqlite3_/ { sub(/^.* _?/,\"\");print }" \
  1044   1156   		| sort >> sqlite3.def
  1045   1157   
  1046   1158   sqlite3.dll: $(LIBOBJ) sqlite3.def
  1047         -	link $(LTLINKOPTS) $(LTLIBPATHS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ) $(LTLIBS) $(TLIBS)
         1159  +	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ) $(LTLIBS) $(TLIBS)

Changes to Makefile.vxworks.

   200    200            callback.o complete.o date.o delete.o expr.o fault.o \
   201    201            fts3.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
   202    202            fts3_tokenizer.o fts3_tokenizer1.o \
   203    203            func.o global.o hash.o \
   204    204            icu.o insert.o journal.o legacy.o loadext.o \
   205    205            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
   206    206            memjournal.o \
   207         -         mutex.o mutex_noop.o mutex_os2.o mutex_unix.o mutex_w32.o \
   208         -         notify.o opcodes.o os.o os_os2.o os_unix.o os_win.o \
          207  +         mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
          208  +         notify.o opcodes.o os.o os_unix.o os_win.o \
   209    209            pager.o parse.o pcache.o pcache1.o pragma.o prepare.o printf.o \
   210    210            random.o resolve.o rowset.o rtree.o select.o status.o \
   211    211            table.o tokenize.o trigger.o \
   212    212            update.o util.o vacuum.o \
   213    213            vdbe.o vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o \
   214    214            walker.o where.o utf.o vtab.o
   215    215   
................................................................................
   252    252     $(TOP)/src/mem2.c \
   253    253     $(TOP)/src/mem3.c \
   254    254     $(TOP)/src/mem5.c \
   255    255     $(TOP)/src/memjournal.c \
   256    256     $(TOP)/src/mutex.c \
   257    257     $(TOP)/src/mutex.h \
   258    258     $(TOP)/src/mutex_noop.c \
   259         -  $(TOP)/src/mutex_os2.c \
   260    259     $(TOP)/src/mutex_unix.c \
   261    260     $(TOP)/src/mutex_w32.c \
   262    261     $(TOP)/src/notify.c \
   263    262     $(TOP)/src/os.c \
   264    263     $(TOP)/src/os.h \
   265    264     $(TOP)/src/os_common.h \
   266         -  $(TOP)/src/os_os2.c \
   267    265     $(TOP)/src/os_unix.c \
   268    266     $(TOP)/src/os_win.c \
   269    267     $(TOP)/src/pager.c \
   270    268     $(TOP)/src/pager.h \
   271    269     $(TOP)/src/parse.y \
   272    270     $(TOP)/src/pcache.c \
   273    271     $(TOP)/src/pcache.h \
................................................................................
   392    390   #TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c
   393    391   #TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c
   394    392   
   395    393   TESTSRC2 = \
   396    394     $(TOP)/src/attach.c $(TOP)/src/backup.c $(TOP)/src/btree.c                   \
   397    395     $(TOP)/src/build.c $(TOP)/src/ctime.c $(TOP)/src/date.c                      \
   398    396     $(TOP)/src/expr.c $(TOP)/src/func.c $(TOP)/src/insert.c $(TOP)/src/os.c      \
   399         -  $(TOP)/src/os_os2.c $(TOP)/src/os_unix.c $(TOP)/src/os_win.c                 \
          397  +  $(TOP)/src/os_unix.c $(TOP)/src/os_win.c                 \
   400    398     $(TOP)/src/pager.c $(TOP)/src/pragma.c $(TOP)/src/prepare.c                  \
   401    399     $(TOP)/src/printf.c $(TOP)/src/random.c $(TOP)/src/pcache.c                  \
   402    400     $(TOP)/src/pcache1.c $(TOP)/src/select.c $(TOP)/src/tokenize.c               \
   403    401     $(TOP)/src/utf.c $(TOP)/src/util.c $(TOP)/src/vdbeapi.c $(TOP)/src/vdbeaux.c \
   404    402     $(TOP)/src/vdbe.c $(TOP)/src/vdbemem.c $(TOP)/src/where.c parse.c            \
   405    403     $(TOP)/ext/fts3/fts3.c $(TOP)/ext/fts3/fts3_expr.c                           \
   406    404     $(TOP)/ext/fts3/fts3_tokenizer.c                                             \

Changes to VERSION.

     1         -3.7.13
            1  +3.7.14

Changes to configure.

     1      1   #! /bin/sh
     2      2   # Guess values for system-dependent variables and create Makefiles.
     3         -# Generated by GNU Autoconf 2.62 for sqlite 3.7.13.
            3  +# Generated by GNU Autoconf 2.62 for sqlite 3.7.14.
     4      4   #
     5      5   # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
     6      6   # 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
     7      7   # This configure script is free software; the Free Software Foundation
     8      8   # gives unlimited permission to copy, distribute and modify it.
     9      9   ## --------------------- ##
    10     10   ## M4sh Initialization.  ##
................................................................................
   739    739   MFLAGS=
   740    740   MAKEFLAGS=
   741    741   SHELL=${CONFIG_SHELL-/bin/sh}
   742    742   
   743    743   # Identity of this package.
   744    744   PACKAGE_NAME='sqlite'
   745    745   PACKAGE_TARNAME='sqlite'
   746         -PACKAGE_VERSION='3.7.13'
   747         -PACKAGE_STRING='sqlite 3.7.13'
          746  +PACKAGE_VERSION='3.7.14'
          747  +PACKAGE_STRING='sqlite 3.7.14'
   748    748   PACKAGE_BUGREPORT=''
   749    749   
   750    750   # Factoring default headers for most tests.
   751    751   ac_includes_default="\
   752    752   #include <stdio.h>
   753    753   #ifdef HAVE_SYS_TYPES_H
   754    754   # include <sys/types.h>
................................................................................
  1481   1481   #
  1482   1482   # Report the --help message.
  1483   1483   #
  1484   1484   if test "$ac_init_help" = "long"; then
  1485   1485     # Omit some internal or obsolete options to make the list less imposing.
  1486   1486     # This message is too long to be a string in the A/UX 3.1 sh.
  1487   1487     cat <<_ACEOF
  1488         -\`configure' configures sqlite 3.7.13 to adapt to many kinds of systems.
         1488  +\`configure' configures sqlite 3.7.14 to adapt to many kinds of systems.
  1489   1489   
  1490   1490   Usage: $0 [OPTION]... [VAR=VALUE]...
  1491   1491   
  1492   1492   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1493   1493   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1494   1494   
  1495   1495   Defaults for the options are specified in brackets.
................................................................................
  1546   1546     --build=BUILD     configure for building on BUILD [guessed]
  1547   1547     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1548   1548   _ACEOF
  1549   1549   fi
  1550   1550   
  1551   1551   if test -n "$ac_init_help"; then
  1552   1552     case $ac_init_help in
  1553         -     short | recursive ) echo "Configuration of sqlite 3.7.13:";;
         1553  +     short | recursive ) echo "Configuration of sqlite 3.7.14:";;
  1554   1554      esac
  1555   1555     cat <<\_ACEOF
  1556   1556   
  1557   1557   Optional Features:
  1558   1558     --disable-option-checking  ignore unrecognized --enable/--with options
  1559   1559     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1560   1560     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1662   1662       cd "$ac_pwd" || { ac_status=$?; break; }
  1663   1663     done
  1664   1664   fi
  1665   1665   
  1666   1666   test -n "$ac_init_help" && exit $ac_status
  1667   1667   if $ac_init_version; then
  1668   1668     cat <<\_ACEOF
  1669         -sqlite configure 3.7.13
         1669  +sqlite configure 3.7.14
  1670   1670   generated by GNU Autoconf 2.62
  1671   1671   
  1672   1672   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
  1673   1673   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  1674   1674   This configure script is free software; the Free Software Foundation
  1675   1675   gives unlimited permission to copy, distribute and modify it.
  1676   1676   _ACEOF
  1677   1677     exit
  1678   1678   fi
  1679   1679   cat >config.log <<_ACEOF
  1680   1680   This file contains any messages produced by compilers while
  1681   1681   running configure, to aid debugging if configure makes a mistake.
  1682   1682   
  1683         -It was created by sqlite $as_me 3.7.13, which was
         1683  +It was created by sqlite $as_me 3.7.14, which was
  1684   1684   generated by GNU Autoconf 2.62.  Invocation command line was
  1685   1685   
  1686   1686     $ $0 $@
  1687   1687   
  1688   1688   _ACEOF
  1689   1689   exec 5>>config.log
  1690   1690   {
................................................................................
 12774  12774           { { $as_echo "$as_me:$LINENO: error: ${with_tclconfig} directory doesn't contain tclConfig.sh" >&5
 12775  12775   $as_echo "$as_me: error: ${with_tclconfig} directory doesn't contain tclConfig.sh" >&2;}
 12776  12776      { (exit 1); exit 1; }; }
 12777  12777         fi
 12778  12778       fi
 12779  12779   
 12780  12780       # Start autosearch by asking tclsh
 12781         -    if test x"$cross_compiling" = xno; then
 12782         -      for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}`
 12783         -      do
 12784         -        if test -f "$i/tclConfig.sh" ; then
 12785         -          ac_cv_c_tclconfig="$i"
 12786         -          break
 12787         -        fi
 12788         -      done
        12781  +    if test x"${ac_cv_c_tclconfig}" = x ; then
        12782  +      if test x"$cross_compiling" = xno; then
        12783  +        for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}`
        12784  +        do
        12785  +          if test -f "$i/tclConfig.sh" ; then
        12786  +            ac_cv_c_tclconfig="$i"
        12787  +            break
        12788  +          fi
        12789  +        done
        12790  +      fi
 12789  12791       fi
 12790  12792   
 12791  12793       # then check for a private Tcl installation
 12792  12794       if test x"${ac_cv_c_tclconfig}" = x ; then
 12793  12795         for i in \
 12794  12796               ../tcl \
 12795  12797               `ls -dr ../tcl[8-9].[0-9].[0-9]* 2>/dev/null` \
................................................................................
 14028  14030   
 14029  14031   exec 6>&1
 14030  14032   
 14031  14033   # Save the log message, to keep $[0] and so on meaningful, and to
 14032  14034   # report actual input values of CONFIG_FILES etc. instead of their
 14033  14035   # values after options handling.
 14034  14036   ac_log="
 14035         -This file was extended by sqlite $as_me 3.7.13, which was
        14037  +This file was extended by sqlite $as_me 3.7.14, which was
 14036  14038   generated by GNU Autoconf 2.62.  Invocation command line was
 14037  14039   
 14038  14040     CONFIG_FILES    = $CONFIG_FILES
 14039  14041     CONFIG_HEADERS  = $CONFIG_HEADERS
 14040  14042     CONFIG_LINKS    = $CONFIG_LINKS
 14041  14043     CONFIG_COMMANDS = $CONFIG_COMMANDS
 14042  14044     $ $0 $@
................................................................................
 14081  14083   $config_commands
 14082  14084   
 14083  14085   Report bugs to <bug-autoconf@gnu.org>."
 14084  14086   
 14085  14087   _ACEOF
 14086  14088   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 14087  14089   ac_cs_version="\\
 14088         -sqlite config.status 3.7.13
        14090  +sqlite config.status 3.7.14
 14089  14091   configured by $0, generated by GNU Autoconf 2.62,
 14090  14092     with options \\"`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"
 14091  14093   
 14092  14094   Copyright (C) 2008 Free Software Foundation, Inc.
 14093  14095   This config.status script is free software; the Free Software Foundation
 14094  14096   gives unlimited permission to copy, distribute and modify it."
 14095  14097   

Changes to configure.ac.

   384    384           ac_cv_c_tclconfig=`(cd ${with_tclconfig}; pwd)`
   385    385         else
   386    386           AC_MSG_ERROR([${with_tclconfig} directory doesn't contain tclConfig.sh])
   387    387         fi
   388    388       fi
   389    389   
   390    390       # Start autosearch by asking tclsh
   391         -    if test x"$cross_compiling" = xno; then
   392         -      for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}`
   393         -      do
   394         -        if test -f "$i/tclConfig.sh" ; then
   395         -          ac_cv_c_tclconfig="$i"
   396         -          break
   397         -        fi
   398         -      done
          391  +    if test x"${ac_cv_c_tclconfig}" = x ; then
          392  +      if test x"$cross_compiling" = xno; then
          393  +        for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}`
          394  +        do
          395  +          if test -f "$i/tclConfig.sh" ; then
          396  +            ac_cv_c_tclconfig="$i"
          397  +            break
          398  +          fi
          399  +        done
          400  +      fi
   399    401       fi
   400    402   
   401    403       # then check for a private Tcl installation
   402    404       if test x"${ac_cv_c_tclconfig}" = x ; then
   403    405         for i in \
   404    406               ../tcl \
   405    407               `ls -dr ../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \

Changes to ext/fts3/fts3.c.

  4432   4432     int nToken = 0;
  4433   4433     int nOr = 0;
  4434   4434   
  4435   4435     /* Allocate a MultiSegReader for each token in the expression. */
  4436   4436     fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
  4437   4437   
  4438   4438     /* Determine which, if any, tokens in the expression should be deferred. */
         4439  +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
  4439   4440     if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
  4440   4441       Fts3TokenAndCost *aTC;
  4441   4442       Fts3Expr **apOr;
  4442   4443       aTC = (Fts3TokenAndCost *)sqlite3_malloc(
  4443   4444           sizeof(Fts3TokenAndCost) * nToken
  4444   4445         + sizeof(Fts3Expr *) * nOr * 2
  4445   4446       );
................................................................................
  4462   4463             rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
  4463   4464           }
  4464   4465         }
  4465   4466   
  4466   4467         sqlite3_free(aTC);
  4467   4468       }
  4468   4469     }
         4470  +#endif
  4469   4471   
  4470   4472     fts3EvalStartReaders(pCsr, pCsr->pExpr, 1, &rc);
  4471   4473     return rc;
  4472   4474   }
  4473   4475   
  4474   4476   /*
  4475   4477   ** Invalidate the current position list for phrase pPhrase.
................................................................................
  4845   4847           bHit = (
  4846   4848               fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
  4847   4849            && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
  4848   4850           );
  4849   4851           break;
  4850   4852   
  4851   4853         default: {
         4854  +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
  4852   4855           if( pCsr->pDeferred 
  4853   4856            && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
  4854   4857           ){
  4855   4858             Fts3Phrase *pPhrase = pExpr->pPhrase;
  4856   4859             assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
  4857   4860             if( pExpr->bDeferred ){
  4858   4861               fts3EvalInvalidatePoslist(pPhrase);
  4859   4862             }
  4860   4863             *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
  4861   4864             bHit = (pPhrase->doclist.pList!=0);
  4862   4865             pExpr->iDocid = pCsr->iPrevId;
  4863         -        }else{
         4866  +        }else
         4867  +#endif
         4868  +        {
  4864   4869             bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
  4865   4870           }
  4866   4871           break;
  4867   4872         }
  4868   4873       }
  4869   4874     }
  4870   4875     return bHit;

Changes to ext/fts3/fts3Int.h.

   423    423   int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
   424    424   int sqlite3Fts3ReadLock(Fts3Table *);
   425    425   int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
   426    426   
   427    427   int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
   428    428   int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
   429    429   
          430  +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
   430    431   void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
   431    432   int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
   432    433   int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
   433    434   void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
          435  +int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
          436  +#else
          437  +# define sqlite3Fts3FreeDeferredTokens(x)
          438  +# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
          439  +# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
          440  +# define sqlite3Fts3FreeDeferredDoclists(x)
          441  +# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
          442  +#endif
          443  +
   434    444   void sqlite3Fts3SegmentsClose(Fts3Table *);
   435    445   int sqlite3Fts3MaxLevel(Fts3Table *, int *);
   436    446   
   437    447   /* Special values interpreted by sqlite3SegReaderCursor() */
   438    448   #define FTS3_SEGCURSOR_PENDING        -1
   439    449   #define FTS3_SEGCURSOR_ALL            -2
   440    450   
................................................................................
   535    545       Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
   536    546   int sqlite3Fts3MsrIncrNext(
   537    547       Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
   538    548   int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); 
   539    549   int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
   540    550   int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
   541    551   
   542         -int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
   543         -
   544    552   /* fts3_unicode2.c (functions generated by parsing unicode text files) */
   545    553   #ifdef SQLITE_ENABLE_FTS4_UNICODE61
   546    554   int sqlite3FtsUnicodeFold(int, int);
   547    555   int sqlite3FtsUnicodeIsalnum(int);
   548    556   int sqlite3FtsUnicodeIsdiacritic(int);
   549    557   #endif
   550    558   
   551    559   #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
   552    560   #endif /* _FTSINT_H */

Changes to ext/fts3/fts3_icu.c.

   195    195       iEnd = ubrk_next(pCsr->pIter);
   196    196       if( iEnd==UBRK_DONE ){
   197    197         return SQLITE_DONE;
   198    198       }
   199    199   
   200    200       while( iStart<iEnd ){
   201    201         int iWhite = iStart;
   202         -      U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
          202  +      U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
   203    203         if( u_isspace(c) ){
   204    204           iStart = iWhite;
   205    205         }else{
   206    206           break;
   207    207         }
   208    208       }
   209    209       assert(iStart<=iEnd);

Changes to ext/fts3/fts3_unicode2.c.

   142    142           iLo = iTest+1;
   143    143         }else{
   144    144           iHi = iTest-1;
   145    145         }
   146    146       }
   147    147       assert( aEntry[0]<key );
   148    148       assert( key>=aEntry[iRes] );
   149         -    return (c >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
          149  +    return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
   150    150     }
   151    151     return 1;
   152    152   }
   153    153   
   154    154   
   155    155   /*
   156    156   ** If the argument is a codepoint corresponding to a lowercase letter

Changes to ext/fts3/fts3_write.c.

  5042   5042     }else{
  5043   5043       rc = SQLITE_ERROR;
  5044   5044     }
  5045   5045   
  5046   5046     return rc;
  5047   5047   }
  5048   5048   
         5049  +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
  5049   5050   /*
  5050   5051   ** Delete all cached deferred doclists. Deferred doclists are cached
  5051   5052   ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
  5052   5053   */
  5053   5054   void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
  5054   5055     Fts3DeferredToken *pDef;
  5055   5056     for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
................................................................................
  5179   5180     pCsr->pDeferred = pDeferred;
  5180   5181   
  5181   5182     assert( pToken->pDeferred==0 );
  5182   5183     pToken->pDeferred = pDeferred;
  5183   5184   
  5184   5185     return SQLITE_OK;
  5185   5186   }
         5187  +#endif
  5186   5188   
  5187   5189   /*
  5188   5190   ** SQLite value pRowid contains the rowid of a row that may or may not be
  5189   5191   ** present in the FTS3 table. If it is, delete it and adjust the contents
  5190   5192   ** of subsiduary data structures accordingly.
  5191   5193   */
  5192   5194   static int fts3DeleteByRowid(

Changes to main.mk.

    56     56            fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
    57     57            fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \
    58     58   	 fts3_unicode.o fts3_unicode2.o \
    59     59            fts3_write.o func.o global.o hash.o \
    60     60            icu.o insert.o journal.o legacy.o loadext.o \
    61     61            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
    62     62            memjournal.o \
    63         -         mutex.o mutex_noop.o mutex_os2.o mutex_unix.o mutex_w32.o \
    64         -         notify.o opcodes.o os.o os_os2.o os_unix.o os_win.o \
           63  +         mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
           64  +         notify.o opcodes.o os.o os_unix.o os_win.o \
    65     65            pager.o parse.o pcache.o pcache1.o pragma.o prepare.o printf.o \
    66     66            random.o resolve.o rowset.o rtree.o select.o status.o \
    67     67            table.o tokenize.o trigger.o \
    68     68            update.o util.o vacuum.o \
    69     69            vdbe.o vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
    70     70   	 vdbetrace.o wal.o walker.o where.o utf.o vtab.o
    71     71   
................................................................................
   109    109     $(TOP)/src/mem2.c \
   110    110     $(TOP)/src/mem3.c \
   111    111     $(TOP)/src/mem5.c \
   112    112     $(TOP)/src/memjournal.c \
   113    113     $(TOP)/src/mutex.c \
   114    114     $(TOP)/src/mutex.h \
   115    115     $(TOP)/src/mutex_noop.c \
   116         -  $(TOP)/src/mutex_os2.c \
   117    116     $(TOP)/src/mutex_unix.c \
   118    117     $(TOP)/src/mutex_w32.c \
   119    118     $(TOP)/src/notify.c \
   120    119     $(TOP)/src/os.c \
   121    120     $(TOP)/src/os.h \
   122    121     $(TOP)/src/os_common.h \
   123         -  $(TOP)/src/os_os2.c \
   124    122     $(TOP)/src/os_unix.c \
   125    123     $(TOP)/src/os_win.c \
   126    124     $(TOP)/src/pager.c \
   127    125     $(TOP)/src/pager.h \
   128    126     $(TOP)/src/parse.y \
   129    127     $(TOP)/src/pcache.c \
   130    128     $(TOP)/src/pcache.h \
................................................................................
   280    278     $(TOP)/src/date.c \
   281    279     $(TOP)/src/expr.c \
   282    280     $(TOP)/src/func.c \
   283    281     $(TOP)/src/insert.c \
   284    282     $(TOP)/src/wal.c \
   285    283     $(TOP)/src/mem5.c \
   286    284     $(TOP)/src/os.c \
   287         -  $(TOP)/src/os_os2.c \
   288    285     $(TOP)/src/os_unix.c \
   289    286     $(TOP)/src/os_win.c \
   290    287     $(TOP)/src/pager.c \
   291    288     $(TOP)/src/pragma.c \
   292    289     $(TOP)/src/prepare.c \
   293    290     $(TOP)/src/printf.c \
   294    291     $(TOP)/src/random.c \
................................................................................
   602    599   	rm -f testloadext.dll libtestloadext.so
   603    600   	rm -f amalgamation-testfixture amalgamation-testfixture.exe
   604    601   	rm -f fts3-testfixture fts3-testfixture.exe
   605    602   	rm -f testfixture testfixture.exe
   606    603   	rm -f threadtest3 threadtest3.exe
   607    604   	rm -f sqlite3.c fts?amal.c tclsqlite3.c
   608    605   	rm -f sqlite3_analyzer sqlite3_analyzer.exe sqlite3_analyzer.c
          606  +	rm -f sqlite-output.vsix

Changes to src/analyze.c.

   172    172         ** side-effect of the CREATE TABLE statement is to leave the rootpage 
   173    173         ** of the new table in register pParse->regRoot. This is important 
   174    174         ** because the OpenWrite opcode below will be needing it. */
   175    175         sqlite3NestedParse(pParse,
   176    176             "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
   177    177         );
   178    178         aRoot[i] = pParse->regRoot;
   179         -      aCreateTbl[i] = 1;
          179  +      aCreateTbl[i] = OPFLAG_P2ISREG;
   180    180       }else{
   181    181         /* The table already exists. If zWhere is not NULL, delete all entries 
   182    182         ** associated with the table zWhere. If zWhere is NULL, delete the
   183    183         ** entire contents of the table. */
   184    184         aRoot[i] = pStat->tnum;
   185    185         sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
   186    186         if( zWhere ){
................................................................................
   252    252     int mxSample;
   253    253     int n;
   254    254   
   255    255     UNUSED_PARAMETER(argc);
   256    256     nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
   257    257     mxSample = sqlite3_value_int(argv[1]);
   258    258     n = sizeof(*p) + sizeof(p->a[0])*mxSample;
   259         -  p = sqlite3_malloc( n );
          259  +  p = sqlite3MallocZero( n );
   260    260     if( p==0 ){
   261    261       sqlite3_result_error_nomem(context);
   262    262       return;
   263    263     }
   264         -  memset(p, 0, n);
   265    264     p->a = (struct Stat3Sample*)&p[1];
   266    265     p->nRow = nRow;
   267    266     p->mxSample = mxSample;
   268    267     p->nPSample = p->nRow/(mxSample/3+1) + 1;
   269    268     sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
   270    269     sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
   271    270   }

Changes to src/backup.c.

   160    160       );
   161    161       p = 0;
   162    162     }else {
   163    163       /* Allocate space for a new sqlite3_backup object...
   164    164       ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
   165    165       ** call to sqlite3_backup_init() and is destroyed by a call to
   166    166       ** sqlite3_backup_finish(). */
   167         -    p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
          167  +    p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
   168    168       if( !p ){
   169    169         sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
   170    170       }
   171    171     }
   172    172   
   173    173     /* If the allocation succeeded, populate the new object. */
   174    174     if( p ){
   175         -    memset(p, 0, sizeof(sqlite3_backup));
   176    175       p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
   177    176       p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
   178    177       p->pDestDb = pDestDb;
   179    178       p->pSrcDb = pSrcDb;
   180    179       p->iNext = 1;
   181    180       p->isAttached = 0;
   182    181   
................................................................................
   539    538   }
   540    539   
   541    540   /*
   542    541   ** Release all resources associated with an sqlite3_backup* handle.
   543    542   */
   544    543   int sqlite3_backup_finish(sqlite3_backup *p){
   545    544     sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
   546         -  MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
          545  +  sqlite3 *pSrcDb;                     /* Source database connection */
   547    546     int rc;                              /* Value to return */
   548    547   
   549    548     /* Enter the mutexes */
   550    549     if( p==0 ) return SQLITE_OK;
   551         -  sqlite3_mutex_enter(p->pSrcDb->mutex);
          550  +  pSrcDb = p->pSrcDb;
          551  +  sqlite3_mutex_enter(pSrcDb->mutex);
   552    552     sqlite3BtreeEnter(p->pSrc);
   553         -  MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
   554    553     if( p->pDestDb ){
   555    554       sqlite3_mutex_enter(p->pDestDb->mutex);
   556    555     }
   557    556   
   558    557     /* Detach this backup from the source pager. */
   559    558     if( p->pDestDb ){
   560    559       p->pSrc->nBackup--;
................................................................................
   572    571   
   573    572     /* Set the error code of the destination database handle. */
   574    573     rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
   575    574     sqlite3Error(p->pDestDb, rc, 0);
   576    575   
   577    576     /* Exit the mutexes and free the backup context structure. */
   578    577     if( p->pDestDb ){
   579         -    sqlite3_mutex_leave(p->pDestDb->mutex);
          578  +    sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
   580    579     }
   581    580     sqlite3BtreeLeave(p->pSrc);
   582    581     if( p->pDestDb ){
   583    582       /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
   584    583       ** call to sqlite3_backup_init() and is destroyed by a call to
   585    584       ** sqlite3_backup_finish(). */
   586    585       sqlite3_free(p);
   587    586     }
   588         -  sqlite3_mutex_leave(mutex);
          587  +  sqlite3LeaveMutexAndCloseZombie(pSrcDb);
   589    588     return rc;
   590    589   }
   591    590   
   592    591   /*
   593    592   ** Return the number of pages still to be backed up as of the most recent
   594    593   ** call to sqlite3_backup_step().
   595    594   */

Changes to src/bitvec.c.

   336    336     int rc = -1;
   337    337     int i, nx, pc, op;
   338    338     void *pTmpSpace;
   339    339   
   340    340     /* Allocate the Bitvec to be tested and a linear array of
   341    341     ** bits to act as the reference */
   342    342     pBitvec = sqlite3BitvecCreate( sz );
   343         -  pV = sqlite3_malloc( (sz+7)/8 + 1 );
          343  +  pV = sqlite3MallocZero( (sz+7)/8 + 1 );
   344    344     pTmpSpace = sqlite3_malloc(BITVEC_SZ);
   345    345     if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
   346         -  memset(pV, 0, (sz+7)/8 + 1);
   347    346   
   348    347     /* NULL pBitvec tests */
   349    348     sqlite3BitvecSet(0, 1);
   350    349     sqlite3BitvecClear(0, 1, pTmpSpace);
   351    350   
   352    351     /* Run the program */
   353    352     pc = 0;

Changes to src/btree.c.

  1463   1463           /* Start of free block is off the page */
  1464   1464           return SQLITE_CORRUPT_BKPT; 
  1465   1465         }
  1466   1466         next = get2byte(&data[pc]);
  1467   1467         size = get2byte(&data[pc+2]);
  1468   1468         if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
  1469   1469           /* Free blocks must be in ascending order. And the last byte of
  1470         -	** the free-block must lie on the database page.  */
         1470  +        ** the free-block must lie on the database page.  */
  1471   1471           return SQLITE_CORRUPT_BKPT; 
  1472   1472         }
  1473   1473         nFree = nFree + size;
  1474   1474         pc = next;
  1475   1475       }
  1476   1476   
  1477   1477       /* At this point, nFree contains the sum of the offset to the start
................................................................................
  2658   2658             btreeInvokeBusyHandler(pBt) );
  2659   2659   
  2660   2660     if( rc==SQLITE_OK ){
  2661   2661       if( p->inTrans==TRANS_NONE ){
  2662   2662         pBt->nTransaction++;
  2663   2663   #ifndef SQLITE_OMIT_SHARED_CACHE
  2664   2664         if( p->sharable ){
  2665         -	assert( p->lock.pBtree==p && p->lock.iTable==1 );
         2665  +        assert( p->lock.pBtree==p && p->lock.iTable==1 );
  2666   2666           p->lock.eLock = READ_LOCK;
  2667   2667           p->lock.pNext = pBt->pLock;
  2668   2668           pBt->pLock = &p->lock;
  2669   2669         }
  2670   2670   #endif
  2671   2671       }
  2672   2672       p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
................................................................................
  5949   5949   ** If aOvflSpace is set to a null pointer, this function returns 
  5950   5950   ** SQLITE_NOMEM.
  5951   5951   */
  5952   5952   static int balance_nonroot(
  5953   5953     MemPage *pParent,               /* Parent page of siblings being balanced */
  5954   5954     int iParentIdx,                 /* Index of "the page" in pParent */
  5955   5955     u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
  5956         -  int isRoot                      /* True if pParent is a root-page */
         5956  +  int isRoot,                     /* True if pParent is a root-page */
         5957  +  int bBulk                       /* True if this call is part of a bulk load */
  5957   5958   ){
  5958   5959     BtShared *pBt;               /* The whole database */
  5959   5960     int nCell = 0;               /* Number of cells in apCell[] */
  5960   5961     int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
  5961   5962     int nNew = 0;                /* Number of pages in apNew[] */
  5962   5963     int nOld;                    /* Number of pages in apOld[] */
  5963   5964     int i, j, k;                 /* Loop counters */
................................................................................
  6013   6014     ** way, the remainder of the function does not have to deal with any
  6014   6015     ** overflow cells in the parent page, since if any existed they will
  6015   6016     ** have already been removed.
  6016   6017     */
  6017   6018     i = pParent->nOverflow + pParent->nCell;
  6018   6019     if( i<2 ){
  6019   6020       nxDiv = 0;
  6020         -    nOld = i+1;
  6021   6021     }else{
  6022         -    nOld = 3;
         6022  +    assert( bBulk==0 || bBulk==1 );
  6023   6023       if( iParentIdx==0 ){                 
  6024   6024         nxDiv = 0;
  6025   6025       }else if( iParentIdx==i ){
  6026         -      nxDiv = i-2;
         6026  +      nxDiv = i-2+bBulk;
  6027   6027       }else{
         6028  +      assert( bBulk==0 );
  6028   6029         nxDiv = iParentIdx-1;
  6029   6030       }
  6030         -    i = 2;
         6031  +    i = 2-bBulk;
  6031   6032     }
         6033  +  nOld = i+1;
  6032   6034     if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
  6033   6035       pRight = &pParent->aData[pParent->hdrOffset+8];
  6034   6036     }else{
  6035   6037       pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
  6036   6038     }
  6037   6039     pgno = get4byte(pRight);
  6038   6040     while( 1 ){
................................................................................
  6233   6235       int r;              /* Index of right-most cell in left sibling */
  6234   6236       int d;              /* Index of first cell to the left of right sibling */
  6235   6237   
  6236   6238       r = cntNew[i-1] - 1;
  6237   6239       d = r + 1 - leafData;
  6238   6240       assert( d<nMaxCells );
  6239   6241       assert( r<nMaxCells );
  6240         -    while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
         6242  +    while( szRight==0 
         6243  +       || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) 
         6244  +    ){
  6241   6245         szRight += szCell[d] + 2;
  6242   6246         szLeft -= szCell[r] + 2;
  6243   6247         cntNew[i-1]--;
  6244   6248         r = cntNew[i-1] - 1;
  6245   6249         d = r + 1 - leafData;
  6246   6250       }
  6247   6251       szNew[i] = szRight;
................................................................................
  6280   6284         pNew = apNew[i] = apOld[i];
  6281   6285         apOld[i] = 0;
  6282   6286         rc = sqlite3PagerWrite(pNew->pDbPage);
  6283   6287         nNew++;
  6284   6288         if( rc ) goto balance_cleanup;
  6285   6289       }else{
  6286   6290         assert( i>0 );
  6287         -      rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
         6291  +      rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
  6288   6292         if( rc ) goto balance_cleanup;
  6289   6293         apNew[i] = pNew;
  6290   6294         nNew++;
  6291   6295   
  6292   6296         /* Set the pointer-map entry for the new sibling page. */
  6293   6297         if( ISAUTOVACUUM ){
  6294   6298           ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
................................................................................
  6492   6496       for(i=0; i<nCell; i++){
  6493   6497         int isDivider = 0;
  6494   6498         while( i==iNextOld ){
  6495   6499           /* Cell i is the cell immediately following the last cell on old
  6496   6500           ** sibling page j. If the siblings are not leaf pages of an
  6497   6501           ** intkey b-tree, then cell i was a divider cell. */
  6498   6502           assert( j+1 < ArraySize(apCopy) );
         6503  +        assert( j+1 < nOld );
  6499   6504           pOld = apCopy[++j];
  6500   6505           iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
  6501   6506           if( pOld->nOverflow ){
  6502   6507             nOverflow = pOld->nOverflow;
  6503   6508             iOverflow = i + !leafData + pOld->aiOvfl[0];
  6504   6509           }
  6505   6510           isDivider = !leafData;  
................................................................................
  6730   6735             ** different page). Once this subsequent call to balance_nonroot() 
  6731   6736             ** has completed, it is safe to release the pSpace buffer used by
  6732   6737             ** the previous call, as the overflow cell data will have been 
  6733   6738             ** copied either into the body of a database page or into the new
  6734   6739             ** pSpace buffer passed to the latter call to balance_nonroot().
  6735   6740             */
  6736   6741             u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
  6737         -          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
         6742  +          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
  6738   6743             if( pFree ){
  6739   6744               /* If pFree is not NULL, it points to the pSpace buffer used 
  6740   6745               ** by a previous call to balance_nonroot(). Its contents are
  6741   6746               ** now stored either on real database pages or within the 
  6742   6747               ** new pSpace buffer, so it may be safely freed here. */
  6743   6748               sqlite3PageFree(pFree);
  6744   6749             }
................................................................................
  8317   8322         }
  8318   8323       }
  8319   8324     }
  8320   8325   
  8321   8326     pBt->btsFlags &= ~BTS_NO_WAL;
  8322   8327     return rc;
  8323   8328   }
         8329  +
         8330  +/*
         8331  +** set the mask of hint flags for cursor pCsr. Currently the only valid
         8332  +** values are 0 and BTREE_BULKLOAD.
         8333  +*/
         8334  +void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
         8335  +  assert( mask==BTREE_BULKLOAD || mask==0 );
         8336  +  pCsr->hints = mask;
         8337  +}

Changes to src/btree.h.

   131    131   #define BTREE_FILE_FORMAT         2
   132    132   #define BTREE_DEFAULT_CACHE_SIZE  3
   133    133   #define BTREE_LARGEST_ROOT_PAGE   4
   134    134   #define BTREE_TEXT_ENCODING       5
   135    135   #define BTREE_USER_VERSION        6
   136    136   #define BTREE_INCR_VACUUM         7
   137    137   
          138  +/*
          139  +** Values that may be OR'd together to form the second argument of an
          140  +** sqlite3BtreeCursorHints() call.
          141  +*/
          142  +#define BTREE_BULKLOAD 0x00000001
          143  +
   138    144   int sqlite3BtreeCursor(
   139    145     Btree*,                              /* BTree containing table to open */
   140    146     int iTable,                          /* Index of root page */
   141    147     int wrFlag,                          /* 1 for writing.  0 for read-only */
   142    148     struct KeyInfo*,                     /* First argument to compare function */
   143    149     BtCursor *pCursor                    /* Space to write cursor structure */
   144    150   );
................................................................................
   174    180   
   175    181   char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
   176    182   struct Pager *sqlite3BtreePager(Btree*);
   177    183   
   178    184   int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
   179    185   void sqlite3BtreeCacheOverflow(BtCursor *);
   180    186   void sqlite3BtreeClearCursor(BtCursor *);
   181         -
   182    187   int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
          188  +void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
   183    189   
   184    190   #ifndef NDEBUG
   185    191   int sqlite3BtreeCursorIsValid(BtCursor*);
   186    192   #endif
   187    193   
   188    194   #ifndef SQLITE_OMIT_BTREECOUNT
   189    195   int sqlite3BtreeCount(BtCursor *, i64 *);

Changes to src/btreeInt.h.

   506    506     u8 wrFlag;                /* True if writable */
   507    507     u8 atLast;                /* Cursor pointing to the last entry */
   508    508     u8 validNKey;             /* True if info.nKey is valid */
   509    509     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
   510    510   #ifndef SQLITE_OMIT_INCRBLOB
   511    511     u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
   512    512   #endif
          513  +  u8 hints;                             /* As configured by CursorSetHints() */
   513    514     i16 iPage;                            /* Index of current page in apPage */
   514    515     u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
   515    516     MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
   516    517   };
   517    518   
   518    519   /*
   519    520   ** Potential values for BtCursor.eState.

Changes to src/build.c.

   530    530     /* Delete all indices associated with this table. */
   531    531     for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
   532    532       pNext = pIndex->pNext;
   533    533       assert( pIndex->pSchema==pTable->pSchema );
   534    534       if( !db || db->pnBytesFreed==0 ){
   535    535         char *zName = pIndex->zName; 
   536    536         TESTONLY ( Index *pOld = ) sqlite3HashInsert(
   537         -	  &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
          537  +         &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
   538    538         );
   539    539         assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
   540    540         assert( pOld==pIndex || pOld==0 );
   541    541       }
   542    542       freeIndex(db, pIndex);
   543    543     }
   544    544   
................................................................................
  1577   1577       */
  1578   1578       if( pSelect ){
  1579   1579         SelectDest dest;
  1580   1580         Table *pSelTab;
  1581   1581   
  1582   1582         assert(pParse->nTab==1);
  1583   1583         sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
  1584         -      sqlite3VdbeChangeP5(v, 1);
         1584  +      sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
  1585   1585         pParse->nTab = 2;
  1586   1586         sqlite3SelectDestInit(&dest, SRT_Table, 1);
  1587   1587         sqlite3Select(pParse, pSelect, &dest);
  1588   1588         sqlite3VdbeAddOp1(v, OP_Close, 1);
  1589   1589         if( pParse->nErr==0 ){
  1590   1590           pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
  1591   1591           if( pSelTab==0 ) return;
................................................................................
  2393   2393     }else{
  2394   2394       tnum = pIndex->tnum;
  2395   2395       sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
  2396   2396     }
  2397   2397     pKey = sqlite3IndexKeyinfo(pParse, pIndex);
  2398   2398     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2399   2399                       (char *)pKey, P4_KEYINFO_HANDOFF);
  2400         -  if( memRootPage>=0 ){
  2401         -    sqlite3VdbeChangeP5(v, 1);
  2402         -  }
         2400  +  sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
  2403   2401   
  2404   2402   #ifndef SQLITE_OMIT_MERGE_SORT
  2405   2403     /* Open the sorter cursor if we are to use one. */
  2406   2404     iSorter = pParse->nTab++;
  2407   2405     sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
  2408   2406   #else
  2409   2407     iSorter = iTab;

Changes to src/delete.c.

   367    367       int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
   368    368       int regRowid;                   /* Actual register containing rowids */
   369    369   
   370    370       /* Collect rowids of every row to be deleted.
   371    371       */
   372    372       sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
   373    373       pWInfo = sqlite3WhereBegin(
   374         -        pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK
          374  +        pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
   375    375       );
   376    376       if( pWInfo==0 ) goto delete_from_cleanup;
   377    377       regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0);
   378    378       sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
   379    379       if( db->flags & SQLITE_CountRows ){
   380    380         sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
   381    381       }

Changes to src/expr.c.

  1697   1697           ** table allocated and opened above.
  1698   1698           */
  1699   1699           SelectDest dest;
  1700   1700           ExprList *pEList;
  1701   1701   
  1702   1702           assert( !isRowid );
  1703   1703           sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
  1704         -        dest.affinity = (u8)affinity;
         1704  +        dest.affSdst = (u8)affinity;
  1705   1705           assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
  1706   1706           pExpr->x.pSelect->iLimit = 0;
  1707   1707           if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
  1708   1708             return 0;
  1709   1709           }
  1710   1710           pEList = pExpr->x.pSelect->pEList;
  1711   1711           if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ 
................................................................................
  1790   1790         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
  1791   1791   
  1792   1792         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
  1793   1793         pSel = pExpr->x.pSelect;
  1794   1794         sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
  1795   1795         if( pExpr->op==TK_SELECT ){
  1796   1796           dest.eDest = SRT_Mem;
  1797         -        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
         1797  +        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
  1798   1798           VdbeComment((v, "Init subquery result"));
  1799   1799         }else{
  1800   1800           dest.eDest = SRT_Exists;
  1801         -        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
         1801  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
  1802   1802           VdbeComment((v, "Init EXISTS result"));
  1803   1803         }
  1804   1804         sqlite3ExprDelete(pParse->db, pSel->pLimit);
  1805   1805         pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
  1806   1806                                     &sqlite3IntTokens[1]);
  1807   1807         pSel->iLimit = 0;
  1808   1808         if( sqlite3Select(pParse, pSel, &dest) ){
  1809   1809           return 0;
  1810   1810         }
  1811         -      rReg = dest.iParm;
         1811  +      rReg = dest.iSDParm;
  1812   1812         ExprSetIrreducible(pExpr);
  1813   1813         break;
  1814   1814       }
  1815   1815     }
  1816   1816   
  1817   1817     if( testAddr>=0 ){
  1818   1818       sqlite3VdbeJumpHere(v, testAddr);
................................................................................
  3119   3119       case TK_FUNCTION: {
  3120   3120         ExprList *pFarg;       /* List of function arguments */
  3121   3121         if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
  3122   3122           pFarg = 0;
  3123   3123         }else{
  3124   3124           pFarg = pExpr->x.pList;
  3125   3125         }
  3126         -      sqlite3ExplainPrintf(pOut, "%sFUNCTION:%s(",
  3127         -                           op==TK_AGG_FUNCTION ? "AGG_" : "",
  3128         -                           pExpr->u.zToken);
         3126  +      if( op==TK_AGG_FUNCTION ){
         3127  +        sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
         3128  +                             pExpr->op2, pExpr->u.zToken);
         3129  +      }else{
         3130  +        sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
         3131  +      }
  3129   3132         if( pFarg ){
  3130   3133           sqlite3ExplainExprList(pOut, pFarg);
  3131   3134         }
  3132   3135         sqlite3ExplainPrintf(pOut, ")");
  3133   3136         break;
  3134   3137       }
  3135   3138   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
  3812   3815       if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
  3813   3816       if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
  3814   3817     }
  3815   3818     return 0;
  3816   3819   }
  3817   3820   
  3818   3821   /*
  3819         -** This is the expression callback for sqlite3FunctionUsesOtherSrc().
  3820         -**
  3821         -** Determine if an expression references any table other than one of the
  3822         -** tables in pWalker->u.pSrcList and abort if it does.
         3822  +** An instance of the following structure is used by the tree walker
         3823  +** to count references to table columns in the arguments of an 
         3824  +** aggregate function, in order to implement the
         3825  +** sqlite3FunctionThisSrc() routine.
         3826  +*/
         3827  +struct SrcCount {
         3828  +  SrcList *pSrc;   /* One particular FROM clause in a nested query */
         3829  +  int nThis;       /* Number of references to columns in pSrcList */
         3830  +  int nOther;      /* Number of references to columns in other FROM clauses */
         3831  +};
         3832  +
         3833  +/*
         3834  +** Count the number of references to columns.
  3823   3835   */
  3824         -static int exprUsesOtherSrc(Walker *pWalker, Expr *pExpr){
  3825         -  if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
         3836  +static int exprSrcCount(Walker *pWalker, Expr *pExpr){
         3837  +  /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
         3838  +  ** is always called before sqlite3ExprAnalyzeAggregates() and so the
         3839  +  ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN.  If
         3840  +  ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
         3841  +  ** NEVER() will need to be removed. */
         3842  +  if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
  3826   3843       int i;
  3827         -    SrcList *pSrc = pWalker->u.pSrcList;
         3844  +    struct SrcCount *p = pWalker->u.pSrcCount;
         3845  +    SrcList *pSrc = p->pSrc;
  3828   3846       for(i=0; i<pSrc->nSrc; i++){
  3829         -      if( pExpr->iTable==pSrc->a[i].iCursor ) return WRC_Continue;
         3847  +      if( pExpr->iTable==pSrc->a[i].iCursor ) break;
  3830   3848       }
  3831         -    return WRC_Abort;
  3832         -  }else{
  3833         -    return WRC_Continue;
         3849  +    if( i<pSrc->nSrc ){
         3850  +      p->nThis++;
         3851  +    }else{
         3852  +      p->nOther++;
         3853  +    }
  3834   3854     }
         3855  +  return WRC_Continue;
  3835   3856   }
  3836   3857   
  3837   3858   /*
  3838         -** Determine if any of the arguments to the pExpr Function references
  3839         -** any SrcList other than pSrcList.  Return true if they do.  Return
  3840         -** false if pExpr has no argument or has only constant arguments or
  3841         -** only references tables named in pSrcList.
         3859  +** Determine if any of the arguments to the pExpr Function reference
         3860  +** pSrcList.  Return true if they do.  Also return true if the function
         3861  +** has no arguments or has only constant arguments.  Return false if pExpr
         3862  +** references columns but not columns of tables found in pSrcList.
  3842   3863   */
  3843         -static int sqlite3FunctionUsesOtherSrc(Expr *pExpr, SrcList *pSrcList){
         3864  +int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
  3844   3865     Walker w;
         3866  +  struct SrcCount cnt;
  3845   3867     assert( pExpr->op==TK_AGG_FUNCTION );
  3846   3868     memset(&w, 0, sizeof(w));
  3847         -  w.xExprCallback = exprUsesOtherSrc;
  3848         -  w.u.pSrcList = pSrcList;
  3849         -  if( sqlite3WalkExprList(&w, pExpr->x.pList)!=WRC_Continue ) return 1;
  3850         -  return 0;
         3869  +  w.xExprCallback = exprSrcCount;
         3870  +  w.u.pSrcCount = &cnt;
         3871  +  cnt.pSrc = pSrcList;
         3872  +  cnt.nThis = 0;
         3873  +  cnt.nOther = 0;
         3874  +  sqlite3WalkExprList(&w, pExpr->x.pList);
         3875  +  return cnt.nThis>0 || cnt.nOther==0;
  3851   3876   }
  3852   3877   
  3853   3878   /*
  3854   3879   ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
  3855   3880   ** the new element.  Return a negative number if malloc fails.
  3856   3881   */
  3857   3882   static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
................................................................................
  3962   3987             } /* endif pExpr->iTable==pItem->iCursor */
  3963   3988           } /* end loop over pSrcList */
  3964   3989         }
  3965   3990         return WRC_Prune;
  3966   3991       }
  3967   3992       case TK_AGG_FUNCTION: {
  3968   3993         if( (pNC->ncFlags & NC_InAggFunc)==0
  3969         -       && !sqlite3FunctionUsesOtherSrc(pExpr, pSrcList)
         3994  +       && pWalker->walkerDepth==pExpr->op2
  3970   3995         ){
  3971   3996           /* Check to see if pExpr is a duplicate of another aggregate 
  3972   3997           ** function that is already in the pAggInfo structure
  3973   3998           */
  3974   3999           struct AggInfo_func *pItem = pAggInfo->aFunc;
  3975   4000           for(i=0; i<pAggInfo->nFunc; i++, pItem++){
  3976   4001             if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){

Changes to src/fkey.c.

   556    556     sNameContext.pParse = pParse;
   557    557     sqlite3ResolveExprNames(&sNameContext, pWhere);
   558    558   
   559    559     /* Create VDBE to loop through the entries in pSrc that match the WHERE
   560    560     ** clause. If the constraint is not deferred, throw an exception for
   561    561     ** each row found. Otherwise, for deferred constraints, increment the
   562    562     ** deferred constraint counter by nIncr for each row selected.  */
   563         -  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0);
          563  +  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
   564    564     if( nIncr>0 && pFKey->isDeferred==0 ){
   565    565       sqlite3ParseToplevel(pParse)->mayAbort = 1;
   566    566     }
   567    567     sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
   568    568     if( pWInfo ){
   569    569       sqlite3WhereEnd(pWInfo);
   570    570     }

Changes to src/func.c.

   862    862   ** "NULL".  Otherwise, the argument is enclosed in single quotes with
   863    863   ** single-quote escapes.
   864    864   */
   865    865   static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   866    866     assert( argc==1 );
   867    867     UNUSED_PARAMETER(argc);
   868    868     switch( sqlite3_value_type(argv[0]) ){
   869         -    case SQLITE_INTEGER:
   870    869       case SQLITE_FLOAT: {
          870  +      double r1, r2;
          871  +      char zBuf[50];
          872  +      r1 = sqlite3_value_double(argv[0]);
          873  +      sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
          874  +      sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
          875  +      if( r1!=r2 ){
          876  +        sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
          877  +      }
          878  +      sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
          879  +      break;
          880  +    }
          881  +    case SQLITE_INTEGER: {
   871    882         sqlite3_result_value(context, argv[0]);
   872    883         break;
   873    884       }
   874    885       case SQLITE_BLOB: {
   875    886         char *zText = 0;
   876    887         char const *zBlob = sqlite3_value_blob(argv[0]);
   877    888         int nBlob = sqlite3_value_bytes(argv[0]);

Changes to src/hash.c.

   109    109       new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
   110    110     }
   111    111     if( new_size==pH->htsize ) return 0;
   112    112   #endif
   113    113   
   114    114     /* The inability to allocates space for a larger hash table is
   115    115     ** a performance hit but it is not a fatal error.  So mark the
   116         -  ** allocation as a benign.
          116  +  ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of 
          117  +  ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
          118  +  ** only zeroes the requested number of bytes whereas this module will
          119  +  ** use the actual amount of space allocated for the hash table (which
          120  +  ** may be larger than the requested amount).
   117    121     */
   118    122     sqlite3BeginBenignMalloc();
   119    123     new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
   120    124     sqlite3EndBenignMalloc();
   121    125   
   122    126     if( new_ht==0 ) return 0;
   123    127     sqlite3_free(pH->ht);

Changes to src/insert.c.

   593    593       int rc, j1;
   594    594   
   595    595       regEof = ++pParse->nMem;
   596    596       sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
   597    597       VdbeComment((v, "SELECT eof flag"));
   598    598       sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
   599    599       addrSelect = sqlite3VdbeCurrentAddr(v)+2;
   600         -    sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
          600  +    sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iSDParm);
   601    601       j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
   602    602       VdbeComment((v, "Jump over SELECT coroutine"));
   603    603   
   604    604       /* Resolve the expressions in the SELECT statement and execute it. */
   605    605       rc = sqlite3Select(pParse, pSelect, &dest);
   606    606       assert( pParse->nErr==0 || rc );
   607    607       if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
   608    608         goto insert_cleanup;
   609    609       }
   610    610       sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
   611         -    sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);   /* yield X */
          611  +    sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);   /* yield X */
   612    612       sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
   613    613       VdbeComment((v, "End of SELECT coroutine"));
   614    614       sqlite3VdbeJumpHere(v, j1);                          /* label B: */
   615    615   
   616         -    regFromSelect = dest.iMem;
          616  +    regFromSelect = dest.iSdst;
   617    617       assert( pSelect->pEList );
   618    618       nColumn = pSelect->pEList->nExpr;
   619         -    assert( dest.nMem==nColumn );
          619  +    assert( dest.nSdst==nColumn );
   620    620   
   621    621       /* Set useTempTable to TRUE if the result of the SELECT statement
   622    622       ** should be written into a temporary table (template 4).  Set to
   623    623       ** FALSE if each* row of the SELECT can be written directly into
   624    624       ** the destination table (template 3).
   625    625       **
   626    626       ** A temp table must be used if the table being updated is also one
................................................................................
   648    648         int addrTop;         /* Label "L" */
   649    649         int addrIf;          /* Address of jump to M */
   650    650   
   651    651         srcTab = pParse->nTab++;
   652    652         regRec = sqlite3GetTempReg(pParse);
   653    653         regTempRowid = sqlite3GetTempReg(pParse);
   654    654         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
   655         -      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
          655  +      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
   656    656         addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
   657    657         sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
   658    658         sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
   659    659         sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
   660    660         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
   661    661         sqlite3VdbeJumpHere(v, addrIf);
   662    662         sqlite3ReleaseTempReg(pParse, regRec);
................................................................................
   785    785       **
   786    786       **      C: yield X
   787    787       **         if EOF goto D
   788    788       **         insert the select result into <table> from R..R+n
   789    789       **         goto C
   790    790       **      D: ...
   791    791       */
   792         -    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
          792  +    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
   793    793       addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
   794    794     }
   795    795   
   796    796     /* Allocate registers for holding the rowid of the new row,
   797    797     ** the content of the new row, and the assemblied row record.
   798    798     */
   799    799     regRowid = regIns = pParse->nMem+1;

Changes to src/main.c.

   763    763       }
   764    764     }
   765    765     sqlite3BtreeLeaveAll(db);
   766    766   #else
   767    767     UNUSED_PARAMETER(db);
   768    768   #endif
   769    769   }
          770  +
          771  +/*
          772  +** Return TRUE if database connection db has unfinalized prepared
          773  +** statements or unfinished sqlite3_backup objects.  
          774  +*/
          775  +static int connectionIsBusy(sqlite3 *db){
          776  +  int j;
          777  +  assert( sqlite3_mutex_held(db->mutex) );
          778  +  if( db->pVdbe ) return 1;
          779  +  for(j=0; j<db->nDb; j++){
          780  +    Btree *pBt = db->aDb[j].pBt;
          781  +    if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
          782  +  }
          783  +  return 0;
          784  +}
   770    785   
   771    786   /*
   772    787   ** Close an existing SQLite database
   773    788   */
   774         -int sqlite3_close(sqlite3 *db){
   775         -  HashElem *i;                    /* Hash table iterator */
   776         -  int j;
   777         -
          789  +static int sqlite3Close(sqlite3 *db, int forceZombie){
   778    790     if( !db ){
   779    791       return SQLITE_OK;
   780    792     }
   781    793     if( !sqlite3SafetyCheckSickOrOk(db) ){
   782    794       return SQLITE_MISUSE_BKPT;
   783    795     }
   784    796     sqlite3_mutex_enter(db->mutex);
................................................................................
   791    803     ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
   792    804     ** call will do so. We need to do this before the check for active
   793    805     ** SQL statements below, as the v-table implementation may be storing
   794    806     ** some prepared statements internally.
   795    807     */
   796    808     sqlite3VtabRollback(db);
   797    809   
   798         -  /* If there are any outstanding VMs, return SQLITE_BUSY. */
   799         -  if( db->pVdbe ){
   800         -    sqlite3Error(db, SQLITE_BUSY, 
   801         -        "unable to close due to unfinalised statements");
          810  +  /* Legacy behavior (sqlite3_close() behavior) is to return
          811  +  ** SQLITE_BUSY if the connection can not be closed immediately.
          812  +  */
          813  +  if( !forceZombie && connectionIsBusy(db) ){
          814  +    sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
          815  +       "statements or unfinished backups");
   802    816       sqlite3_mutex_leave(db->mutex);
   803    817       return SQLITE_BUSY;
   804    818     }
   805         -  assert( sqlite3SafetyCheckSickOrOk(db) );
   806         -
   807         -  for(j=0; j<db->nDb; j++){
   808         -    Btree *pBt = db->aDb[j].pBt;
   809         -    if( pBt && sqlite3BtreeIsInBackup(pBt) ){
   810         -      sqlite3Error(db, SQLITE_BUSY, 
   811         -          "unable to close due to unfinished backup operation");
   812         -      sqlite3_mutex_leave(db->mutex);
   813         -      return SQLITE_BUSY;
   814         -    }
   815         -  }
          819  +
          820  +  /* Convert the connection into a zombie and then close it.
          821  +  */
          822  +  db->magic = SQLITE_MAGIC_ZOMBIE;
          823  +  sqlite3LeaveMutexAndCloseZombie(db);
          824  +  return SQLITE_OK;
          825  +}
          826  +
          827  +/*
          828  +** Two variations on the public interface for closing a database
          829  +** connection. The sqlite3_close() version returns SQLITE_BUSY and
          830  +** leaves the connection option if there are unfinalized prepared
          831  +** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
          832  +** version forces the connection to become a zombie if there are
          833  +** unclosed resources, and arranges for deallocation when the last
          834  +** prepare statement or sqlite3_backup closes.
          835  +*/
          836  +int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
          837  +int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
          838  +
          839  +
          840  +/*
          841  +** Close the mutex on database connection db.
          842  +**
          843  +** Furthermore, if database connection db is a zombie (meaning that there
          844  +** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
          845  +** every sqlite3_stmt has now been finalized and every sqlite3_backup has
          846  +** finished, then free all resources.
          847  +*/
          848  +void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
          849  +  HashElem *i;                    /* Hash table iterator */
          850  +  int j;
          851  +
          852  +  /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
          853  +  ** or if the connection has not yet been closed by sqlite3_close_v2(),
          854  +  ** then just leave the mutex and return.
          855  +  */
          856  +  if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
          857  +    sqlite3_mutex_leave(db->mutex);
          858  +    return;
          859  +  }
          860  +
          861  +  /* If we reach this point, it means that the database connection has
          862  +  ** closed all sqlite3_stmt and sqlite3_backup objects and has been
          863  +  ** pased to sqlite3_close (meaning that it is a zombie).  Therefore,
          864  +  ** go ahead and free all resources.
          865  +  */
   816    866   
   817    867     /* Free any outstanding Savepoint structures. */
   818    868     sqlite3CloseSavepoints(db);
   819    869   
   820    870     /* Close all database connections */
   821    871     for(j=0; j<db->nDb; j++){
   822    872       struct Db *pDb = &db->aDb[j];
................................................................................
   901    951       sqlite3_free(db->lookaside.pStart);
   902    952     }
   903    953   #ifdef SQLITE_ENABLE_SQLRR
   904    954     SRRecClose(db);
   905    955   #endif
   906    956     
   907    957     sqlite3_free(db);
   908         -  return SQLITE_OK;
   909    958   }
   910    959   
   911    960   /*
   912    961   ** Rollback all database files.  If tripCode is not SQLITE_OK, then
   913    962   ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
   914    963   ** breaker") and made to return tripCode if there are any further
   915    964   ** attempts to use that cursor.

Changes to src/mem1.c.

   227    227     sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
   228    228     if( cpuCount>1 ){
   229    229       /* defer MT decisions to system malloc */
   230    230       _sqliteZone_ = malloc_default_zone();
   231    231     }else{
   232    232       /* only 1 core, use our own zone to contention over global locks, 
   233    233       ** e.g. we have our own dedicated locks */
   234         -    bool success;		
          234  +    bool success;
   235    235       malloc_zone_t* newzone = malloc_create_zone(4096, 0);
   236    236       malloc_set_zone_name(newzone, "Sqlite_Heap");
   237    237       do{
   238    238         success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
   239    239                                    (void * volatile *)&_sqliteZone_);
   240    240       }while(!_sqliteZone_);
   241         -    if( !success ){	
          241  +    if( !success ){
   242    242         /* somebody registered a zone first */
   243    243         malloc_destroy_zone(newzone);
   244    244       }
   245    245     }
   246    246   #endif
   247    247     UNUSED_PARAMETER(NotUsed);
   248    248     return SQLITE_OK;

Changes to src/mutex.h.

    32     32   **                             mutual exclusion is provided.  But this
    33     33   **                             implementation can be overridden at
    34     34   **                             start-time.
    35     35   **
    36     36   **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
    37     37   **
    38     38   **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
    39         -**
    40         -**   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
    41     39   */
    42     40   #if !SQLITE_THREADSAFE
    43     41   # define SQLITE_MUTEX_OMIT
    44     42   #endif
    45     43   #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
    46     44   #  if SQLITE_OS_UNIX
    47     45   #    define SQLITE_MUTEX_PTHREADS
    48     46   #  elif SQLITE_OS_WIN
    49     47   #    define SQLITE_MUTEX_W32
    50         -#  elif SQLITE_OS_OS2
    51         -#    define SQLITE_MUTEX_OS2
    52     48   #  else
    53     49   #    define SQLITE_MUTEX_NOOP
    54     50   #  endif
    55     51   #endif
    56     52   
    57     53   #ifdef SQLITE_MUTEX_OMIT
    58     54   /*

Deleted src/mutex_os2.c.

     1         -/*
     2         -** 2007 August 28
     3         -**
     4         -** The author disclaims copyright to this source code.  In place of
     5         -** a legal notice, here is a blessing:
     6         -**
     7         -**    May you do good and not evil.
     8         -**    May you find forgiveness for yourself and forgive others.
     9         -**    May you share freely, never taking more than you give.
    10         -**
    11         -*************************************************************************
    12         -** This file contains the C functions that implement mutexes for OS/2
    13         -*/
    14         -#include "sqliteInt.h"
    15         -
    16         -/*
    17         -** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
    18         -** See the mutex.h file for details.
    19         -*/
    20         -#ifdef SQLITE_MUTEX_OS2
    21         -
    22         -/********************** OS/2 Mutex Implementation **********************
    23         -**
    24         -** This implementation of mutexes is built using the OS/2 API.
    25         -*/
    26         -
    27         -/*
    28         -** The mutex object
    29         -** Each recursive mutex is an instance of the following structure.
    30         -*/
    31         -struct sqlite3_mutex {
    32         -  HMTX mutex;       /* Mutex controlling the lock */
    33         -  int  id;          /* Mutex type */
    34         -#ifdef SQLITE_DEBUG
    35         - int   trace;       /* True to trace changes */
    36         -#endif
    37         -};
    38         -
    39         -#ifdef SQLITE_DEBUG
    40         -#define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
    41         -#else
    42         -#define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
    43         -#endif
    44         -
    45         -/*
    46         -** Initialize and deinitialize the mutex subsystem.
    47         -*/
    48         -static int os2MutexInit(void){ return SQLITE_OK; }
    49         -static int os2MutexEnd(void){ return SQLITE_OK; }
    50         -
    51         -/*
    52         -** The sqlite3_mutex_alloc() routine allocates a new
    53         -** mutex and returns a pointer to it.  If it returns NULL
    54         -** that means that a mutex could not be allocated. 
    55         -** SQLite will unwind its stack and return an error.  The argument
    56         -** to sqlite3_mutex_alloc() is one of these integer constants:
    57         -**
    58         -** <ul>
    59         -** <li>  SQLITE_MUTEX_FAST
    60         -** <li>  SQLITE_MUTEX_RECURSIVE
    61         -** <li>  SQLITE_MUTEX_STATIC_MASTER
    62         -** <li>  SQLITE_MUTEX_STATIC_MEM
    63         -** <li>  SQLITE_MUTEX_STATIC_MEM2
    64         -** <li>  SQLITE_MUTEX_STATIC_PRNG
    65         -** <li>  SQLITE_MUTEX_STATIC_LRU
    66         -** <li>  SQLITE_MUTEX_STATIC_LRU2
    67         -** </ul>
    68         -**
    69         -** The first two constants cause sqlite3_mutex_alloc() to create
    70         -** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
    71         -** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
    72         -** The mutex implementation does not need to make a distinction
    73         -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
    74         -** not want to.  But SQLite will only request a recursive mutex in
    75         -** cases where it really needs one.  If a faster non-recursive mutex
    76         -** implementation is available on the host platform, the mutex subsystem
    77         -** might return such a mutex in response to SQLITE_MUTEX_FAST.
    78         -**
    79         -** The other allowed parameters to sqlite3_mutex_alloc() each return
    80         -** a pointer to a static preexisting mutex.  Six static mutexes are
    81         -** used by the current version of SQLite.  Future versions of SQLite
    82         -** may add additional static mutexes.  Static mutexes are for internal
    83         -** use by SQLite only.  Applications that use SQLite mutexes should
    84         -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
    85         -** SQLITE_MUTEX_RECURSIVE.
    86         -**
    87         -** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
    88         -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
    89         -** returns a different mutex on every call.  But for the static
    90         -** mutex types, the same mutex is returned on every call that has
    91         -** the same type number.
    92         -*/
    93         -static sqlite3_mutex *os2MutexAlloc(int iType){
    94         -  sqlite3_mutex *p = NULL;
    95         -  switch( iType ){
    96         -    case SQLITE_MUTEX_FAST:
    97         -    case SQLITE_MUTEX_RECURSIVE: {
    98         -      p = sqlite3MallocZero( sizeof(*p) );
    99         -      if( p ){
   100         -        p->id = iType;
   101         -        if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
   102         -          sqlite3_free( p );
   103         -          p = NULL;
   104         -        }
   105         -      }
   106         -      break;
   107         -    }
   108         -    default: {
   109         -      static volatile int isInit = 0;
   110         -      static sqlite3_mutex staticMutexes[6] = {
   111         -        SQLITE3_MUTEX_INITIALIZER,
   112         -        SQLITE3_MUTEX_INITIALIZER,
   113         -        SQLITE3_MUTEX_INITIALIZER,
   114         -        SQLITE3_MUTEX_INITIALIZER,
   115         -        SQLITE3_MUTEX_INITIALIZER,
   116         -        SQLITE3_MUTEX_INITIALIZER,
   117         -      };
   118         -      if ( !isInit ){
   119         -        APIRET rc;
   120         -        PTIB ptib;
   121         -        PPIB ppib;
   122         -        HMTX mutex;
   123         -        char name[32];
   124         -        DosGetInfoBlocks( &ptib, &ppib );
   125         -        sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
   126         -                          ppib->pib_ulpid );
   127         -        while( !isInit ){
   128         -          mutex = 0;
   129         -          rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
   130         -          if( rc == NO_ERROR ){
   131         -            unsigned int i;
   132         -            if( !isInit ){
   133         -              for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
   134         -                DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
   135         -              }
   136         -              isInit = 1;
   137         -            }
   138         -            DosCloseMutexSem( mutex );
   139         -          }else if( rc == ERROR_DUPLICATE_NAME ){
   140         -            DosSleep( 1 );
   141         -          }else{
   142         -            return p;
   143         -          }
   144         -        }
   145         -      }
   146         -      assert( iType-2 >= 0 );
   147         -      assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
   148         -      p = &staticMutexes[iType-2];
   149         -      p->id = iType;
   150         -      break;
   151         -    }
   152         -  }
   153         -  return p;
   154         -}
   155         -
   156         -
   157         -/*
   158         -** This routine deallocates a previously allocated mutex.
   159         -** SQLite is careful to deallocate every mutex that it allocates.
   160         -*/
   161         -static void os2MutexFree(sqlite3_mutex *p){
   162         -#ifdef SQLITE_DEBUG
   163         -  TID tid;
   164         -  PID pid;
   165         -  ULONG ulCount;
   166         -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   167         -  assert( ulCount==0 );
   168         -  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   169         -#endif
   170         -  DosCloseMutexSem( p->mutex );
   171         -  sqlite3_free( p );
   172         -}
   173         -
   174         -#ifdef SQLITE_DEBUG
   175         -/*
   176         -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   177         -** intended for use inside assert() statements.
   178         -*/
   179         -static int os2MutexHeld(sqlite3_mutex *p){
   180         -  TID tid;
   181         -  PID pid;
   182         -  ULONG ulCount;
   183         -  PTIB ptib;
   184         -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   185         -  if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
   186         -    return 0;
   187         -  DosGetInfoBlocks(&ptib, NULL);
   188         -  return tid==ptib->tib_ptib2->tib2_ultid;
   189         -}
   190         -static int os2MutexNotheld(sqlite3_mutex *p){
   191         -  TID tid;
   192         -  PID pid;
   193         -  ULONG ulCount;
   194         -  PTIB ptib;
   195         -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   196         -  if( ulCount==0 )
   197         -    return 1;
   198         -  DosGetInfoBlocks(&ptib, NULL);
   199         -  return tid!=ptib->tib_ptib2->tib2_ultid;
   200         -}
   201         -static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
   202         -  TID   tid;
   203         -  PID   pid;
   204         -  ULONG ulCount;
   205         -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   206         -  printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
   207         -}
   208         -#endif
   209         -
   210         -/*
   211         -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   212         -** to enter a mutex.  If another thread is already within the mutex,
   213         -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   214         -** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   215         -** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   216         -** be entered multiple times by the same thread.  In such cases the,
   217         -** mutex must be exited an equal number of times before another thread
   218         -** can enter.  If the same thread tries to enter any other kind of mutex
   219         -** more than once, the behavior is undefined.
   220         -*/
   221         -static void os2MutexEnter(sqlite3_mutex *p){
   222         -  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
   223         -  DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
   224         -#ifdef SQLITE_DEBUG
   225         -  if( p->trace ) os2MutexTrace(p, "enter");
   226         -#endif
   227         -}
   228         -static int os2MutexTry(sqlite3_mutex *p){
   229         -  int rc = SQLITE_BUSY;
   230         -  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
   231         -  if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
   232         -    rc = SQLITE_OK;
   233         -#ifdef SQLITE_DEBUG
   234         -    if( p->trace ) os2MutexTrace(p, "try");
   235         -#endif
   236         -  }
   237         -  return rc;
   238         -}
   239         -
   240         -/*
   241         -** The sqlite3_mutex_leave() routine exits a mutex that was
   242         -** previously entered by the same thread.  The behavior
   243         -** is undefined if the mutex is not currently entered or
   244         -** is not currently allocated.  SQLite will never do either.
   245         -*/
   246         -static void os2MutexLeave(sqlite3_mutex *p){
   247         -  assert( os2MutexHeld(p) );
   248         -  DosReleaseMutexSem(p->mutex);
   249         -#ifdef SQLITE_DEBUG
   250         -  if( p->trace ) os2MutexTrace(p, "leave");
   251         -#endif
   252         -}
   253         -
   254         -sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   255         -  static const sqlite3_mutex_methods sMutex = {
   256         -    os2MutexInit,
   257         -    os2MutexEnd,
   258         -    os2MutexAlloc,
   259         -    os2MutexFree,
   260         -    os2MutexEnter,
   261         -    os2MutexTry,
   262         -    os2MutexLeave,
   263         -#ifdef SQLITE_DEBUG
   264         -    os2MutexHeld,
   265         -    os2MutexNotheld
   266         -#else
   267         -    0,
   268         -    0
   269         -#endif
   270         -  };
   271         -
   272         -  return &sMutex;
   273         -}
   274         -#endif /* SQLITE_MUTEX_OS2 */

Changes to src/mutex_w32.c.

   105    105   /* As winMutexInit() and winMutexEnd() are called as part
   106    106   ** of the sqlite3_initialize and sqlite3_shutdown()
   107    107   ** processing, the "interlocked" magic is probably not
   108    108   ** strictly necessary.
   109    109   */
   110    110   static long winMutex_lock = 0;
   111    111   
   112         -extern void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
          112  +void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
   113    113   
   114    114   static int winMutexInit(void){ 
   115    115     /* The first to increment to 1 does actual initialization */
   116    116     if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
   117    117       int i;
   118    118       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
   119    119   #if SQLITE_OS_WINRT

Changes to src/os.h.

    19     19   */
    20     20   #ifndef _SQLITE_OS_H_
    21     21   #define _SQLITE_OS_H_
    22     22   
    23     23   /*
    24     24   ** Figure out if we are dealing with Unix, Windows, or some other
    25     25   ** operating system.  After the following block of preprocess macros,
    26         -** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER 
           26  +** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
    27     27   ** will defined to either 1 or 0.  One of the four will be 1.  The other 
    28     28   ** three will be 0.
    29     29   */
    30     30   #if defined(SQLITE_OS_OTHER)
    31     31   # if SQLITE_OS_OTHER==1
    32     32   #   undef SQLITE_OS_UNIX
    33     33   #   define SQLITE_OS_UNIX 0
    34     34   #   undef SQLITE_OS_WIN
    35     35   #   define SQLITE_OS_WIN 0
    36         -#   undef SQLITE_OS_OS2
    37         -#   define SQLITE_OS_OS2 0
    38     36   # else
    39     37   #   undef SQLITE_OS_OTHER
    40     38   # endif
    41     39   #endif
    42     40   #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
    43     41   # define SQLITE_OS_OTHER 0
    44     42   # ifndef SQLITE_OS_WIN
    45     43   #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
    46     44   #     define SQLITE_OS_WIN 1
    47     45   #     define SQLITE_OS_UNIX 0
    48         -#     define SQLITE_OS_OS2 0
    49         -#   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
    50         -#     define SQLITE_OS_WIN 0
    51         -#     define SQLITE_OS_UNIX 0
    52         -#     define SQLITE_OS_OS2 1
    53     46   #   else
    54     47   #     define SQLITE_OS_WIN 0
    55     48   #     define SQLITE_OS_UNIX 1
    56         -#     define SQLITE_OS_OS2 0
    57     49   #  endif
    58     50   # else
    59     51   #  define SQLITE_OS_UNIX 0
    60         -#  define SQLITE_OS_OS2 0
    61     52   # endif
    62     53   #else
    63     54   # ifndef SQLITE_OS_WIN
    64     55   #  define SQLITE_OS_WIN 0
    65     56   # endif
    66     57   #endif
    67     58   
    68     59   #if SQLITE_OS_WIN
    69     60   # include <windows.h>
    70     61   #endif
    71     62   
    72         -#if SQLITE_OS_OS2
    73         -# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
    74         -#  include <os2safe.h> /* has to be included before os2.h for linking to work */
    75         -# endif
    76         -# define INCL_DOSDATETIME
    77         -# define INCL_DOSFILEMGR
    78         -# define INCL_DOSERRORS
    79         -# define INCL_DOSMISC
    80         -# define INCL_DOSPROCESS
    81         -# define INCL_DOSMODULEMGR
    82         -# define INCL_DOSSEMAPHORES
    83         -# include <os2.h>
    84         -# include <uconv.h>
    85         -#endif
    86         -
    87     63   /*
    88     64   ** Determine if we are dealing with Windows NT.
    89     65   **
    90     66   ** We ought to be able to determine if we are compiling for win98 or winNT
    91     67   ** using the _WIN32_WINNT macro as follows:
    92     68   **
    93     69   ** #if defined(_WIN32_WINNT)
................................................................................
   112     88   #if defined(_WIN32_WCE)
   113     89   # define SQLITE_OS_WINCE 1
   114     90   #else
   115     91   # define SQLITE_OS_WINCE 0
   116     92   #endif
   117     93   
   118     94   /*
   119         -** Determine if we are dealing with WindowsRT (Metro) as this has a different and
   120         -** incompatible API from win32.
           95  +** Determine if we are dealing with WinRT, which provides only a subset of
           96  +** the full Win32 API.
   121     97   */
   122     98   #if !defined(SQLITE_OS_WINRT)
   123     99   # define SQLITE_OS_WINRT 0
   124    100   #endif
   125    101   
   126    102   /*
   127    103   ** When compiled for WinCE or WinRT, there is no concept of the current

Deleted src/os_os2.c.

     1         -/*
     2         -** 2006 Feb 14
     3         -**
     4         -** The author disclaims copyright to this source code.  In place of
     5         -** a legal notice, here is a blessing:
     6         -**
     7         -**    May you do good and not evil.
     8         -**    May you find forgiveness for yourself and forgive others.
     9         -**    May you share freely, never taking more than you give.
    10         -**
    11         -******************************************************************************
    12         -**
    13         -** This file contains code that is specific to OS/2.
    14         -*/
    15         -
    16         -#include "sqliteInt.h"
    17         -
    18         -#if SQLITE_OS_OS2
    19         -
    20         -/*
    21         -** A Note About Memory Allocation:
    22         -**
    23         -** This driver uses malloc()/free() directly rather than going through
    24         -** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
    25         -** are designed for use on embedded systems where memory is scarce and
    26         -** malloc failures happen frequently.  OS/2 does not typically run on
    27         -** embedded systems, and when it does the developers normally have bigger
    28         -** problems to worry about than running out of memory.  So there is not
    29         -** a compelling need to use the wrappers.
    30         -**
    31         -** But there is a good reason to not use the wrappers.  If we use the
    32         -** wrappers then we will get simulated malloc() failures within this
    33         -** driver.  And that causes all kinds of problems for our tests.  We
    34         -** could enhance SQLite to deal with simulated malloc failures within
    35         -** the OS driver, but the code to deal with those failure would not
    36         -** be exercised on Linux (which does not need to malloc() in the driver)
    37         -** and so we would have difficulty writing coverage tests for that
    38         -** code.  Better to leave the code out, we think.
    39         -**
    40         -** The point of this discussion is as follows:  When creating a new
    41         -** OS layer for an embedded system, if you use this file as an example,
    42         -** avoid the use of malloc()/free().  Those routines work ok on OS/2
    43         -** desktops but not so well in embedded systems.
    44         -*/
    45         -
    46         -/*
    47         -** Macros used to determine whether or not to use threads.
    48         -*/
    49         -#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
    50         -# define SQLITE_OS2_THREADS 1
    51         -#endif
    52         -
    53         -/*
    54         -** Include code that is common to all os_*.c files
    55         -*/
    56         -#include "os_common.h"
    57         -
    58         -/* Forward references */
    59         -typedef struct os2File os2File;         /* The file structure */
    60         -typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
    61         -typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
    62         -
    63         -/*
    64         -** The os2File structure is subclass of sqlite3_file specific for the OS/2
    65         -** protability layer.
    66         -*/
    67         -struct os2File {
    68         -  const sqlite3_io_methods *pMethod;  /* Always the first entry */
    69         -  HFILE h;                  /* Handle for accessing the file */
    70         -  int flags;                /* Flags provided to os2Open() */
    71         -  int locktype;             /* Type of lock currently held on this file */
    72         -  int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
    73         -  char *zFullPathCp;        /* Full path name of this file */
    74         -  os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
    75         -};
    76         -
    77         -#define LOCK_TIMEOUT 10L /* the default locking timeout */
    78         -
    79         -/*
    80         -** Missing from some versions of the OS/2 toolkit -
    81         -** used to allocate from high memory if possible
    82         -*/
    83         -#ifndef OBJ_ANY
    84         -# define OBJ_ANY 0x00000400
    85         -#endif
    86         -
    87         -/*****************************************************************************
    88         -** The next group of routines implement the I/O methods specified
    89         -** by the sqlite3_io_methods object.
    90         -******************************************************************************/
    91         -
    92         -/*
    93         -** Close a file.
    94         -*/
    95         -static int os2Close( sqlite3_file *id ){
    96         -  APIRET rc;
    97         -  os2File *pFile = (os2File*)id;
    98         -
    99         -  assert( id!=0 );
   100         -  OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
   101         -
   102         -  rc = DosClose( pFile->h );
   103         -
   104         -  if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
   105         -    DosForceDelete( (PSZ)pFile->zFullPathCp );
   106         -
   107         -  free( pFile->zFullPathCp );
   108         -  pFile->zFullPathCp = NULL;
   109         -  pFile->locktype = NO_LOCK;
   110         -  pFile->h = (HFILE)-1;
   111         -  pFile->flags = 0;
   112         -
   113         -  OpenCounter( -1 );
   114         -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   115         -}
   116         -
   117         -/*
   118         -** Read data from a file into a buffer.  Return SQLITE_OK if all
   119         -** bytes were read successfully and SQLITE_IOERR if anything goes
   120         -** wrong.
   121         -*/
   122         -static int os2Read(
   123         -  sqlite3_file *id,               /* File to read from */
   124         -  void *pBuf,                     /* Write content into this buffer */
   125         -  int amt,                        /* Number of bytes to read */
   126         -  sqlite3_int64 offset            /* Begin reading at this offset */
   127         -){
   128         -  ULONG fileLocation = 0L;
   129         -  ULONG got;
   130         -  os2File *pFile = (os2File*)id;
   131         -  assert( id!=0 );
   132         -  SimulateIOError( return SQLITE_IOERR_READ );
   133         -  OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
   134         -  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
   135         -    return SQLITE_IOERR;
   136         -  }
   137         -  if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
   138         -    return SQLITE_IOERR_READ;
   139         -  }
   140         -  if( got == (ULONG)amt )
   141         -    return SQLITE_OK;
   142         -  else {
   143         -    /* Unread portions of the input buffer must be zero-filled */
   144         -    memset(&((char*)pBuf)[got], 0, amt-got);
   145         -    return SQLITE_IOERR_SHORT_READ;
   146         -  }
   147         -}
   148         -
   149         -/*
   150         -** Write data from a buffer into a file.  Return SQLITE_OK on success
   151         -** or some other error code on failure.
   152         -*/
   153         -static int os2Write(
   154         -  sqlite3_file *id,               /* File to write into */
   155         -  const void *pBuf,               /* The bytes to be written */
   156         -  int amt,                        /* Number of bytes to write */
   157         -  sqlite3_int64 offset            /* Offset into the file to begin writing at */
   158         -){
   159         -  ULONG fileLocation = 0L;
   160         -  APIRET rc = NO_ERROR;
   161         -  ULONG wrote;
   162         -  os2File *pFile = (os2File*)id;
   163         -  assert( id!=0 );
   164         -  SimulateIOError( return SQLITE_IOERR_WRITE );
   165         -  SimulateDiskfullError( return SQLITE_FULL );
   166         -  OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
   167         -  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
   168         -    return SQLITE_IOERR;
   169         -  }
   170         -  assert( amt>0 );
   171         -  while( amt > 0 &&
   172         -         ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
   173         -         wrote > 0
   174         -  ){
   175         -    amt -= wrote;
   176         -    pBuf = &((char*)pBuf)[wrote];
   177         -  }
   178         -
   179         -  return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
   180         -}
   181         -
   182         -/*
   183         -** Truncate an open file to a specified size
   184         -*/
   185         -static int os2Truncate( sqlite3_file *id, i64 nByte ){
   186         -  APIRET rc;
   187         -  os2File *pFile = (os2File*)id;
   188         -  assert( id!=0 );
   189         -  OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
   190         -  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
   191         -
   192         -  /* If the user has configured a chunk-size for this file, truncate the
   193         -  ** file so that it consists of an integer number of chunks (i.e. the
   194         -  ** actual file size after the operation may be larger than the requested
   195         -  ** size).
   196         -  */
   197         -  if( pFile->szChunk ){
   198         -    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
   199         -  }
   200         -  
   201         -  rc = DosSetFileSize( pFile->h, nByte );
   202         -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
   203         -}
   204         -
   205         -#ifdef SQLITE_TEST
   206         -/*
   207         -** Count the number of fullsyncs and normal syncs.  This is used to test
   208         -** that syncs and fullsyncs are occuring at the right times.
   209         -*/
   210         -int sqlite3_sync_count = 0;
   211         -int sqlite3_fullsync_count = 0;
   212         -#endif
   213         -
   214         -/*
   215         -** Make sure all writes to a particular file are committed to disk.
   216         -*/
   217         -static int os2Sync( sqlite3_file *id, int flags ){
   218         -  os2File *pFile = (os2File*)id;
   219         -  OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
   220         -#ifdef SQLITE_TEST
   221         -  if( flags & SQLITE_SYNC_FULL){
   222         -    sqlite3_fullsync_count++;
   223         -  }
   224         -  sqlite3_sync_count++;
   225         -#endif
   226         -  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
   227         -  ** no-op
   228         -  */
   229         -#ifdef SQLITE_NO_SYNC
   230         -  UNUSED_PARAMETER(pFile);
   231         -  return SQLITE_OK;
   232         -#else
   233         -  return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   234         -#endif
   235         -}
   236         -
   237         -/*
   238         -** Determine the current size of a file in bytes
   239         -*/
   240         -static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
   241         -  APIRET rc = NO_ERROR;
   242         -  FILESTATUS3 fsts3FileInfo;
   243         -  memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
   244         -  assert( id!=0 );
   245         -  SimulateIOError( return SQLITE_IOERR_FSTAT );
   246         -  rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
   247         -  if( rc == NO_ERROR ){
   248         -    *pSize = fsts3FileInfo.cbFile;
   249         -    return SQLITE_OK;
   250         -  }else{
   251         -    return SQLITE_IOERR_FSTAT;
   252         -  }
   253         -}
   254         -
   255         -/*
   256         -** Acquire a reader lock.
   257         -*/
   258         -static int getReadLock( os2File *pFile ){
   259         -  FILELOCK  LockArea,
   260         -            UnlockArea;
   261         -  APIRET res;
   262         -  memset(&LockArea, 0, sizeof(LockArea));
   263         -  memset(&UnlockArea, 0, sizeof(UnlockArea));
   264         -  LockArea.lOffset = SHARED_FIRST;
   265         -  LockArea.lRange = SHARED_SIZE;
   266         -  UnlockArea.lOffset = 0L;
   267         -  UnlockArea.lRange = 0L;
   268         -  res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
   269         -  OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
   270         -  return res;
   271         -}
   272         -
   273         -/*
   274         -** Undo a readlock
   275         -*/
   276         -static int unlockReadLock( os2File *id ){
   277         -  FILELOCK  LockArea,
   278         -            UnlockArea;
   279         -  APIRET res;
   280         -  memset(&LockArea, 0, sizeof(LockArea));
   281         -  memset(&UnlockArea, 0, sizeof(UnlockArea));
   282         -  LockArea.lOffset = 0L;
   283         -  LockArea.lRange = 0L;
   284         -  UnlockArea.lOffset = SHARED_FIRST;
   285         -  UnlockArea.lRange = SHARED_SIZE;
   286         -  res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
   287         -  OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
   288         -  return res;
   289         -}
   290         -
   291         -/*
   292         -** Lock the file with the lock specified by parameter locktype - one
   293         -** of the following:
   294         -**
   295         -**     (1) SHARED_LOCK
   296         -**     (2) RESERVED_LOCK
   297         -**     (3) PENDING_LOCK
   298         -**     (4) EXCLUSIVE_LOCK
   299         -**
   300         -** Sometimes when requesting one lock state, additional lock states
   301         -** are inserted in between.  The locking might fail on one of the later
   302         -** transitions leaving the lock state different from what it started but
   303         -** still short of its goal.  The following chart shows the allowed
   304         -** transitions and the inserted intermediate states:
   305         -**
   306         -**    UNLOCKED -> SHARED
   307         -**    SHARED -> RESERVED
   308         -**    SHARED -> (PENDING) -> EXCLUSIVE
   309         -**    RESERVED -> (PENDING) -> EXCLUSIVE
   310         -**    PENDING -> EXCLUSIVE
   311         -**
   312         -** This routine will only increase a lock.  The os2Unlock() routine
   313         -** erases all locks at once and returns us immediately to locking level 0.
   314         -** It is not possible to lower the locking level one step at a time.  You
   315         -** must go straight to locking level 0.
   316         -*/
   317         -static int os2Lock( sqlite3_file *id, int locktype ){
   318         -  int rc = SQLITE_OK;       /* Return code from subroutines */
   319         -  APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
   320         -  int newLocktype;       /* Set pFile->locktype to this value before exiting */
   321         -  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
   322         -  FILELOCK  LockArea,
   323         -            UnlockArea;
   324         -  os2File *pFile = (os2File*)id;
   325         -  memset(&LockArea, 0, sizeof(LockArea));
   326         -  memset(&UnlockArea, 0, sizeof(UnlockArea));
   327         -  assert( pFile!=0 );
   328         -  OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
   329         -
   330         -  /* If there is already a lock of this type or more restrictive on the
   331         -  ** os2File, do nothing. Don't use the end_lock: exit path, as
   332         -  ** sqlite3_mutex_enter() hasn't been called yet.
   333         -  */
   334         -  if( pFile->locktype>=locktype ){
   335         -    OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
   336         -    return SQLITE_OK;
   337         -  }
   338         -
   339         -  /* Make sure the locking sequence is correct
   340         -  */
   341         -  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
   342         -  assert( locktype!=PENDING_LOCK );
   343         -  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
   344         -
   345         -  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
   346         -  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
   347         -  ** the PENDING_LOCK byte is temporary.
   348         -  */
   349         -  newLocktype = pFile->locktype;
   350         -  if( pFile->locktype==NO_LOCK
   351         -      || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
   352         -  ){
   353         -    LockArea.lOffset = PENDING_BYTE;
   354         -    LockArea.lRange = 1L;
   355         -    UnlockArea.lOffset = 0L;
   356         -    UnlockArea.lRange = 0L;
   357         -
   358         -    /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
   359         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
   360         -    if( res == NO_ERROR ){
   361         -      gotPendingLock = 1;
   362         -      OSTRACE(( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res ));
   363         -    }
   364         -  }
   365         -
   366         -  /* Acquire a shared lock
   367         -  */
   368         -  if( locktype==SHARED_LOCK && res == NO_ERROR ){
   369         -    assert( pFile->locktype==NO_LOCK );
   370         -    res = getReadLock(pFile);
   371         -    if( res == NO_ERROR ){
   372         -      newLocktype = SHARED_LOCK;
   373         -    }
   374         -    OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
   375         -  }
   376         -
   377         -  /* Acquire a RESERVED lock
   378         -  */
   379         -  if( locktype==RESERVED_LOCK && res == NO_ERROR ){
   380         -    assert( pFile->locktype==SHARED_LOCK );
   381         -    LockArea.lOffset = RESERVED_BYTE;
   382         -    LockArea.lRange = 1L;
   383         -    UnlockArea.lOffset = 0L;
   384         -    UnlockArea.lRange = 0L;
   385         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   386         -    if( res == NO_ERROR ){
   387         -      newLocktype = RESERVED_LOCK;
   388         -    }
   389         -    OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
   390         -  }
   391         -
   392         -  /* Acquire a PENDING lock
   393         -  */
   394         -  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
   395         -    newLocktype = PENDING_LOCK;
   396         -    gotPendingLock = 0;
   397         -    OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
   398         -               pFile->h ));
   399         -  }
   400         -
   401         -  /* Acquire an EXCLUSIVE lock
   402         -  */
   403         -  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
   404         -    assert( pFile->locktype>=SHARED_LOCK );
   405         -    res = unlockReadLock(pFile);
   406         -    OSTRACE(( "unreadlock = %d\n", res ));
   407         -    LockArea.lOffset = SHARED_FIRST;
   408         -    LockArea.lRange = SHARED_SIZE;
   409         -    UnlockArea.lOffset = 0L;
   410         -    UnlockArea.lRange = 0L;
   411         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   412         -    if( res == NO_ERROR ){
   413         -      newLocktype = EXCLUSIVE_LOCK;
   414         -    }else{
   415         -      OSTRACE(( "OS/2 error-code = %d\n", res ));
   416         -      getReadLock(pFile);
   417         -    }
   418         -    OSTRACE(( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res ));
   419         -  }
   420         -
   421         -  /* If we are holding a PENDING lock that ought to be released, then
   422         -  ** release it now.
   423         -  */
   424         -  if( gotPendingLock && locktype==SHARED_LOCK ){
   425         -    int r;
   426         -    LockArea.lOffset = 0L;
   427         -    LockArea.lRange = 0L;
   428         -    UnlockArea.lOffset = PENDING_BYTE;
   429         -    UnlockArea.lRange = 1L;
   430         -    r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   431         -    OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
   432         -  }
   433         -
   434         -  /* Update the state of the lock has held in the file descriptor then
   435         -  ** return the appropriate result code.
   436         -  */
   437         -  if( res == NO_ERROR ){
   438         -    rc = SQLITE_OK;
   439         -  }else{
   440         -    OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
   441         -              locktype, newLocktype ));
   442         -    rc = SQLITE_BUSY;
   443         -  }
   444         -  pFile->locktype = newLocktype;
   445         -  OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
   446         -  return rc;
   447         -}
   448         -
   449         -/*
   450         -** This routine checks if there is a RESERVED lock held on the specified
   451         -** file by this or any other process. If such a lock is held, return
   452         -** non-zero, otherwise zero.
   453         -*/
   454         -static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
   455         -  int r = 0;
   456         -  os2File *pFile = (os2File*)id;
   457         -  assert( pFile!=0 );
   458         -  if( pFile->locktype>=RESERVED_LOCK ){
   459         -    r = 1;
   460         -    OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
   461         -  }else{
   462         -    FILELOCK  LockArea,
   463         -              UnlockArea;
   464         -    APIRET rc = NO_ERROR;
   465         -    memset(&LockArea, 0, sizeof(LockArea));
   466         -    memset(&UnlockArea, 0, sizeof(UnlockArea));
   467         -    LockArea.lOffset = RESERVED_BYTE;
   468         -    LockArea.lRange = 1L;
   469         -    UnlockArea.lOffset = 0L;
   470         -    UnlockArea.lRange = 0L;
   471         -    rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   472         -    OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
   473         -    if( rc == NO_ERROR ){
   474         -      APIRET rcu = NO_ERROR; /* return code for unlocking */
   475         -      LockArea.lOffset = 0L;
   476         -      LockArea.lRange = 0L;
   477         -      UnlockArea.lOffset = RESERVED_BYTE;
   478         -      UnlockArea.lRange = 1L;
   479         -      rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   480         -      OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
   481         -    }
   482         -    r = !(rc == NO_ERROR);
   483         -    OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
   484         -  }
   485         -  *pOut = r;
   486         -  return SQLITE_OK;
   487         -}
   488         -
   489         -/*
   490         -** Lower the locking level on file descriptor id to locktype.  locktype
   491         -** must be either NO_LOCK or SHARED_LOCK.
   492         -**
   493         -** If the locking level of the file descriptor is already at or below
   494         -** the requested locking level, this routine is a no-op.
   495         -**
   496         -** It is not possible for this routine to fail if the second argument
   497         -** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
   498         -** might return SQLITE_IOERR;
   499         -*/
   500         -static int os2Unlock( sqlite3_file *id, int locktype ){
   501         -  int type;
   502         -  os2File *pFile = (os2File*)id;
   503         -  APIRET rc = SQLITE_OK;
   504         -  APIRET res = NO_ERROR;
   505         -  FILELOCK  LockArea,
   506         -            UnlockArea;
   507         -  memset(&LockArea, 0, sizeof(LockArea));
   508         -  memset(&UnlockArea, 0, sizeof(UnlockArea));
   509         -  assert( pFile!=0 );
   510         -  assert( locktype<=SHARED_LOCK );
   511         -  OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
   512         -  type = pFile->locktype;
   513         -  if( type>=EXCLUSIVE_LOCK ){
   514         -    LockArea.lOffset = 0L;
   515         -    LockArea.lRange = 0L;
   516         -    UnlockArea.lOffset = SHARED_FIRST;
   517         -    UnlockArea.lRange = SHARED_SIZE;
   518         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   519         -    OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
   520         -    if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
   521         -      /* This should never happen.  We should always be able to
   522         -      ** reacquire the read lock */
   523         -      OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
   524         -      rc = SQLITE_IOERR_UNLOCK;
   525         -    }
   526         -  }
   527         -  if( type>=RESERVED_LOCK ){
   528         -    LockArea.lOffset = 0L;
   529         -    LockArea.lRange = 0L;
   530         -    UnlockArea.lOffset = RESERVED_BYTE;
   531         -    UnlockArea.lRange = 1L;
   532         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   533         -    OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
   534         -  }
   535         -  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
   536         -    res = unlockReadLock(pFile);
   537         -    OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
   538         -              pFile->h, type, locktype, res ));
   539         -  }
   540         -  if( type>=PENDING_LOCK ){
   541         -    LockArea.lOffset = 0L;
   542         -    LockArea.lRange = 0L;
   543         -    UnlockArea.lOffset = PENDING_BYTE;
   544         -    UnlockArea.lRange = 1L;
   545         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   546         -    OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
   547         -  }
   548         -  pFile->locktype = locktype;
   549         -  OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
   550         -  return rc;
   551         -}
   552         -
   553         -/*
   554         -** Control and query of the open file handle.
   555         -*/
   556         -static int os2FileControl(sqlite3_file *id, int op, void *pArg){
   557         -  switch( op ){
   558         -    case SQLITE_FCNTL_LOCKSTATE: {
   559         -      *(int*)pArg = ((os2File*)id)->locktype;
   560         -      OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
   561         -                ((os2File*)id)->h, ((os2File*)id)->locktype ));
   562         -      return SQLITE_OK;
   563         -    }
   564         -    case SQLITE_FCNTL_CHUNK_SIZE: {
   565         -      ((os2File*)id)->szChunk = *(int*)pArg;
   566         -      return SQLITE_OK;
   567         -    }
   568         -    case SQLITE_FCNTL_SIZE_HINT: {
   569         -      sqlite3_int64 sz = *(sqlite3_int64*)pArg;
   570         -      SimulateIOErrorBenign(1);
   571         -      os2Truncate(id, sz);
   572         -      SimulateIOErrorBenign(0);
   573         -      return SQLITE_OK;
   574         -    }
   575         -    case SQLITE_FCNTL_SYNC_OMITTED: {
   576         -      return SQLITE_OK;
   577         -    }
   578         -  }
   579         -  return SQLITE_NOTFOUND;
   580         -}
   581         -
   582         -/*
   583         -** Return the sector size in bytes of the underlying block device for
   584         -** the specified file. This is almost always 512 bytes, but may be
   585         -** larger for some devices.
   586         -**
   587         -** SQLite code assumes this function cannot fail. It also assumes that
   588         -** if two files are created in the same file-system directory (i.e.
   589         -** a database and its journal file) that the sector size will be the
   590         -** same for both.
   591         -*/
   592         -static int os2SectorSize(sqlite3_file *id){
   593         -  UNUSED_PARAMETER(id);
   594         -  return SQLITE_DEFAULT_SECTOR_SIZE;
   595         -}
   596         -
   597         -/*
   598         -** Return a vector of device characteristics.
   599         -*/
   600         -static int os2DeviceCharacteristics(sqlite3_file *id){
   601         -  UNUSED_PARAMETER(id);
   602         -  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
   603         -}
   604         -
   605         -
   606         -/*
   607         -** Character set conversion objects used by conversion routines.
   608         -*/
   609         -static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
   610         -static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
   611         -
   612         -/*
   613         -** Helper function to initialize the conversion objects from and to UTF-8.
   614         -*/
   615         -static void initUconvObjects( void ){
   616         -  if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
   617         -    ucUtf8 = NULL;
   618         -  if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
   619         -    uclCp = NULL;
   620         -}
   621         -
   622         -/*
   623         -** Helper function to free the conversion objects from and to UTF-8.
   624         -*/
   625         -static void freeUconvObjects( void ){
   626         -  if ( ucUtf8 )
   627         -    UniFreeUconvObject( ucUtf8 );
   628         -  if ( uclCp )
   629         -    UniFreeUconvObject( uclCp );
   630         -  ucUtf8 = NULL;
   631         -  uclCp = NULL;
   632         -}
   633         -
   634         -/*
   635         -** Helper function to convert UTF-8 filenames to local OS/2 codepage.
   636         -** The two-step process: first convert the incoming UTF-8 string
   637         -** into UCS-2 and then from UCS-2 to the current codepage.
   638         -** The returned char pointer has to be freed.
   639         -*/
   640         -static char *convertUtf8PathToCp( const char *in ){
   641         -  UniChar tempPath[CCHMAXPATH];
   642         -  char *out = (char *)calloc( CCHMAXPATH, 1 );
   643         -
   644         -  if( !out )
   645         -    return NULL;
   646         -
   647         -  if( !ucUtf8 || !uclCp )
   648         -    initUconvObjects();
   649         -
   650         -  /* determine string for the conversion of UTF-8 which is CP1208 */
   651         -  if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
   652         -    return out; /* if conversion fails, return the empty string */
   653         -
   654         -  /* conversion for current codepage which can be used for paths */
   655         -  UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
   656         -
   657         -  return out;
   658         -}
   659         -
   660         -/*
   661         -** Helper function to convert filenames from local codepage to UTF-8.
   662         -** The two-step process: first convert the incoming codepage-specific
   663         -** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
   664         -** The returned char pointer has to be freed.
   665         -**
   666         -** This function is non-static to be able to use this in shell.c and
   667         -** similar applications that take command line arguments.
   668         -*/
   669         -char *convertCpPathToUtf8( const char *in ){
   670         -  UniChar tempPath[CCHMAXPATH];
   671         -  char *out = (char *)calloc( CCHMAXPATH, 1 );
   672         -
   673         -  if( !out )
   674         -    return NULL;
   675         -
   676         -  if( !ucUtf8 || !uclCp )
   677         -    initUconvObjects();
   678         -
   679         -  /* conversion for current codepage which can be used for paths */
   680         -  if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
   681         -    return out; /* if conversion fails, return the empty string */
   682         -
   683         -  /* determine string for the conversion of UTF-8 which is CP1208 */
   684         -  UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
   685         -
   686         -  return out;
   687         -}
   688         -
   689         -
   690         -#ifndef SQLITE_OMIT_WAL
   691         -
   692         -/*
   693         -** Use main database file for interprocess locking. If un-defined
   694         -** a separate file is created for this purpose. The file will be
   695         -** used only to set file locks. There will be no data written to it.
   696         -*/
   697         -#define SQLITE_OS2_NO_WAL_LOCK_FILE     
   698         -
   699         -#if 0
   700         -static void _ERR_TRACE( const char *fmt, ... ) {
   701         -  va_list  ap;
   702         -  va_start(ap, fmt);
   703         -  vfprintf(stderr, fmt, ap);
   704         -  fflush(stderr);
   705         -}
   706         -#define ERR_TRACE(rc, msg)        \
   707         -        if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
   708         -#else
   709         -#define ERR_TRACE(rc, msg)
   710         -#endif
   711         -
   712         -/*
   713         -** Helper functions to obtain and relinquish the global mutex. The
   714         -** global mutex is used to protect os2ShmNodeList.
   715         -**
   716         -** Function os2ShmMutexHeld() is used to assert() that the global mutex 
   717         -** is held when required. This function is only used as part of assert() 
   718         -** statements. e.g.
   719         -**
   720         -**   os2ShmEnterMutex()
   721         -**     assert( os2ShmMutexHeld() );
   722         -**   os2ShmLeaveMutex()
   723         -*/
   724         -static void os2ShmEnterMutex(void){
   725         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   726         -}
   727         -static void os2ShmLeaveMutex(void){
   728         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   729         -}
   730         -#ifdef SQLITE_DEBUG
   731         -static int os2ShmMutexHeld(void) {
   732         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   733         -}
   734         -int GetCurrentProcessId(void) {
   735         -  PPIB pib;
   736         -  DosGetInfoBlocks(NULL, &pib);
   737         -  return (int)pib->pib_ulpid;
   738         -}
   739         -#endif
   740         -
   741         -/*
   742         -** Object used to represent a the shared memory area for a single log file.
   743         -** When multiple threads all reference the same log-summary, each thread has
   744         -** its own os2File object, but they all point to a single instance of this 
   745         -** object.  In other words, each log-summary is opened only once per process.
   746         -**
   747         -** os2ShmMutexHeld() must be true when creating or destroying
   748         -** this object or while reading or writing the following fields:
   749         -**
   750         -**      nRef
   751         -**      pNext 
   752         -**
   753         -** The following fields are read-only after the object is created:
   754         -** 
   755         -**      szRegion
   756         -**      hLockFile
   757         -**      shmBaseName
   758         -**
   759         -** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
   760         -** os2ShmMutexHeld() is true when reading or writing any other field
   761         -** in this structure.
   762         -**
   763         -*/
   764         -struct os2ShmNode {
   765         -  sqlite3_mutex *mutex;      /* Mutex to access this object */
   766         -  os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
   767         -
   768         -  int szRegion;              /* Size of shared-memory regions */
   769         -
   770         -  int nRegion;               /* Size of array apRegion */
   771         -  void **apRegion;           /* Array of pointers to shared-memory regions */
   772         -
   773         -  int nRef;                  /* Number of os2ShmLink objects pointing to this */
   774         -  os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
   775         -
   776         -  HFILE hLockFile;           /* File used for inter-process memory locking */
   777         -  char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
   778         -};
   779         -
   780         -
   781         -/*
   782         -** Structure used internally by this VFS to record the state of an
   783         -** open shared memory connection.
   784         -**
   785         -** The following fields are initialized when this object is created and
   786         -** are read-only thereafter:
   787         -**
   788         -**    os2Shm.pShmNode
   789         -**    os2Shm.id
   790         -**
   791         -** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
   792         -** while accessing any read/write fields.
   793         -*/
   794         -struct os2ShmLink {
   795         -  os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
   796         -  os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
   797         -  u32 sharedMask;            /* Mask of shared locks held */
   798         -  u32 exclMask;              /* Mask of exclusive locks held */
   799         -#ifdef SQLITE_DEBUG
   800         -  u8 id;                     /* Id of this connection with its os2ShmNode */
   801         -#endif
   802         -};
   803         -
   804         -
   805         -/*
   806         -** A global list of all os2ShmNode objects.
   807         -**
   808         -** The os2ShmMutexHeld() must be true while reading or writing this list.
   809         -*/
   810         -static os2ShmNode *os2ShmNodeList = NULL;
   811         -
   812         -/*
   813         -** Constants used for locking
   814         -*/
   815         -#ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
   816         -#define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
   817         -#else
   818         -#define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
   819         -#endif
   820         -
   821         -#define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
   822         -
   823         -/*
   824         -** Apply advisory locks for all n bytes beginning at ofst.
   825         -*/
   826         -#define _SHM_UNLCK  1   /* no lock */
   827         -#define _SHM_RDLCK  2   /* shared lock, no wait */
   828         -#define _SHM_WRLCK  3   /* exlusive lock, no wait */
   829         -#define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
   830         -static int os2ShmSystemLock(
   831         -  os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
   832         -  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
   833         -  int ofst,             /* Offset to first byte to be locked/unlocked */
   834         -  int nByte             /* Number of bytes to lock or unlock */
   835         -){
   836         -  APIRET rc;
   837         -  FILELOCK area;
   838         -  ULONG mode, timeout;
   839         -
   840         -  /* Access to the os2ShmNode object is serialized by the caller */
   841         -  assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
   842         -
   843         -  mode = 1;     /* shared lock */
   844         -  timeout = 0;  /* no wait */
   845         -  area.lOffset = ofst;
   846         -  area.lRange = nByte;
   847         -
   848         -  switch( lockType ) {
   849         -    case _SHM_WRLCK_WAIT:
   850         -      timeout = (ULONG)-1;      /* wait forever */
   851         -    case _SHM_WRLCK:
   852         -      mode = 0;                 /* exclusive lock */
   853         -    case _SHM_RDLCK:
   854         -      rc = DosSetFileLocks(pNode->hLockFile, 
   855         -                           NULL, &area, timeout, mode);
   856         -      break;
   857         -    /* case _SHM_UNLCK: */
   858         -    default:
   859         -      rc = DosSetFileLocks(pNode->hLockFile, 
   860         -                           &area, NULL, 0, 0);
   861         -      break;
   862         -  }
   863         -                          
   864         -  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
   865         -           pNode->hLockFile,
   866         -           rc==SQLITE_OK ? "ok" : "failed",
   867         -           lockType==_SHM_UNLCK ? "Unlock" : "Lock",
   868         -           rc));
   869         -
   870         -  ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
   871         -
   872         -  return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
   873         -}
   874         -
   875         -/*
   876         -** Find an os2ShmNode in global list or allocate a new one, if not found.
   877         -**
   878         -** This is not a VFS shared-memory method; it is a utility function called
   879         -** by VFS shared-memory methods.
   880         -*/
   881         -static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
   882         -  os2ShmLink *pLink;
   883         -  os2ShmNode *pNode;
   884         -  int cbShmName, rc = SQLITE_OK;
   885         -  char shmName[CCHMAXPATH + 30];
   886         -#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
   887         -  ULONG action;
   888         -#endif
   889         -  
   890         -  /* We need some additional space at the end to append the region number */
   891         -  cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
   892         -  if( cbShmName >= CCHMAXPATH-8 )
   893         -    return SQLITE_IOERR_SHMOPEN; 
   894         -
   895         -  /* Replace colon in file name to form a valid shared memory name */
   896         -  shmName[10+1] = '!';
   897         -
   898         -  /* Allocate link object (we free it later in case of failure) */
   899         -  pLink = sqlite3_malloc( sizeof(*pLink) );
   900         -  if( !pLink )
   901         -    return SQLITE_NOMEM;
   902         -
   903         -  /* Access node list */
   904         -  os2ShmEnterMutex();
   905         -
   906         -  /* Find node by it's shared memory base name */
   907         -  for( pNode = os2ShmNodeList; 
   908         -       pNode && stricmp(shmName, pNode->shmBaseName) != 0; 
   909         -       pNode = pNode->pNext )   ;
   910         -
   911         -  /* Not found: allocate a new node */
   912         -  if( !pNode ) {
   913         -    pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
   914         -    if( pNode ) {
   915         -      memset(pNode, 0, sizeof(*pNode) );
   916         -      pNode->szRegion = szRegion;
   917         -      pNode->hLockFile = (HFILE)-1;      
   918         -      strcpy(pNode->shmBaseName, shmName);
   919         -
   920         -#ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
   921         -      if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
   922         -#else
   923         -      sprintf(shmName, "%s-lck", fd->zFullPathCp);
   924         -      if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL, 
   925         -                  OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
   926         -                  OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | 
   927         -                  OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
   928         -                  NULL) != 0 ) {
   929         -#endif
   930         -        sqlite3_free(pNode);  
   931         -        rc = SQLITE_IOERR;
   932         -      } else {
   933         -        pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
   934         -        if( !pNode->mutex ) {
   935         -          sqlite3_free(pNode);  
   936         -          rc = SQLITE_NOMEM;
   937         -        }
   938         -      }   
   939         -    } else {
   940         -      rc = SQLITE_NOMEM;
   941         -    }
   942         -    
   943         -    if( rc == SQLITE_OK ) {
   944         -      pNode->pNext = os2ShmNodeList;
   945         -      os2ShmNodeList = pNode;
   946         -    } else {
   947         -      pNode = NULL;
   948         -    }
   949         -  } else if( pNode->szRegion != szRegion ) {
   950         -    rc = SQLITE_IOERR_SHMSIZE;
   951         -    pNode = NULL;
   952         -  }
   953         -
   954         -  if( pNode ) {
   955         -    sqlite3_mutex_enter(pNode->mutex);
   956         -
   957         -    memset(pLink, 0, sizeof(*pLink));
   958         -
   959         -    pLink->pShmNode = pNode;
   960         -    pLink->pNext = pNode->pFirst;
   961         -    pNode->pFirst = pLink;
   962         -    pNode->nRef++;
   963         -
   964         -    fd->pShmLink = pLink;
   965         -
   966         -    sqlite3_mutex_leave(pNode->mutex);
   967         -    
   968         -  } else {
   969         -    /* Error occured. Free our link object. */
   970         -    sqlite3_free(pLink);  
   971         -  }
   972         -
   973         -  os2ShmLeaveMutex();
   974         -
   975         -  ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))  
   976         -  
   977         -  return rc;
   978         -}
   979         -
   980         -/*
   981         -** Purge the os2ShmNodeList list of all entries with nRef==0.
   982         -**
   983         -** This is not a VFS shared-memory method; it is a utility function called
   984         -** by VFS shared-memory methods.
   985         -*/
   986         -static void os2PurgeShmNodes( int deleteFlag ) {
   987         -  os2ShmNode *pNode;
   988         -  os2ShmNode **ppNode;
   989         -
   990         -  os2ShmEnterMutex();
   991         -  
   992         -  ppNode = &os2ShmNodeList;
   993         -
   994         -  while( *ppNode ) {
   995         -    pNode = *ppNode;
   996         -
   997         -    if( pNode->nRef == 0 ) {
   998         -      *ppNode = pNode->pNext;   
   999         -     
  1000         -      if( pNode->apRegion ) {
  1001         -        /* Prevent other processes from resizing the shared memory */
  1002         -        os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
  1003         -
  1004         -        while( pNode->nRegion-- ) {
  1005         -#ifdef SQLITE_DEBUG
  1006         -          int rc = 
  1007         -#endif          
  1008         -          DosFreeMem(pNode->apRegion[pNode->nRegion]);
  1009         -
  1010         -          OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
  1011         -                  (int)GetCurrentProcessId(), pNode->nRegion,
  1012         -                  rc == 0 ? "ok" : "failed"));
  1013         -        }
  1014         -
  1015         -        /* Allow other processes to resize the shared memory */
  1016         -        os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
  1017         -
  1018         -        sqlite3_free(pNode->apRegion);
  1019         -      }  
  1020         -
  1021         -      DosClose(pNode->hLockFile);
  1022         -      
  1023         -#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
  1024         -      if( deleteFlag ) {
  1025         -         char fileName[CCHMAXPATH];
  1026         -         /* Skip "\\SHAREMEM\\" */
  1027         -         sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
  1028         -         /* restore colon */
  1029         -         fileName[1] = ':';
  1030         -         
  1031         -         DosForceDelete(fileName); 
  1032         -      }
  1033         -#endif
  1034         -
  1035         -      sqlite3_mutex_free(pNode->mutex);
  1036         -
  1037         -      sqlite3_free(pNode);
  1038         -      
  1039         -    } else {
  1040         -      ppNode = &pNode->pNext;
  1041         -    }
  1042         -  } 
  1043         -
  1044         -  os2ShmLeaveMutex();
  1045         -}
  1046         -
  1047         -/*
  1048         -** This function is called to obtain a pointer to region iRegion of the
  1049         -** shared-memory associated with the database file id. Shared-memory regions
  1050         -** are numbered starting from zero. Each shared-memory region is szRegion
  1051         -** bytes in size.
  1052         -**
  1053         -** If an error occurs, an error code is returned and *pp is set to NULL.
  1054         -**
  1055         -** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
  1056         -** region has not been allocated (by any client, including one running in a
  1057         -** separate process), then *pp is set to NULL and SQLITE_OK returned. If
  1058         -** bExtend is non-zero and the requested shared-memory region has not yet
  1059         -** been allocated, it is allocated by this function.
  1060         -**
  1061         -** If the shared-memory region has already been allocated or is allocated by
  1062         -** this call as described above, then it is mapped into this processes
  1063         -** address space (if it is not already), *pp is set to point to the mapped
  1064         -** memory and SQLITE_OK returned.
  1065         -*/
  1066         -static int os2ShmMap(
  1067         -  sqlite3_file *id,               /* Handle open on database file */
  1068         -  int iRegion,                    /* Region to retrieve */
  1069         -  int szRegion,                   /* Size of regions */
  1070         -  int bExtend,                    /* True to extend block if necessary */
  1071         -  void volatile **pp              /* OUT: Mapped memory */
  1072         -){
  1073         -  PVOID pvTemp;
  1074         -  void **apRegion;
  1075         -  os2ShmNode *pNode;
  1076         -  int n, rc = SQLITE_OK;
  1077         -  char shmName[CCHMAXPATH];
  1078         -  os2File *pFile = (os2File*)id;
  1079         -  
  1080         -  *pp = NULL;
  1081         -
  1082         -  if( !pFile->pShmLink )
  1083         -    rc = os2OpenSharedMemory( pFile, szRegion );
  1084         -  
  1085         -  if( rc == SQLITE_OK ) {
  1086         -    pNode = pFile->pShmLink->pShmNode ;
  1087         -    
  1088         -    sqlite3_mutex_enter(pNode->mutex);
  1089         -    
  1090         -    assert( szRegion==pNode->szRegion );
  1091         -
  1092         -    /* Unmapped region ? */
  1093         -    if( iRegion >= pNode->nRegion ) {
  1094         -      /* Prevent other processes from resizing the shared memory */
  1095         -      os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
  1096         -
  1097         -      apRegion = sqlite3_realloc(
  1098         -        pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
  1099         -
  1100         -      if( apRegion ) {
  1101         -        pNode->apRegion = apRegion;
  1102         -
  1103         -        while( pNode->nRegion <= iRegion ) {
  1104         -          sprintf(shmName, "%s-%u", 
  1105         -                  pNode->shmBaseName, pNode->nRegion);
  1106         -
  1107         -          if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName, 
  1108         -                PAG_READ | PAG_WRITE) != NO_ERROR ) {
  1109         -            if( !bExtend )
  1110         -              break;
  1111         -
  1112         -            if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
  1113         -                  PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR && 
  1114         -                DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
  1115         -                  PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) { 
  1116         -              rc = SQLITE_NOMEM;
  1117         -              break;
  1118         -            }
  1119         -          }
  1120         -
  1121         -          apRegion[pNode->nRegion++] = pvTemp;
  1122         -        }
  1123         -
  1124         -        /* zero out remaining entries */ 
  1125         -        for( n = pNode->nRegion; n <= iRegion; n++ )
  1126         -          pNode->apRegion[n] = NULL;
  1127         -
  1128         -        /* Return this region (maybe zero) */
  1129         -        *pp = pNode->apRegion[iRegion];
  1130         -      } else {
  1131         -        rc = SQLITE_NOMEM;
  1132         -      }
  1133         -
  1134         -      /* Allow other processes to resize the shared memory */
  1135         -      os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
  1136         -      
  1137         -    } else {
  1138         -      /* Region has been mapped previously */
  1139         -      *pp = pNode->apRegion[iRegion];
  1140         -    }
  1141         -
  1142         -    sqlite3_mutex_leave(pNode->mutex);
  1143         -  } 
  1144         -
  1145         -  ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n", 
  1146         -                 pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
  1147         -          
  1148         -  return rc;
  1149         -}
  1150         -
  1151         -/*
  1152         -** Close a connection to shared-memory.  Delete the underlying
  1153         -** storage if deleteFlag is true.
  1154         -**
  1155         -** If there is no shared memory associated with the connection then this
  1156         -** routine is a harmless no-op.
  1157         -*/
  1158         -static int os2ShmUnmap(
  1159         -  sqlite3_file *id,               /* The underlying database file */
  1160         -  int deleteFlag                  /* Delete shared-memory if true */
  1161         -){
  1162         -  os2File *pFile = (os2File*)id;
  1163         -  os2ShmLink *pLink = pFile->pShmLink;
  1164         -  
  1165         -  if( pLink ) {
  1166         -    int nRef = -1;
  1167         -    os2ShmLink **ppLink;
  1168         -    os2ShmNode *pNode = pLink->pShmNode;
  1169         -
  1170         -    sqlite3_mutex_enter(pNode->mutex);
  1171         -    
  1172         -    for( ppLink = &pNode->pFirst;
  1173         -         *ppLink && *ppLink != pLink;
  1174         -         ppLink = &(*ppLink)->pNext )   ;
  1175         -         
  1176         -    assert(*ppLink);
  1177         -
  1178         -    if( *ppLink ) {
  1179         -      *ppLink = pLink->pNext;
  1180         -      nRef = --pNode->nRef;
  1181         -    } else {
  1182         -      ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n", 
  1183         -                    pNode->shmBaseName))
  1184         -    }
  1185         -    
  1186         -    pFile->pShmLink = NULL;
  1187         -    sqlite3_free(pLink);
  1188         -
  1189         -    sqlite3_mutex_leave(pNode->mutex);
  1190         -    
  1191         -    if( nRef == 0 )
  1192         -      os2PurgeShmNodes( deleteFlag );
  1193         -  }
  1194         -
  1195         -  return SQLITE_OK;
  1196         -}
  1197         -
  1198         -/*
  1199         -** Change the lock state for a shared-memory segment.
  1200         -**
  1201         -** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
  1202         -** different here than in posix.  In xShmLock(), one can go from unlocked
  1203         -** to shared and back or from unlocked to exclusive and back.  But one may
  1204         -** not go from shared to exclusive or from exclusive to shared.
  1205         -*/
  1206         -static int os2ShmLock(
  1207         -  sqlite3_file *id,          /* Database file holding the shared memory */
  1208         -  int ofst,                  /* First lock to acquire or release */
  1209         -  int n,                     /* Number of locks to acquire or release */
  1210         -  int flags                  /* What to do with the lock */
  1211         -){
  1212         -  u32 mask;                             /* Mask of locks to take or release */
  1213         -  int rc = SQLITE_OK;                   /* Result code */
  1214         -  os2File *pFile = (os2File*)id;
  1215         -  os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
  1216         -  os2ShmLink *pX;                       /* For looping over all siblings */
  1217         -  os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
  1218         -  
  1219         -  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
  1220         -  assert( n>=1 );
  1221         -  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
  1222         -       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
  1223         -       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
  1224         -       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
  1225         -  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
  1226         -
  1227         -  mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
  1228         -  assert( n>1 || mask==(1<<ofst) );
  1229         -
  1230         -
  1231         -  sqlite3_mutex_enter(pShmNode->mutex);
  1232         -
  1233         -  if( flags & SQLITE_SHM_UNLOCK ){
  1234         -    u32 allMask = 0; /* Mask of locks held by siblings */
  1235         -
  1236         -    /* See if any siblings hold this same lock */
  1237         -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
  1238         -      if( pX==p ) continue;
  1239         -      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
  1240         -      allMask |= pX->sharedMask;
  1241         -    }
  1242         -
  1243         -    /* Unlock the system-level locks */
  1244         -    if( (mask & allMask)==0 ){
  1245         -      rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
  1246         -    }else{
  1247         -      rc = SQLITE_OK;
  1248         -    }
  1249         -
  1250         -    /* Undo the local locks */
  1251         -    if( rc==SQLITE_OK ){
  1252         -      p->exclMask &= ~mask;
  1253         -      p->sharedMask &= ~mask;
  1254         -    } 
  1255         -  }else if( flags & SQLITE_SHM_SHARED ){
  1256         -    u32 allShared = 0;  /* Union of locks held by connections other than "p" */
  1257         -
  1258         -    /* Find out which shared locks are already held by sibling connections.
  1259         -    ** If any sibling already holds an exclusive lock, go ahead and return
  1260         -    ** SQLITE_BUSY.
  1261         -    */
  1262         -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
  1263         -      if( (pX->exclMask & mask)!=0 ){
  1264         -        rc = SQLITE_BUSY;
  1265         -        break;
  1266         -      }
  1267         -      allShared |= pX->sharedMask;
  1268         -    }
  1269         -
  1270         -    /* Get shared locks at the system level, if necessary */
  1271         -    if( rc==SQLITE_OK ){
  1272         -      if( (allShared & mask)==0 ){
  1273         -        rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
  1274         -      }else{
  1275         -        rc = SQLITE_OK;
  1276         -      }
  1277         -    }
  1278         -
  1279         -    /* Get the local shared locks */
  1280         -    if( rc==SQLITE_OK ){
  1281         -      p->sharedMask |= mask;
  1282         -    }
  1283         -  }else{
  1284         -    /* Make sure no sibling connections hold locks that will block this
  1285         -    ** lock.  If any do, return SQLITE_BUSY right away.
  1286         -    */
  1287         -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
  1288         -      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
  1289         -        rc = SQLITE_BUSY;
  1290         -        break;
  1291         -      }
  1292         -    }
  1293         -  
  1294         -    /* Get the exclusive locks at the system level.  Then if successful
  1295         -    ** also mark the local connection as being locked.
  1296         -    */
  1297         -    if( rc==SQLITE_OK ){
  1298         -      rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
  1299         -      if( rc==SQLITE_OK ){
  1300         -        assert( (p->sharedMask & mask)==0 );
  1301         -        p->exclMask |= mask;
  1302         -      }
  1303         -    }
  1304         -  }
  1305         -
  1306         -  sqlite3_mutex_leave(pShmNode->mutex);
  1307         -  
  1308         -  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
  1309         -           p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
  1310         -           rc ? "failed" : "ok"));
  1311         -
  1312         -  ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n", 
  1313         -                 ofst, n, flags, rc))
  1314         -                  
  1315         -  return rc; 
  1316         -}
  1317         -
  1318         -/*
  1319         -** Implement a memory barrier or memory fence on shared memory.
  1320         -**
  1321         -** All loads and stores begun before the barrier must complete before
  1322         -** any load or store begun after the barrier.
  1323         -*/
  1324         -static void os2ShmBarrier(
  1325         -  sqlite3_file *id                /* Database file holding the shared memory */
  1326         -){
  1327         -  UNUSED_PARAMETER(id);
  1328         -  os2ShmEnterMutex();
  1329         -  os2ShmLeaveMutex();
  1330         -}
  1331         -
  1332         -#else
  1333         -# define os2ShmMap     0
  1334         -# define os2ShmLock    0
  1335         -# define os2ShmBarrier 0
  1336         -# define os2ShmUnmap   0
  1337         -#endif /* #ifndef SQLITE_OMIT_WAL */
  1338         -
  1339         -
  1340         -/*
  1341         -** This vector defines all the methods that can operate on an
  1342         -** sqlite3_file for os2.
  1343         -*/
  1344         -static const sqlite3_io_methods os2IoMethod = {
  1345         -  2,                              /* iVersion */
  1346         -  os2Close,                       /* xClose */
  1347         -  os2Read,                        /* xRead */
  1348         -  os2Write,                       /* xWrite */
  1349         -  os2Truncate,                    /* xTruncate */
  1350         -  os2Sync,                        /* xSync */
  1351         -  os2FileSize,                    /* xFileSize */
  1352         -  os2Lock,                        /* xLock */
  1353         -  os2Unlock,                      /* xUnlock */
  1354         -  os2CheckReservedLock,           /* xCheckReservedLock */
  1355         -  os2FileControl,                 /* xFileControl */
  1356         -  os2SectorSize,                  /* xSectorSize */
  1357         -  os2DeviceCharacteristics,       /* xDeviceCharacteristics */
  1358         -  os2ShmMap,                      /* xShmMap */
  1359         -  os2ShmLock,                     /* xShmLock */
  1360         -  os2ShmBarrier,                  /* xShmBarrier */
  1361         -  os2ShmUnmap                     /* xShmUnmap */
  1362         -};
  1363         -
  1364         -
  1365         -/***************************************************************************
  1366         -** Here ends the I/O methods that form the sqlite3_io_methods object.
  1367         -**
  1368         -** The next block of code implements the VFS methods.
  1369         -****************************************************************************/
  1370         -
  1371         -/*
  1372         -** Create a temporary file name in zBuf.  zBuf must be big enough to
  1373         -** hold at pVfs->mxPathname characters.
  1374         -*/
  1375         -static int getTempname(int nBuf, char *zBuf ){
  1376         -  static const char zChars[] =
  1377         -    "abcdefghijklmnopqrstuvwxyz"
  1378         -    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1379         -    "0123456789";
  1380         -  int i, j;
  1381         -  PSZ zTempPathCp;      
  1382         -  char zTempPath[CCHMAXPATH];
  1383         -  ULONG ulDriveNum, ulDriveMap;
  1384         -  
  1385         -  /* It's odd to simulate an io-error here, but really this is just
  1386         -  ** using the io-error infrastructure to test that SQLite handles this
  1387         -  ** function failing. 
  1388         -  */
  1389         -  SimulateIOError( return SQLITE_IOERR );
  1390         -
  1391         -  if( sqlite3_temp_directory ) {
  1392         -    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
  1393         -  } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
  1394         -             DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
  1395         -             DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
  1396         -    char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
  1397         -    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
  1398         -    free( zTempPathUTF );
  1399         -  } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
  1400         -    zTempPath[0] = (char)('A' + ulDriveNum - 1);
  1401         -    zTempPath[1] = ':'; 
  1402         -    zTempPath[2] = '\0'; 
  1403         -  } else {
  1404         -    zTempPath[0] = '\0'; 
  1405         -  }
  1406         -  
  1407         -  /* Strip off a trailing slashes or backslashes, otherwise we would get *
  1408         -   * multiple (back)slashes which causes DosOpen() to fail.              *
  1409         -   * Trailing spaces are not allowed, either.                            */
  1410         -  j = sqlite3Strlen30(zTempPath);
  1411         -  while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' || 
  1412         -                    zTempPath[j-1] == ' ' ) ){
  1413         -    j--;
  1414         -  }
  1415         -  zTempPath[j] = '\0';
  1416         -  
  1417         -  /* We use 20 bytes to randomize the name */
  1418         -  sqlite3_snprintf(nBuf-22, zBuf,
  1419         -                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
  1420         -  j = sqlite3Strlen30(zBuf);
  1421         -  sqlite3_randomness( 20, &zBuf[j] );
  1422         -  for( i = 0; i < 20; i++, j++ ){
  1423         -    zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  1424         -  }
  1425         -  zBuf[j] = 0;
  1426         -
  1427         -  OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
  1428         -  return SQLITE_OK;
  1429         -}
  1430         -
  1431         -
  1432         -/*
  1433         -** Turn a relative pathname into a full pathname.  Write the full
  1434         -** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
  1435         -** bytes in size.
  1436         -*/
  1437         -static int os2FullPathname(
  1438         -  sqlite3_vfs *pVfs,          /* Pointer to vfs object */
  1439         -  const char *zRelative,      /* Possibly relative input path */
  1440         -  int nFull,                  /* Size of output buffer in bytes */
  1441         -  char *zFull                 /* Output buffer */
  1442         -){
  1443         -  char *zRelativeCp = convertUtf8PathToCp( zRelative );
  1444         -  char zFullCp[CCHMAXPATH] = "\0";
  1445         -  char *zFullUTF;
  1446         -  APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME, 
  1447         -                                zFullCp, CCHMAXPATH );
  1448         -  free( zRelativeCp );
  1449         -  zFullUTF = convertCpPathToUtf8( zFullCp );
  1450         -  sqlite3_snprintf( nFull, zFull, zFullUTF );
  1451         -  free( zFullUTF );
  1452         -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
  1453         -}
  1454         -
  1455         -
  1456         -/*
  1457         -** Open a file.
  1458         -*/
  1459         -static int os2Open(
  1460         -  sqlite3_vfs *pVfs,            /* Not used */
  1461         -  const char *zName,            /* Name of the file (UTF-8) */
  1462         -  sqlite3_file *id,             /* Write the SQLite file handle here */
  1463         -  int flags,                    /* Open mode flags */
  1464         -  int *pOutFlags                /* Status return flags */
  1465         -){
  1466         -  HFILE h;
  1467         -  ULONG ulOpenFlags = 0;
  1468         -  ULONG ulOpenMode = 0;
  1469         -  ULONG ulAction = 0;
  1470         -  ULONG rc;
  1471         -  os2File *pFile = (os2File*)id;
  1472         -  const char *zUtf8Name = zName;
  1473         -  char *zNameCp;
  1474         -  char  zTmpname[CCHMAXPATH];
  1475         -
  1476         -  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
  1477         -  int isCreate     = (flags & SQLITE_OPEN_CREATE);
  1478         -  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
  1479         -#ifndef NDEBUG
  1480         -  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  1481         -  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  1482         -  int eType        = (flags & 0xFFFFFF00);
  1483         -  int isOpenJournal = (isCreate && (
  1484         -        eType==SQLITE_OPEN_MASTER_JOURNAL 
  1485         -     || eType==SQLITE_OPEN_MAIN_JOURNAL 
  1486         -     || eType==SQLITE_OPEN_WAL
  1487         -  ));
  1488         -#endif
  1489         -
  1490         -  UNUSED_PARAMETER(pVfs);
  1491         -  assert( id!=0 );
  1492         -
  1493         -  /* Check the following statements are true: 
  1494         -  **
  1495         -  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
  1496         -  **   (b) if CREATE is set, then READWRITE must also be set, and
  1497         -  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
  1498         -  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  1499         -  */
  1500         -  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
  1501         -  assert(isCreate==0 || isReadWrite);
  1502         -  assert(isExclusive==0 || isCreate);
  1503         -  assert(isDelete==0 || isCreate);
  1504         -
  1505         -  /* The main DB, main journal, WAL file and master journal are never 
  1506         -  ** automatically deleted. Nor are they ever temporary files.  */
  1507         -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
  1508         -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
  1509         -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
  1510         -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
  1511         -
  1512         -  /* Assert that the upper layer has set one of the "file-type" flags. */
  1513         -  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
  1514         -       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
  1515         -       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
  1516         -       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  1517         -  );
  1518         -
  1519         -  memset( pFile, 0, sizeof(*pFile) );
  1520         -  pFile->h = (HFILE)-1;
  1521         -
  1522         -  /* If the second argument to this function is NULL, generate a 
  1523         -  ** temporary file name to use 
  1524         -  */
  1525         -  if( !zUtf8Name ){
  1526         -    assert(isDelete && !isOpenJournal);
  1527         -    rc = getTempname(CCHMAXPATH, zTmpname);
  1528         -    if( rc!=SQLITE_OK ){
  1529         -      return rc;
  1530         -    }
  1531         -    zUtf8Name = zTmpname;
  1532         -  }
  1533         -
  1534         -  if( isReadWrite ){
  1535         -    ulOpenMode |= OPEN_ACCESS_READWRITE;
  1536         -  }else{
  1537         -    ulOpenMode |= OPEN_ACCESS_READONLY;
  1538         -  }
  1539         -
  1540         -  /* Open in random access mode for possibly better speed.  Allow full
  1541         -  ** sharing because file locks will provide exclusive access when needed.
  1542         -  ** The handle should not be inherited by child processes and we don't 
  1543         -  ** want popups from the critical error handler.
  1544         -  */
  1545         -  ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE | 
  1546         -                OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
  1547         -
  1548         -  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
  1549         -  ** created. SQLite doesn't use it to indicate "exclusive access" 
  1550         -  ** as it is usually understood.
  1551         -  */
  1552         -  if( isExclusive ){
  1553         -    /* Creates a new file, only if it does not already exist. */
  1554         -    /* If the file exists, it fails. */
  1555         -    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
  1556         -  }else if( isCreate ){
  1557         -    /* Open existing file, or create if it doesn't exist */
  1558         -    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  1559         -  }else{
  1560         -    /* Opens a file, only if it exists. */
  1561         -    ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  1562         -  }
  1563         -
  1564         -  zNameCp = convertUtf8PathToCp( zUtf8Name );
  1565         -  rc = DosOpen( (PSZ)zNameCp,
  1566         -                &h,
  1567         -                &ulAction,
  1568         -                0L,
  1569         -                FILE_NORMAL,
  1570         -                ulOpenFlags,
  1571         -                ulOpenMode,
  1572         -                (PEAOP2)NULL );
  1573         -  free( zNameCp );
  1574         -
  1575         -  if( rc != NO_ERROR ){
  1576         -    OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
  1577         -              rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
  1578         -
  1579         -    if( isReadWrite ){
  1580         -      return os2Open( pVfs, zName, id,
  1581         -                      ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
  1582         -                      pOutFlags );
  1583         -    }else{
  1584         -      return SQLITE_CANTOPEN;
  1585         -    }
  1586         -  }
  1587         -
  1588         -  if( pOutFlags ){
  1589         -    *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
  1590         -  }
  1591         -
  1592         -  os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
  1593         -  pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
  1594         -  pFile->pMethod = &os2IoMethod;
  1595         -  pFile->flags = flags;
  1596         -  pFile->h = h;
  1597         -
  1598         -  OpenCounter(+1);
  1599         -  OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
  1600         -  return SQLITE_OK;
  1601         -}
  1602         -
  1603         -/*
  1604         -** Delete the named file.
  1605         -*/
  1606         -static int os2Delete(
  1607         -  sqlite3_vfs *pVfs,                     /* Not used on os2 */
  1608         -  const char *zFilename,                 /* Name of file to delete */
  1609         -  int syncDir                            /* Not used on os2 */
  1610         -){
  1611         -  APIRET rc;
  1612         -  char *zFilenameCp;
  1613         -  SimulateIOError( return SQLITE_IOERR_DELETE );
  1614         -  zFilenameCp = convertUtf8PathToCp( zFilename );
  1615         -  rc = DosDelete( (PSZ)zFilenameCp );
  1616         -  free( zFilenameCp );
  1617         -  OSTRACE(( "DELETE \"%s\"\n", zFilename ));
  1618         -  return (rc == NO_ERROR ||
  1619         -          rc == ERROR_FILE_NOT_FOUND ||
  1620         -          rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
  1621         -}
  1622         -
  1623         -/*
  1624         -** Check the existance and status of a file.
  1625         -*/
  1626         -static int os2Access(
  1627         -  sqlite3_vfs *pVfs,        /* Not used on os2 */
  1628         -  const char *zFilename,    /* Name of file to check */
  1629         -  int flags,                /* Type of test to make on this file */
  1630         -  int *pOut                 /* Write results here */
  1631         -){
  1632         -  APIRET rc;
  1633         -  FILESTATUS3 fsts3ConfigInfo;
  1634         -  char *zFilenameCp;
  1635         -
  1636         -  UNUSED_PARAMETER(pVfs);
  1637         -  SimulateIOError( return SQLITE_IOERR_ACCESS; );
  1638         -  
  1639         -  zFilenameCp = convertUtf8PathToCp( zFilename );
  1640         -  rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
  1641         -                         &fsts3ConfigInfo, sizeof(FILESTATUS3) );
  1642         -  free( zFilenameCp );
  1643         -  OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
  1644         -            fsts3ConfigInfo.attrFile, flags, rc ));
  1645         -
  1646         -  switch( flags ){
  1647         -    case SQLITE_ACCESS_EXISTS:
  1648         -      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
  1649         -      ** as if it does not exist.
  1650         -      */
  1651         -      if( fsts3ConfigInfo.cbFile == 0 ) 
  1652         -        rc = ERROR_FILE_NOT_FOUND;
  1653         -      break;
  1654         -    case SQLITE_ACCESS_READ:
  1655         -      break;
  1656         -    case SQLITE_ACCESS_READWRITE:
  1657         -      if( fsts3ConfigInfo.attrFile & FILE_READONLY )
  1658         -        rc = ERROR_ACCESS_DENIED;
  1659         -      break;
  1660         -    default:
  1661         -      rc = ERROR_FILE_NOT_FOUND;
  1662         -      assert( !"Invalid flags argument" );
  1663         -  }
  1664         -
  1665         -  *pOut = (rc == NO_ERROR);
  1666         -  OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
  1667         -
  1668         -  return SQLITE_OK;
  1669         -}
  1670         -
  1671         -
  1672         -#ifndef SQLITE_OMIT_LOAD_EXTENSION
  1673         -/*
  1674         -** Interfaces for opening a shared library, finding entry points
  1675         -** within the shared library, and closing the shared library.
  1676         -*/
  1677         -/*
  1678         -** Interfaces for opening a shared library, finding entry points
  1679         -** within the shared library, and closing the shared library.
  1680         -*/
  1681         -static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
  1682         -  HMODULE hmod;
  1683         -  APIRET rc;
  1684         -  char *zFilenameCp = convertUtf8PathToCp(zFilename);
  1685         -  rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
  1686         -  free(zFilenameCp);
  1687         -  return rc != NO_ERROR ? 0 : (void*)hmod;
  1688         -}
  1689         -/*
  1690         -** A no-op since the error code is returned on the DosLoadModule call.
  1691         -** os2Dlopen returns zero if DosLoadModule is not successful.
  1692         -*/
  1693         -static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
  1694         -/* no-op */
  1695         -}
  1696         -static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
  1697         -  PFN pfn;
  1698         -  APIRET rc;
  1699         -  rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
  1700         -  if( rc != NO_ERROR ){
  1701         -    /* if the symbol itself was not found, search again for the same
  1702         -     * symbol with an extra underscore, that might be needed depending
  1703         -     * on the calling convention */
  1704         -    char _zSymbol[256] = "_";
  1705         -    strncat(_zSymbol, zSymbol, 254);
  1706         -    rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
  1707         -  }
  1708         -  return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
  1709         -}
  1710         -static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
  1711         -  DosFreeModule((HMODULE)pHandle);
  1712         -}
  1713         -#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
  1714         -  #define os2DlOpen 0
  1715         -  #define os2DlError 0
  1716         -  #define os2DlSym 0
  1717         -  #define os2DlClose 0
  1718         -#endif
  1719         -
  1720         -
  1721         -/*
  1722         -** Write up to nBuf bytes of randomness into zBuf.
  1723         -*/
  1724         -static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
  1725         -  int n = 0;
  1726         -#if defined(SQLITE_TEST)
  1727         -  n = nBuf;
  1728         -  memset(zBuf, 0, nBuf);
  1729         -#else
  1730         -  int i;                           
  1731         -  PPIB ppib;
  1732         -  PTIB ptib;
  1733         -  DATETIME dt; 
  1734         -  static unsigned c = 0;
  1735         -  /* Ordered by variation probability */
  1736         -  static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
  1737         -                            QSV_MAXPRMEM, QSV_MAXSHMEM,
  1738         -                            QSV_TOTAVAILMEM, QSV_TOTRESMEM };
  1739         -
  1740         -  /* 8 bytes; timezone and weekday don't increase the randomness much */
  1741         -  if( (int)sizeof(dt)-3 <= nBuf - n ){
  1742         -    c += 0x0100;
  1743         -    DosGetDateTime(&dt);
  1744         -    dt.year = (USHORT)((dt.year - 1900) | c);
  1745         -    memcpy(&zBuf[n], &dt, sizeof(dt)-3);
  1746         -    n += sizeof(dt)-3;
  1747         -  }
  1748         -
  1749         -  /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
  1750         -  if( (int)sizeof(ULONG) <= nBuf - n ){
  1751         -    DosGetInfoBlocks(&ptib, &ppib);
  1752         -    *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
  1753         -                                 ptib->tib_ptib2->tib2_ultid);
  1754         -    n += sizeof(ULONG);
  1755         -  }
  1756         -
  1757         -  /* Up to 6 * 4 bytes; variables depend on the system state */
  1758         -  for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
  1759         -    DosQuerySysInfo(svIdx[i], svIdx[i], 
  1760         -                    (PULONG)&zBuf[n], sizeof(ULONG));
  1761         -    n += sizeof(ULONG);
  1762         -  } 
  1763         -#endif
  1764         -
  1765         -  return n;
  1766         -}
  1767         -
  1768         -/*
  1769         -** Sleep for a little while.  Return the amount of time slept.
  1770         -** The argument is the number of microseconds we want to sleep.
  1771         -** The return value is the number of microseconds of sleep actually
  1772         -** requested from the underlying operating system, a number which
  1773         -** might be greater than or equal to the argument, but not less
  1774         -** than the argument.
  1775         -*/
  1776         -static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
  1777         -  DosSleep( (microsec/1000) );
  1778         -  return microsec;
  1779         -}
  1780         -
  1781         -/*
  1782         -** The following variable, if set to a non-zero value, becomes the result
  1783         -** returned from sqlite3OsCurrentTime().  This is used for testing.
  1784         -*/
  1785         -#ifdef SQLITE_TEST
  1786         -int sqlite3_current_time = 0;
  1787         -#endif
  1788         -
  1789         -/*
  1790         -** Find the current time (in Universal Coordinated Time).  Write into *piNow
  1791         -** the current time and date as a Julian Day number times 86_400_000.  In
  1792         -** other words, write into *piNow the number of milliseconds since the Julian
  1793         -** epoch of noon in Greenwich on November 24, 4714 B.C according to the
  1794         -** proleptic Gregorian calendar.
  1795         -**
  1796         -** On success, return 0.  Return 1 if the time and date cannot be found.
  1797         -*/
  1798         -static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
  1799         -#ifdef SQLITE_TEST
  1800         -  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
  1801         -#endif
  1802         -  int year, month, datepart, timepart;
  1803         - 
  1804         -  DATETIME dt;
  1805         -  DosGetDateTime( &dt );
  1806         -
  1807         -  year = dt.year;
  1808         -  month = dt.month;
  1809         -
  1810         -  /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
  1811         -  ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
  1812         -  ** Calculate the Julian days
  1813         -  */
  1814         -  datepart = (int)dt.day - 32076 +
  1815         -    1461*(year + 4800 + (month - 14)/12)/4 +
  1816         -    367*(month - 2 - (month - 14)/12*12)/12 -
  1817         -    3*((year + 4900 + (month - 14)/12)/100)/4;
  1818         -
  1819         -  /* Time in milliseconds, hours to noon added */
  1820         -  timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
  1821         -    ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
  1822         -
  1823         -  *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
  1824         -   
  1825         -#ifdef SQLITE_TEST
  1826         -  if( sqlite3_current_time ){
  1827         -    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
  1828         -  }
  1829         -#endif
  1830         -
  1831         -  UNUSED_PARAMETER(pVfs);
  1832         -  return 0;
  1833         -}
  1834         -
  1835         -/*
  1836         -** Find the current time (in Universal Coordinated Time).  Write the
  1837         -** current time and date as a Julian Day number into *prNow and
  1838         -** return 0.  Return 1 if the time and date cannot be found.
  1839         -*/
  1840         -static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
  1841         -  int rc;
  1842         -  sqlite3_int64 i;
  1843         -  rc = os2CurrentTimeInt64(pVfs, &i);
  1844         -  if( !rc ){
  1845         -    *prNow = i/86400000.0;
  1846         -  }
  1847         -  return rc;
  1848         -}
  1849         -
  1850         -/*
  1851         -** The idea is that this function works like a combination of
  1852         -** GetLastError() and FormatMessage() on windows (or errno and
  1853         -** strerror_r() on unix). After an error is returned by an OS
  1854         -** function, SQLite calls this function with zBuf pointing to
  1855         -** a buffer of nBuf bytes. The OS layer should populate the
  1856         -** buffer with a nul-terminated UTF-8 encoded error message
  1857         -** describing the last IO error to have occurred within the calling
  1858         -** thread.
  1859         -**
  1860         -** If the error message is too large for the supplied buffer,
  1861         -** it should be truncated. The return value of xGetLastError
  1862         -** is zero if the error message fits in the buffer, or non-zero
  1863         -** otherwise (if the message was truncated). If non-zero is returned,
  1864         -** then it is not necessary to include the nul-terminator character
  1865         -** in the output buffer.
  1866         -**
  1867         -** Not supplying an error message will have no adverse effect
  1868         -** on SQLite. It is fine to have an implementation that never
  1869         -** returns an error message:
  1870         -**
  1871         -**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  1872         -**     assert(zBuf[0]=='\0');
  1873         -**     return 0;
  1874         -**   }
  1875         -**
  1876         -** However if an error message is supplied, it will be incorporated
  1877         -** by sqlite into the error message available to the user using
  1878         -** sqlite3_errmsg(), possibly making IO errors easier to debug.
  1879         -*/
  1880         -static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  1881         -  assert(zBuf[0]=='\0');
  1882         -  return 0;
  1883         -}
  1884         -
  1885         -/*
  1886         -** Initialize and deinitialize the operating system interface.
  1887         -*/
  1888         -int sqlite3_os_init(void){
  1889         -  static sqlite3_vfs os2Vfs = {
  1890         -    3,                 /* iVersion */
  1891         -    sizeof(os2File),   /* szOsFile */
  1892         -    CCHMAXPATH,        /* mxPathname */
  1893         -    0,                 /* pNext */
  1894         -    "os2",             /* zName */
  1895         -    0,                 /* pAppData */
  1896         -
  1897         -    os2Open,           /* xOpen */
  1898         -    os2Delete,         /* xDelete */
  1899         -    os2Access,         /* xAccess */
  1900         -    os2FullPathname,   /* xFullPathname */
  1901         -    os2DlOpen,         /* xDlOpen */
  1902         -    os2DlError,        /* xDlError */
  1903         -    os2DlSym,          /* xDlSym */
  1904         -    os2DlClose,        /* xDlClose */
  1905         -    os2Randomness,     /* xRandomness */
  1906         -    os2Sleep,          /* xSleep */
  1907         -    os2CurrentTime,    /* xCurrentTime */
  1908         -    os2GetLastError,   /* xGetLastError */
  1909         -    os2CurrentTimeInt64, /* xCurrentTimeInt64 */
  1910         -    0,                 /* xSetSystemCall */
  1911         -    0,                 /* xGetSystemCall */
  1912         -    0                  /* xNextSystemCall */
  1913         -  };
  1914         -  sqlite3_vfs_register(&os2Vfs, 1);
  1915         -  initUconvObjects();
  1916         -/*  sqlite3OSTrace = 1; */
  1917         -  return SQLITE_OK;
  1918         -}
  1919         -int sqlite3_os_end(void){
  1920         -  freeUconvObjects();
  1921         -  return SQLITE_OK;
  1922         -}
  1923         -
  1924         -#endif /* SQLITE_OS_OS2 */

Changes to src/os_unix.c.

  1006   1006       /* random NFS retry error, unless during file system support 
  1007   1007        * introspection, in which it actually means what it says */
  1008   1008       return SQLITE_BUSY;
  1009   1009       
  1010   1010     case EACCES: 
  1011   1011       /* EACCES is like EAGAIN during locking operations, but not any other time*/
  1012   1012       if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
  1013         -	(sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
  1014         -	(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
  1015         -	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
         1013  +        (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
         1014  +        (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
         1015  +        (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
  1016   1016         return SQLITE_BUSY;
  1017   1017       }
  1018   1018       /* else fall through */
  1019   1019     case EPERM: 
  1020   1020       return SQLITE_PERM;
  1021   1021       
  1022   1022     /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
................................................................................
  2121   2121           tErrno = errno;
  2122   2122           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  2123   2123           if( IS_LOCK_ERROR(rc) ){
  2124   2124             storeLastErrno(pFile, tErrno);
  2125   2125           }
  2126   2126   #else
  2127   2127           rc = SQLITE_IOERR_UNLOCK;
  2128         -	    storeLastErrno(pFile, errno);
         2128  +        storeLastErrno(pFile, errno);
  2129   2129   #endif
  2130   2130           pInode->eFileLock = NO_LOCK;
  2131   2131           pFile->eFileLock = NO_LOCK;
  2132   2132         }
  2133   2133       }
  2134   2134   
  2135   2135       /* Decrement the count of locks against this same file.  When the
................................................................................
  2138   2138       */
  2139   2139       pInode->nLock--;
  2140   2140       assert( pInode->nLock>=0 );
  2141   2141       if( pInode->nLock==0 ){
  2142   2142         closePendingFds(pFile);
  2143   2143       }
  2144   2144     }
  2145         -	
         2145  +
  2146   2146   end_unlock:
  2147   2147     unixLeaveMutex();
  2148   2148     if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
  2149   2149     return rc;
  2150   2150   }
  2151   2151   
  2152   2152   /*
................................................................................
  2436   2436   static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
  2437   2437     unixFile *pFile = (unixFile*)id;
  2438   2438     char *zLockFile = (char *)pFile->lockingContext;
  2439   2439     int rc;
  2440   2440   
  2441   2441     assert( pFile );
  2442   2442     OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
  2443         -	   pFile->eFileLock, getpid()));
         2443  +           pFile->eFileLock, getpid()));
  2444   2444     assert( eFileLock<=SHARED_LOCK );
  2445   2445     
  2446   2446     /* no-op if possible */
  2447   2447     if( pFile->eFileLock==eFileLock ){
  2448   2448       return SQLITE_OK;
  2449   2449     }
  2450   2450   
................................................................................
  2831   2831   static int semUnlock(sqlite3_file *id, int eFileLock) {
  2832   2832     unixFile *pFile = (unixFile*)id;
  2833   2833     sem_t *pSem = pFile->pInode->pSem;
  2834   2834   
  2835   2835     assert( pFile );
  2836   2836     assert( pSem );
  2837   2837     OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
  2838         -	   pFile->eFileLock, getpid()));
         2838  +           pFile->eFileLock, getpid()));
  2839   2839     assert( eFileLock<=SHARED_LOCK );
  2840   2840     
  2841   2841     /* no-op if possible */
  2842   2842     if( pFile->eFileLock==eFileLock ){
  2843   2843       return SQLITE_OK;
  2844   2844     }
  2845   2845     
................................................................................
  6830   6830   ** as POSIX read & write locks over fixed set of locations (via fsctl),
  6831   6831   ** on AFP and SMB only exclusive byte-range locks are available via fsctl
  6832   6832   ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
  6833   6833   ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
  6834   6834   ** address in the shared range is taken for a SHARED lock, the entire
  6835   6835   ** shared range is taken for an EXCLUSIVE lock):
  6836   6836   **
  6837         -**      PENDING_BYTE        0x40000000		   	
         6837  +**      PENDING_BYTE        0x40000000
  6838   6838   **      RESERVED_BYTE       0x40000001
  6839   6839   **      SHARED_RANGE        0x40000002 -> 0x40000200
  6840   6840   **
  6841   6841   ** This works well on the local file system, but shows a nearly 100x
  6842   6842   ** slowdown in read performance on AFP because the AFP client disables
  6843   6843   ** the read cache when byte-range locks are present.  Enabling the read
  6844   6844   ** cache exposes a cache coherency problem that is present on all OS X

Changes to src/os_win.c.

    42     42   # define FILE_FLAG_MASK          (0xFF3C0000)
    43     43   #endif
    44     44   
    45     45   #ifndef FILE_ATTRIBUTE_MASK
    46     46   # define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
    47     47   #endif
    48     48   
           49  +#ifndef SQLITE_OMIT_WAL
    49     50   /* Forward references */
    50     51   typedef struct winShm winShm;           /* A connection to shared-memory */
    51     52   typedef struct winShmNode winShmNode;   /* A region of shared-memory */
           53  +#endif
    52     54   
    53     55   /*
    54     56   ** WinCE lacks native support for file locking so we have to fake it
    55     57   ** with some code of our own.
    56     58   */
    57     59   #if SQLITE_OS_WINCE
    58     60   typedef struct winceLock {
................................................................................
    72     74     const sqlite3_io_methods *pMethod; /*** Must be first ***/
    73     75     sqlite3_vfs *pVfs;      /* The VFS used to open this file */
    74     76     HANDLE h;               /* Handle for accessing the file */
    75     77     u8 locktype;            /* Type of lock currently held on this file */
    76     78     short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
    77     79     u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
    78     80     DWORD lastErrno;        /* The Windows errno from the last I/O error */
           81  +#ifndef SQLITE_OMIT_WAL
    79     82     winShm *pShm;           /* Instance of shared memory on this file */
           83  +#endif
    80     84     const char *zPath;      /* Full pathname of this file */
    81     85     int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
    82     86   #if SQLITE_OS_WINCE
    83     87     LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
    84     88     HANDLE hMutex;          /* Mutex used to control access to shared lock */  
    85     89     HANDLE hShared;         /* Shared memory segment used for locking */
    86     90     winceLock local;        /* Locks obtained by this instance of winFile */
................................................................................
    97    101   /*
    98    102    * The size of the buffer used by sqlite3_win32_write_debug().
    99    103    */
   100    104   #ifndef SQLITE_WIN32_DBG_BUF_SIZE
   101    105   #  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
   102    106   #endif
   103    107   
          108  +/*
          109  + * The value used with sqlite3_win32_set_directory() to specify that
          110  + * the data directory should be changed.
          111  + */
          112  +#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
          113  +#  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
          114  +#endif
          115  +
          116  +/*
          117  + * The value used with sqlite3_win32_set_directory() to specify that
          118  + * the temporary directory should be changed.
          119  + */
          120  +#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
          121  +#  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
          122  +#endif
          123  +
   104    124   /*
   105    125    * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
   106    126    * various Win32 API heap functions instead of our own.
   107    127    */
   108    128   #ifdef SQLITE_WIN32_MALLOC
   109    129   
   110    130   /*
................................................................................
   284    304   #else
   285    305     { "CreateFileW",             (SYSCALL)0,                       0 },
   286    306   #endif
   287    307   
   288    308   #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
   289    309           LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
   290    310   
   291         -#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
          311  +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
          312  +        !defined(SQLITE_OMIT_WAL))
   292    313     { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
   293    314   #else
   294    315     { "CreateFileMappingW",      (SYSCALL)0,                       0 },
   295    316   #endif
   296    317   
   297    318   #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
   298    319           DWORD,DWORD,DWORD,LPCWSTR))aSyscall[6].pCurrent)
................................................................................
   596    617   #endif
   597    618   
   598    619   #ifndef osLockFileEx
   599    620   #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
   600    621           LPOVERLAPPED))aSyscall[45].pCurrent)
   601    622   #endif
   602    623   
   603         -#if !SQLITE_OS_WINRT
          624  +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
   604    625     { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
   605    626   #else
   606    627     { "MapViewOfFile",           (SYSCALL)0,                       0 },
   607    628   #endif
   608    629   
   609    630   #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   610    631           SIZE_T))aSyscall[46].pCurrent)
................................................................................
   666    687   #else
   667    688     { "UnlockFileEx",            (SYSCALL)0,                       0 },
   668    689   #endif
   669    690   
   670    691   #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   671    692           LPOVERLAPPED))aSyscall[55].pCurrent)
   672    693   
          694  +#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
   673    695     { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
          696  +#else
          697  +  { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
          698  +#endif
   674    699   
   675    700   #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[56].pCurrent)
   676    701   
   677    702     { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
   678    703   
   679    704   #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
   680    705           LPCSTR,LPBOOL))aSyscall[57].pCurrent)
................................................................................
   698    723   #else
   699    724     { "WaitForSingleObject",     (SYSCALL)0,                       0 },
   700    725   #endif
   701    726   
   702    727   #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
   703    728           DWORD))aSyscall[60].pCurrent)
   704    729   
   705         -#if !SQLITE_OS_WINCE
          730  +#if SQLITE_OS_WINRT
   706    731     { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
   707    732   #else
   708    733     { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
   709    734   #endif
   710    735   
   711    736   #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
   712    737           BOOL))aSyscall[61].pCurrent)
   713    738   
   714         -#if !SQLITE_OS_WINCE
          739  +#if SQLITE_OS_WINRT
   715    740     { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
   716    741   #else
   717    742     { "SetFilePointerEx",        (SYSCALL)0,                       0 },
   718    743   #endif
   719    744   
   720    745   #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
   721    746           PLARGE_INTEGER,DWORD))aSyscall[62].pCurrent)
................................................................................
   725    750   #else
   726    751     { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
   727    752   #endif
   728    753   
   729    754   #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
   730    755           FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[63].pCurrent)
   731    756   
   732         -#if SQLITE_OS_WINRT
          757  +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
   733    758     { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
   734    759   #else
   735    760     { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
   736    761   #endif
   737    762   
   738    763   #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
   739    764           SIZE_T))aSyscall[64].pCurrent)
................................................................................
   789    814   
   790    815   #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[70].pCurrent)
   791    816   
   792    817     { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
   793    818   
   794    819   #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[71].pCurrent)
   795    820   
   796         -#if SQLITE_OS_WINRT
          821  +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
   797    822     { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
   798    823   #else
   799    824     { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
   800    825   #endif
   801    826   
   802    827   #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
   803    828           LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[72].pCurrent)
................................................................................
  1305   1330       return 0;
  1306   1331     }
  1307   1332     zFilenameMbcs = unicodeToMbcs(zTmpWide);
  1308   1333     sqlite3_free(zTmpWide);
  1309   1334     return zFilenameMbcs;
  1310   1335   }
  1311   1336   
         1337  +/*
         1338  +** This function sets the data directory or the temporary directory based on
         1339  +** the provided arguments.  The type argument must be 1 in order to set the
         1340  +** data directory or 2 in order to set the temporary directory.  The zValue
         1341  +** argument is the name of the directory to use.  The return value will be
         1342  +** SQLITE_OK if successful.
         1343  +*/
         1344  +int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
         1345  +  char **ppDirectory = 0;
         1346  +#ifndef SQLITE_OMIT_AUTOINIT
         1347  +  int rc = sqlite3_initialize();
         1348  +  if( rc ) return rc;
         1349  +#endif
         1350  +  if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
         1351  +    ppDirectory = &sqlite3_data_directory;
         1352  +  }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
         1353  +    ppDirectory = &sqlite3_temp_directory;
         1354  +  }
         1355  +  assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
         1356  +          || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
         1357  +  );
         1358  +  assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
         1359  +  if( ppDirectory ){
         1360  +    char *zValueUtf8 = 0;
         1361  +    if( zValue && zValue[0] ){
         1362  +      zValueUtf8 = unicodeToUtf8(zValue);
         1363  +      if ( zValueUtf8==0 ){
         1364  +        return SQLITE_NOMEM;
         1365  +      }
         1366  +    }
         1367  +    sqlite3_free(*ppDirectory);
         1368  +    *ppDirectory = zValueUtf8;
         1369  +    return SQLITE_OK;
         1370  +  }
         1371  +  return SQLITE_ERROR;
         1372  +}
  1312   1373   
  1313   1374   /*
  1314   1375   ** The return value of getLastErrorMsg
  1315   1376   ** is zero if the error message fits in the buffer, or non-zero
  1316   1377   ** otherwise (if the message was truncated).
  1317   1378   */
  1318   1379   static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
................................................................................
  1921   1982   */
  1922   1983   #define MX_CLOSE_ATTEMPT 3
  1923   1984   static int winClose(sqlite3_file *id){
  1924   1985     int rc, cnt = 0;
  1925   1986     winFile *pFile = (winFile*)id;
  1926   1987   
  1927   1988     assert( id!=0 );
         1989  +#ifndef SQLITE_OMIT_WAL
  1928   1990     assert( pFile->pShm==0 );
         1991  +#endif
  1929   1992     OSTRACE(("CLOSE %d\n", pFile->h));
  1930   1993     do{
  1931   1994       rc = osCloseHandle(pFile->h);
  1932   1995       /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
  1933   1996     }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
  1934   1997   #if SQLITE_OS_WINCE
  1935   1998   #define WINCE_DELETION_ATTEMPTS 3
................................................................................
  3680   3743     }
  3681   3744   
  3682   3745     memset(pFile, 0, sizeof(*pFile));
  3683   3746     pFile->pMethod = &winIoMethod;
  3684   3747     pFile->h = h;
  3685   3748     pFile->lastErrno = NO_ERROR;
  3686   3749     pFile->pVfs = pVfs;
         3750  +#ifndef SQLITE_OMIT_WAL
  3687   3751     pFile->pShm = 0;
         3752  +#endif
  3688   3753     pFile->zPath = zName;
  3689   3754     if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
  3690   3755       pFile->ctrlFlags |= WINFILE_PSOW;
  3691   3756     }
  3692   3757   
  3693   3758   #if SQLITE_OS_WINCE
  3694   3759     if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB

Changes to src/pcache1.c.

   392    392     nNew = p->nHash*2;
   393    393     if( nNew<256 ){
   394    394       nNew = 256;
   395    395     }
   396    396   
   397    397     pcache1LeaveMutex(p->pGroup);
   398    398     if( p->nHash ){ sqlite3BeginBenignMalloc(); }
   399         -  apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
          399  +  apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
   400    400     if( p->nHash ){ sqlite3EndBenignMalloc(); }
   401    401     pcache1EnterMutex(p->pGroup);
   402    402     if( apNew ){
   403         -    memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
   404    403       for(i=0; i<p->nHash; i++){
   405    404         PgHdr1 *pPage;
   406    405         PgHdr1 *pNext = p->apHash[i];
   407    406         while( (pPage = pNext)!=0 ){
   408    407           unsigned int h = pPage->iKey % nNew;
   409    408           pNext = pPage->pNext;
   410    409           pPage->pNext = apNew[h];
................................................................................
   580    579     int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
   581    580   #endif
   582    581   
   583    582     assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
   584    583     assert( szExtra < 300 );
   585    584   
   586    585     sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
   587         -  pCache = (PCache1 *)sqlite3_malloc(sz);
          586  +  pCache = (PCache1 *)sqlite3MallocZero(sz);
   588    587     if( pCache ){
   589         -    memset(pCache, 0, sz);
   590    588       if( separateCache ){
   591    589         pGroup = (PGroup*)&pCache[1];
   592    590         pGroup->mxPinned = 10;
   593    591       }else{
   594    592         pGroup = &pcache1.grp;
   595    593       }
   596    594       pCache->pGroup = pGroup;

Changes to src/pragma.c.

  1175   1175         { OP_AddImm,      1, 0,        0},    /* 0 */
  1176   1176         { OP_IfNeg,       1, 0,        0},    /* 1 */
  1177   1177         { OP_String8,     0, 3,        0},    /* 2 */
  1178   1178         { OP_ResultRow,   3, 1,        0},
  1179   1179       };
  1180   1180   
  1181   1181       int isQuick = (sqlite3Tolower(zLeft[0])=='q');
         1182  +
         1183  +    /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
         1184  +    ** then iDb is set to the index of the database identified by <db>.
         1185  +    ** In this case, the integrity of database iDb only is verified by
         1186  +    ** the VDBE created below.
         1187  +    **
         1188  +    ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
         1189  +    ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
         1190  +    ** to -1 here, to indicate that the VDBE should verify the integrity
         1191  +    ** of all attached databases.  */
         1192  +    assert( iDb>=0 );
         1193  +    assert( iDb==0 || pId2->z );
         1194  +    if( pId2->z==0 ) iDb = -1;
  1182   1195   
  1183   1196       /* Initialize the VDBE program */
  1184   1197       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  1185   1198       pParse->nMem = 6;
  1186   1199       sqlite3VdbeSetNumCols(v, 1);
  1187   1200       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
  1188   1201   
................................................................................
  1199   1212       /* Do an integrity check on each database file */
  1200   1213       for(i=0; i<db->nDb; i++){
  1201   1214         HashElem *x;
  1202   1215         Hash *pTbls;
  1203   1216         int cnt = 0;
  1204   1217   
  1205   1218         if( OMIT_TEMPDB && i==1 ) continue;
         1219  +      if( iDb>=0 && i!=iDb ) continue;
  1206   1220   
  1207   1221         sqlite3CodeVerifySchema(pParse, i);
  1208   1222         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
  1209   1223         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1210   1224         sqlite3VdbeJumpHere(v, addr);
  1211   1225   
  1212   1226         /* Do an integrity check of the B-Tree
  1213   1227         **
  1214   1228         ** Begin by filling registers 2, 3, ... with the root pages numbers
  1215   1229         ** for all tables and indices in the database.
  1216   1230         */
  1217         -      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
         1231  +      assert( sqlite3SchemaMutexHeld(db, i, 0) );
  1218   1232         pTbls = &db->aDb[i].pSchema->tblHash;
  1219   1233         for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  1220   1234           Table *pTab = sqliteHashData(x);
  1221   1235           Index *pIdx;
  1222   1236           sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
  1223   1237           cnt++;
  1224   1238           for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){

Changes to src/printf.c.

   120    120   ** The counter *cnt is incremented each time.  After counter exceeds
   121    121   ** 16 (the number of significant digits in a 64-bit float) '0' is
   122    122   ** always returned.
   123    123   */
   124    124   static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
   125    125     int digit;
   126    126     LONGDOUBLE_TYPE d;
   127         -  if( (*cnt)++ >= 16 ) return '0';
          127  +  if( (*cnt)<=0 ) return '0';
          128  +  (*cnt)--;
   128    129     digit = (int)*val;
   129    130     d = digit;
   130    131     digit += '0';
   131    132     *val = (*val - d)*10.0;
   132    133     return (char)digit;
   133    134   }
   134    135   #endif /* SQLITE_OMIT_FLOATING_POINT */
................................................................................
   424    425           exp = 0;
   425    426           if( sqlite3IsNaN((double)realvalue) ){
   426    427             bufpt = "NaN";
   427    428             length = 3;
   428    429             break;
   429    430           }
   430    431           if( realvalue>0.0 ){
   431         -          while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
   432         -          while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
   433         -          while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
          432  +          LONGDOUBLE_TYPE scale = 1.0;
          433  +          while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
          434  +          while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
          435  +          while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
          436  +          while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
          437  +          realvalue /= scale;
   434    438             while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
   435    439             while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
   436    440             if( exp>350 ){
   437    441               if( prefix=='-' ){
   438    442                 bufpt = "-Inf";
   439    443               }else if( prefix=='+' ){
   440    444                 bufpt = "+Inf";
................................................................................
   459    463             if( exp<-4 || exp>precision ){
   460    464               xtype = etEXP;
   461    465             }else{
   462    466               precision = precision - exp;
   463    467               xtype = etFLOAT;
   464    468             }
   465    469           }else{
   466         -          flag_rtz = 0;
          470  +          flag_rtz = flag_altform2;
   467    471           }
   468    472           if( xtype==etEXP ){
   469    473             e2 = 0;
   470    474           }else{
   471    475             e2 = exp;
   472    476           }
   473    477           if( e2+precision+width > etBUFSIZE - 15 ){
................................................................................
   474    478             bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
   475    479             if( bufpt==0 ){
   476    480               pAccum->mallocFailed = 1;
   477    481               return;
   478    482             }
   479    483           }
   480    484           zOut = bufpt;
   481         -        nsd = 0;
          485  +        nsd = 16 + flag_altform2*10;
   482    486           flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
   483    487           /* The sign in front of the number */
   484    488           if( prefix ){
   485    489             *(bufpt++) = prefix;
   486    490           }
   487    491           /* Digits prior to the decimal point */
   488    492           if( e2<0 ){

Changes to src/resolve.c.

    13     13   ** This file contains routines used for walking the parser tree and
    14     14   ** resolve all identifiers by associating them with a particular
    15     15   ** table and column.
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <stdlib.h>
    19     19   #include <string.h>
           20  +
           21  +/*
           22  +** Walk the expression tree pExpr and increase the aggregate function
           23  +** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
           24  +** This needs to occur when copying a TK_AGG_FUNCTION node from an
           25  +** outer query into an inner subquery.
           26  +**
           27  +** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
           28  +** is a helper function - a callback for the tree walker.
           29  +*/
           30  +static int incrAggDepth(Walker *pWalker, Expr *pExpr){
           31  +  if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
           32  +  return WRC_Continue;
           33  +}
           34  +static void incrAggFunctionDepth(Expr *pExpr, int N){
           35  +  if( N>0 ){
           36  +    Walker w;
           37  +    memset(&w, 0, sizeof(w));
           38  +    w.xExprCallback = incrAggDepth;
           39  +    w.u.i = N;
           40  +    sqlite3WalkExpr(&w, pExpr);
           41  +  }
           42  +}
    20     43   
    21     44   /*
    22     45   ** Turn the pExpr expression into an alias for the iCol-th column of the
    23     46   ** result set in pEList.
    24     47   **
    25     48   ** If the result set column is a simple column reference, then this routine
    26     49   ** makes an exact copy.  But for any other kind of expression, this
................................................................................
    40     63   ** Is equivalent to:
    41     64   **
    42     65   **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
    43     66   **
    44     67   ** The result of random()%5 in the GROUP BY clause is probably different
    45     68   ** from the result in the result-set.  We might fix this someday.  Or
    46     69   ** then again, we might not...
           70  +**
           71  +** The nSubquery parameter specifies how many levels of subquery the
           72  +** alias is removed from the original expression.  The usually value is
           73  +** zero but it might be more if the alias is contained within a subquery
           74  +** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
           75  +** structures must be increased by the nSubquery amount.
    47     76   */
    48     77   static void resolveAlias(
    49     78     Parse *pParse,         /* Parsing context */
    50     79     ExprList *pEList,      /* A result set */
    51     80     int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
    52     81     Expr *pExpr,           /* Transform this into an alias to the result set */
    53         -  const char *zType      /* "GROUP" or "ORDER" or "" */
           82  +  const char *zType,     /* "GROUP" or "ORDER" or "" */
           83  +  int nSubquery          /* Number of subqueries that the label is moving */
    54     84   ){
    55     85     Expr *pOrig;           /* The iCol-th column of the result set */
    56     86     Expr *pDup;            /* Copy of pOrig */
    57     87     sqlite3 *db;           /* The database connection */
    58     88   
    59     89     assert( iCol>=0 && iCol<pEList->nExpr );
    60     90     pOrig = pEList->a[iCol].pExpr;
    61     91     assert( pOrig!=0 );
    62     92     assert( pOrig->flags & EP_Resolved );
    63     93     db = pParse->db;
    64     94     if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
    65     95       pDup = sqlite3ExprDup(db, pOrig, 0);
           96  +    incrAggFunctionDepth(pDup, nSubquery);
    66     97       pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
    67     98       if( pDup==0 ) return;
    68     99       if( pEList->a[iCol].iAlias==0 ){
    69    100         pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
    70    101       }
    71    102       pDup->iTable = pEList->a[iCol].iAlias;
    72    103     }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
................................................................................
   147    178     Parse *pParse,       /* The parsing context */
   148    179     const char *zDb,     /* Name of the database containing table, or NULL */
   149    180     const char *zTab,    /* Name of table containing column, or NULL */
   150    181     const char *zCol,    /* Name of the column. */
   151    182     NameContext *pNC,    /* The name context used to resolve the name */
   152    183     Expr *pExpr          /* Make this EXPR node point to the selected column */
   153    184   ){
   154         -  int i, j;            /* Loop counters */
          185  +  int i, j;                         /* Loop counters */
   155    186     int cnt = 0;                      /* Number of matching column names */
   156    187     int cntTab = 0;                   /* Number of matching table names */
          188  +  int nSubquery = 0;                /* How many levels of subquery */
   157    189     sqlite3 *db = pParse->db;         /* The database connection */
   158    190     struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
   159    191     struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
   160    192     NameContext *pTopNC = pNC;        /* First namecontext in the list */
   161    193     Schema *pSchema = 0;              /* Schema of the expression */
   162    194     int isTrigger = 0;
   163    195   
................................................................................
   311    343             assert( pExpr->x.pList==0 );
   312    344             assert( pExpr->x.pSelect==0 );
   313    345             pOrig = pEList->a[j].pExpr;
   314    346             if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
   315    347               sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
   316    348               return WRC_Abort;
   317    349             }
   318         -          resolveAlias(pParse, pEList, j, pExpr, "");
          350  +          resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
   319    351             cnt = 1;
   320    352             pMatch = 0;
   321    353             assert( zTab==0 && zDb==0 );
   322    354             goto lookupname_end;
   323    355           }
   324    356         } 
   325    357       }
   326    358   
   327    359       /* Advance to the next name context.  The loop will exit when either
   328    360       ** we have a match (cnt>0) or when we run out of name contexts.
   329    361       */
   330    362       if( cnt==0 ){
   331    363         pNC = pNC->pNext;
          364  +      nSubquery++;
   332    365       }
   333    366     }
   334    367   
   335    368     /*
   336    369     ** If X and Y are NULL (in other words if only the column name Z is
   337    370     ** supplied) and the value of Z is enclosed in double-quotes, then
   338    371     ** Z is a string literal if it doesn't match any column names.  In that
................................................................................
   564    597           sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
   565    598           pNC->nErr++;
   566    599         }else if( wrong_num_args ){
   567    600           sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
   568    601                nId, zId);
   569    602           pNC->nErr++;
   570    603         }
   571         -      if( is_agg ){
   572         -        pExpr->op = TK_AGG_FUNCTION;
   573         -        pNC->ncFlags |= NC_HasAgg;
   574         -      }
   575    604         if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
   576    605         sqlite3WalkExprList(pWalker, pList);
   577         -      if( is_agg ) pNC->ncFlags |= NC_AllowAgg;
          606  +      if( is_agg ){
          607  +        NameContext *pNC2 = pNC;
          608  +        pExpr->op = TK_AGG_FUNCTION;
          609  +        pExpr->op2 = 0;
          610  +        while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
          611  +          pExpr->op2++;
          612  +          pNC2 = pNC2->pNext;
          613  +        }
          614  +        if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
          615  +        pNC->ncFlags |= NC_AllowAgg;
          616  +      }
   578    617         /* FIX ME:  Compute pExpr->affinity based on the expected return
   579    618         ** type of the function 
   580    619         */
   581    620         return WRC_Prune;
   582    621       }
   583    622   #ifndef SQLITE_OMIT_SUBQUERY
   584    623       case TK_SELECT:
................................................................................
   849    888     assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
   850    889     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
   851    890       if( pItem->iOrderByCol ){
   852    891         if( pItem->iOrderByCol>pEList->nExpr ){
   853    892           resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
   854    893           return 1;
   855    894         }
   856         -      resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType);
          895  +      resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
   857    896       }
   858    897     }
   859    898     return 0;
   860    899   }
   861    900   
   862    901   /*
   863    902   ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.

Changes to src/select.c.

    32     32   }
    33     33   
    34     34   /*
    35     35   ** Initialize a SelectDest structure.
    36     36   */
    37     37   void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
    38     38     pDest->eDest = (u8)eDest;
    39         -  pDest->iParm = iParm;
    40         -  pDest->affinity = 0;
    41         -  pDest->iMem = 0;
    42         -  pDest->nMem = 0;
           39  +  pDest->iSDParm = iParm;
           40  +  pDest->affSdst = 0;
           41  +  pDest->iSdst = 0;
           42  +  pDest->nSdst = 0;
    43     43   }
    44     44   
    45     45   
    46     46   /*
    47     47   ** Allocate a new Select structure and return a pointer to that
    48     48   ** structure.
    49     49   */
................................................................................
   547    547     int iBreak              /* Jump here to break out of the inner loop */
   548    548   ){
   549    549     Vdbe *v = pParse->pVdbe;
   550    550     int i;
   551    551     int hasDistinct;        /* True if the DISTINCT keyword is present */
   552    552     int regResult;              /* Start of memory holding result set */
   553    553     int eDest = pDest->eDest;   /* How to dispose of results */
   554         -  int iParm = pDest->iParm;   /* First argument to disposal method */
          554  +  int iParm = pDest->iSDParm; /* First argument to disposal method */
   555    555     int nResultCol;             /* Number of result columns */
   556    556   
   557    557     assert( v );
   558    558     if( NEVER(v==0) ) return;
   559    559     assert( pEList!=0 );
   560    560     hasDistinct = distinct>=0;
   561    561     if( pOrderBy==0 && !hasDistinct ){
................................................................................
   565    565     /* Pull the requested columns.
   566    566     */
   567    567     if( nColumn>0 ){
   568    568       nResultCol = nColumn;
   569    569     }else{
   570    570       nResultCol = pEList->nExpr;
   571    571     }
   572         -  if( pDest->iMem==0 ){
   573         -    pDest->iMem = pParse->nMem+1;
   574         -    pDest->nMem = nResultCol;
          572  +  if( pDest->iSdst==0 ){
          573  +    pDest->iSdst = pParse->nMem+1;
          574  +    pDest->nSdst = nResultCol;
   575    575       pParse->nMem += nResultCol;
   576    576     }else{ 
   577         -    assert( pDest->nMem==nResultCol );
          577  +    assert( pDest->nSdst==nResultCol );
   578    578     }
   579         -  regResult = pDest->iMem;
          579  +  regResult = pDest->iSdst;
   580    580     if( nColumn>0 ){
   581    581       for(i=0; i<nColumn; i++){
   582    582         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
   583    583       }
   584    584     }else if( eDest!=SRT_Exists ){
   585    585       /* If the destination is an EXISTS(...) expression, the actual
   586    586       ** values returned by the SELECT are not required.
................................................................................
   651    651   #ifndef SQLITE_OMIT_SUBQUERY
   652    652       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
   653    653       ** then there should be a single item on the stack.  Write this
   654    654       ** item into the set table with bogus data.
   655    655       */
   656    656       case SRT_Set: {
   657    657         assert( nColumn==1 );
   658         -      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
          658  +      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
   659    659         if( pOrderBy ){
   660    660           /* At first glance you would think we could optimize out the
   661    661           ** ORDER BY in this case since the order of entries in the set
   662    662           ** does not matter.  But there might be a LIMIT clause, in which
   663    663           ** case the order does matter */
   664    664           pushOntoSorter(pParse, pOrderBy, p, regResult);
   665    665         }else{
................................................................................
   706    706         testcase( eDest==SRT_Output );
   707    707         if( pOrderBy ){
   708    708           int r1 = sqlite3GetTempReg(pParse);
   709    709           sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
   710    710           pushOntoSorter(pParse, pOrderBy, p, r1);
   711    711           sqlite3ReleaseTempReg(pParse, r1);
   712    712         }else if( eDest==SRT_Coroutine ){
   713         -        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
          713  +        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
   714    714         }else{
   715    715           sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
   716    716           sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
   717    717         }
   718    718         break;
   719    719       }
   720    720   
................................................................................
   886    886     int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
   887    887     int addr;
   888    888     int iTab;
   889    889     int pseudoTab = 0;
   890    890     ExprList *pOrderBy = p->pOrderBy;
   891    891   
   892    892     int eDest = pDest->eDest;
   893         -  int iParm = pDest->iParm;
          893  +  int iParm = pDest->iSDParm;
   894    894   
   895    895     int regRow;
   896    896     int regRowid;
   897    897   
   898    898     iTab = pOrderBy->iECursor;
   899    899     regRow = sqlite3GetTempReg(pParse);
   900    900     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
................................................................................
   945    945   #endif
   946    946       default: {
   947    947         int i;
   948    948         assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
   949    949         testcase( eDest==SRT_Output );
   950    950         testcase( eDest==SRT_Coroutine );
   951    951         for(i=0; i<nColumn; i++){
   952         -        assert( regRow!=pDest->iMem+i );
   953         -        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
          952  +        assert( regRow!=pDest->iSdst+i );
          953  +        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
   954    954           if( i==0 ){
   955    955             sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
   956    956           }
   957    957         }
   958    958         if( eDest==SRT_Output ){
   959         -        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
   960         -        sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
          959  +        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
          960  +        sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
   961    961         }else{
   962         -        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
          962  +        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
   963    963         }
   964    964         break;
   965    965       }
   966    966     }
   967    967     sqlite3ReleaseTempReg(pParse, regRow);
   968    968     sqlite3ReleaseTempReg(pParse, regRowid);
   969    969   
................................................................................
  1606   1606     v = sqlite3GetVdbe(pParse);
  1607   1607     assert( v!=0 );  /* The VDBE already created by calling function */
  1608   1608   
  1609   1609     /* Create the destination temporary table if necessary
  1610   1610     */
  1611   1611     if( dest.eDest==SRT_EphemTab ){
  1612   1612       assert( p->pEList );
  1613         -    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
         1613  +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
  1614   1614       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  1615   1615       dest.eDest = SRT_Table;
  1616   1616     }
  1617   1617   
  1618   1618     /* Make sure all SELECTs in the statement have the same number of elements
  1619   1619     ** in their result sets.
  1620   1620     */
................................................................................
  1692   1692           /* We can reuse a temporary table generated by a SELECT to our
  1693   1693           ** right.
  1694   1694           */
  1695   1695           assert( p->pRightmost!=p );  /* Can only happen for leftward elements
  1696   1696                                        ** of a 3-way or more compound */
  1697   1697           assert( p->pLimit==0 );      /* Not allowed on leftward elements */
  1698   1698           assert( p->pOffset==0 );     /* Not allowed on leftward elements */
  1699         -        unionTab = dest.iParm;
         1699  +        unionTab = dest.iSDParm;
  1700   1700         }else{
  1701   1701           /* We will need to create our own temporary table to hold the
  1702   1702           ** intermediate results.
  1703   1703           */
  1704   1704           unionTab = pParse->nTab++;
  1705   1705           assert( p->pOrderBy==0 );
  1706   1706           addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
................................................................................
  1749   1749         p->pOffset = pOffset;
  1750   1750         p->iLimit = 0;
  1751   1751         p->iOffset = 0;
  1752   1752   
  1753   1753         /* Convert the data in the temporary table into whatever form
  1754   1754         ** it is that we currently need.
  1755   1755         */
  1756         -      assert( unionTab==dest.iParm || dest.eDest!=priorOp );
         1756  +      assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
  1757   1757         if( dest.eDest!=priorOp ){
  1758   1758           int iCont, iBreak, iStart;
  1759   1759           assert( p->pEList );
  1760   1760           if( dest.eDest==SRT_Output ){
  1761   1761             Select *pFirst = p;
  1762   1762             while( pFirst->pPrior ) pFirst = pFirst->pPrior;
  1763   1763             generateColumnNames(pParse, 0, pFirst->pEList);
................................................................................
  1813   1813         assert( p->addrOpenEphm[1] == -1 );
  1814   1814         p->addrOpenEphm[1] = addr;
  1815   1815         p->pPrior = 0;
  1816   1816         pLimit = p->pLimit;
  1817   1817         p->pLimit = 0;
  1818   1818         pOffset = p->pOffset;
  1819   1819         p->pOffset = 0;
  1820         -      intersectdest.iParm = tab2;
         1820  +      intersectdest.iSDParm = tab2;
  1821   1821         explainSetInteger(iSub2, pParse->iNextSelectId);
  1822   1822         rc = sqlite3Select(pParse, p, &intersectdest);
  1823   1823         testcase( rc!=SQLITE_OK );
  1824   1824         pDelete = p->pPrior;
  1825   1825         p->pPrior = pPrior;
  1826   1826         if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
  1827   1827         sqlite3ExprDelete(db, p->pLimit);
................................................................................
  1907   1907           pLoop->addrOpenEphm[i] = -1;
  1908   1908         }
  1909   1909       }
  1910   1910       sqlite3DbFree(db, pKeyInfo);
  1911   1911     }
  1912   1912   
  1913   1913   multi_select_end:
  1914         -  pDest->iMem = dest.iMem;
  1915         -  pDest->nMem = dest.nMem;
         1914  +  pDest->iSdst = dest.iSdst;
         1915  +  pDest->nSdst = dest.nSdst;
  1916   1916     sqlite3SelectDelete(db, pDelete);
  1917   1917     return rc;
  1918   1918   }
  1919   1919   #endif /* SQLITE_OMIT_COMPOUND_SELECT */
  1920   1920   
  1921   1921   /*
  1922   1922   ** Code an output subroutine for a coroutine implementation of a
  1923   1923   ** SELECT statment.
  1924   1924   **
  1925         -** The data to be output is contained in pIn->iMem.  There are
  1926         -** pIn->nMem columns to be output.  pDest is where the output should
         1925  +** The data to be output is contained in pIn->iSdst.  There are
         1926  +** pIn->nSdst columns to be output.  pDest is where the output should
  1927   1927   ** be sent.
  1928   1928   **
  1929   1929   ** regReturn is the number of the register holding the subroutine
  1930   1930   ** return address.
  1931   1931   **
  1932   1932   ** If regPrev>0 then it is the first register in a vector that
  1933   1933   ** records the previous output.  mem[regPrev] is a flag that is false
................................................................................
  1957   1957     iContinue = sqlite3VdbeMakeLabel(v);
  1958   1958   
  1959   1959     /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
  1960   1960     */
  1961   1961     if( regPrev ){
  1962   1962       int j1, j2;
  1963   1963       j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
  1964         -    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
         1964  +    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
  1965   1965                                 (char*)pKeyInfo, p4type);
  1966   1966       sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
  1967   1967       sqlite3VdbeJumpHere(v, j1);
  1968         -    sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
         1968  +    sqlite3ExprCodeCopy(pParse, pIn->iSdst, regPrev+1, pIn->nSdst);
  1969   1969       sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
  1970   1970     }
  1971   1971     if( pParse->db->mallocFailed ) return 0;
  1972   1972   
  1973   1973     /* Suppress the the first OFFSET entries if there is an OFFSET clause
  1974   1974     */
  1975   1975     codeOffset(v, p, iContinue);
................................................................................
  1979   1979       */
  1980   1980       case SRT_Table:
  1981   1981       case SRT_EphemTab: {
  1982   1982         int r1 = sqlite3GetTempReg(pParse);
  1983   1983         int r2 = sqlite3GetTempReg(pParse);
  1984   1984         testcase( pDest->eDest==SRT_Table );
  1985   1985         testcase( pDest->eDest==SRT_EphemTab );
  1986         -      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
  1987         -      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
  1988         -      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
         1986  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
         1987  +      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
         1988  +      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
  1989   1989         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1990   1990         sqlite3ReleaseTempReg(pParse, r2);
  1991   1991         sqlite3ReleaseTempReg(pParse, r1);
  1992   1992         break;
  1993   1993       }
  1994   1994   
  1995   1995   #ifndef SQLITE_OMIT_SUBQUERY
  1996   1996       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
  1997   1997       ** then there should be a single item on the stack.  Write this
  1998   1998       ** item into the set table with bogus data.
  1999   1999       */
  2000   2000       case SRT_Set: {
  2001   2001         int r1;
  2002         -      assert( pIn->nMem==1 );
         2002  +      assert( pIn->nSdst==1 );
  2003   2003         p->affinity = 
  2004         -         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
         2004  +         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
  2005   2005         r1 = sqlite3GetTempReg(pParse);
  2006         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
  2007         -      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
  2008         -      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
         2006  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &p->affinity, 1);
         2007  +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
         2008  +      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
  2009   2009         sqlite3ReleaseTempReg(pParse, r1);
  2010   2010         break;
  2011   2011       }
  2012   2012   
  2013   2013   #if 0  /* Never occurs on an ORDER BY query */
  2014   2014       /* If any row exist in the result set, record that fact and abort.
  2015   2015       */
  2016   2016       case SRT_Exists: {
  2017         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
         2017  +      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
  2018   2018         /* The LIMIT clause will terminate the loop for us */
  2019   2019         break;
  2020   2020       }
  2021   2021   #endif
  2022   2022   
  2023   2023       /* If this is a scalar select that is part of an expression, then
  2024   2024       ** store the results in the appropriate memory cell and break out
  2025   2025       ** of the scan loop.
  2026   2026       */
  2027   2027       case SRT_Mem: {
  2028         -      assert( pIn->nMem==1 );
  2029         -      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
         2028  +      assert( pIn->nSdst==1 );
         2029  +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
  2030   2030         /* The LIMIT clause will jump out of the loop for us */
  2031   2031         break;
  2032   2032       }
  2033   2033   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
  2034   2034   
  2035   2035       /* The results are stored in a sequence of registers
  2036         -    ** starting at pDest->iMem.  Then the co-routine yields.
         2036  +    ** starting at pDest->iSdst.  Then the co-routine yields.
  2037   2037       */
  2038   2038       case SRT_Coroutine: {
  2039         -      if( pDest->iMem==0 ){
  2040         -        pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
  2041         -        pDest->nMem = pIn->nMem;
         2039  +      if( pDest->iSdst==0 ){
         2040  +        pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
         2041  +        pDest->nSdst = pIn->nSdst;
  2042   2042         }
  2043         -      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
  2044         -      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
         2043  +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
         2044  +      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
  2045   2045         break;
  2046   2046       }
  2047   2047   
  2048   2048       /* If none of the above, then the result destination must be
  2049   2049       ** SRT_Output.  This routine is never called with any other
  2050   2050       ** destination other than the ones handled above or SRT_Output.
  2051   2051       **
  2052   2052       ** For SRT_Output, results are stored in a sequence of registers.  
  2053   2053       ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
  2054   2054       ** return the next row of result.
  2055   2055       */
  2056   2056       default: {
  2057   2057         assert( pDest->eDest==SRT_Output );
  2058         -      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
  2059         -      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
         2058  +      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
         2059  +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
  2060   2060         break;
  2061   2061       }
  2062   2062     }
  2063   2063   
  2064   2064     /* Jump to the end of the loop if the LIMIT is reached.
  2065   2065     */
  2066   2066     if( p->iLimit ){
................................................................................
  2471   2471     sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
  2472   2472     sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
  2473   2473   
  2474   2474     /* Implement the main merge loop
  2475   2475     */
  2476   2476     sqlite3VdbeResolveLabel(v, labelCmpr);
  2477   2477     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
  2478         -  sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
         2478  +  sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
  2479   2479                            (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
  2480   2480     sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
  2481   2481   
  2482   2482     /* Release temporary registers
  2483   2483     */
  2484   2484     if( regPrev ){
  2485   2485       sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
................................................................................
  3145   3145     }
  3146   3146     pTab = p->pSrc->a[0].pTab;
  3147   3147     pExpr = p->pEList->a[0].pExpr;
  3148   3148     assert( pTab && !pTab->pSelect && pExpr );
  3149   3149   
  3150   3150     if( IsVirtual(pTab) ) return 0;
  3151   3151     if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
  3152         -  if( pAggInfo->nFunc==0 ) return 0;
         3152  +  if( NEVER(pAggInfo->nFunc==0) ) return 0;
  3153   3153     if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
  3154   3154     if( pExpr->flags&EP_Distinct ) return 0;
  3155   3155   
  3156   3156     return pTab;
  3157   3157   }
  3158   3158   
  3159   3159   /*
................................................................................
  3517   3517     w.pParse = pParse;
  3518   3518     sqlite3WalkSelect(&w, pSelect);
  3519   3519   #endif
  3520   3520   }
  3521   3521   
  3522   3522   
  3523   3523   /*
  3524         -** This routine sets of a SELECT statement for processing.  The
         3524  +** This routine sets up a SELECT statement for processing.  The
  3525   3525   ** following is accomplished:
  3526   3526   **
  3527   3527   **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
  3528   3528   **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
  3529   3529   **     *  ON and USING clauses are shifted into WHERE statements
  3530   3530   **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
  3531   3531   **     *  Identifiers in expression are matched to tables.
................................................................................
  3549   3549   }
  3550   3550   
  3551   3551   /*
  3552   3552   ** Reset the aggregate accumulator.
  3553   3553   **
  3554   3554   ** The aggregate accumulator is a set of memory cells that hold
  3555   3555   ** intermediate results while calculating an aggregate.  This
  3556         -** routine simply stores NULLs in all of those memory cells.
         3556  +** routine generates code that stores NULLs in all of those memory
         3557  +** cells.
  3557   3558   */
  3558   3559   static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
  3559   3560     Vdbe *v = pParse->pVdbe;
  3560   3561     int i;
  3561   3562     struct AggInfo_func *pFunc;
  3562   3563     if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
  3563   3564       return;
................................................................................
  3717   3718   **     pDest->eDest    Result
  3718   3719   **     ------------    -------------------------------------------
  3719   3720   **     SRT_Output      Generate a row of output (using the OP_ResultRow
  3720   3721   **                     opcode) for each row in the result set.
  3721   3722   **
  3722   3723   **     SRT_Mem         Only valid if the result is a single column.
  3723   3724   **                     Store the first column of the first result row
  3724         -**                     in register pDest->iParm then abandon the rest
         3725  +**                     in register pDest->iSDParm then abandon the rest
  3725   3726   **                     of the query.  This destination implies "LIMIT 1".
  3726   3727   **
  3727   3728   **     SRT_Set         The result must be a single column.  Store each
  3728         -**                     row of result as the key in table pDest->iParm. 
  3729         -**                     Apply the affinity pDest->affinity before storing
         3729  +**                     row of result as the key in table pDest->iSDParm. 
         3730  +**                     Apply the affinity pDest->affSdst before storing
  3730   3731   **                     results.  Used to implement "IN (SELECT ...)".
  3731   3732   **
  3732         -**     SRT_Union       Store results as a key in a temporary table pDest->iParm.
         3733  +**     SRT_Union       Store results as a key in a temporary table 
         3734  +**                     identified by pDest->iSDParm.
  3733   3735   **
  3734         -**     SRT_Except      Remove results from the temporary table pDest->iParm.
         3736  +**     SRT_Except      Remove results from the temporary table pDest->iSDParm.
  3735   3737   **
  3736         -**     SRT_Table       Store results in temporary table pDest->iParm.
         3738  +**     SRT_Table       Store results in temporary table pDest->iSDParm.
  3737   3739   **                     This is like SRT_EphemTab except that the table
  3738   3740   **                     is assumed to already be open.
  3739   3741   **
  3740         -**     SRT_EphemTab    Create an temporary table pDest->iParm and store
         3742  +**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
  3741   3743   **                     the result there. The cursor is left open after
  3742   3744   **                     returning.  This is like SRT_Table except that
  3743   3745   **                     this destination uses OP_OpenEphemeral to create
  3744   3746   **                     the table first.
  3745   3747   **
  3746   3748   **     SRT_Coroutine   Generate a co-routine that returns a new row of
  3747   3749   **                     results each time it is invoked.  The entry point
  3748         -**                     of the co-routine is stored in register pDest->iParm.
         3750  +**                     of the co-routine is stored in register pDest->iSDParm.
  3749   3751   **
  3750         -**     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
         3752  +**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
  3751   3753   **                     set is not empty.
  3752   3754   **
  3753   3755   **     SRT_Discard     Throw the results away.  This is used by SELECT
  3754   3756   **                     statements within triggers whose only purpose is
  3755   3757   **                     the side-effects of functions.
  3756   3758   **
  3757   3759   ** This routine returns the number of errors.  If any errors are
................................................................................
  3987   3989     }else{
  3988   3990       addrSortIndex = -1;
  3989   3991     }
  3990   3992   
  3991   3993     /* If the output is destined for a temporary table, open that table.
  3992   3994     */
  3993   3995     if( pDest->eDest==SRT_EphemTab ){
  3994         -    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
         3996  +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
  3995   3997     }
  3996   3998   
  3997   3999     /* Set the limiter.
  3998   4000     */
  3999   4001     iEnd = sqlite3VdbeMakeLabel(v);
  4000   4002     p->nSelectRow = (double)LARGEST_INT64;
  4001   4003     computeLimitRegisters(pParse, p, iEnd);
................................................................................
  4018   4020     }
  4019   4021   
  4020   4022     /* Aggregate and non-aggregate queries are handled differently */
  4021   4023     if( !isAgg && pGroupBy==0 ){
  4022   4024       ExprList *pDist = (isDistinct ? p->pEList : 0);
  4023   4025   
  4024   4026       /* Begin the database scan. */
  4025         -    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0);
         4027  +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0,0);
  4026   4028       if( pWInfo==0 ) goto select_end;
  4027   4029       if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
  4028   4030   
  4029   4031       /* If sorting index that was created by a prior OP_OpenEphemeral 
  4030   4032       ** instruction ended up not being needed, then change the OP_OpenEphemeral
  4031   4033       ** into an OP_Noop.
  4032   4034       */
................................................................................
  4191   4193   
  4192   4194         /* Begin a loop that will extract all source rows in GROUP BY order.
  4193   4195         ** This might involve two separate loops with an OP_Sort in between, or
  4194   4196         ** it might be a single loop that uses an index to extract information
  4195   4197         ** in the right order to begin with.
  4196   4198         */
  4197   4199         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
  4198         -      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0);
         4200  +      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0, 0);
  4199   4201         if( pWInfo==0 ) goto select_end;
  4200   4202         if( pGroupBy==0 ){
  4201   4203           /* The optimizer is able to deliver rows in group by order so
  4202   4204           ** we do not have to sort.  The OP_OpenEphemeral table will be
  4203   4205           ** cancelled later because we still need to use the pKeyInfo
  4204   4206           */
  4205   4207           pGroupBy = p->pGroupBy;
................................................................................
  4460   4462           }
  4461   4463     
  4462   4464           /* This case runs if the aggregate has no GROUP BY clause.  The
  4463   4465           ** processing is much simpler since there is only a single row
  4464   4466           ** of output.
  4465   4467           */
  4466   4468           resetAccumulator(pParse, &sAggInfo);
  4467         -        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, 0, flag);
         4469  +        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax,0,flag,0);
  4468   4470           if( pWInfo==0 ){
  4469   4471             sqlite3ExprListDelete(db, pDel);
  4470   4472             goto select_end;
  4471   4473           }
  4472   4474           updateAccumulator(pParse, &sAggInfo);
  4473   4475           if( !pMinMax && flag ){
  4474   4476             sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);

Changes to src/shell.c.

    32     32   #include <string.h>
    33     33   #include <stdio.h>
    34     34   #include <assert.h>
    35     35   #include "sqlite3.h"
    36     36   #include <ctype.h>
    37     37   #include <stdarg.h>
    38     38   
    39         -#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
           39  +#if !defined(_WIN32) && !defined(WIN32)
    40     40   # include <signal.h>
    41     41   # if !defined(__RTP__) && !defined(_WRS_KERNEL)
    42     42   #  include <pwd.h>
    43     43   # endif
    44     44   # include <unistd.h>
    45     45   # include <sys/types.h>
    46     46   #endif
    47     47   
    48         -#ifdef __OS2__
    49         -# include <unistd.h>
    50         -#endif
    51         -
    52     48   #ifdef HAVE_EDITLINE
    53     49   # include <editline/editline.h>
    54     50   #endif
    55     51   #if defined(HAVE_READLINE) && HAVE_READLINE==1
    56     52   # include <readline/readline.h>
    57     53   # include <readline/history.h>
    58     54   #endif
................................................................................
    88     84   static int enableTimer = 0;
    89     85   
    90     86   /* ctype macros that work with signed characters */
    91     87   #define IsSpace(X)  isspace((unsigned char)X)
    92     88   #define IsDigit(X)  isdigit((unsigned char)X)
    93     89   #define ToLower(X)  (char)tolower((unsigned char)X)
    94     90   
    95         -#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
           91  +#if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL)
    96     92   #include <sys/time.h>
    97     93   #include <sys/resource.h>
    98     94   
    99     95   /* Saved resource information for the beginning of an operation */
   100     96   static struct rusage sBegin;
   101     97   
   102     98   /*
................................................................................
  1449   1445   
  1450   1446   /*
  1451   1447   ** Make sure the database is open.  If it is not, then open it.  If
  1452   1448   ** the database fails to open, print an error message and exit.
  1453   1449   */
  1454   1450   static void open_db(struct callback_data *p){
  1455   1451     if( p->db==0 ){
         1452  +    sqlite3_initialize();
  1456   1453       sqlite3_open(p->zDbFilename, &p->db);
  1457   1454       db = p->db;
  1458   1455       if( db && sqlite3_errcode(db)==SQLITE_OK ){
  1459   1456         sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
  1460   1457             shellstaticFunc, 0, 0);
  1461   1458       }
  1462   1459       if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){
................................................................................
  2464   2461       enableTimer = booleanValue(azArg[1]);
  2465   2462     }else
  2466   2463     
  2467   2464     if( c=='t' && strncmp(azArg[0], "trace", n)==0 && nArg>1 ){
  2468   2465       open_db(p);
  2469   2466       output_file_close(p->traceOut);
  2470   2467       p->traceOut = output_file_open(azArg[1]);
  2471         -#ifndef SQLITE_OMIT_TRACE
         2468  +#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
  2472   2469       if( p->traceOut==0 ){
  2473   2470         sqlite3_trace(p->db, 0, 0);
  2474   2471       }else{
  2475   2472         sqlite3_trace(p->db, sql_trace_callback, p->traceOut);
  2476   2473       }
  2477   2474   #endif
  2478   2475     }else
................................................................................
  2692   2689   ** Return a pathname which is the user's home directory.  A
  2693   2690   ** 0 return indicates an error of some kind.
  2694   2691   */
  2695   2692   static char *find_home_dir(void){
  2696   2693     static char *home_dir = NULL;
  2697   2694     if( home_dir ) return home_dir;
  2698   2695   
  2699         -#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
  2700         -  struct passwd *pwent;
  2701         -  uid_t uid = getuid();
  2702         -  if( (pwent=getpwuid(uid)) != NULL) {
  2703         -    home_dir = pwent->pw_dir;
         2696  +#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
         2697  +  {
         2698  +    struct passwd *pwent;
         2699  +    uid_t uid = getuid();
         2700  +    if( (pwent=getpwuid(uid)) != NULL) {
         2701  +      home_dir = pwent->pw_dir;
         2702  +    }
  2704   2703     }
  2705   2704   #endif
  2706   2705   
  2707   2706   #if defined(_WIN32_WCE)
  2708   2707     /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
  2709   2708      */
  2710   2709     home_dir = "/";
  2711   2710   #else
  2712   2711   
  2713         -#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
         2712  +#if defined(_WIN32) || defined(WIN32)
  2714   2713     if (!home_dir) {
  2715   2714       home_dir = getenv("USERPROFILE");
  2716   2715     }
  2717   2716   #endif
  2718   2717   
  2719   2718     if (!home_dir) {
  2720   2719       home_dir = getenv("HOME");
  2721   2720     }
  2722   2721   
  2723         -#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
         2722  +#if defined(_WIN32) || defined(WIN32)
  2724   2723     if (!home_dir) {
  2725   2724       char *zDrive, *zPath;
  2726   2725       int n;
  2727   2726       zDrive = getenv("HOMEDRIVE");
  2728   2727       zPath = getenv("HOMEPATH");
  2729   2728       if( zDrive && zPath ){
  2730   2729         n = strlen30(zDrive) + strlen30(zPath) + 1;
................................................................................
  2769   2768       home_dir = find_home_dir();
  2770   2769       if( home_dir==0 ){
  2771   2770   #if !defined(__RTP__) && !defined(_WRS_KERNEL)
  2772   2771         fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0);
  2773   2772   #endif
  2774   2773         return 1;
  2775   2774       }
         2775  +    sqlite3_initialize();
  2776   2776       zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
  2777   2777       sqliterc = zBuf;
  2778   2778     }
  2779   2779     in = fopen(sqliterc,"rb");
  2780   2780     if( in ){
  2781   2781       if( stdin_is_interactive ){
  2782   2782         fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
................................................................................
  2932   2932         }else{
  2933   2933           fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]);
  2934   2934           exit(1);
  2935   2935         }
  2936   2936       }
  2937   2937     }
  2938   2938     if( i<argc ){
  2939         -#if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
  2940         -    data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
  2941         -#else
  2942   2939       data.zDbFilename = argv[i++];
  2943         -#endif
  2944   2940     }else{
  2945   2941   #ifndef SQLITE_OMIT_MEMORYDB
  2946   2942       data.zDbFilename = ":memory:";
  2947   2943   #else
  2948   2944       data.zDbFilename = 0;
  2949   2945   #endif
  2950   2946     }

Changes to src/sqlite.h.in.

   210    210   ** CAPI3REF: Database Connection Handle
   211    211   ** KEYWORDS: {database connection} {database connections}
   212    212   **
   213    213   ** Each open SQLite database is represented by a pointer to an instance of
   214    214   ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
   215    215   ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
   216    216   ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
   217         -** is its destructor.  There are many other interfaces (such as
          217  +** and [sqlite3_close_v2()] are its destructors.  There are many other
          218  +** interfaces (such as
   218    219   ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
   219    220   ** [sqlite3_busy_timeout()] to name but three) that are methods on an
   220    221   ** sqlite3 object.
   221    222   */
   222    223   typedef struct sqlite3 sqlite3;
   223    224   
   224    225   /*
................................................................................
   257    258   #ifdef SQLITE_OMIT_FLOATING_POINT
   258    259   # define double sqlite3_int64
   259    260   #endif
   260    261   
   261    262   /*
   262    263   ** CAPI3REF: Closing A Database Connection
   263    264   **
   264         -** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
   265         -** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
   266         -** successfully destroyed and all associated resources are deallocated.
          265  +** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
          266  +** for the [sqlite3] object.
          267  +** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
          268  +** the [sqlite3] object is successfully destroyed and all associated
          269  +** resources are deallocated.
   267    270   **
   268         -** Applications must [sqlite3_finalize | finalize] all [prepared statements]
   269         -** and [sqlite3_blob_close | close] all [BLOB handles] associated with
   270         -** the [sqlite3] object prior to attempting to close the object.  ^If
          271  +** ^If the database connection is associated with unfinalized prepared
          272  +** statements or unfinished sqlite3_backup objects then sqlite3_close()
          273  +** will leave the database connection open and return [SQLITE_BUSY].
          274  +** ^If sqlite3_close_v2() is called with unfinalized prepared statements
          275  +** and unfinished sqlite3_backups, then the database connection becomes
          276  +** an unusable "zombie" which will automatically be deallocated when the
          277  +** last prepared statement is finalized or the last sqlite3_backup is
          278  +** finished.  The sqlite3_close_v2() interface is intended for use with
          279  +** host languages that are garbage collected, and where the order in which
          280  +** destructors are called is arbitrary.
          281  +**
          282  +** Applications should [sqlite3_finalize | finalize] all [prepared statements],
          283  +** [sqlite3_blob_close | close] all [BLOB handles], and 
          284  +** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
          285  +** with the [sqlite3] object prior to attempting to close the object.  ^If
   271    286   ** sqlite3_close() is called on a [database connection] that still has
   272         -** outstanding [prepared statements] or [BLOB handles], then it returns
   273         -** SQLITE_BUSY.
          287  +** outstanding [prepared statements], [BLOB handles], and/or
          288  +** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
          289  +** of resources is deferred until all [prepared statements], [BLOB handles],
          290  +** and [sqlite3_backup] objects are also destroyed.
   274    291   **
   275         -** ^If [sqlite3_close()] is invoked while a transaction is open,
          292  +** ^If an [sqlite3] object is destroyed while a transaction is open,
   276    293   ** the transaction is automatically rolled back.
   277    294   **
   278         -** The C parameter to [sqlite3_close(C)] must be either a NULL
          295  +** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
          296  +** must be either a NULL
   279    297   ** pointer or an [sqlite3] object pointer obtained
   280    298   ** from [sqlite3_open()], [sqlite3_open16()], or
   281    299   ** [sqlite3_open_v2()], and not previously closed.
   282         -** ^Calling sqlite3_close() with a NULL pointer argument is a 
   283         -** harmless no-op.
          300  +** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
          301  +** argument is a harmless no-op.
   284    302   */
   285         -int sqlite3_close(sqlite3 *);
          303  +int sqlite3_close(sqlite3*);
          304  +int sqlite3_close_v2(sqlite3*);
   286    305   
   287    306   /*
   288    307   ** The type for a callback function.
   289    308   ** This is legacy and deprecated.  It is included for historical
   290    309   ** compatibility and is not documented.
   291    310   */
   292    311   typedef int (*sqlite3_callback)(void*,int,char**, char**);
................................................................................
  3136   3155   ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
  3137   3156   **
  3138   3157   ** ^The third argument is the value to bind to the parameter.
  3139   3158   **
  3140   3159   ** ^(In those routines that have a fourth argument, its value is the
  3141   3160   ** number of bytes in the parameter.  To be clear: the value is the
  3142   3161   ** number of <u>bytes</u> in the value, not the number of characters.)^
  3143         -** ^If the fourth parameter is negative, the length of the string is
         3162  +** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
         3163  +** is negative, then the length of the string is
  3144   3164   ** the number of bytes up to the first zero terminator.
         3165  +** If the fourth parameter to sqlite3_bind_blob() is negative, then
         3166  +** the behavior is undefined.
  3145   3167   ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
  3146   3168   ** or sqlite3_bind_text16() then that parameter must be the byte offset
  3147   3169   ** where the NUL terminator would occur assuming the string were NUL
  3148   3170   ** terminated.  If any NUL characters occur at byte offsets less than 
  3149   3171   ** the value of the fourth parameter then the resulting string value will
  3150   3172   ** contain embedded NULs.  The result of expressions involving strings
  3151   3173   ** with embedded NULs is undefined.
................................................................................
  4134   4156   ** they return.  Hence, the calling function can deallocate or
  4135   4157   ** modify the text after they return without harm.
  4136   4158   ** ^The sqlite3_result_error_code() function changes the error code
  4137   4159   ** returned by SQLite as a result of an error in a function.  ^By default,
  4138   4160   ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
  4139   4161   ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
  4140   4162   **
  4141         -** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
  4142         -** indicating that a string or BLOB is too long to represent.
         4163  +** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
         4164  +** error indicating that a string or BLOB is too long to represent.
  4143   4165   **
  4144         -** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
  4145         -** indicating that a memory allocation failed.
         4166  +** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
         4167  +** error indicating that a memory allocation failed.
  4146   4168   **
  4147   4169   ** ^The sqlite3_result_int() interface sets the return value
  4148   4170   ** of the application-defined function to be the 32-bit signed integer
  4149   4171   ** value given in the 2nd argument.
  4150   4172   ** ^The sqlite3_result_int64() interface sets the return value
  4151   4173   ** of the application-defined function to be the 64-bit signed integer
  4152   4174   ** value given in the 2nd argument.
................................................................................
  5495   5517   **
  5496   5518   ** The SQLite source code contains multiple implementations
  5497   5519   ** of these mutex routines.  An appropriate implementation
  5498   5520   ** is selected automatically at compile-time.  ^(The following
  5499   5521   ** implementations are available in the SQLite core:
  5500   5522   **
  5501   5523   ** <ul>
  5502         -** <li>   SQLITE_MUTEX_OS2
  5503   5524   ** <li>   SQLITE_MUTEX_PTHREADS
  5504   5525   ** <li>   SQLITE_MUTEX_W32
  5505   5526   ** <li>   SQLITE_MUTEX_NOOP
  5506   5527   ** </ul>)^
  5507   5528   **
  5508   5529   ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
  5509   5530   ** that does no real locking and is appropriate for use in
  5510         -** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
  5511         -** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
  5512         -** are appropriate for use on OS/2, Unix, and Windows.
         5531  +** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
         5532  +** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
         5533  +** and Windows.
  5513   5534   **
  5514   5535   ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
  5515   5536   ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
  5516   5537   ** implementation is included with the library. In this case the
  5517   5538   ** application must supply a custom mutex implementation using the
  5518   5539   ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
  5519   5540   ** before calling sqlite3_initialize() or any other public sqlite3_

Changes to src/sqliteInt.h.

   145    145   
   146    146   /*
   147    147   ** Exactly one of the following macros must be defined in order to
   148    148   ** specify which memory allocation subsystem to use.
   149    149   **
   150    150   **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
   151    151   **     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
          152  +**     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
   152    153   **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
   153    154   **
   154    155   ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
   155    156   ** assert() macro is enabled, each call into the Win32 native heap subsystem
   156    157   ** will cause HeapValidate to be called.  If heap validation should fail, an
   157    158   ** assertion will be triggered.
   158    159   **
   159    160   ** (Historical note:  There used to be several other options, but we've
   160    161   ** pared it down to just these three.)
   161    162   **
   162    163   ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
   163    164   ** the default.
   164    165   */
   165         -#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
   166         -# error "At most one of the following compile-time configuration options\
   167         - is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
          166  +#if defined(SQLITE_SYSTEM_MALLOC) \
          167  +  + defined(SQLITE_WIN32_MALLOC) \
          168  +  + defined(SQLITE_ZERO_MALLOC) \
          169  +  + defined(SQLITE_MEMDEBUG)>1
          170  +# error "Two or more of the following compile-time configuration options\
          171  + are defined but at most one is allowed:\
          172  + SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
          173  + SQLITE_ZERO_MALLOC"
   168    174   #endif
   169         -#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
          175  +#if defined(SQLITE_SYSTEM_MALLOC) \
          176  +  + defined(SQLITE_WIN32_MALLOC) \
          177  +  + defined(SQLITE_ZERO_MALLOC) \
          178  +  + defined(SQLITE_MEMDEBUG)==0
   170    179   # define SQLITE_SYSTEM_MALLOC 1
   171    180   #endif
   172    181   
   173    182   /*
   174    183   ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
   175    184   ** sizes of memory allocations below this value where possible.
   176    185   */
................................................................................
   980    989   ** than being distinct from one another.
   981    990   */
   982    991   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
   983    992   #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
   984    993   #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
   985    994   #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
   986    995   #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
          996  +#define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
   987    997   
   988    998   /*
   989    999   ** Each SQL function is defined by an instance of the following
   990   1000   ** structure.  A pointer to this structure is stored in the sqlite.aFunc
   991   1001   ** hash table.  When multiple functions have the same name, the hash table
   992   1002   ** points to a linked list of these structures.
   993   1003   */
................................................................................
  1686   1696                            ** TK_REGISTER: register number
  1687   1697                            ** TK_TRIGGER: 1 -> new, 0 -> old */
  1688   1698     ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
  1689   1699                            ** TK_VARIABLE: variable number (always >= 1). */
  1690   1700     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  1691   1701     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
  1692   1702     u8 flags2;             /* Second set of flags.  EP2_... */
  1693         -  u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
  1694         -                         /* If TK_COLUMN, the value of p5 for OP_Column */
         1703  +  u8 op2;                /* TK_REGISTER: original value of Expr.op
         1704  +                         ** TK_COLUMN: the value of p5 for OP_Column
         1705  +                         ** TK_AGG_FUNCTION: nesting depth */
  1695   1706     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  1696   1707     Table *pTab;           /* Table for TK_COLUMN expressions. */
  1697   1708   #if SQLITE_MAX_EXPR_DEPTH>0
  1698   1709     int nHeight;           /* Height of the tree headed by this node */
  1699   1710   #endif
  1700   1711   };
  1701   1712   
................................................................................
  1942   1953       struct {
  1943   1954         int nIn;              /* Number of entries in aInLoop[] */
  1944   1955         struct InLoop {
  1945   1956           int iCur;              /* The VDBE cursor used by this IN operator */
  1946   1957           int addrInTop;         /* Top of the IN loop */
  1947   1958         } *aInLoop;           /* Information about each nested IN operator */
  1948   1959       } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
         1960  +    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
  1949   1961     } u;
  1950   1962   
  1951   1963     /* The following field is really not part of the current level.  But
  1952   1964     ** we need a place to cache virtual table index information for each
  1953   1965     ** virtual table in the FROM clause and the WhereLevel structure is
  1954   1966     ** a convenient place since there is one WhereLevel for each FROM clause
  1955   1967     ** element.
................................................................................
  2114   2126   /*
  2115   2127   ** A structure used to customize the behavior of sqlite3Select(). See
  2116   2128   ** comments above sqlite3Select() for details.
  2117   2129   */
  2118   2130   typedef struct SelectDest SelectDest;
  2119   2131   struct SelectDest {
  2120   2132     u8 eDest;         /* How to dispose of the results */
  2121         -  u8 affinity;      /* Affinity used when eDest==SRT_Set */
  2122         -  int iParm;        /* A parameter used by the eDest disposal method */
  2123         -  int iMem;         /* Base register where results are written */
  2124         -  int nMem;         /* Number of registers allocated */
         2133  +  u8 affSdst;       /* Affinity used when eDest==SRT_Set */
         2134  +  int iSDParm;      /* A parameter used by the eDest disposal method */
         2135  +  int iSdst;        /* Base register where results are written */
         2136  +  int nSdst;        /* Number of registers allocated */
  2125   2137   };
  2126   2138   
  2127   2139   /*
  2128   2140   ** During code generation of statements that do inserts into AUTOINCREMENT 
  2129   2141   ** tables, the following information is attached to the Table.u.autoInc.p
  2130   2142   ** pointer of each autoincrement table to record some side information that
  2131   2143   ** the code generator needs.  We have to keep per-table autoincrement
................................................................................
  2313   2325   #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
  2314   2326   #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
  2315   2327   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
  2316   2328   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
  2317   2329   #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
  2318   2330   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
  2319   2331   #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
         2332  +#define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
         2333  +#define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
  2320   2334   
  2321   2335   /*
  2322   2336    * Each trigger present in the database schema is stored as an instance of
  2323   2337    * struct Trigger. 
  2324   2338    *
  2325   2339    * Pointers to instances of struct Trigger are stored in two ways.
  2326   2340    * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
................................................................................
  2492   2506   /*
  2493   2507   ** Context pointer passed down through the tree-walk.
  2494   2508   */
  2495   2509   struct Walker {
  2496   2510     int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
  2497   2511     int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
  2498   2512     Parse *pParse;                            /* Parser context.  */
         2513  +  int walkerDepth;                          /* Number of subqueries */
  2499   2514     union {                                   /* Extra data for callback */
  2500   2515       NameContext *pNC;                          /* Naming context */
  2501   2516       int i;                                     /* Integer value */
  2502   2517       SrcList *pSrcList;                         /* FROM clause */
         2518  +    struct SrcCount *pSrcCount;                /* Counting column references */
  2503   2519     } u;
  2504   2520   };
  2505   2521   
  2506   2522   /* Forward declarations */
  2507   2523   int sqlite3WalkExpr(Walker*, Expr*);
  2508   2524   int sqlite3WalkExprList(Walker*, ExprList*);
  2509   2525   int sqlite3WalkSelect(Walker*, Select*);
................................................................................
  2797   2813   int sqlite3IsReadOnly(Parse*, Table*, int);
  2798   2814   void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
  2799   2815   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
  2800   2816   Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
  2801   2817   #endif
  2802   2818   void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
  2803   2819   void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  2804         -WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**,ExprList*,u16);
         2820  +WhereInfo *sqlite3WhereBegin(
         2821  +    Parse*,SrcList*,Expr*,ExprList**,ExprList*,u16,int);
  2805   2822   void sqlite3WhereEnd(WhereInfo*);
  2806   2823   int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
  2807   2824   void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
  2808   2825   void sqlite3ExprCodeMove(Parse*, int, int, int);
  2809   2826   void sqlite3ExprCodeCopy(Parse*, int, int, int);
  2810   2827   void sqlite3ExprCacheStore(Parse*, int, int, int);
  2811   2828   void sqlite3ExprCachePush(Parse*);
................................................................................
  2829   2846   void sqlite3Vacuum(Parse*);
  2830   2847   int sqlite3RunVacuum(char**, sqlite3*);
  2831   2848   char *sqlite3NameFromToken(sqlite3*, Token*);
  2832   2849   int sqlite3ExprCompare(Expr*, Expr*);
  2833   2850   int sqlite3ExprListCompare(ExprList*, ExprList*);
  2834   2851   void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
  2835   2852   void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
         2853  +int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
  2836   2854   Vdbe *sqlite3GetVdbe(Parse*);
  2837   2855   void sqlite3PrngSaveState(void);
  2838   2856   void sqlite3PrngRestoreState(void);
  2839   2857   void sqlite3PrngResetState(void);
  2840   2858   void sqlite3RollbackAll(sqlite3*,int);
  2841   2859   void sqlite3CodeVerifySchema(Parse*, int);
  2842   2860   void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
  2843   2861   void sqlite3BeginTransaction(Parse*, int);
  2844   2862   void sqlite3CommitTransaction(Parse*);
  2845   2863   void sqlite3RollbackTransaction(Parse*);
  2846   2864   void sqlite3Savepoint(Parse*, int, Token*);
  2847   2865   void sqlite3CloseSavepoints(sqlite3 *);
         2866  +void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
  2848   2867   int sqlite3ExprIsConstant(Expr*);
  2849   2868   int sqlite3ExprIsConstantNotJoin(Expr*);
  2850   2869   int sqlite3ExprIsConstantOrFunction(Expr*);
  2851   2870   int sqlite3ExprIsInteger(Expr*, int*);
  2852   2871   int sqlite3ExprCanBeNull(const Expr*);
  2853   2872   void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
  2854   2873   int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);

Changes to src/test1.c.

  6122   6122     int objc,
  6123   6123     Tcl_Obj *CONST objv[]
  6124   6124   ){
  6125   6125     int i;
  6126   6126     sqlite3 *db;
  6127   6127     const char *zOpt;
  6128   6128     int onoff;
  6129         -  int mask;
         6129  +  int mask = 0;
  6130   6130     static const struct {
  6131   6131       const char *zOptName;
  6132   6132       int mask;
  6133   6133     } aOpt[] = {
  6134   6134       { "all",              SQLITE_OptMask        },
  6135   6135       { "query-flattener",  SQLITE_QueryFlattener },
  6136   6136       { "column-cache",     SQLITE_ColumnCache    },

Changes to src/test8.c.

  1365   1365     rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
  1366   1366     if( rc!=SQLITE_OK ){
  1367   1367       Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
  1368   1368       return TCL_ERROR;
  1369   1369     }
  1370   1370     return TCL_OK;
  1371   1371   }
         1372  +
         1373  +#include "test_spellfix.c"
         1374  +
         1375  +/*
         1376  +** Register the spellfix virtual table module.
         1377  +*/
         1378  +static int register_spellfix_module(
         1379  +  ClientData clientData,
         1380  +  Tcl_Interp *interp,
         1381  +  int objc,
         1382  +  Tcl_Obj *CONST objv[]
         1383  +){
         1384  +  sqlite3 *db;
         1385  +
         1386  +  if( objc!=2 ){
         1387  +    Tcl_WrongNumArgs(interp, 1, objv, "DB");
         1388  +    return TCL_ERROR;
         1389  +  }
         1390  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
         1391  +
         1392  +  sqlite3Spellfix1Register(db);
         1393  +  return TCL_OK;
         1394  +}
  1372   1395   
  1373   1396   #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
  1374   1397   
  1375   1398   /*
  1376   1399   ** Register commands with the TCL interpreter.
  1377   1400   */
  1378   1401   int Sqlitetest8_Init(Tcl_Interp *interp){
  1379   1402   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1380   1403     static struct {
  1381   1404        char *zName;
  1382   1405        Tcl_ObjCmdProc *xProc;
  1383   1406        void *clientData;
  1384   1407     } aObjCmd[] = {
  1385         -     { "register_echo_module",   register_echo_module, 0 },
  1386         -     { "sqlite3_declare_vtab",   declare_vtab, 0 },
         1408  +     { "register_echo_module",       register_echo_module, 0 },
         1409  +     { "register_spellfix_module",   register_spellfix_module, 0 },
         1410  +     { "sqlite3_declare_vtab",       declare_vtab, 0 },
  1387   1411     };
  1388   1412     int i;
  1389   1413     for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
  1390   1414       Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 
  1391   1415           aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
  1392   1416     }
  1393   1417   #endif
  1394   1418     return TCL_OK;
  1395   1419   }

Changes to src/test_config.c.

   314    314   #endif
   315    315   
   316    316   #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_ENABLE_FTS4_UNICODE61)
   317    317     Tcl_SetVar2(interp, "sqlite_options", "fts3_unicode", "1", TCL_GLOBAL_ONLY);
   318    318   #else
   319    319     Tcl_SetVar2(interp, "sqlite_options", "fts3_unicode", "0", TCL_GLOBAL_ONLY);
   320    320   #endif
          321  +
          322  +#ifdef SQLITE_DISABLE_FTS4_DEFERRED
          323  +  Tcl_SetVar2(interp, "sqlite_options", "fts4_deferred", "0", TCL_GLOBAL_ONLY);
          324  +#else
          325  +  Tcl_SetVar2(interp, "sqlite_options", "fts4_deferred", "1", TCL_GLOBAL_ONLY);
          326  +#endif
   321    327   
   322    328   #ifdef SQLITE_OMIT_GET_TABLE
   323    329     Tcl_SetVar2(interp, "sqlite_options", "gettable", "0", TCL_GLOBAL_ONLY);
   324    330   #else
   325    331     Tcl_SetVar2(interp, "sqlite_options", "gettable", "1", TCL_GLOBAL_ONLY);
   326    332   #endif
   327    333   
................................................................................
   648    654     LINKVAR( MAX_DEFAULT_PAGE_SIZE );
   649    655   
   650    656     {
   651    657       static const int cv_TEMP_STORE = SQLITE_TEMP_STORE;
   652    658       Tcl_LinkVar(interp, "TEMP_STORE", (char *)&(cv_TEMP_STORE),
   653    659                   TCL_LINK_INT | TCL_LINK_READ_ONLY);
   654    660     }
          661  +
          662  +#ifdef _MSC_VER
          663  +  {
          664  +    static const int cv__MSC_VER = 1;
          665  +    Tcl_LinkVar(interp, "_MSC_VER", (char *)&(cv__MSC_VER),
          666  +                TCL_LINK_INT | TCL_LINK_READ_ONLY);
          667  +  }
          668  +#endif
          669  +#ifdef __GNUC__
          670  +  {
          671  +    static const int cv___GNUC__ = 1;
          672  +    Tcl_LinkVar(interp, "__GNUC__", (char *)&(cv___GNUC__),
          673  +                TCL_LINK_INT | TCL_LINK_READ_ONLY);
          674  +  }
          675  +#endif
   655    676   }
   656    677   
   657    678   
   658    679   /*
   659    680   ** Register commands with the TCL interpreter.
   660    681   */
   661    682   int Sqliteconfig_Init(Tcl_Interp *interp){
   662    683     set_options(interp);
   663    684     return TCL_OK;
   664    685   }

Changes to src/test_func.c.

   417    417       sqlite3_result_error_nomem(pCtx);
   418    418     }else{
   419    419       testHexToBin(zIn, zOut);
   420    420       sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free);
   421    421     }
   422    422   }
   423    423   #endif
          424  +
          425  +/*
          426  +** SQL function:   real2hex(X)
          427  +**
          428  +** If argument X is a real number, then convert it into a string which is
          429  +** the big-endian hexadecimal representation of the ieee754 encoding of
          430  +** that number.  If X is not a real number, return NULL.
          431  +*/
          432  +static void real2hex(
          433  +  sqlite3_context *context,
          434  +  int argc,
          435  +  sqlite3_value **argv
          436  +){
          437  +  union {
          438  +    sqlite3_uint64 i;
          439  +    double r;
          440  +    unsigned char x[8];
          441  +  } v;
          442  +  char zOut[20];
          443  +  int i;
          444  +  int bigEndian;
          445  +  v.i = 1;
          446  +  bigEndian = v.x[0]==0;
          447  +  v.r = sqlite3_value_double(argv[0]);
          448  +  for(i=0; i<8; i++){
          449  +    if( bigEndian ){
          450  +      zOut[i*2]   = "0123456789abcdef"[v.x[i]>>4];
          451  +      zOut[i*2+1] = "0123456789abcdef"[v.x[i]&0xf];
          452  +    }else{
          453  +      zOut[14-i*2]   = "0123456789abcdef"[v.x[i]>>4];
          454  +      zOut[14-i*2+1] = "0123456789abcdef"[v.x[i]&0xf];
          455  +    }
          456  +  }
          457  +  zOut[16] = 0;
          458  +  sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT);
          459  +}
          460  +
   424    461   
   425    462   static int registerTestFunctions(sqlite3 *db){
   426    463     static const struct {
   427    464        char *zName;
   428    465        signed char nArg;
   429    466        unsigned char eTextRep; /* 1: UTF-16.  0: UTF-8 */
   430    467        void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
................................................................................
   440    477       { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count},
   441    478       { "test_auxdata",         -1, SQLITE_UTF8, test_auxdata},
   442    479       { "test_error",            1, SQLITE_UTF8, test_error},
   443    480       { "test_error",            2, SQLITE_UTF8, test_error},
   444    481       { "test_eval",             1, SQLITE_UTF8, test_eval},
   445    482       { "test_isolation",        2, SQLITE_UTF8, test_isolation},
   446    483       { "test_counter",          1, SQLITE_UTF8, counterFunc},
          484  +    { "real2hex",              1, SQLITE_UTF8, real2hex},
   447    485     };
   448    486     int i;
   449    487   
   450    488     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   451    489       sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
   452    490           aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0);
   453    491     }

Changes to src/test_multiplex.c.

   498    498     const char *zName,         /* Name of file to be opened */
   499    499     sqlite3_file *pConn,       /* Fill in this file descriptor */
   500    500     int flags,                 /* Flags to control the opening */
   501    501     int *pOutFlags             /* Flags showing results of opening */
   502    502   ){
   503    503     int rc = SQLITE_OK;                  /* Result code */
   504    504     multiplexConn *pMultiplexOpen;       /* The new multiplex file descriptor */
   505         -  multiplexGroup *pGroup;              /* Corresponding multiplexGroup object */
          505  +  multiplexGroup *pGroup = 0;          /* Corresponding multiplexGroup object */
   506    506     sqlite3_file *pSubOpen = 0;                    /* Real file descriptor */
   507    507     sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
   508         -  int nName;
   509         -  int sz;
          508  +  int nName = 0;
          509  +  int sz = 0;
   510    510     char *zToFree = 0;
   511    511   
   512    512     UNUSED_PARAMETER(pVfs);
   513    513     memset(pConn, 0, pVfs->szOsFile);
   514    514     assert( zName || (flags & SQLITE_OPEN_DELETEONCLOSE) );
   515    515   
   516    516     /* We need to create a group structure and manage

Changes to src/test_quota.c.

    44     44   #define sqlite3_mutex_notheld(X)  ((void)(X),1)
    45     45   #endif /* SQLITE_THREADSAFE==0 */
    46     46   
    47     47   
    48     48   /*
    49     49   ** Figure out if we are dealing with Unix, Windows, or some other
    50     50   ** operating system.  After the following block of preprocess macros,
    51         -** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER 
           51  +** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
    52     52   ** will defined to either 1 or 0.  One of the four will be 1.  The other 
    53     53   ** three will be 0.
    54     54   */
    55     55   #if defined(SQLITE_OS_OTHER)
    56     56   # if SQLITE_OS_OTHER==1
    57     57   #   undef SQLITE_OS_UNIX
    58     58   #   define SQLITE_OS_UNIX 0
    59     59   #   undef SQLITE_OS_WIN
    60     60   #   define SQLITE_OS_WIN 0
    61         -#   undef SQLITE_OS_OS2
    62         -#   define SQLITE_OS_OS2 0
    63     61   # else
    64     62   #   undef SQLITE_OS_OTHER
    65     63   # endif
    66     64   #endif
    67     65   #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
    68     66   # define SQLITE_OS_OTHER 0
    69     67   # ifndef SQLITE_OS_WIN
    70     68   #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) \
    71     69                          || defined(__MINGW32__) || defined(__BORLANDC__)
    72     70   #     define SQLITE_OS_WIN 1
    73     71   #     define SQLITE_OS_UNIX 0
    74         -#     define SQLITE_OS_OS2 0
    75         -#   elif defined(__EMX__) || defined(_OS2) || defined(OS2) \
    76         -                          || defined(_OS2_) || defined(__OS2__)
    77         -#     define SQLITE_OS_WIN 0
    78         -#     define SQLITE_OS_UNIX 0
    79         -#     define SQLITE_OS_OS2 1
    80     72   #   else
    81     73   #     define SQLITE_OS_WIN 0
    82     74   #     define SQLITE_OS_UNIX 1
    83         -#     define SQLITE_OS_OS2 0
    84     75   #  endif
    85     76   # else
    86     77   #  define SQLITE_OS_UNIX 0
    87         -#  define SQLITE_OS_OS2 0
    88     78   # endif
    89     79   #else
    90     80   # ifndef SQLITE_OS_WIN
    91     81   #  define SQLITE_OS_WIN 0
    92     82   # endif
    93     83   #endif
    94     84   

Changes to src/test_spellfix.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13         -** This module implements a VIRTUAL TABLE that can be used to search
    14         -** a large vocabulary for close matches.  For example, this virtual
    15         -** table can be used to suggest corrections to misspelled words.  Or,
    16         -** it could be used with FTS4 to do full-text search using potentially
    17         -** misspelled words.
    18         -**
    19         -** Create an instance of the virtual table this way:
    20         -**
    21         -**    CREATE VIRTUAL TABLE demo USING spellfix1;
    22         -**
    23         -** The "spellfix1" term is the name of this module.  The "demo" is the
    24         -** name of the virtual table you will be creating.  The table is initially
    25         -** empty.  You have to populate it with your vocabulary.  Suppose you
    26         -** have a list of words in a table named "big_vocabulary".  Then do this:
    27         -**
    28         -**    INSERT INTO demo(word) SELECT word FROM big_vocabulary;
    29         -**
    30         -** If you intend to use this virtual table in cooperation with an FTS4
    31         -** table (for spelling correctly of search terms) then you can extract
    32         -** the vocabulary using an fts3aux table:
    33         -**
    34         -**    INSERT INTO demo(word) SELECT term FROM search_aux WHERE col='*';
    35         -**
    36         -** You can also provide the virtual table with a "rank" for each word.
    37         -** The "rank" is an estimate of how common the word is.  Larger numbers
    38         -** mean the word is more common.  If you omit the rank when populating
    39         -** the table, then a rank of 1 is assumed.  But if you have rank 
    40         -** information, you can supply it and the virtual table will show a
    41         -** slight preference for selecting more commonly used terms.  To
    42         -** populate the rank from an fts4aux table "search_aux" do something
    43         -** like this:
    44         -**
    45         -**    INSERT INTO demo(word,rank)
    46         -**        SELECT term, documents FROM search_aux WHERE col='*';
    47         -**
    48         -** To query the virtual table, include a MATCH operator in the WHERE
    49         -** clause.  For example:
    50         -**
    51         -**    SELECT word FROM demo WHERE word MATCH 'kennasaw';
    52         -**
    53         -** Using a dataset of American place names (derived from
    54         -** http://geonames.usgs.gov/domestic/download_data.htm) the query above
    55         -** returns 20 results beginning with:
    56         -**
    57         -**    kennesaw
    58         -**    kenosha
    59         -**    kenesaw
    60         -**    kenaga
    61         -**    keanak
    62         -**
    63         -** If you append the character '*' to the end of the pattern, then
    64         -** a prefix search is performed.  For example:
    65         -**
    66         -**    SELECT word FROM demo WHERE word MATCH 'kennes*';
    67         -**
    68         -** Yields 20 results beginning with:
    69         -**
    70         -**    kennesaw
    71         -**    kennestone
    72         -**    kenneson
    73         -**    kenneys
    74         -**    keanes
    75         -**    keenes
    76         -**
    77         -** The virtual table actually has a unique rowid with five columns plus three
    78         -** extra hidden columns.  The columns are as follows:
    79         -**
    80         -**    rowid         A unique integer number associated with each
    81         -**                  vocabulary item in the table.  This can be used
    82         -**                  as a foreign key on other tables in the database.
    83         -**
    84         -**    word          The text of the word that matches the pattern.
    85         -**                  Both word and pattern can contains unicode characters
    86         -**                  and can be mixed case.
    87         -**
    88         -**    rank          This is the rank of the word, as specified in the
    89         -**                  original INSERT statement.
    90         -**
    91         -**    distance      This is an edit distance or Levensthein distance going
    92         -**                  from the pattern to the word.
    93         -**
    94         -**    langid        This is the language-id of the word.  All queries are
    95         -**                  against a single language-id, which defaults to 0.
    96         -**                  For any given query this value is the same on all rows.
    97         -**
    98         -**    score         The score is a combination of rank and distance.  The
    99         -**                  idea is that a lower score is better.  The virtual table
   100         -**                  attempts to find words with the lowest score and 
   101         -**                  by default (unless overridden by ORDER BY) returns
   102         -**                  results in order of increasing score.
   103         -**
   104         -**    top           (HIDDEN)  For any query, this value is the same on all
   105         -**                  rows.  It is an integer which is the maximum number of
   106         -**                  rows that will be output.  The actually number of rows
   107         -**                  output might be less than this number, but it will never
   108         -**                  be greater.  The default value for top is 20, but that
   109         -**                  can be changed for each query by including a term of
   110         -**                  the form "top=N" in the WHERE clause of the query.
   111         -**
   112         -**    scope         (HIDDEN)  For any query, this value is the same on all
   113         -**                  rows.  The scope is a measure of how widely the virtual
   114         -**                  table looks for matching words.  Smaller values of
   115         -**                  scope cause a broader search.  The scope is normally
   116         -**                  choosen automatically and is capped at 4.  Applications
   117         -**                  can change the scope by including a term of the form
   118         -**                  "scope=N" in the WHERE clause of the query.  Increasing
   119         -**                  the scope will make the query run faster, but will reduce
   120         -**                  the possible corrections.
   121         -**
   122         -**    srchcnt       (HIDDEN)  For any query, this value is the same on all
   123         -**                  rows.  This value is an integer which is the number of
   124         -**                  of words examined using the edit-distance algorithm to
   125         -**                  find the top matches that are ultimately displayed.  This
   126         -**                  value is for diagnostic use only.
   127         -**
   128         -**    soundslike    (HIDDEN)  When inserting vocabulary entries, this field
   129         -**                  can be set to an spelling that matches what the word
   130         -**                  sounds like.  See the DEALING WITH UNUSUAL AND DIFFICULT
   131         -**                  SPELLINGS section below for details.
   132         -**
   133         -** When inserting into or updating the virtual table, only the rowid, word,
   134         -** rank, and langid may be changes.  Any attempt to set or modify the values
   135         -** of distance, score, top, scope, or srchcnt is silently ignored.
   136         -**
   137         -** ALGORITHM
   138         -**
   139         -** A shadow table named "%_vocab" (where the % is replaced by the name of
   140         -** the virtual table; Ex: "demo_vocab" for the "demo" virtual table) is
   141         -** constructed with these columns:
   142         -**
   143         -**    id            The unique id (INTEGER PRIMARY KEY)
   144         -**
   145         -**    rank          The rank of word.
   146         -**
   147         -**    langid        The language id for this entry.
   148         -**
   149         -**    word          The original UTF8 text of the vocabulary word
   150         -**
   151         -**    k1            The word transliterated into lower-case ASCII.  
   152         -**                  There is a standard table of mappings from non-ASCII
   153         -**                  characters into ASCII.  Examples: "æ" -> "ae",
   154         -**                  "þ" -> "th", "ß" -> "ss", "á" -> "a", ...  The
   155         -**                  accessory function spellfix1_translit(X) will do
   156         -**                  the non-ASCII to ASCII mapping.  The built-in lower(X)
   157         -**                  function will convert to lower-case.  Thus:
   158         -**                  k1 = lower(spellfix1_translit(word)).
   159         -**
   160         -**    k2            This field holds a phonetic code derived from k1.  Letters
   161         -**                  that have similar sounds are mapped into the same symbol.
   162         -**                  For example, all vowels and vowel clusters become the
   163         -**                  single symbol "A".  And the letters "p", "b", "f", and
   164         -**                  "v" all become "B".  All nasal sounds are represented
   165         -**                  as "N".  And so forth.  The mapping is base on
   166         -**                  ideas found in Soundex, Metaphone, and other
   167         -**                  long-standing phonetic matching systems.  This key can
   168         -**                  be generated by the function spellfix1_charclass(X).  
   169         -**                  Hence: k2 = spellfix1_charclass(k1)
   170         -**
   171         -** There is also a function for computing the Wagner edit distance or the
   172         -** Levenshtein distance between a pattern and a word.  This function
   173         -** is exposed as spellfix1_editdist(X,Y).  The edit distance function
   174         -** returns the "cost" of converting X into Y.  Some transformations
   175         -** cost more than others.  Changing one vowel into a different vowel,
   176         -** for example is relatively cheap, as is doubling a constant, or
   177         -** omitting the second character of a double-constant.  Other transformations
   178         -** or more expensive.  The idea is that the edit distance function returns
   179         -** a low cost of words that are similar and a higher cost for words
   180         -** that are futher apart.  In this implementation, the maximum cost
   181         -** of any single-character edit (delete, insert, or substitute) is 100,
   182         -** with lower costs for some edits (such as transforming vowels).
   183         -**
   184         -** The "score" for a comparison is the edit distance between the pattern
   185         -** and the word, adjusted down by the base-2 logorithm of the word rank.
   186         -** For example, a match with distance 100 but rank 1000 would have a
   187         -** score of 122 (= 100 - log2(1000) + 32) where as a match with distance
   188         -** 100 with a rank of 1 would have a score of 131 (100 - log2(1) + 32).
   189         -** (NB:  The constant 32 is added to each score to keep it from going
   190         -** negative in case the edit distance is zero.)  In this way, frequently
   191         -** used words get a slightly lower cost which tends to move them toward
   192         -** the top of the list of alternative spellings.
   193         -**
   194         -** A straightforward implementation of a spelling corrector would be
   195         -** to compare the search term against every word in the vocabulary
   196         -** and select the 20 with the lowest scores.  However, there will 
   197         -** typically be hundreds of thousands or millions of words in the
   198         -** vocabulary, and so this approach is not fast enough.
   199         -**
   200         -** Suppose the term that is being spell-corrected is X.  To limit
   201         -** the search space, X is converted to a k2-like key using the
   202         -** equivalent of:
   203         -**
   204         -**    key = spellfix1_charclass(lower(spellfix1_translit(X)))
   205         -**
   206         -** This key is then limited to "scope" characters.  The default scope
   207         -** value is 4, but an alternative scope can be specified using the
   208         -** "scope=N" term in the WHERE clause.  After the key has been truncated,
   209         -** the edit distance is run against every term in the vocabulary that
   210         -** has a k2 value that begins with the abbreviated key.
   211         -**
   212         -** For example, suppose the input word is "Paskagula".  The phonetic 
   213         -** key is "BACACALA" which is then truncated to 4 characters "BACA".
   214         -** The edit distance is then run on the 4980 entries (out of
   215         -** 272,597 entries total) of the vocabulary whose k2 values begin with
   216         -** BACA, yielding "Pascagoula" as the best match.
   217         -** 
   218         -** Only terms of the vocabulary with a matching langid are searched.
   219         -** Hence, the same table can contain entries from multiple languages
   220         -** and only the requested language will be used.  The default langid
   221         -** is 0.
   222         -**
   223         -** DEALING WITH UNUSUAL AND DIFFICULT SPELLINGS
   224         -**
   225         -** The algorithm above works quite well for most cases, but there are
   226         -** exceptions.  These exceptions can be dealt with by making additional
   227         -** entries in the virtual table using the "soundslike" column.
   228         -**
   229         -** For example, many words of Greek origin begin with letters "ps" where
   230         -** the "p" is silent.  Ex:  psalm, pseudonym, psoriasis, psyche.  In
   231         -** another example, many Scottish surnames can be spelled with an
   232         -** initial "Mac" or "Mc".  Thus, "MacKay" and "McKay" are both pronounced
   233         -** the same.
   234         -**
   235         -** Accommodation can be made for words that are not spelled as they
   236         -** sound by making additional entries into the virtual table for the
   237         -** same word, but adding an alternative spelling in the "soundslike"
   238         -** column.  For example, the canonical entry for "psalm" would be this:
   239         -**
   240         -**   INSERT INTO demo(word) VALUES('psalm');
   241         -**
   242         -** To enhance the ability to correct the spelling of "salm" into
   243         -** "psalm", make an addition entry like this:
   244         -**
   245         -**   INSERT INTO demo(word,soundslike) VALUES('psalm','salm');
   246         -**
   247         -** It is ok to make multiple entries for the same word as long as
   248         -** each entry has a different soundslike value.  Note that if no
   249         -** soundslike value is specified, the soundslike defaults to the word
   250         -** itself.
   251         -**
   252         -** Listed below are some cases where it might make sense to add additional
   253         -** soundslike entries.  The specific entries will depend on the application
   254         -** and the target language.
   255         -**
   256         -**   *   Silent "p" in words beginning with "ps":  psalm, psyche
   257         -**
   258         -**   *   Silent "p" in words beginning with "pn":  pneumonia, pneumatic
   259         -**
   260         -**   *   Silent "p" in words beginning with "pt":  pterodactyl, ptolemaic
   261         -**
   262         -**   *   Silent "d" in words beginning with "dj":  djinn, Djikarta
   263         -**
   264         -**   *   Silent "k" in words beginning with "kn":  knight, Knuthson
   265         -**
   266         -**   *   Silent "g" in words beginning with "gn":  gnarly, gnome, gnat
   267         -**
   268         -**   *   "Mac" versus "Mc" beginning Scottish surnames
   269         -**
   270         -**   *   "Tch" sounds in Slavic words:  Tchaikovsky vs. Chaykovsky
   271         -**
   272         -**   *   The letter "j" pronounced like "h" in Spanish:  LaJolla
   273         -**
   274         -**   *   Words beginning with "wr" versus "r":  write vs. rite
   275         -**
   276         -**   *   Miscellanous problem words such as "debt", "tsetse",
   277         -**       "Nguyen", "Van Nuyes".
           13  +** This module implements the spellfix1 VIRTUAL TABLE that can be used
           14  +** to search a large vocabulary for close matches.  See separate
           15  +** documentation files (spellfix1.wiki and editdist3.wiki) for details.
   278     16   */
   279     17   #if SQLITE_CORE
   280     18   # include "sqliteInt.h"
   281     19   #else
   282     20   # include <string.h>
   283     21   # include <stdio.h>
   284     22   # include <stdlib.h>
   285     23   # include "sqlite3ext.h"
   286     24     SQLITE_EXTENSION_INIT1
   287     25   #endif /* !SQLITE_CORE */
           26  +#include <ctype.h>
   288     27   
   289     28   /*
   290     29   ** Character classes for ASCII characters:
   291     30   **
   292     31   **   0   ''        Silent letters:   H W
   293     32   **   1   'A'       Any vowel:   A E I O U (Y)
   294     33   **   2   'B'       A bilabeal stop or fricative:  B F P V
   295     34   **   3   'C'       Other fricatives or back stops:  C G J K Q S X Z
   296     35   **   4   'D'       Alveolar stops:  D T
   297     36   **   5   'H'       Letter H at the beginning of a word
   298         -**   6   'L'       Glides:  L R
   299         -**   7   'M'       Nasals:  M N
   300         -**   8   'W'       Letter W at the beginning of a word
   301         -**   9   'Y'       Letter Y at the beginning of a word.
   302         -**   10  '9'       A digit: 0 1 2 3 4 5 6 7 8 9
   303         -**   11  ' '       White space
   304         -**   12  '?'       Other.
           37  +**   6   'L'       Glide:  L
           38  +**   7   'R'       Semivowel:  R
           39  +**   8   'M'       Nasals:  M N
           40  +**   9   'W'       Letter W at the beginning of a word
           41  +**   10  'Y'       Letter Y at the beginning of a word.
           42  +**   11  '9'       Digits: 0 1 2 3 4 5 6 7 8 9
           43  +**   12  ' '       White space
           44  +**   13  '?'       Other.
   305     45   */
   306     46   #define CCLASS_SILENT         0
   307     47   #define CCLASS_VOWEL          1
   308     48   #define CCLASS_B              2
   309     49   #define CCLASS_C              3
   310     50   #define CCLASS_D              4
   311     51   #define CCLASS_H              5
   312     52   #define CCLASS_L              6
   313         -#define CCLASS_M              7
   314         -#define CCLASS_W              8
   315         -#define CCLASS_Y              9
   316         -#define CCLASS_DIGIT         10
   317         -#define CCLASS_SPACE         11
   318         -#define CCLASS_OTHER         12
           53  +#define CCLASS_R              7
           54  +#define CCLASS_M              8
           55  +#define CCLASS_W              9
           56  +#define CCLASS_Y             10
           57  +#define CCLASS_DIGIT         11
           58  +#define CCLASS_SPACE         12
           59  +#define CCLASS_OTHER         13
   319     60   
   320     61   /*
   321     62   ** The following table gives the character class for non-initial ASCII
   322     63   ** characters.
   323     64   */
   324     65   static const unsigned char midClass[] = {
   325         -          /* x0  x1  x2  x3  x4  x5  x6  x7    x8  x9  xa  xb  xc  xd  xe  xf */
   326         -  /* 0x */   12, 12, 12, 12, 12, 12, 12, 12,   12, 11, 11, 12, 11, 12, 12, 12,
   327         -  /* 1x */   12, 12, 12, 12, 12, 12, 12, 12,   12, 12, 12, 12, 12, 12, 12, 12,
   328         -  /* 2x */   11, 12, 12, 12, 12, 12, 12, 12,   12, 12, 12, 12, 12, 12, 12, 12,
   329         -  /* 3x */   10, 10, 10, 10, 10, 10, 10, 10,   10, 10, 12, 12, 12, 12, 12, 12,
   330         -  /* 4x */   12,  1,  2,  3,  4,  1,  2,  3,    0,  1,  3,  3,  6,  7,  7,  1,
   331         -  /* 5x */    2,  3,  6,  3,  4,  1,  2,  0,    3,  1,  3, 12, 12, 12, 12, 12,
   332         -  /* 6x */   12,  1,  2,  3,  4,  1,  2,  3,    0,  1,  3,  3,  6,  7,  7,  1,
   333         -  /* 7x */    2,  3,  6,  3,  4,  1,  2,  0,    3,  1,  3, 12, 12, 12, 12, 12,
   334         -};
   335         -
           66  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           67  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           68  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           69  + /*   */ CCLASS_SPACE,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           70  + /*   */ CCLASS_SPACE,    /*   */ CCLASS_SPACE,   /*   */ CCLASS_OTHER,
           71  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           72  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           73  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           74  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           75  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
           76  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_SPACE,
           77  + /* ! */ CCLASS_OTHER,    /* " */ CCLASS_OTHER,   /* # */ CCLASS_OTHER,
           78  + /* $ */ CCLASS_OTHER,    /* % */ CCLASS_OTHER,   /* & */ CCLASS_OTHER,
           79  + /* ' */ CCLASS_SILENT,   /* ( */ CCLASS_OTHER,   /* ) */ CCLASS_OTHER,
           80  + /* * */ CCLASS_OTHER,    /* + */ CCLASS_OTHER,   /* , */ CCLASS_OTHER,
           81  + /* - */ CCLASS_OTHER,    /* . */ CCLASS_OTHER,   /* / */ CCLASS_OTHER,
           82  + /* 0 */ CCLASS_DIGIT,    /* 1 */ CCLASS_DIGIT,   /* 2 */ CCLASS_DIGIT,
           83  + /* 3 */ CCLASS_DIGIT,    /* 4 */ CCLASS_DIGIT,   /* 5 */ CCLASS_DIGIT,
           84  + /* 6 */ CCLASS_DIGIT,    /* 7 */ CCLASS_DIGIT,   /* 8 */ CCLASS_DIGIT,
           85  + /* 9 */ CCLASS_DIGIT,    /* : */ CCLASS_OTHER,   /* ; */ CCLASS_OTHER,
           86  + /* < */ CCLASS_OTHER,    /* = */ CCLASS_OTHER,   /* > */ CCLASS_OTHER,
           87  + /* ? */ CCLASS_OTHER,    /* @ */ CCLASS_OTHER,   /* A */ CCLASS_VOWEL,
           88  + /* B */ CCLASS_B,        /* C */ CCLASS_C,       /* D */ CCLASS_D,
           89  + /* E */ CCLASS_VOWEL,    /* F */ CCLASS_B,       /* G */ CCLASS_C,
           90  + /* H */ CCLASS_SILENT,   /* I */ CCLASS_VOWEL,   /* J */ CCLASS_C,
           91  + /* K */ CCLASS_C,        /* L */ CCLASS_L,       /* M */ CCLASS_M,
           92  + /* N */ CCLASS_M,        /* O */ CCLASS_VOWEL,   /* P */ CCLASS_B,
           93  + /* Q */ CCLASS_C,        /* R */ CCLASS_R,       /* S */ CCLASS_C,
           94  + /* T */ CCLASS_D,        /* U */ CCLASS_VOWEL,   /* V */ CCLASS_B,
           95  + /* W */ CCLASS_SILENT,   /* X */ CCLASS_C,       /* Y */ CCLASS_VOWEL,
           96  + /* Z */ CCLASS_C,        /* [ */ CCLASS_OTHER,   /* \ */ CCLASS_OTHER,
           97  + /* ] */ CCLASS_OTHER,    /* ^ */ CCLASS_OTHER,   /* _ */ CCLASS_OTHER,
           98  + /* ` */ CCLASS_OTHER,    /* a */ CCLASS_VOWEL,   /* b */ CCLASS_B,
           99  + /* c */ CCLASS_C,        /* d */ CCLASS_D,       /* e */ CCLASS_VOWEL,
          100  + /* f */ CCLASS_B,        /* g */ CCLASS_C,       /* h */ CCLASS_SILENT,
          101  + /* i */ CCLASS_VOWEL,    /* j */ CCLASS_C,       /* k */ CCLASS_C,
          102  + /* l */ CCLASS_L,        /* m */ CCLASS_M,       /* n */ CCLASS_M,
          103  + /* o */ CCLASS_VOWEL,    /* p */ CCLASS_B,       /* q */ CCLASS_C,
          104  + /* r */ CCLASS_R,        /* s */ CCLASS_C,       /* t */ CCLASS_D,
          105  + /* u */ CCLASS_VOWEL,    /* v */ CCLASS_B,       /* w */ CCLASS_SILENT,
          106  + /* x */ CCLASS_C,        /* y */ CCLASS_VOWEL,   /* z */ CCLASS_C,
          107  + /* { */ CCLASS_OTHER,    /* | */ CCLASS_OTHER,   /* } */ CCLASS_OTHER,
          108  + /* ~ */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   
          109  +};
   336    110   /* 
   337    111   ** This tables gives the character class for ASCII characters that form the
   338    112   ** initial character of a word.  The only difference from midClass is with
   339    113   ** the letters H, W, and Y.
   340    114   */
   341    115   static const unsigned char initClass[] = {
   342         -          /* x0  x1  x2  x3  x4  x5  x6  x7    x8  x9  xa  xb  xc  xd  xe  xf */
   343         -  /* 0x */   12, 12, 12, 12, 12, 12, 12, 12,   12, 11, 11, 12, 11, 12, 12, 12,
   344         -  /* 1x */   12, 12, 12, 12, 12, 12, 12, 12,   12, 12, 12, 12, 12, 12, 12, 12,
   345         -  /* 2x */   11, 12, 12, 12, 12, 12, 12, 12,   12, 12, 12, 12, 12, 12, 12, 12,
   346         -  /* 3x */   10, 10, 10, 10, 10, 10, 10, 10,   10, 10, 12, 12, 12, 12, 12, 12,
   347         -  /* 4x */   12,  1,  2,  3,  4,  1,  2,  3,    5,  1,  3,  3,  6,  7,  7,  1,
   348         -  /* 5x */    2,  3,  6,  3,  4,  1,  2,  8,    3,  9,  3, 12, 12, 12, 12, 12,
   349         -  /* 6x */   12,  1,  2,  3,  4,  1,  2,  3,    5,  1,  3,  3,  6,  7,  7,  1,
   350         -  /* 7x */    2,  3,  6,  3,  4,  1,  2,  8,    3,  9,  3, 12, 12, 12, 12, 12,
   351         -};
   352         -
   353         -/*
   354         -** Mapping from the character class number (0-12) to a symbol for each
          116  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          117  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          118  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          119  + /*   */ CCLASS_SPACE,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          120  + /*   */ CCLASS_SPACE,    /*   */ CCLASS_SPACE,   /*   */ CCLASS_OTHER,
          121  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          122  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          123  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          124  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          125  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
          126  + /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_SPACE,
          127  + /* ! */ CCLASS_OTHER,    /* " */ CCLASS_OTHER,   /* # */ CCLASS_OTHER,
          128  + /* $ */ CCLASS_OTHER,    /* % */ CCLASS_OTHER,   /* & */ CCLASS_OTHER,
          129  + /* ' */ CCLASS_OTHER,    /* ( */ CCLASS_OTHER,   /* ) */ CCLASS_OTHER,
          130  + /* * */ CCLASS_OTHER,    /* + */ CCLASS_OTHER,   /* , */ CCLASS_OTHER,
          131  + /* - */ CCLASS_OTHER,    /* . */ CCLASS_OTHER,   /* / */ CCLASS_OTHER,
          132  + /* 0 */ CCLASS_DIGIT,    /* 1 */ CCLASS_DIGIT,   /* 2 */ CCLASS_DIGIT,
          133  + /* 3 */ CCLASS_DIGIT,    /* 4 */ CCLASS_DIGIT,   /* 5 */ CCLASS_DIGIT,
          134  + /* 6 */ CCLASS_DIGIT,    /* 7 */ CCLASS_DIGIT,   /* 8 */ CCLASS_DIGIT,
          135  + /* 9 */ CCLASS_DIGIT,    /* : */ CCLASS_OTHER,   /* ; */ CCLASS_OTHER,
          136  + /* < */ CCLASS_OTHER,    /* = */ CCLASS_OTHER,   /* > */ CCLASS_OTHER,
          137  + /* ? */ CCLASS_OTHER,    /* @ */ CCLASS_OTHER,   /* A */ CCLASS_VOWEL,
          138  + /* B */ CCLASS_B,        /* C */ CCLASS_C,       /* D */ CCLASS_D,
          139  + /* E */ CCLASS_VOWEL,    /* F */ CCLASS_B,       /* G */ CCLASS_C,
          140  + /* H */ CCLASS_SILENT,   /* I */ CCLASS_VOWEL,   /* J */ CCLASS_C,
          141  + /* K */ CCLASS_C,        /* L */ CCLASS_L,       /* M */ CCLASS_M,
          142  + /* N */ CCLASS_M,        /* O */ CCLASS_VOWEL,   /* P */ CCLASS_B,
          143  + /* Q */ CCLASS_C,        /* R */ CCLASS_R,       /* S */ CCLASS_C,
          144  + /* T */ CCLASS_D,        /* U */ CCLASS_VOWEL,   /* V */ CCLASS_B,
          145  + /* W */ CCLASS_W,        /* X */ CCLASS_C,       /* Y */ CCLASS_Y,
          146  + /* Z */ CCLASS_C,        /* [ */ CCLASS_OTHER,   /* \ */ CCLASS_OTHER,
          147  + /* ] */ CCLASS_OTHER,    /* ^ */ CCLASS_OTHER,   /* _ */ CCLASS_OTHER,
          148  + /* ` */ CCLASS_OTHER,    /* a */ CCLASS_VOWEL,   /* b */ CCLASS_B,
          149  + /* c */ CCLASS_C,        /* d */ CCLASS_D,       /* e */ CCLASS_VOWEL,
          150  + /* f */ CCLASS_B,        /* g */ CCLASS_C,       /* h */ CCLASS_SILENT,
          151  + /* i */ CCLASS_VOWEL,    /* j */ CCLASS_C,       /* k */ CCLASS_C,
          152  + /* l */ CCLASS_L,        /* m */ CCLASS_M,       /* n */ CCLASS_M,
          153  + /* o */ CCLASS_VOWEL,    /* p */ CCLASS_B,       /* q */ CCLASS_C,
          154  + /* r */ CCLASS_R,        /* s */ CCLASS_C,       /* t */ CCLASS_D,
          155  + /* u */ CCLASS_VOWEL,    /* v */ CCLASS_B,       /* w */ CCLASS_W,
          156  + /* x */ CCLASS_C,        /* y */ CCLASS_Y,       /* z */ CCLASS_C,
          157  + /* { */ CCLASS_OTHER,    /* | */ CCLASS_OTHER,   /* } */ CCLASS_OTHER,
          158  + /* ~ */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   
          159  +};
          160  +
          161  +/*
          162  +** Mapping from the character class number (0-13) to a symbol for each
   355    163   ** character class.  Note that initClass[] can be used to map the class
   356    164   ** symbol back into the class number.
   357    165   */
   358         -static const unsigned char className[] = ".ABCDHLMWY9 ?";
          166  +static const unsigned char className[] = ".ABCDHLRMWY9 ?";
   359    167   
   360    168   /*
   361         -** Generate a string of character classes corresponding to the
   362         -** ASCII characters in the input string zIn.  If the input is not
   363         -** ASCII then the behavior is undefined.
          169  +** Generate a "phonetic hash" from a string of ASCII characters
          170  +** in zIn[0..nIn-1].
          171  +**
          172  +**   * Map characters by character class as defined above.
          173  +**   * Omit double-letters
          174  +**   * Omit vowels beside R and L
          175  +**   * Omit T when followed by CH
          176  +**   * Omit W when followed by R
          177  +**   * Omit D when followed by J or G
          178  +**   * Omit K in KN or G in GN at the beginning of a word
   364    179   **
   365    180   ** Space to hold the result is obtained from sqlite3_malloc()
   366    181   **
   367    182   ** Return NULL if memory allocation fails.  
   368    183   */
   369         -static unsigned char *characterClassString(const unsigned char *zIn, int nIn){
          184  +static unsigned char *phoneticHash(const unsigned char *zIn, int nIn){
   370    185     unsigned char *zOut = sqlite3_malloc( nIn + 1 );
   371    186     int i;
   372    187     int nOut = 0;
   373    188     char cPrev = 0x77;
          189  +  char cPrevX = 0x77;
   374    190     const unsigned char *aClass = initClass;
   375    191   
   376    192     if( zOut==0 ) return 0;
          193  +  if( nIn>2 ){
          194  +    switch( zIn[0] ){
          195  +      case 'g': 
          196  +      case 'k': {
          197  +        if( zIn[1]=='n' ){ zIn++; nIn--; }
          198  +        break;
          199  +      }
          200  +    }
          201  +  }
   377    202     for(i=0; i<nIn; i++){
   378    203       unsigned char c = zIn[i];
          204  +    if( i+1<nIn ){
          205  +      if( c=='w' && zIn[i+1]=='r' ) continue;
          206  +      if( c=='d' && (zIn[i+1]=='j' || zIn[i+1]=='g') ) continue;
          207  +      if( i+2<nIn ){
          208  +        if( c=='t' && zIn[i+1]=='c' && zIn[i+2]=='h' ) continue;
          209  +      }
          210  +    }
   379    211       c = aClass[c&0x7f];
          212  +    if( c==CCLASS_SPACE ) continue;
   380    213       if( c==CCLASS_OTHER && cPrev!=CCLASS_DIGIT ) continue;
          214  +    aClass = midClass;
          215  +    if( c==CCLASS_VOWEL && (cPrevX==CCLASS_R || cPrevX==CCLASS_L) ){
          216  +       continue; /* No vowels beside L or R */ 
          217  +    }
          218  +    if( (c==CCLASS_R || c==CCLASS_L) && cPrevX==CCLASS_VOWEL ){
          219  +       nOut--;   /* No vowels beside L or R */
          220  +    }
   381    221       cPrev = c;
   382    222       if( c==CCLASS_SILENT ) continue;
   383         -    if( c==CCLASS_SPACE ) continue;
   384         -    aClass = midClass;
          223  +    cPrevX = c;
   385    224       c = className[c];
   386    225       if( c!=zOut[nOut-1] ) zOut[nOut++] = c;
   387    226     }
   388    227     zOut[nOut] = 0;
   389    228     return zOut;
   390    229   }
   391    230   
   392    231   /*
   393         -** This is an SQL function wrapper around characterClassString().  See
   394         -** the description of characterClassString() for additional information.
          232  +** This is an SQL function wrapper around phoneticHash().  See
          233  +** the description of phoneticHash() for additional information.
   395    234   */
   396         -static void characterClassSqlFunc(
          235  +static void phoneticHashSqlFunc(
   397    236     sqlite3_context *context,
   398    237     int argc,
   399    238     sqlite3_value **argv
   400    239   ){
   401    240     const unsigned char *zIn;
   402    241     unsigned char *zOut;
   403    242   
   404    243     zIn = sqlite3_value_text(argv[0]);
   405    244     if( zIn==0 ) return;
   406         -  zOut = characterClassString(zIn, sqlite3_value_bytes(argv[0]));
          245  +  zOut = phoneticHash(zIn, sqlite3_value_bytes(argv[0]));
   407    246     if( zOut==0 ){
   408    247       sqlite3_result_error_nomem(context);
   409    248     }else{
   410    249       sqlite3_result_text(context, (char*)zOut, -1, sqlite3_free);
   411    250     }
   412    251   }
   413    252   
................................................................................
   420    259   }
   421    260   
   422    261   /*
   423    262   ** Return the cost of inserting or deleting character c immediately
   424    263   ** following character cPrev.  If cPrev==0, that means c is the first
   425    264   ** character of the word.
   426    265   */
   427         -static int insertOrDeleteCost(char cPrev, char c){
          266  +static int insertOrDeleteCost(char cPrev, char c, char cNext){
   428    267     char classC = characterClass(cPrev, c);
   429    268     char classCprev;
   430    269   
   431    270     if( classC==CCLASS_SILENT ){
   432    271       /* Insert or delete "silent" characters such as H or W */
   433    272       return 1;
   434    273     }
   435    274     if( cPrev==c ){
   436    275       /* Repeated characters, or miss a repeat */
   437    276       return 10;
          277  +  }
          278  +  if( classC==CCLASS_VOWEL && (cPrev=='r' || cNext=='r') ){
          279  +    return 20;  /* Insert a vowel before or after 'r' */
   438    280     }
   439    281     classCprev = characterClass(cPrev, cPrev);
   440    282     if( classC==classCprev ){
   441    283       if( classC==CCLASS_VOWEL ){
   442    284         /* Remove or add a new vowel to a vowel cluster */
   443    285         return 15;
   444    286       }else{
................................................................................
   472    314       /* differ only in case */
   473    315       return 0;
   474    316     }
   475    317     classFrom = characterClass(cPrev, cFrom);
   476    318     classTo = characterClass(cPrev, cTo);
   477    319     if( classFrom==classTo ){
   478    320       /* Same character class */
   479         -    return classFrom=='A' ? 25 : 40;
          321  +    return 40;
   480    322     }
   481    323     if( classFrom>=CCLASS_B && classFrom<=CCLASS_Y
   482    324         && classTo>=CCLASS_B && classTo<=CCLASS_Y ){
   483    325       /* Convert from one consonant to another, but in a different class */
   484    326       return 75;
   485    327     }
   486    328     /* Any other subsitution */
................................................................................
   495    337   **
   496    338   ** Smaller numbers mean a closer match.
   497    339   **
   498    340   ** Negative values indicate an error:
   499    341   **    -1  One of the inputs is NULL
   500    342   **    -2  Non-ASCII characters on input
   501    343   **    -3  Unable to allocate memory 
          344  +**
          345  +** If pnMatch is not NULL, then *pnMatch is set to the number of bytes
          346  +** of zB that matched the pattern in zA. If zA does not end with a '*',
          347  +** then this value is always the number of bytes in zB (i.e. strlen(zB)).
          348  +** If zA does end in a '*', then it is the number of bytes in the prefix
          349  +** of zB that was deemed to match zA.
   502    350   */
   503         -static int editdist(const char *zA, const char *zB){
          351  +static int editdist1(const char *zA, const char *zB, int *pnMatch){
   504    352     int nA, nB;            /* Number of characters in zA[] and zB[] */
   505    353     int xA, xB;            /* Loop counters for zA[] and zB[] */
   506    354     char cA, cB;           /* Current character of zA and zB */
   507    355     char cAprev, cBprev;   /* Previous character of zA and zB */
          356  +  char cAnext, cBnext;   /* Next character in zA and zB */
   508    357     int d;                 /* North-west cost value */
   509    358     int dc = 0;            /* North-west character value */
   510    359     int res;               /* Final result */
   511    360     int *m;                /* The cost matrix */
   512    361     char *cx;              /* Corresponding character values */
   513    362     int *toFree = 0;       /* Malloced space */
   514    363     int mStack[60+15];     /* Stack space to use if not too much is needed */
          364  +  int nMatch = 0;
   515    365   
   516    366     /* Early out if either input is NULL */
   517    367     if( zA==0 || zB==0 ) return -1;
   518    368   
   519    369     /* Skip any common prefix */
   520         -  while( zA[0] && zA[0]==zB[0] ){ dc = zA[0]; zA++; zB++; }
          370  +  while( zA[0] && zA[0]==zB[0] ){ dc = zA[0]; zA++; zB++; nMatch++; }
          371  +  if( pnMatch ) *pnMatch = nMatch;
   521    372     if( zA[0]==0 && zB[0]==0 ) return 0;
   522    373   
   523    374   #if 0
   524    375     printf("A=\"%s\" B=\"%s\" dc=%c\n", zA, zB, dc?dc:' ');
   525    376   #endif
   526    377   
   527    378     /* Verify input strings and measure their lengths */
   528    379     for(nA=0; zA[nA]; nA++){
   529         -    if( zA[nA]>127 ) return -2;
          380  +    if( zA[nA]&0x80 ) return -2;
   530    381     }
   531    382     for(nB=0; zB[nB]; nB++){
   532         -    if( zB[nB]>127 ) return -2;
          383  +    if( zB[nB]&0x80 ) return -2;
   533    384     }
   534    385   
   535    386     /* Special processing if either string is empty */
   536    387     if( nA==0 ){
   537    388       cBprev = dc;
   538    389       for(xB=res=0; (cB = zB[xB])!=0; xB++){
   539         -      res += insertOrDeleteCost(cBprev, cB)/FINAL_INS_COST_DIV;
          390  +      res += insertOrDeleteCost(cBprev, cB, zB[xB+1])/FINAL_INS_COST_DIV;
   540    391         cBprev = cB;
   541    392       }
   542    393       return res;
   543    394     }
   544    395     if( nB==0 ){
   545    396       cAprev = dc;
   546    397       for(xA=res=0; (cA = zA[xA])!=0; xA++){
   547         -      res += insertOrDeleteCost(cAprev, cA);
          398  +      res += insertOrDeleteCost(cAprev, cA, zA[xA+1]);
   548    399         cAprev = cA;
   549    400       }
   550    401       return res;
   551    402     }
   552    403   
   553    404     /* A is a prefix of B */
   554    405     if( zA[0]=='*' && zA[1]==0 ) return 0;
................................................................................
   563    414     cx = (char*)&m[nB+1];
   564    415   
   565    416     /* Compute the Wagner edit distance */
   566    417     m[0] = 0;
   567    418     cx[0] = dc;
   568    419     cBprev = dc;
   569    420     for(xB=1; xB<=nB; xB++){
          421  +    cBnext = zB[xB];
   570    422       cB = zB[xB-1];
   571    423       cx[xB] = cB;
   572         -    m[xB] = m[xB-1] + insertOrDeleteCost(cBprev, cB);
          424  +    m[xB] = m[xB-1] + insertOrDeleteCost(cBprev, cB, cBnext);
   573    425       cBprev = cB;
   574    426     }
   575    427     cAprev = dc;
   576    428     for(xA=1; xA<=nA; xA++){
   577    429       int lastA = (xA==nA);
   578    430       cA = zA[xA-1];
          431  +    cAnext = zA[xA];
   579    432       if( cA=='*' && lastA ) break;
   580    433       d = m[0];
   581    434       dc = cx[0];
   582         -    m[0] = d + insertOrDeleteCost(cAprev, cA);
          435  +    m[0] = d + insertOrDeleteCost(cAprev, cA, cAnext);
   583    436       cBprev = 0;
   584    437       for(xB=1; xB<=nB; xB++){
   585    438         int totalCost, insCost, delCost, subCost, ncx;
   586    439         cB = zB[xB-1];
          440  +      cBnext = zB[xB];
   587    441   
   588    442         /* Cost to insert cB */
   589         -      insCost = insertOrDeleteCost(cx[xB-1], cB);
          443  +      insCost = insertOrDeleteCost(cx[xB-1], cB, cBnext);
   590    444         if( lastA ) insCost /= FINAL_INS_COST_DIV;
   591    445   
   592    446         /* Cost to delete cA */
   593         -      delCost = insertOrDeleteCost(cx[xB], cA);
          447  +      delCost = insertOrDeleteCost(cx[xB], cA, cBnext);
   594    448   
   595    449         /* Cost to substitute cA->cB */
   596    450         subCost = substituteCost(cx[xB-1], cA, cB);
   597    451   
   598    452         /* Best cost */
   599    453         totalCost = insCost + m[xB-1];
   600    454         ncx = cB;
................................................................................
   620    474         cx[xB] = ncx;
   621    475         cBprev = cB;
   622    476       }
   623    477       cAprev = cA;
   624    478     }
   625    479   
   626    480     /* Free the wagner matrix and return the result */
   627         -  if( cA=='*' && nB>nA ){
   628         -    res = m[nA];
   629         -    for(xB=nA+1; xB<=nB; xB++){
   630         -      if( m[xB]<res ) res = m[xB];
          481  +  if( cA=='*' ){
          482  +    res = m[1];
          483  +    for(xB=1; xB<=nB; xB++){
          484  +      if( m[xB]<res ){
          485  +        res = m[xB];
          486  +        if( pnMatch ) *pnMatch = xB+nMatch;
          487  +      }
   631    488       }
   632    489     }else{
   633    490       res = m[nB];
          491  +    /* In the current implementation, pnMatch is always NULL if zA does
          492  +    ** not end in "*" */
          493  +    assert( pnMatch==0 );
   634    494     }
   635    495     sqlite3_free(toFree);
   636    496     return res;
   637    497   }
   638    498   
   639    499   /*
   640    500   ** Function:    editdist(A,B)
................................................................................
   645    505   ** cost.
   646    506   */
   647    507   static void editdistSqlFunc(
   648    508     sqlite3_context *context,
   649    509     int argc,
   650    510     sqlite3_value **argv
   651    511   ){
   652         -  int res = editdist((const char*)sqlite3_value_text(argv[0]),
   653         -                    (const char*)sqlite3_value_text(argv[1]));
          512  +  int res = editdist1(
          513  +                    (const char*)sqlite3_value_text(argv[0]),
          514  +                    (const char*)sqlite3_value_text(argv[1]),
          515  +                    0);
   654    516     if( res<0 ){
   655    517       if( res==(-3) ){
   656    518         sqlite3_result_error_nomem(context);
   657    519       }else if( res==(-2) ){
   658    520         sqlite3_result_error(context, "non-ASCII input to editdist()", -1);
   659    521       }else{
   660    522         sqlite3_result_error(context, "NULL input to editdist()", -1);
   661    523       }
   662    524     }else{ 
   663    525       sqlite3_result_int(context, res);
   664    526     }
   665    527   }
   666    528   
   667         -#if !SQLITE_CORE
          529  +/* End of the fixed-cost edit distance implementation
          530  +******************************************************************************
          531  +*****************************************************************************
          532  +** Begin: Configurable cost unicode edit distance routines
          533  +*/
          534  +/* Forward declaration of structures */
          535  +typedef struct EditDist3Cost EditDist3Cost;
          536  +typedef struct EditDist3Config EditDist3Config;
          537  +typedef struct EditDist3Point EditDist3Point;
          538  +typedef struct EditDist3From EditDist3From;
          539  +typedef struct EditDist3FromString EditDist3FromString;
          540  +typedef struct EditDist3To EditDist3To;
          541  +typedef struct EditDist3ToString EditDist3ToString;
          542  +typedef struct EditDist3Lang EditDist3Lang;
          543  +
          544  +
          545  +/*
          546  +** An entry in the edit cost table
          547  +*/
          548  +struct EditDist3Cost {
          549  +  EditDist3Cost *pNext;     /* Next cost element */
          550  +  u8 nFrom;                 /* Number of bytes in aFrom */
          551  +  u8 nTo;                   /* Number of bytes in aTo */
          552  +  u16 iCost;                /* Cost of this transformation */
          553  +  char a[4]    ;            /* FROM string followed by TO string */
          554  +  /* Additional TO and FROM string bytes appended as necessary */
          555  +};
          556  +
          557  +/*
          558  +** Edit costs for a particular language ID 
          559  +*/
          560  +struct EditDist3Lang {
          561  +  int iLang;             /* Language ID */
          562  +  int iInsCost;          /* Default insertion cost */
          563  +  int iDelCost;          /* Default deletion cost */
          564  +  int iSubCost;          /* Default substitution cost */
          565  +  EditDist3Cost *pCost;  /* Costs */
          566  +};
          567  +
          568  +
          569  +/*
          570  +** The default EditDist3Lang object, with default costs.
          571  +*/
          572  +static const EditDist3Lang editDist3Lang = { 0, 100, 100, 150, 0 };
          573  +
          574  +/*
          575  +** Complete configuration
          576  +*/
          577  +struct EditDist3Config {
          578  +  int nLang;             /* Number of language IDs.  Size of a[] */
          579  +  EditDist3Lang *a;      /* One for each distinct language ID */
          580  +};
          581  +
          582  +/*
          583  +** Extra information about each character in the FROM string.
          584  +*/
          585  +struct EditDist3From {
          586  +  int nSubst;              /* Number of substitution cost entries */
          587  +  int nDel;                /* Number of deletion cost entries */
          588  +  int nByte;               /* Number of bytes in this character */
          589  +  EditDist3Cost **apSubst; /* Array of substitution costs for this element */
          590  +  EditDist3Cost **apDel;   /* Array of deletion cost entries */
          591  +};
          592  +
          593  +/*
          594  +** A precompiled FROM string.
          595  +*
          596  +** In the common case we expect the FROM string to be reused multiple times.
          597  +** In other words, the common case will be to measure the edit distance
          598  +** from a single origin string to multiple target strings.
          599  +*/
          600  +struct EditDist3FromString {
          601  +  char *z;                 /* The complete text of the FROM string */
          602  +  int n;                   /* Number of characters in the FROM string */
          603  +  int isPrefix;            /* True if ends with '*' character */
          604  +  EditDist3From *a;        /* Extra info about each char of the FROM string */
          605  +};
          606  +
          607  +/*
          608  +** Extra information about each character in the TO string.
          609  +*/
          610  +struct EditDist3To {
          611  +  int nIns;                /* Number of insertion cost entries */
          612  +  int nByte;               /* Number of bytes in this character */
          613  +  EditDist3Cost **apIns;   /* Array of deletion cost entries */
          614  +};
          615  +
          616  +/*
          617  +** A precompiled FROM string
          618  +*/
          619  +struct EditDist3ToString {
          620  +  char *z;                 /* The complete text of the TO string */
          621  +  int n;                   /* Number of characters in the TO string */
          622  +  EditDist3To *a;          /* Extra info about each char of the TO string */
          623  +};
          624  +
          625  +/*
          626  +** Clear or delete an instance of the object that records all edit-distance
          627  +** weights.
          628  +*/
          629  +static void editDist3ConfigClear(EditDist3Config *p){
          630  +  int i;
          631  +  if( p==0 ) return;
          632  +  for(i=0; i<p->nLang; i++){
          633  +    EditDist3Cost *pCost, *pNext;
          634  +    pCost = p->a[i].pCost;
          635  +    while( pCost ){
          636  +      pNext = pCost->pNext;
          637  +      sqlite3_free(pCost);
          638  +      pCost = pNext;
          639  +    }
          640  +  }
          641  +  sqlite3_free(p->a);
          642  +  memset(p, 0, sizeof(*p));
          643  +}
          644  +static void editDist3ConfigDelete(void *pIn){
          645  +  EditDist3Config *p = (EditDist3Config*)pIn;
          646  +  editDist3ConfigClear(p);
          647  +  sqlite3_free(p);
          648  +}
          649  +
          650  +/*
          651  +** Load all edit-distance weights from a table.
          652  +*/
          653  +static int editDist3ConfigLoad(
          654  +  EditDist3Config *p,      /* The edit distance configuration to load */
          655  +  sqlite3 *db,            /* Load from this database */
          656  +  const char *zTable      /* Name of the table from which to load */
          657  +){
          658  +  sqlite3_stmt *pStmt;
          659  +  int rc, rc2;
          660  +  char *zSql;
          661  +  int iLangPrev = -9999;
          662  +  EditDist3Lang *pLang = 0;
          663  +
          664  +  zSql = sqlite3_mprintf("SELECT iLang, cFrom, cTo, iCost"
          665  +                         " FROM \"%w\" WHERE iLang>=0 ORDER BY iLang", zTable);
          666  +  if( zSql==0 ) return SQLITE_NOMEM;
          667  +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
          668  +  sqlite3_free(zSql);
          669  +  if( rc ) return rc;
          670  +  editDist3ConfigClear(p);
          671  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
          672  +    int iLang = sqlite3_column_int(pStmt, 0);
          673  +    const char *zFrom = (const char*)sqlite3_column_text(pStmt, 1);
          674  +    int nFrom = zFrom ? sqlite3_column_bytes(pStmt, 1) : 0;
          675  +    const char *zTo = (const char*)sqlite3_column_text(pStmt, 2);
          676  +    int nTo = zTo ? sqlite3_column_bytes(pStmt, 2) : 0;
          677  +    int iCost = sqlite3_column_int(pStmt, 3);
          678  +
          679  +    assert( zFrom!=0 || nFrom==0 );
          680  +    assert( zTo!=0 || nTo==0 );
          681  +    if( nFrom>100 || nTo>100 ) continue;
          682  +    if( iCost<0 ) continue;
          683  +    if( pLang==0 || iLang!=iLangPrev ){
          684  +      EditDist3Lang *pNew;
          685  +      pNew = sqlite3_realloc(p->a, (p->nLang+1)*sizeof(p->a[0]));
          686  +      if( pNew==0 ){ rc = SQLITE_NOMEM; break; }
          687  +      p->a = pNew;
          688  +      pLang = &p->a[p->nLang];
          689  +      p->nLang++;
          690  +      pLang->iLang = iLang;
          691  +      pLang->iInsCost = 100;
          692  +      pLang->iDelCost = 100;
          693  +      pLang->iSubCost = 150;
          694  +      pLang->pCost = 0;
          695  +      iLangPrev = iLang;
          696  +    }
          697  +    if( nFrom==1 && zFrom[0]=='?' && nTo==0 ){
          698  +      pLang->iDelCost = iCost;
          699  +    }else if( nFrom==0 && nTo==1 && zTo[0]=='?' ){
          700  +      pLang->iInsCost = iCost;
          701  +    }else if( nFrom==1 && nTo==1 && zFrom[0]=='?' && zTo[0]=='?' ){
          702  +      pLang->iSubCost = iCost;
          703  +    }else{
          704  +      EditDist3Cost *pCost;
          705  +      int nExtra = nFrom + nTo - 4;
          706  +      if( nExtra<0 ) nExtra = 0;
          707  +      pCost = sqlite3_malloc( sizeof(*pCost) + nExtra );
          708  +      if( pCost==0 ){ rc = SQLITE_NOMEM; break; }
          709  +      pCost->nFrom = nFrom;
          710  +      pCost->nTo = nTo;
          711  +      pCost->iCost = iCost;
          712  +      memcpy(pCost->a, zFrom, nFrom);
          713  +      memcpy(pCost->a + nFrom, zTo, nTo);
          714  +      pCost->pNext = pLang->pCost;
          715  +      pLang->pCost = pCost; 
          716  +    }
          717  +  }
          718  +  rc2 = sqlite3_finalize(pStmt);
          719  +  if( rc==SQLITE_OK ) rc = rc2;
          720  +  return rc;
          721  +}
          722  +
          723  +/*
          724  +** Return the length (in bytes) of a utf-8 character.  Or return a maximum
          725  +** of N.
          726  +*/
          727  +static int utf8Len(unsigned char c, int N){
          728  +  int len = 1;
          729  +  if( c>0x7f ){
          730  +    if( (c&0xe0)==0xc0 ){
          731  +      len = 2;
          732  +    }else if( (c&0xf0)==0xe0 ){
          733  +      len = 3;
          734  +    }else{
          735  +      len = 4;
          736  +    }
          737  +  }
          738  +  if( len>N ) len = N;
          739  +  return len;
          740  +}
          741  +
          742  +/*
          743  +** Return TRUE (non-zero) of the To side of the given cost matches
          744  +** the given string.
          745  +*/
          746  +static int matchTo(EditDist3Cost *p, const char *z, int n){
          747  +  if( p->nTo>n ) return 0;
          748  +  if( memcmp(p->a+p->nFrom, z, p->nTo)!=0 ) return 0;
          749  +  return 1;
          750  +}
          751  +
          752  +/*
          753  +** Return TRUE (non-zero) of the To side of the given cost matches
          754  +** the given string.
          755  +*/
          756  +static int matchFrom(EditDist3Cost *p, const char *z, int n){
          757  +  assert( p->nFrom<=n );
          758  +  if( memcmp(p->a, z, p->nFrom)!=0 ) return 0;
          759  +  return 1;
          760  +}
          761  +
          762  +/*
          763  +** Return TRUE (non-zero) of the next FROM character and the next TO
          764  +** character are the same.
          765  +*/
          766  +static int matchFromTo(
          767  +  EditDist3FromString *pStr,  /* Left hand string */
          768  +  int n1,                     /* Index of comparison character on the left */
          769  +  const char *z2,             /* Right-handl comparison character */
          770  +  int n2                      /* Bytes remaining in z2[] */
          771  +){
          772  +  int b1 = pStr->a[n1].nByte;
          773  +  if( b1>n2 ) return 0;
          774  +  if( memcmp(pStr->z+n1, z2, b1)!=0 ) return 0;
          775  +  return 1;
          776  +}
          777  +
          778  +/*
          779  +** Delete an EditDist3FromString objecct
          780  +*/
          781  +static void editDist3FromStringDelete(EditDist3FromString *p){
          782  +  int i;
          783  +  if( p ){
          784  +    for(i=0; i<p->n; i++){
          785  +      sqlite3_free(p->a[i].apDel);
          786  +      sqlite3_free(p->a[i].apSubst);
          787  +    }
          788  +    sqlite3_free(p);
          789  +  }
          790  +}
          791  +
          792  +/*
          793  +** Create a EditDist3FromString object.
          794  +*/
          795  +static EditDist3FromString *editDist3FromStringNew(
          796  +  const EditDist3Lang *pLang,
          797  +  const char *z,
          798  +  int n
          799  +){
          800  +  EditDist3FromString *pStr;
          801  +  EditDist3Cost *p;
          802  +  int i;
          803  +
          804  +  if( z==0 ) return 0;
          805  +  if( n<0 ) n = (int)strlen(z);
          806  +  pStr = sqlite3_malloc( sizeof(*pStr) + sizeof(pStr->a[0])*n + n + 1 );
          807  +  if( pStr==0 ) return 0;
          808  +  pStr->a = (EditDist3From*)&pStr[1];
          809  +  memset(pStr->a, 0, sizeof(pStr->a[0])*n);
          810  +  pStr->n = n;
          811  +  pStr->z = (char*)&pStr->a[n];
          812  +  memcpy(pStr->z, z, n+1);
          813  +  if( n && z[n-1]=='*' ){
          814  +    pStr->isPrefix = 1;
          815  +    n--;
          816  +    pStr->n--;
          817  +    pStr->z[n] = 0;
          818  +  }else{
          819  +    pStr->isPrefix = 0;
          820  +  }
          821  +
          822  +  for(i=0; i<n; i++){
          823  +    EditDist3From *pFrom = &pStr->a[i];
          824  +    memset(pFrom, 0, sizeof(*pFrom));
          825  +    pFrom->nByte = utf8Len((unsigned char)z[i], n-i);
          826  +    for(p=pLang->pCost; p; p=p->pNext){
          827  +      EditDist3Cost **apNew;
          828  +      if( i+p->nFrom>n ) continue;
          829  +      if( matchFrom(p, z+i, n-i)==0 ) continue;
          830  +      if( p->nTo==0 ){
          831  +        apNew = sqlite3_realloc(pFrom->apDel,
          832  +                                sizeof(*apNew)*(pFrom->nDel+1));
          833  +        if( apNew==0 ) break;
          834  +        pFrom->apDel = apNew;
          835  +        apNew[pFrom->nDel++] = p;
          836  +      }else{
          837  +        apNew = sqlite3_realloc(pFrom->apSubst,
          838  +                                sizeof(*apNew)*(pFrom->nSubst+1));
          839  +        if( apNew==0 ) break;
          840  +        pFrom->apSubst = apNew;
          841  +        apNew[pFrom->nSubst++] = p;
          842  +      }
          843  +    }
          844  +    if( p ){
          845  +      editDist3FromStringDelete(pStr);
          846  +      pStr = 0;
          847  +      break;
          848  +    }
          849  +  }
          850  +  return pStr;
          851  +}
          852  +
          853  +/*
          854  +** Update entry m[i] such that it is the minimum of its current value
          855  +** and m[j]+iCost.
          856  +**
          857  +** If the iCost is 1,000,000 or greater, then consider the cost to be
          858  +** infinite and skip the update.
          859  +*/
          860  +static void updateCost(
          861  +  unsigned int *m,
          862  +  int i,
          863  +  int j,
          864  +  int iCost
          865  +){
          866  +  assert( iCost>=0 );
          867  +  if( iCost<10000 ){
          868  +    unsigned int b = m[j] + iCost;
          869  +    if( b<m[i] ) m[i] = b;
          870  +  }
          871  +}
          872  +
          873  +/* Compute the edit distance between two strings.
          874  +**
          875  +** If an error occurs, return a negative number which is the error code.
          876  +**
          877  +** If pnMatch is not NULL, then *pnMatch is set to the number of characters
          878  +** (not bytes) in z2 that matched the search pattern in *pFrom. If pFrom does
          879  +** not contain the pattern for a prefix-search, then this is always the number
          880  +** of characters in z2. If pFrom does contain a prefix search pattern, then
          881  +** it is the number of characters in the prefix of z2 that was deemed to 
          882  +** match pFrom.
          883  +*/
          884  +static int editDist3Core(
          885  +  EditDist3FromString *pFrom,  /* The FROM string */
          886  +  const char *z2,              /* The TO string */
          887  +  int n2,                      /* Length of the TO string */
          888  +  const EditDist3Lang *pLang,  /* Edit weights for a particular language ID */
          889  +  int *pnMatch                 /* OUT: Characters in matched prefix */
          890  +){
          891  +  int k, n;
          892  +  int i1, b1;
          893  +  int i2, b2;
          894  +  EditDist3FromString f = *pFrom;
          895  +  EditDist3To *a2;
          896  +  unsigned int *m;
          897  +  int szRow;
          898  +  EditDist3Cost *p;
          899  +  int res;
          900  +
          901  +  /* allocate the Wagner matrix and the aTo[] array for the TO string */
          902  +  n = (f.n+1)*(n2+1);
          903  +  n = (n+1)&~1;
          904  +  m = sqlite3_malloc( n*sizeof(m[0]) + sizeof(a2[0])*n2 );
          905  +  if( m==0 ) return -1;            /* Out of memory */
          906  +  a2 = (EditDist3To*)&m[n];
          907  +  memset(a2, 0, sizeof(a2[0])*n2);
          908  +
          909  +  /* Fill in the a1[] matrix for all characters of the TO string */
          910  +  for(i2=0; i2<n2; i2++){
          911  +    a2[i2].nByte = utf8Len((unsigned char)z2[i2], n2-i2);
          912  +    for(p=pLang->pCost; p; p=p->pNext){
          913  +      EditDist3Cost **apNew;
          914  +      if( p->nFrom>0 ) continue;
          915  +      if( i2+p->nTo>n2 ) continue;
          916  +      if( matchTo(p, z2+i2, n2-i2)==0 ) continue;
          917  +      a2[i2].nIns++;
          918  +      apNew = sqlite3_realloc(a2[i2].apIns, sizeof(*apNew)*a2[i2].nIns);
          919  +      if( apNew==0 ){
          920  +        res = -1;  /* Out of memory */
          921  +        goto editDist3Abort;
          922  +      }
          923  +      a2[i2].apIns = apNew;
          924  +      a2[i2].apIns[a2[i2].nIns-1] = p;
          925  +    }
          926  +  }
          927  +
          928  +  /* Prepare to compute the minimum edit distance */
          929  +  szRow = f.n+1;
          930  +  memset(m, 0x01, (n2+1)*szRow*sizeof(m[0]));
          931  +  m[0] = 0;
          932  +
          933  +  /* First fill in the top-row of the matrix with FROM deletion costs */
          934  +  for(i1=0; i1<f.n; i1 += b1){
          935  +    b1 = f.a[i1].nByte;
          936  +    updateCost(m, i1+b1, i1, pLang->iDelCost);
          937  +    for(k=0; k<f.a[i1].nDel; k++){
          938  +      p = f.a[i1].apDel[k];
          939  +      updateCost(m, i1+p->nFrom, i1, p->iCost);
          940  +    }
          941  +  }
          942  +
          943  +  /* Fill in all subsequent rows, top-to-bottom, left-to-right */
          944  +  for(i2=0; i2<n2; i2 += b2){
          945  +    int rx;      /* Starting index for current row */
          946  +    int rxp;     /* Starting index for previous row */
          947  +    b2 = a2[i2].nByte;
          948  +    rx = szRow*(i2+b2);
          949  +    rxp = szRow*i2;
          950  +    updateCost(m, rx, rxp, pLang->iInsCost);
          951  +    for(k=0; k<a2[i2].nIns; k++){
          952  +      p = a2[i2].apIns[k];
          953  +      updateCost(m, szRow*(i2+p->nTo), rxp, p->iCost);
          954  +    }
          955  +    for(i1=0; i1<f.n; i1+=b1){
          956  +      int cx;    /* Index of current cell */
          957  +      int cxp;   /* Index of cell immediately to the left */
          958  +      int cxd;   /* Index of cell to the left and one row above */
          959  +      int cxu;   /* Index of cell immediately above */
          960  +      b1 = f.a[i1].nByte;
          961  +      cxp = rx + i1;
          962  +      cx = cxp + b1;
          963  +      cxd = rxp + i1;
          964  +      cxu = cxd + b1;
          965  +      updateCost(m, cx, cxp, pLang->iDelCost);
          966  +      for(k=0; k<f.a[i1].nDel; k++){
          967  +        p = f.a[i1].apDel[k];
          968  +        updateCost(m, cxp+p->nFrom, cxp, p->iCost);
          969  +      }
          970  +      updateCost(m, cx, cxu, pLang->iInsCost);
          971  +      if( matchFromTo(&f, i1, z2+i2, n2-i2) ){
          972  +        updateCost(m, cx, cxd, 0);
          973  +      }
          974  +      updateCost(m, cx, cxd, pLang->iSubCost);
          975  +      for(k=0; k<f.a[i1].nSubst; k++){
          976  +        p = f.a[i1].apSubst[k];
          977  +        if( matchTo(p, z2+i2, n2-i2) ){
          978  +          updateCost(m, cxd+p->nFrom+szRow*p->nTo, cxd, p->iCost);
          979  +        }
          980  +      }
          981  +    }
          982  +  }
          983  +
          984  +#if 0  /* Enable for debugging */
          985  +  printf("         ^");
          986  +  for(i1=0; i1<f.n; i1++) printf(" %c-%2x", f.z[i1], f.z[i1]&0xff);
          987  +  printf("\n   ^:");
          988  +  for(i1=0; i1<szRow; i1++){
          989  +    int v = m[i1];
          990  +    if( v>9999 ) printf(" ****");
          991  +    else         printf(" %4d", v);
          992  +  }
          993  +  printf("\n");
          994  +  for(i2=0; i2<n2; i2++){
          995  +    printf("%c-%02x:", z2[i2], z2[i2]&0xff);
          996  +    for(i1=0; i1<szRow; i1++){
          997  +      int v = m[(i2+1)*szRow+i1];
          998  +      if( v>9999 ) printf(" ****");
          999  +      else         printf(" %4d", v);
         1000  +    }
         1001  +    printf("\n");
         1002  +  }
         1003  +#endif
         1004  +
         1005  +  /* Free memory allocations and return the result */
         1006  +  res = (int)m[szRow*(n2+1)-1];
         1007  +  n = n2;
         1008  +  if( f.isPrefix ){
         1009  +    for(i2=1; i2<=n2; i2++){
         1010  +      int b = m[szRow*i2-1];
         1011  +      if( b<=res ){ 
         1012  +        res = b;
         1013  +        n = i2 - 1;
         1014  +      }
         1015  +    }
         1016  +  }
         1017  +  if( pnMatch ){
         1018  +    int nExtra = 0;
         1019  +    for(k=0; k<n; k++){
         1020  +      if( (z2[k] & 0xc0)==0x80 ) nExtra++;
         1021  +    }
         1022  +    *pnMatch = n - nExtra;
         1023  +  }
         1024  +
         1025  +editDist3Abort:
         1026  +  for(i2=0; i2<n2; i2++) sqlite3_free(a2[i2].apIns);
         1027  +  sqlite3_free(m);
         1028  +  return res;
         1029  +}
         1030  +
         1031  +/*
         1032  +** Get an appropriate EditDist3Lang object.
         1033  +*/
         1034  +static const EditDist3Lang *editDist3FindLang(
         1035  +  EditDist3Config *pConfig,
         1036  +  int iLang
         1037  +){
         1038  +  int i;
         1039  +  for(i=0; i<pConfig->nLang; i++){
         1040  +    if( pConfig->a[i].iLang==iLang ) return &pConfig->a[i];
         1041  +  }
         1042  +  return &editDist3Lang;
         1043  +}
         1044  +
         1045  +/*
         1046  +** Function:    editdist3(A,B,iLang)
         1047  +**              editdist3(tablename)
         1048  +**
         1049  +** Return the cost of transforming string A into string B using edit
         1050  +** weights for iLang.
         1051  +**
         1052  +** The second form loads edit weights into memory from a table.
         1053  +*/
         1054  +static void editDist3SqlFunc(
         1055  +  sqlite3_context *context,
         1056  +  int argc,
         1057  +  sqlite3_value **argv
         1058  +){
         1059  +  EditDist3Config *pConfig = (EditDist3Config*)sqlite3_user_data(context);
         1060  +  sqlite3 *db = sqlite3_context_db_handle(context);
         1061  +  int rc;
         1062  +  if( argc==1 ){
         1063  +    const char *zTable = (const char*)sqlite3_value_text(argv[0]);
         1064  +    rc = editDist3ConfigLoad(pConfig, db, zTable);
         1065  +    if( rc ) sqlite3_result_error_code(context, rc);
         1066  +  }else{
         1067  +    const char *zA = (const char*)sqlite3_value_text(argv[0]);
         1068  +    const char *zB = (const char*)sqlite3_value_text(argv[1]);
         1069  +    int nA = sqlite3_value_bytes(argv[0]);
         1070  +    int nB = sqlite3_value_bytes(argv[1]);
         1071  +    int iLang = argc==3 ? sqlite3_value_int(argv[2]) : 0;
         1072  +    const EditDist3Lang *pLang = editDist3FindLang(pConfig, iLang);
         1073  +    EditDist3FromString *pFrom;
         1074  +    int dist;
         1075  +
         1076  +    pFrom = editDist3FromStringNew(pLang, zA, nA);
         1077  +    if( pFrom==0 ){
         1078  +      sqlite3_result_error_nomem(context);
         1079  +      return;
         1080  +    }
         1081  +    dist = editDist3Core(pFrom, zB, nB, pLang, 0);
         1082  +    editDist3FromStringDelete(pFrom);
         1083  +    if( dist==(-1) ){
         1084  +      sqlite3_result_error_nomem(context);
         1085  +    }else{
         1086  +      sqlite3_result_int(context, dist);
         1087  +    }
         1088  +  } 
         1089  +}
         1090  +
         1091  +/*
         1092  +** Register the editDist3 function with SQLite
         1093  +*/
         1094  +static int editDist3Install(sqlite3 *db){
         1095  +  int rc;
         1096  +  EditDist3Config *pConfig = sqlite3_malloc( sizeof(*pConfig) );
         1097  +  if( pConfig==0 ) return SQLITE_NOMEM;
         1098  +  memset(pConfig, 0, sizeof(*pConfig));
         1099  +  rc = sqlite3_create_function_v2(db, "editdist3",
         1100  +              2, SQLITE_UTF8, pConfig, editDist3SqlFunc, 0, 0, 0);
         1101  +  if( rc==SQLITE_OK ){
         1102  +    rc = sqlite3_create_function_v2(db, "editdist3",
         1103  +                3, SQLITE_UTF8, pConfig, editDist3SqlFunc, 0, 0, 0);
         1104  +  }
         1105  +  if( rc==SQLITE_OK ){
         1106  +    rc = sqlite3_create_function_v2(db, "editdist3",
         1107  +                1, SQLITE_UTF8, pConfig, editDist3SqlFunc, 0, 0,
         1108  +                editDist3ConfigDelete);
         1109  +  }else{
         1110  +    sqlite3_free(pConfig);
         1111  +  }
         1112  +  return rc;
         1113  +}
         1114  +/* End configurable cost unicode edit distance routines
         1115  +******************************************************************************
         1116  +******************************************************************************
         1117  +** Begin transliterate unicode-to-ascii implementation
         1118  +*/
         1119  +
         1120  +#if !SQLITE_AMALGAMATION
   668   1121   /*
   669   1122   ** This lookup table is used to help decode the first byte of
   670   1123   ** a multi-byte UTF8 character.
   671   1124   */
   672   1125   static const unsigned char sqlite3Utf8Trans1[] = {
   673   1126     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   674   1127     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
................................................................................
   683   1136   
   684   1137   /*
   685   1138   ** Return the value of the first UTF-8 character in the string.
   686   1139   */
   687   1140   static int utf8Read(const unsigned char *z, int n, int *pSize){
   688   1141     int c, i;
   689   1142   
   690         -  if( n==0 ){
         1143  +  /* All callers to this routine (in the current implementation)
         1144  +  ** always have n>0. */
         1145  +  if( NEVER(n==0) ){
   691   1146       c = i = 0;
   692   1147     }else{
   693   1148       c = z[0];
   694   1149       i = 1;
   695   1150       if( c>=0xc0 ){
   696   1151         c = sqlite3Utf8Trans1[c-0xc0];
   697   1152         while( i<n && (z[i] & 0xc0)==0x80 ){
................................................................................
   698   1153           c = (c<<6) + (0x3f & z[i++]);
   699   1154         }
   700   1155       }
   701   1156     }
   702   1157     *pSize = i;
   703   1158     return c;
   704   1159   }
         1160  +
         1161  +/*
         1162  +** Return the number of characters in the utf-8 string in the nIn byte
         1163  +** buffer pointed to by zIn.
         1164  +*/
         1165  +static int utf8Charlen(const char *zIn, int nIn){
         1166  +  int i;
         1167  +  int nChar = 0;
         1168  +  for(i=0; i<nIn; nChar++){
         1169  +    int sz;
         1170  +    utf8Read((const unsigned char *)&zIn[i], nIn-i, &sz);
         1171  +    i += sz;
         1172  +  }
         1173  +  return nChar;
         1174  +}
   705   1175   
   706   1176   /*
   707   1177   ** Table of translations from unicode characters into ASCII.
   708   1178   */
   709   1179   static const struct {
   710   1180    unsigned short int cFrom;
   711   1181    unsigned char cTo0, cTo1;
................................................................................
  1014   1484     { 0x0423,  0x55, 0x00 },  /* У to U */
  1015   1485     { 0x0424,  0x46, 0x00 },  /* Ф to F */
  1016   1486     { 0x0425,  0x4b, 0x68 },  /* Х to Kh */
  1017   1487     { 0x0426,  0x54, 0x63 },  /* Ц to Tc */
  1018   1488     { 0x0427,  0x43, 0x68 },  /* Ч to Ch */
  1019   1489     { 0x0428,  0x53, 0x68 },  /* Ш to Sh */
  1020   1490     { 0x0429,  0x53, 0x68 },  /* Щ to Shch */
         1491  +  { 0x042A,  0x61, 0x00 },  /*  to A */
  1021   1492     { 0x042B,  0x59, 0x00 },  /* Ы to Y */
         1493  +  { 0x042C,  0x59, 0x00 },  /*  to Y */
  1022   1494     { 0x042D,  0x45, 0x00 },  /* Э to E */
  1023   1495     { 0x042E,  0x49, 0x75 },  /* Ю to Iu */
  1024   1496     { 0x042F,  0x49, 0x61 },  /* Я to Ia */
  1025   1497     { 0x0430,  0x61, 0x00 },  /* а to a */
  1026   1498     { 0x0431,  0x62, 0x00 },  /* б to b */
  1027   1499     { 0x0432,  0x76, 0x00 },  /* в to v */
  1028   1500     { 0x0433,  0x67, 0x00 },  /* г to g */
................................................................................
  1044   1516     { 0x0443,  0x75, 0x00 },  /* у to u */
  1045   1517     { 0x0444,  0x66, 0x00 },  /* ф to f */
  1046   1518     { 0x0445,  0x6b, 0x68 },  /* х to kh */
  1047   1519     { 0x0446,  0x74, 0x63 },  /* ц to tc */
  1048   1520     { 0x0447,  0x63, 0x68 },  /* ч to ch */
  1049   1521     { 0x0448,  0x73, 0x68 },  /* ш to sh */
  1050   1522     { 0x0449,  0x73, 0x68 },  /* щ to shch */
         1523  +  { 0x044A,  0x61, 0x00 },  /*  to a */
  1051   1524     { 0x044B,  0x79, 0x00 },  /* ы to y */
         1525  +  { 0x044C,  0x79, 0x00 },  /*  to y */
  1052   1526     { 0x044D,  0x65, 0x00 },  /* э to e */
  1053   1527     { 0x044E,  0x69, 0x75 },  /* ю to iu */
  1054   1528     { 0x044F,  0x69, 0x61 },  /* я to ia */
  1055   1529     { 0x0450,  0x65, 0x00 },  /* ѐ to e */
  1056   1530     { 0x0451,  0x65, 0x00 },  /* ё to e */
  1057   1531     { 0x0452,  0x64, 0x00 },  /* ђ to d */
  1058   1532     { 0x0453,  0x67, 0x00 },  /* ѓ to g */
................................................................................
  1105   1579   ** The returned string might contain more characters than the input.
  1106   1580   **
  1107   1581   ** Space to hold the returned string comes from sqlite3_malloc() and
  1108   1582   ** should be freed by the caller.
  1109   1583   */
  1110   1584   static unsigned char *transliterate(const unsigned char *zIn, int nIn){
  1111   1585     unsigned char *zOut = sqlite3_malloc( nIn*4 + 1 );
  1112         -  int i, c, sz, nOut;
         1586  +  int c, sz, nOut;
  1113   1587     if( zOut==0 ) return 0;
  1114         -  i = nOut = 0;
  1115         -  while( i<nIn ){
         1588  +  nOut = 0;
         1589  +  while( nIn>0 ){
  1116   1590       c = utf8Read(zIn, nIn, &sz);
  1117   1591       zIn += sz;
  1118   1592       nIn -= sz;
  1119   1593       if( c<=127 ){
  1120   1594         zOut[nOut++] = c;
  1121   1595       }else{
  1122   1596         int xTop, xBtm, x;
................................................................................
  1144   1618         }
  1145   1619         if( c ) zOut[nOut++] = '?';
  1146   1620       }
  1147   1621     }
  1148   1622     zOut[nOut] = 0;
  1149   1623     return zOut;
  1150   1624   }
         1625  +
         1626  +/*
         1627  +** Return the number of characters in the shortest prefix of the input
         1628  +** string that transliterates to an ASCII string nTrans bytes or longer.
         1629  +** Or, if the transliteration of the input string is less than nTrans
         1630  +** bytes in size, return the number of characters in the input string.
         1631  +*/
         1632  +static int translen_to_charlen(const char *zIn, int nIn, int nTrans){
         1633  +  int i, c, sz, nOut;
         1634  +  int nChar;
         1635  +
         1636  +  i = nOut = 0;
         1637  +  for(nChar=0; i<nIn && nOut<nTrans; nChar++){
         1638  +    c = utf8Read((const unsigned char *)&zIn[i], nIn-i, &sz);
         1639  +    i += sz;
         1640  +
         1641  +    nOut++;
         1642  +    if( c>=128 ){
         1643  +      int xTop, xBtm, x;
         1644  +      xTop = sizeof(translit)/sizeof(translit[0]) - 1;
         1645  +      xBtm = 0;
         1646  +      while( xTop>=xBtm ){
         1647  +        x = (xTop + xBtm)/2;
         1648  +        if( translit[x].cFrom==c ){
         1649  +          if( translit[x].cTo1 ) nOut++;
         1650  +          if( c==0x0429 || c== 0x0449 ) nOut += 2;
         1651  +          break;
         1652  +        }else if( translit[x].cFrom>c ){
         1653  +          xTop = x-1;
         1654  +        }else{
         1655  +          xBtm = x+1;
         1656  +        }
         1657  +      }
         1658  +    }
         1659  +  }
         1660  +
         1661  +  return nChar;
         1662  +}
         1663  +
  1151   1664   
  1152   1665   /*
  1153   1666   **    spellfix1_translit(X)
  1154   1667   **
  1155   1668   ** Convert a string that contains non-ASCII Roman characters into 
  1156   1669   ** pure ASCII.
  1157   1670   */
................................................................................
  1218   1731       case SCRIPT_CYRILLIC:  res = 220; break;
  1219   1732       case SCRIPT_GREEK:     res = 200; break;
  1220   1733       default:               res = 998; break;
  1221   1734     }
  1222   1735     sqlite3_result_int(context, res);
  1223   1736   }
  1224   1737   
  1225         -/*****************************************************************************
  1226         -** Fuzzy-search virtual table
  1227         -*****************************************************************************/
         1738  +/* End transliterate
         1739  +******************************************************************************
         1740  +******************************************************************************
         1741  +** Begin spellfix1 virtual table.
         1742  +*/
         1743  +
         1744  +/* Maximum length of a phonehash used for querying the shadow table */
         1745  +#define SPELLFIX_MX_HASH  8
         1746  +
         1747  +/* Maximum number of hash strings to examine per query */
         1748  +#define SPELLFIX_MX_RUN   1
  1228   1749   
  1229   1750   typedef struct spellfix1_vtab spellfix1_vtab;
  1230   1751   typedef struct spellfix1_cursor spellfix1_cursor;
  1231   1752   
  1232   1753   /* Fuzzy-search virtual table object */
  1233   1754   struct spellfix1_vtab {
  1234         -  sqlite3_vtab base;      /* Base class - must be first */
  1235         -  sqlite3 *db;            /* Database connection */
  1236         -  char *zDbName;          /* Name of database holding this table */
  1237         -  char *zTableName;       /* Name of the virtual table */
         1755  +  sqlite3_vtab base;         /* Base class - must be first */
         1756  +  sqlite3 *db;               /* Database connection */
         1757  +  char *zDbName;             /* Name of database holding this table */
         1758  +  char *zTableName;          /* Name of the virtual table */
         1759  +  char *zCostTable;          /* Table holding edit-distance cost numbers */
         1760  +  EditDist3Config *pConfig3; /* Parsed edit distance costs */
  1238   1761   };
  1239   1762   
  1240   1763   /* Fuzzy-search cursor object */
  1241   1764   struct spellfix1_cursor {
  1242   1765     sqlite3_vtab_cursor base;    /* Base class - must be first */
  1243         -  spellfix1_vtab *pVTab;         /* The table to which this cursor belongs */
         1766  +  spellfix1_vtab *pVTab;       /* The table to which this cursor belongs */
         1767  +  char *zPattern;              /* rhs of MATCH clause */
  1244   1768     int nRow;                    /* Number of rows of content */
  1245   1769     int nAlloc;                  /* Number of allocated rows */
  1246   1770     int iRow;                    /* Current row of content */
  1247         -  int iLang;                   /* Value of the lang= constraint */
         1771  +  int iLang;                   /* Value of the langid= constraint */
  1248   1772     int iTop;                    /* Value of the top= constraint */
  1249   1773     int iScope;                  /* Value of the scope= constraint */
  1250   1774     int nSearch;                 /* Number of vocabulary items checked */
  1251         -  struct spellfix1_row {         /* For each row of content */
         1775  +  sqlite3_stmt *pFullScan;     /* Shadow query for a full table scan */
         1776  +  struct spellfix1_row {       /* For each row of content */
  1252   1777       sqlite3_int64 iRowid;         /* Rowid for this row */
  1253   1778       char *zWord;                  /* Text for this row */
  1254   1779       int iRank;                    /* Rank for this row */
  1255   1780       int iDistance;                /* Distance from pattern for this row */
  1256   1781       int iScore;                   /* Score for sorting */
         1782  +    int iMatchlen;                /* Value of matchlen column (or -1) */
         1783  +    char zHash[SPELLFIX_MX_HASH]; /* the phonehash used for this match */
  1257   1784     } *a; 
  1258   1785   };
  1259   1786   
  1260   1787   /*
  1261   1788   ** Construct one or more SQL statements from the format string given
  1262   1789   ** and then evaluate those statements. The success code is written
  1263   1790   ** into *pRc.
................................................................................
  1293   1820     if( isDestroy ){
  1294   1821       sqlite3 *db = p->db;
  1295   1822       spellfix1DbExec(&rc, db, "DROP TABLE IF EXISTS \"%w\".\"%w_vocab\"",
  1296   1823                     p->zDbName, p->zTableName);
  1297   1824     }
  1298   1825     if( rc==SQLITE_OK ){
  1299   1826       sqlite3_free(p->zTableName);
         1827  +    editDist3ConfigDelete(p->pConfig3);
         1828  +    sqlite3_free(p->zCostTable);
  1300   1829       sqlite3_free(p);
  1301   1830     }
  1302   1831     return rc;
  1303   1832   }
  1304   1833   static int spellfix1Disconnect(sqlite3_vtab *pVTab){
  1305   1834     return spellfix1Uninit(0, pVTab);
  1306   1835   }
  1307   1836   static int spellfix1Destroy(sqlite3_vtab *pVTab){
  1308   1837     return spellfix1Uninit(1, pVTab);
  1309   1838   }
         1839  +
         1840  +/*
         1841  +** Make a copy of a string.  Remove leading and trailing whitespace
         1842  +** and dequote it.
         1843  +*/
         1844  +static char *spellfix1Dequote(const char *zIn){
         1845  +  char *zOut;
         1846  +  int i, j;
         1847  +  char c;
         1848  +  while( isspace(zIn[0]) ) zIn++;
         1849  +  zOut = sqlite3_mprintf("%s", zIn);
         1850  +  if( zOut==0 ) return 0;
         1851  +  i = (int)strlen(zOut);
         1852  +#if 0  /* The parser will never leave spaces at the end */
         1853  +  while( i>0 && isspace(zOut[i-1]) ){ i--; }
         1854  +#endif
         1855  +  zOut[i] = 0;
         1856  +  c = zOut[0];
         1857  +  if( c=='\'' || c=='"' ){
         1858  +    for(i=1, j=0; ALWAYS(zOut[i]); i++){
         1859  +      zOut[j++] = zOut[i];
         1860  +      if( zOut[i]==c ){
         1861  +        if( zOut[i+1]==c ){
         1862  +          i++;
         1863  +        }else{
         1864  +          zOut[j-1] = 0;
         1865  +          break;
         1866  +        }
         1867  +      }
         1868  +    }
         1869  +  }
         1870  +  return zOut;
         1871  +}
         1872  +
  1310   1873   
  1311   1874   /*
  1312   1875   ** xConnect/xCreate method for the spellfix1 module. Arguments are:
  1313   1876   **
  1314   1877   **   argv[0]   -> module name  ("spellfix1")
  1315   1878   **   argv[1]   -> database name
  1316   1879   **   argv[2]   -> table name
  1317         -**   argv[3].. -> optional arguments (currently ignored)
         1880  +**   argv[3].. -> optional arguments (i.e. "edit_cost_table" parameter)
  1318   1881   */
  1319   1882   static int spellfix1Init(
  1320   1883     int isCreate,
  1321   1884     sqlite3 *db,
  1322   1885     void *pAux,
  1323   1886     int argc, const char *const*argv,
  1324   1887     sqlite3_vtab **ppVTab,
................................................................................
  1326   1889   ){
  1327   1890     spellfix1_vtab *pNew = 0;
  1328   1891     const char *zModule = argv[0];
  1329   1892     const char *zDbName = argv[1];
  1330   1893     const char *zTableName = argv[2];
  1331   1894     int nDbName;
  1332   1895     int rc = SQLITE_OK;
         1896  +  int i;
  1333   1897   
  1334         -  if( argc<3 ){
  1335         -    *pzErr = sqlite3_mprintf(
  1336         -        "%s: wrong number of CREATE VIRTUAL TABLE arguments", argv[0]
  1337         -    );
  1338         -    rc = SQLITE_ERROR;
         1898  +  nDbName = strlen(zDbName);
         1899  +  pNew = sqlite3_malloc( sizeof(*pNew) + nDbName + 1);
         1900  +  if( pNew==0 ){
         1901  +    rc = SQLITE_NOMEM;
  1339   1902     }else{
  1340         -    nDbName = strlen(zDbName);
  1341         -    pNew = sqlite3_malloc( sizeof(*pNew) + nDbName + 1);
  1342         -    if( pNew==0 ){
         1903  +    memset(pNew, 0, sizeof(*pNew));
         1904  +    pNew->zDbName = (char*)&pNew[1];
         1905  +    memcpy(pNew->zDbName, zDbName, nDbName+1);
         1906  +    pNew->zTableName = sqlite3_mprintf("%s", zTableName);
         1907  +    pNew->db = db;
         1908  +    if( pNew->zTableName==0 ){
  1343   1909         rc = SQLITE_NOMEM;
  1344   1910       }else{
  1345         -      memset(pNew, 0, sizeof(*pNew));
  1346         -      pNew->zDbName = (char*)&pNew[1];
  1347         -      memcpy(pNew->zDbName, zDbName, nDbName+1);
  1348         -      pNew->zTableName = sqlite3_mprintf("%s", zTableName);
  1349         -      pNew->db = db;
  1350         -      if( pNew->zTableName==0 ){
  1351         -        rc = SQLITE_NOMEM;
  1352         -      }else{
  1353         -        rc = sqlite3_declare_vtab(db, 
  1354         -             "CREATE TABLE x(word,rank,distance,langid,"
  1355         -             "score,top HIDDEN,scope HIDDEN,srchcnt HIDDEN,"
  1356         -             "soundslike HIDDEN)"
  1357         -        );
  1358         -      }
  1359         -      if( rc==SQLITE_OK && isCreate ){
  1360         -        sqlite3_uint64 r;
  1361         -        spellfix1DbExec(&rc, db,
  1362         -           "CREATE TABLE IF NOT EXISTS \"%w\".\"%w_vocab\"(\n"
  1363         -           "  id INTEGER PRIMARY KEY,\n"
  1364         -           "  rank INT,\n"
  1365         -           "  langid INT,\n"
  1366         -           "  word TEXT,\n"
  1367         -           "  k1 TEXT,\n"
  1368         -           "  k2 TEXT\n"
  1369         -           ");\n",
  1370         -           zDbName, zTableName
  1371         -        );
  1372         -        sqlite3_randomness(sizeof(r), &r);
  1373         -        spellfix1DbExec(&rc, db,
  1374         -           "CREATE INDEX IF NOT EXISTS \"%w\".\"%w_index_%llx\" "
  1375         -              "ON \"%w_vocab\"(langid,k2);",
  1376         -           zDbName, zModule, r, zTableName
  1377         -        );
         1911  +      rc = sqlite3_declare_vtab(db, 
         1912  +           "CREATE TABLE x(word,rank,distance,langid, "
         1913  +           "score, matchlen, phonehash HIDDEN, "
         1914  +           "top HIDDEN, scope HIDDEN, srchcnt HIDDEN, "
         1915  +           "soundslike HIDDEN, command HIDDEN)"
         1916  +      );
         1917  +#define SPELLFIX_COL_WORD            0
         1918  +#define SPELLFIX_COL_RANK            1
         1919  +#define SPELLFIX_COL_DISTANCE        2
         1920  +#define SPELLFIX_COL_LANGID          3
         1921  +#define SPELLFIX_COL_SCORE           4
         1922  +#define SPELLFIX_COL_MATCHLEN        5
         1923  +#define SPELLFIX_COL_PHONEHASH       6
         1924  +#define SPELLFIX_COL_TOP             7
         1925  +#define SPELLFIX_COL_SCOPE           8
         1926  +#define SPELLFIX_COL_SRCHCNT         9
         1927  +#define SPELLFIX_COL_SOUNDSLIKE     10
         1928  +#define SPELLFIX_COL_COMMAND        11
         1929  +    }
         1930  +    if( rc==SQLITE_OK && isCreate ){
         1931  +      sqlite3_uint64 r;
         1932  +      spellfix1DbExec(&rc, db,
         1933  +         "CREATE TABLE IF NOT EXISTS \"%w\".\"%w_vocab\"(\n"
         1934  +         "  id INTEGER PRIMARY KEY,\n"
         1935  +         "  rank INT,\n"
         1936  +         "  langid INT,\n"
         1937  +         "  word TEXT,\n"
         1938  +         "  k1 TEXT,\n"
         1939  +         "  k2 TEXT\n"
         1940  +         ");\n",
         1941  +         zDbName, zTableName
         1942  +      );
         1943  +      sqlite3_randomness(sizeof(r), &r);
         1944  +      spellfix1DbExec(&rc, db,
         1945  +         "CREATE INDEX IF NOT EXISTS \"%w\".\"%w_index_%llx\" "
         1946  +            "ON \"%w_vocab\"(langid,k2);",
         1947  +         zDbName, zModule, r, zTableName
         1948  +      );
         1949  +    }
         1950  +    for(i=3; rc==SQLITE_OK && i<argc; i++){
         1951  +      if( memcmp(argv[i],"edit_cost_table=",16)==0 && pNew->zCostTable==0 ){
         1952  +        pNew->zCostTable = spellfix1Dequote(&argv[i][16]);
         1953  +        if( pNew->zCostTable==0 ) rc = SQLITE_NOMEM;
         1954  +        continue;
  1378   1955         }
         1956  +      *pzErr = sqlite3_mprintf("bad argument to spellfix1(): \"%s\"", argv[i]);
         1957  +      rc = SQLITE_ERROR; 
  1379   1958       }
  1380   1959     }
  1381   1960   
  1382         -  *ppVTab = (sqlite3_vtab *)pNew;
         1961  +  if( rc && pNew ){
         1962  +    *ppVTab = 0;
         1963  +    spellfix1Uninit(0, &pNew->base);
         1964  +  }else{
         1965  +    *ppVTab = (sqlite3_vtab *)pNew;
         1966  +  }
  1383   1967     return rc;
  1384   1968   }
  1385   1969   
  1386   1970   /*
  1387   1971   ** The xConnect and xCreate methods
  1388   1972   */
  1389   1973   static int spellfix1Connect(
................................................................................
  1402   1986     sqlite3_vtab **ppVTab,
  1403   1987     char **pzErr
  1404   1988   ){
  1405   1989     return spellfix1Init(1, db, pAux, argc, argv, ppVTab, pzErr);
  1406   1990   }
  1407   1991   
  1408   1992   /*
  1409         -** Reset a cursor so that it contains zero rows of content but holds
  1410         -** space for N rows.
         1993  +** Clear all of the content from a cursor.
  1411   1994   */
  1412         -static void spellfix1ResetCursor(spellfix1_cursor *pCur, int N){
         1995  +static void spellfix1ResetCursor(spellfix1_cursor *pCur){
  1413   1996     int i;
  1414   1997     for(i=0; i<pCur->nRow; i++){
  1415   1998       sqlite3_free(pCur->a[i].zWord);
  1416   1999     }
  1417         -  pCur->a = sqlite3_realloc(pCur->a, sizeof(pCur->a[0])*N);
  1418         -  pCur->nAlloc = N;
  1419   2000     pCur->nRow = 0;
  1420   2001     pCur->iRow = 0;
  1421   2002     pCur->nSearch = 0;
         2003  +  if( pCur->pFullScan ){
         2004  +    sqlite3_finalize(pCur->pFullScan);
         2005  +    pCur->pFullScan = 0;
         2006  +  }
  1422   2007   }
         2008  +
         2009  +/*
         2010  +** Resize the cursor to hold up to N rows of content
         2011  +*/
         2012  +static void spellfix1ResizeCursor(spellfix1_cursor *pCur, int N){
         2013  +  struct spellfix1_row *aNew;
         2014  +  assert( N>=pCur->nRow );
         2015  +  aNew = sqlite3_realloc(pCur->a, sizeof(pCur->a[0])*N);
         2016  +  if( aNew==0 && N>0 ){
         2017  +    spellfix1ResetCursor(pCur);
         2018  +    sqlite3_free(pCur->a);
         2019  +    pCur->nAlloc = 0;
         2020  +    pCur->a = 0;
         2021  +  }else{
         2022  +    pCur->nAlloc = N;
         2023  +    pCur->a = aNew;
         2024  +  }
         2025  +}
         2026  +
  1423   2027   
  1424   2028   /*
  1425   2029   ** Close a fuzzy-search cursor.
  1426   2030   */
  1427   2031   static int spellfix1Close(sqlite3_vtab_cursor *cur){
  1428   2032     spellfix1_cursor *pCur = (spellfix1_cursor *)cur;
  1429         -  spellfix1ResetCursor(pCur, 0);
         2033  +  spellfix1ResetCursor(pCur);
         2034  +  spellfix1ResizeCursor(pCur, 0);
         2035  +  sqlite3_free(pCur->zPattern);
  1430   2036     sqlite3_free(pCur);
  1431   2037     return SQLITE_OK;
  1432   2038   }
  1433   2039   
  1434   2040   /*
  1435   2041   ** Search for terms of these forms:
  1436   2042   **
  1437   2043   **   (A)    word MATCH $str
  1438   2044   **   (B)    langid == $langid
  1439   2045   **   (C)    top = $top
  1440   2046   **   (D)    scope = $scope
         2047  +**   (E)    distance < $distance
         2048  +**   (F)    distance <= $distance
  1441   2049   **
  1442   2050   ** The plan number is a bit mask formed with these bits:
  1443   2051   **
  1444   2052   **   0x01   (A) is found
  1445   2053   **   0x02   (B) is found
  1446   2054   **   0x04   (C) is found
  1447   2055   **   0x08   (D) is found
         2056  +**   0x10   (E) is found
         2057  +**   0x20   (F) is found
  1448   2058   **
  1449   2059   ** filter.argv[*] values contains $str, $langid, $top, and $scope,
  1450   2060   ** if specified and in that order.
  1451   2061   */
  1452   2062   static int spellfix1BestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
  1453   2063     int iPlan = 0;
  1454   2064     int iLangTerm = -1;
  1455   2065     int iTopTerm = -1;
  1456   2066     int iScopeTerm = -1;
         2067  +  int iDistTerm = -1;
  1457   2068     int i;
  1458   2069     const struct sqlite3_index_constraint *pConstraint;
  1459   2070     pConstraint = pIdxInfo->aConstraint;
  1460   2071     for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
  1461   2072       if( pConstraint->usable==0 ) continue;
  1462   2073   
  1463   2074       /* Terms of the form:  word MATCH $str */
  1464   2075       if( (iPlan & 1)==0 
  1465         -     && pConstraint->iColumn==0
         2076  +     && pConstraint->iColumn==SPELLFIX_COL_WORD
  1466   2077        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH
  1467   2078       ){
  1468   2079         iPlan |= 1;
  1469   2080         pIdxInfo->aConstraintUsage[i].argvIndex = 1;
  1470   2081         pIdxInfo->aConstraintUsage[i].omit = 1;
  1471   2082       }
  1472   2083   
  1473   2084       /* Terms of the form:  langid = $langid  */
  1474   2085       if( (iPlan & 2)==0
  1475         -     && pConstraint->iColumn==3
         2086  +     && pConstraint->iColumn==SPELLFIX_COL_LANGID
  1476   2087        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
  1477   2088       ){
  1478   2089         iPlan |= 2;
  1479   2090         iLangTerm = i;
  1480   2091       }
  1481   2092   
  1482   2093       /* Terms of the form:  top = $top */
  1483   2094       if( (iPlan & 4)==0
  1484         -     && pConstraint->iColumn==5
         2095  +     && pConstraint->iColumn==SPELLFIX_COL_TOP
  1485   2096        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
  1486   2097       ){
  1487   2098         iPlan |= 4;
  1488   2099         iTopTerm = i;
  1489   2100       }
  1490   2101   
  1491   2102       /* Terms of the form:  scope = $scope */
  1492   2103       if( (iPlan & 8)==0
  1493         -     && pConstraint->iColumn==6
         2104  +     && pConstraint->iColumn==SPELLFIX_COL_SCOPE
  1494   2105        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
  1495   2106       ){
  1496   2107         iPlan |= 8;
  1497   2108         iScopeTerm = i;
  1498   2109       }
         2110  +
         2111  +    /* Terms of the form:  distance < $dist or distance <= $dist */
         2112  +    if( (iPlan & (16|32))==0
         2113  +     && pConstraint->iColumn==SPELLFIX_COL_DISTANCE
         2114  +     && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
         2115  +          || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE)
         2116  +    ){
         2117  +      iPlan |= pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ? 16 : 32;
         2118  +      iDistTerm = i;
         2119  +    }
  1499   2120     }
  1500   2121     if( iPlan&1 ){
  1501   2122       int idx = 2;
  1502   2123       pIdxInfo->idxNum = iPlan;
  1503   2124       if( pIdxInfo->nOrderBy==1
  1504         -     && pIdxInfo->aOrderBy[0].iColumn==4
         2125  +     && pIdxInfo->aOrderBy[0].iColumn==SPELLFIX_COL_SCORE
  1505   2126        && pIdxInfo->aOrderBy[0].desc==0
  1506   2127       ){
  1507   2128         pIdxInfo->orderByConsumed = 1;  /* Default order by iScore */
  1508   2129       }
  1509   2130       if( iPlan&2 ){
  1510   2131         pIdxInfo->aConstraintUsage[iLangTerm].argvIndex = idx++;
  1511   2132         pIdxInfo->aConstraintUsage[iLangTerm].omit = 1;
................................................................................
  1514   2135         pIdxInfo->aConstraintUsage[iTopTerm].argvIndex = idx++;
  1515   2136         pIdxInfo->aConstraintUsage[iTopTerm].omit = 1;
  1516   2137       }
  1517   2138       if( iPlan&8 ){
  1518   2139         pIdxInfo->aConstraintUsage[iScopeTerm].argvIndex = idx++;
  1519   2140         pIdxInfo->aConstraintUsage[iScopeTerm].omit = 1;
  1520   2141       }
         2142  +    if( iPlan&(16|32) ){
         2143  +      pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = idx++;
         2144  +      pIdxInfo->aConstraintUsage[iDistTerm].omit = 1;
         2145  +    }
  1521   2146       pIdxInfo->estimatedCost = (double)10000;
  1522   2147     }else{
  1523   2148       pIdxInfo->idxNum = 0;
  1524   2149       pIdxInfo->estimatedCost = (double)10000000;
  1525   2150     }
  1526   2151     return SQLITE_OK;
  1527   2152   }
................................................................................
  1555   2180   ** that they sort in order of increasing distance.
  1556   2181   */
  1557   2182   static int spellfix1RowCompare(const void *A, const void *B){
  1558   2183     const struct spellfix1_row *a = (const struct spellfix1_row*)A;
  1559   2184     const struct spellfix1_row *b = (const struct spellfix1_row*)B;
  1560   2185     return a->iScore - b->iScore;
  1561   2186   }
         2187  +
         2188  +/*
         2189  +** A structure used to pass information from spellfix1FilterForMatch()
         2190  +** into spellfix1RunQuery().
         2191  +*/
         2192  +typedef struct MatchQuery {
         2193  +  spellfix1_cursor *pCur;          /* The cursor being queried */
         2194  +  sqlite3_stmt *pStmt;             /* shadow table query statment */
         2195  +  char zHash[SPELLFIX_MX_HASH];    /* The current phonehash for zPattern */
         2196  +  const char *zPattern;            /* Transliterated input string */
         2197  +  int nPattern;                    /* Length of zPattern */
         2198  +  EditDist3FromString *pMatchStr3; /* Original unicode string */
         2199  +  EditDist3Config *pConfig3;       /* Edit-distance cost coefficients */
         2200  +  const EditDist3Lang *pLang;      /* The selected language coefficients */
         2201  +  int iLang;                       /* The language id */
         2202  +  int iScope;                      /* Default scope */
         2203  +  int iMaxDist;                    /* Maximum allowed edit distance, or -1 */
         2204  +  int rc;                          /* Error code */
         2205  +  int nRun;                  /* Number of prior runs for the same zPattern */
         2206  +  char azPrior[SPELLFIX_MX_RUN][SPELLFIX_MX_HASH];  /* Prior hashes */
         2207  +} MatchQuery;
         2208  +
         2209  +/*
         2210  +** Run a query looking for the best matches against zPattern using
         2211  +** zHash as the character class seed hash.
         2212  +*/
         2213  +static void spellfix1RunQuery(MatchQuery *p, const char *zQuery, int nQuery){
         2214  +  const char *zK1;
         2215  +  const char *zWord;
         2216  +  int iDist;
         2217  +  int iRank;
         2218  +  int iScore;
         2219  +  int iWorst = 0;
         2220  +  int idx;
         2221  +  int idxWorst = -1;
         2222  +  int i;
         2223  +  int iScope = p->iScope;
         2224  +  spellfix1_cursor *pCur = p->pCur;
         2225  +  sqlite3_stmt *pStmt = p->pStmt;
         2226  +  char zHash1[SPELLFIX_MX_HASH];
         2227  +  char zHash2[SPELLFIX_MX_HASH];
         2228  +  char *zClass;
         2229  +  int nClass;
         2230  +  int rc;
         2231  +
         2232  +  if( pCur->a==0 || p->rc ) return;   /* Prior memory allocation failure */
         2233  +  zClass = (char*)phoneticHash((unsigned char*)zQuery, nQuery);
         2234  +  if( zClass==0 ){
         2235  +    p->rc = SQLITE_NOMEM;
         2236  +    return;
         2237  +  }
         2238  +  nClass = strlen(zClass);
         2239  +  if( nClass>SPELLFIX_MX_HASH-2 ){
         2240  +    nClass = SPELLFIX_MX_HASH-2;
         2241  +    zClass[nClass] = 0;
         2242  +  }
         2243  +  if( nClass<=iScope ){
         2244  +    if( nClass>2 ){
         2245  +      iScope = nClass-1;
         2246  +    }else{
         2247  +      iScope = nClass;
         2248  +    }
         2249  +  }
         2250  +  memcpy(zHash1, zClass, iScope);
         2251  +  sqlite3_free(zClass);
         2252  +  zHash1[iScope] = 0;
         2253  +  memcpy(zHash2, zHash1, iScope);
         2254  +  zHash2[iScope] = 'Z';
         2255  +  zHash2[iScope+1] = 0;
         2256  +#if SPELLFIX_MX_RUN>1
         2257  +  for(i=0; i<p->nRun; i++){
         2258  +    if( strcmp(p->azPrior[i], zHash1)==0 ) return;
         2259  +  }
         2260  +#endif
         2261  +  assert( p->nRun<SPELLFIX_MX_RUN );
         2262  +  memcpy(p->azPrior[p->nRun++], zHash1, iScope+1);
         2263  +  if( sqlite3_bind_text(pStmt, 1, zHash1, -1, SQLITE_STATIC)==SQLITE_NOMEM
         2264  +   || sqlite3_bind_text(pStmt, 2, zHash2, -1, SQLITE_STATIC)==SQLITE_NOMEM
         2265  +  ){
         2266  +    p->rc = SQLITE_NOMEM;
         2267  +    return;
         2268  +  }
         2269  +#if SPELLFIX_MX_RUN>1
         2270  +  for(i=0; i<pCur->nRow; i++){
         2271  +    if( pCur->a[i].iScore>iWorst ){
         2272  +      iWorst = pCur->a[i].iScore;
         2273  +      idxWorst = i;
         2274  +    }
         2275  +  }
         2276  +#endif
         2277  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
         2278  +    int iMatchlen = -1;
         2279  +    iRank = sqlite3_column_int(pStmt, 2);
         2280  +    if( p->pMatchStr3 ){
         2281  +      int nWord = sqlite3_column_bytes(pStmt, 1);
         2282  +      zWord = (const char*)sqlite3_column_text(pStmt, 1);
         2283  +      iDist = editDist3Core(p->pMatchStr3, zWord, nWord, p->pLang, &iMatchlen);
         2284  +    }else{
         2285  +      zK1 = (const char*)sqlite3_column_text(pStmt, 3);
         2286  +      if( zK1==0 ) continue;
         2287  +      iDist = editdist1(p->zPattern, zK1, 0);
         2288  +    }
         2289  +    if( iDist<0 ){
         2290  +      p->rc = SQLITE_NOMEM;
         2291  +      break;
         2292  +    }
         2293  +    pCur->nSearch++;
         2294  +    iScore = spellfix1Score(iDist,iRank);
         2295  +    if( p->iMaxDist>=0 ){
         2296  +      if( iDist>p->iMaxDist ) continue;
         2297  +      if( pCur->nRow>=pCur->nAlloc-1 ){
         2298  +        spellfix1ResizeCursor(pCur, pCur->nAlloc*2 + 10);
         2299  +        if( pCur->a==0 ) break;
         2300  +      }
         2301  +      idx = pCur->nRow;
         2302  +    }else if( pCur->nRow<pCur->nAlloc ){
         2303  +      idx = pCur->nRow;
         2304  +    }else if( iScore<iWorst ){
         2305  +      idx = idxWorst;
         2306  +      sqlite3_free(pCur->a[idx].zWord);
         2307  +    }else{
         2308  +      continue;
         2309  +    }
         2310  +    pCur->a[idx].zWord = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
         2311  +    if( pCur->a[idx].zWord==0 ){
         2312  +      p->rc = SQLITE_NOMEM;
         2313  +      break;
         2314  +    }
         2315  +    pCur->a[idx].iRowid = sqlite3_column_int64(pStmt, 0);
         2316  +    pCur->a[idx].iRank = iRank;
         2317  +    pCur->a[idx].iDistance = iDist;
         2318  +    pCur->a[idx].iScore = iScore;
         2319  +    pCur->a[idx].iMatchlen = iMatchlen;
         2320  +    memcpy(pCur->a[idx].zHash, zHash1, iScope+1);
         2321  +    if( pCur->nRow<pCur->nAlloc ) pCur->nRow++;
         2322  +    if( pCur->nRow==pCur->nAlloc ){
         2323  +      iWorst = pCur->a[0].iScore;
         2324  +      idxWorst = 0;
         2325  +      for(i=1; i<pCur->nRow; i++){
         2326  +        iScore = pCur->a[i].iScore;
         2327  +        if( iWorst<iScore ){
         2328  +          iWorst = iScore;
         2329  +          idxWorst = i;
         2330  +        }
         2331  +      }
         2332  +    }
         2333  +  }
         2334  +  rc = sqlite3_reset(pStmt);
         2335  +  if( rc ) p->rc = rc;
         2336  +}
  1562   2337   
  1563   2338   /*
  1564   2339   ** This version of the xFilter method work if the MATCH term is present
  1565   2340   ** and we are doing a scan.
  1566   2341   */
  1567   2342   static int spellfix1FilterForMatch(
  1568   2343     spellfix1_cursor *pCur,
  1569   2344     int idxNum,
  1570   2345     int argc,
  1571   2346     sqlite3_value **argv
  1572   2347   ){
  1573         -  const unsigned char *zPatternIn;
  1574         -  char *zPattern;
  1575         -  int nPattern;
  1576         -  char *zClass;
  1577         -  int nClass;
  1578         -  int iLimit = 20;
  1579         -  int iScope = 4;
  1580         -  int iLang = 0;
  1581         -  char *zSql;
  1582         -  int rc;
  1583         -  sqlite3_stmt *pStmt;
  1584         -  int idx = 1;
  1585         -  spellfix1_vtab *p = pCur->pVTab;
         2348  +  const unsigned char *zMatchThis;   /* RHS of the MATCH operator */
         2349  +  EditDist3FromString *pMatchStr3 = 0; /* zMatchThis as an editdist string */
         2350  +  char *zPattern;                    /* Transliteration of zMatchThis */
         2351  +  int nPattern;                      /* Length of zPattern */
         2352  +  int iLimit = 20;                   /* Max number of rows of output */
         2353  +  int iScope = 3;                    /* Use this many characters of zClass */
         2354  +  int iLang = 0;                     /* Language code */
         2355  +  char *zSql;                        /* SQL of shadow table query */
         2356  +  sqlite3_stmt *pStmt = 0;           /* Shadow table query */
         2357  +  int rc;                            /* Result code */
         2358  +  int idx = 1;                       /* Next available filter parameter */
         2359  +  spellfix1_vtab *p = pCur->pVTab;   /* The virtual table that owns pCur */
         2360  +  MatchQuery x;                      /* For passing info to RunQuery() */
         2361  +
         2362  +  /* Load the cost table if we have not already done so */
         2363  +  if( p->zCostTable!=0 && p->pConfig3==0 ){
         2364  +    p->pConfig3 = sqlite3_malloc( sizeof(p->pConfig3[0]) );
         2365  +    if( p->pConfig3==0 ) return SQLITE_NOMEM;
         2366  +    memset(p->pConfig3, 0, sizeof(p->pConfig3[0]));
         2367  +    rc = editDist3ConfigLoad(p->pConfig3, p->db, p->zCostTable);
         2368  +    if( rc ) return rc;
         2369  +  }
         2370  +  memset(&x, 0, sizeof(x));
         2371  +  x.iScope = 3;  /* Default scope if none specified by "WHERE scope=N" */
         2372  +  x.iMaxDist = -1;   /* Maximum allowed edit distance */
  1586   2373   
  1587   2374     if( idxNum&2 ){
  1588   2375       iLang = sqlite3_value_int(argv[idx++]);
  1589   2376     }
  1590   2377     if( idxNum&4 ){
  1591   2378       iLimit = sqlite3_value_int(argv[idx++]);
  1592   2379       if( iLimit<1 ) iLimit = 1;
  1593   2380     }
  1594   2381     if( idxNum&8 ){
  1595         -    iScope = sqlite3_value_int(argv[idx++]);
  1596         -    if( iScope<1 ) iScope = 1;
         2382  +    x.iScope = sqlite3_value_int(argv[idx++]);
         2383  +    if( x.iScope<1 ) x.iScope = 1;
         2384  +    if( x.iScope>SPELLFIX_MX_HASH-2 ) x.iScope = SPELLFIX_MX_HASH-2;
  1597   2385     }
  1598         -  spellfix1ResetCursor(pCur, iLimit);
  1599         -  zPatternIn = sqlite3_value_text(argv[0]);
  1600         -  if( zPatternIn==0 ) return SQLITE_OK;
  1601         -  zPattern = (char*)transliterate(zPatternIn, sqlite3_value_bytes(argv[0]));
  1602         -  if( zPattern==0 ) return SQLITE_NOMEM;
         2386  +  if( idxNum&(16|32) ){
         2387  +    x.iMaxDist = sqlite3_value_int(argv[idx++]);
         2388  +    if( idxNum&16 ) x.iMaxDist--;
         2389  +    if( x.iMaxDist<0 ) x.iMaxDist = 0;
         2390  +  }
         2391  +  spellfix1ResetCursor(pCur);
         2392  +  spellfix1ResizeCursor(pCur, iLimit);
         2393  +  zMatchThis = sqlite3_value_text(argv[0]);
         2394  +  if( zMatchThis==0 ) return SQLITE_OK;
         2395  +  if( p->pConfig3 ){
         2396  +    x.pLang = editDist3FindLang(p->pConfig3, iLang);
         2397  +    pMatchStr3 = editDist3FromStringNew(x.pLang, (const char*)zMatchThis, -1);
         2398  +    if( pMatchStr3==0 ){
         2399  +      x.rc = SQLITE_NOMEM;
         2400  +      goto filter_exit;
         2401  +    }
         2402  +  }else{
         2403  +    x.pLang = 0;
         2404  +  }
         2405  +  zPattern = (char*)transliterate(zMatchThis, sqlite3_value_bytes(argv[0]));
         2406  +  sqlite3_free(pCur->zPattern);
         2407  +  pCur->zPattern = zPattern;
         2408  +  if( zPattern==0 ){
         2409  +    x.rc = SQLITE_NOMEM;
         2410  +    goto filter_exit;
         2411  +  }
  1603   2412     nPattern = strlen(zPattern);
  1604   2413     if( zPattern[nPattern-1]=='*' ) nPattern--;
  1605         -  if( nPattern<iScope ) iScope = nPattern;
  1606         -  zClass = (char*)characterClassString((unsigned char*)zPattern,
  1607         -                                       strlen(zPattern));
  1608         -  nClass = strlen(zClass);
  1609         -  if( nClass>iScope ){
  1610         -    zClass[iScope] = 0;
  1611         -    nClass = iScope;
  1612         -  }
  1613   2414     zSql = sqlite3_mprintf(
  1614   2415        "SELECT id, word, rank, k1"
  1615   2416        "  FROM \"%w\".\"%w_vocab\""
  1616         -     " WHERE langid=%d AND k2 GLOB '%q*'",
  1617         -     p->zDbName, p->zTableName, iLang, zClass
         2417  +     " WHERE langid=%d AND k2>=?1 AND k2<?2",
         2418  +     p->zDbName, p->zTableName, iLang
  1618   2419     );
         2420  +  if( zSql==0 ){
         2421  +    x.rc = SQLITE_NOMEM;
         2422  +    pStmt = 0;
         2423  +    goto filter_exit;
         2424  +  }
  1619   2425     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
  1620   2426     sqlite3_free(zSql);
  1621         -  if( rc==SQLITE_OK ){
  1622         -    const char *zK1;
  1623         -    int iDist;
  1624         -    int iRank;
  1625         -    int iScore;
  1626         -    int iWorst = 999999999;
  1627         -    int idx;
  1628         -    int idxWorst;
  1629         -    int i;
  1630         -
  1631         -    while( sqlite3_step(pStmt)==SQLITE_ROW ){
  1632         -      zK1 = (const char*)sqlite3_column_text(pStmt, 3);
  1633         -      if( zK1==0 ) continue;
  1634         -      pCur->nSearch++;
  1635         -      iRank = sqlite3_column_int(pStmt, 2);
  1636         -      iDist = editdist(zPattern, zK1);
  1637         -      iScore = spellfix1Score(iDist,iRank);
  1638         -      if( pCur->nRow<pCur->nAlloc ){
  1639         -        idx = pCur->nRow;
  1640         -      }else if( iScore<iWorst ){
  1641         -        idx = idxWorst;
  1642         -        sqlite3_free(pCur->a[idx].zWord);
  1643         -      }else{
  1644         -        continue;
  1645         -      }
  1646         -      pCur->a[idx].zWord = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
  1647         -      pCur->a[idx].iRowid = sqlite3_column_int64(pStmt, 0);
  1648         -      pCur->a[idx].iRank = iRank;
  1649         -      pCur->a[idx].iDistance = iDist;
  1650         -      pCur->a[idx].iScore = iScore;
  1651         -      if( pCur->nRow<pCur->nAlloc ) pCur->nRow++;
  1652         -      if( pCur->nRow==pCur->nAlloc ){
  1653         -        iWorst = pCur->a[0].iScore;
  1654         -        idxWorst = 0;
  1655         -        for(i=1; i<pCur->nRow; i++){
  1656         -          iScore = pCur->a[i].iScore;
  1657         -          if( iWorst<iScore ){
  1658         -            iWorst = iScore;
  1659         -            idxWorst = i;
  1660         -          }
  1661         -        }
  1662         -      }
  1663         -    }
  1664         -  }
  1665         -  qsort(pCur->a, pCur->nRow, sizeof(pCur->a[0]), spellfix1RowCompare);
  1666         -  pCur->iTop = iLimit;
  1667         -  pCur->iScope = iScope;
         2427  +  pCur->iLang = iLang;
         2428  +  x.pCur = pCur;
         2429  +  x.pStmt = pStmt;
         2430  +  x.zPattern = zPattern;
         2431  +  x.nPattern = nPattern;
         2432  +  x.pMatchStr3 = pMatchStr3;
         2433  +  x.iLang = iLang;
         2434  +  x.rc = rc;
         2435  +  x.pConfig3 = p->pConfig3;
         2436  +  if( x.rc==SQLITE_OK ){
         2437  +    spellfix1RunQuery(&x, zPattern, nPattern);
         2438  +  }
         2439  +
         2440  +  if( pCur->a ){
         2441  +    qsort(pCur->a, pCur->nRow, sizeof(pCur->a[0]), spellfix1RowCompare);
         2442  +    pCur->iTop = iLimit;
         2443  +    pCur->iScope = iScope;
         2444  +  }else{
         2445  +    x.rc = SQLITE_NOMEM;
         2446  +  }
         2447  +
         2448  +filter_exit:
  1668   2449     sqlite3_finalize(pStmt);
  1669         -  sqlite3_free(zPattern);
  1670         -  sqlite3_free(zClass);
  1671         -  return SQLITE_OK;
         2450  +  editDist3FromStringDelete(pMatchStr3);
         2451  +  return x.rc;
  1672   2452   }
  1673   2453   
  1674   2454   /*
  1675   2455   ** This version of xFilter handles a full-table scan case
  1676   2456   */
  1677   2457   static int spellfix1FilterForFullScan(
  1678   2458     spellfix1_cursor *pCur,
  1679   2459     int idxNum,
  1680   2460     int argc,
  1681   2461     sqlite3_value **argv
  1682   2462   ){
  1683         -  spellfix1ResetCursor(pCur, 0);
  1684         -  return SQLITE_OK;
         2463  +  int rc;
         2464  +  char *zSql;
         2465  +  spellfix1_vtab *pVTab = pCur->pVTab;
         2466  +  spellfix1ResetCursor(pCur);
         2467  +  zSql = sqlite3_mprintf(
         2468  +     "SELECT word, rank, NULL, langid, id FROM \"%w\".\"%w_vocab\"",
         2469  +     pVTab->zDbName, pVTab->zTableName);
         2470  +  if( zSql==0 ) return SQLITE_NOMEM;
         2471  +  rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pFullScan, 0);
         2472  +  sqlite3_free(zSql);
         2473  +  pCur->nRow = pCur->iRow = 0;
         2474  +  if( rc==SQLITE_OK ){
         2475  +    rc = sqlite3_step(pCur->pFullScan);
         2476  +    if( rc==SQLITE_ROW ){ pCur->iRow = -1; rc = SQLITE_OK; }
         2477  +    if( rc==SQLITE_DONE ){ rc = SQLITE_OK; }
         2478  +  }else{
         2479  +    pCur->iRow = 0;
         2480  +  }
         2481  +  return rc;
  1685   2482   }
  1686   2483   
  1687   2484   
  1688   2485   /*
  1689   2486   ** Called to "rewind" a cursor back to the beginning so that
  1690   2487   ** it starts its output over again.  Always called at least once
  1691   2488   ** prior to any spellfix1Column, spellfix1Rowid, or spellfix1Eof call.
................................................................................
  1707   2504   
  1708   2505   
  1709   2506   /*
  1710   2507   ** Advance a cursor to its next row of output
  1711   2508   */
  1712   2509   static int spellfix1Next(sqlite3_vtab_cursor *cur){
  1713   2510     spellfix1_cursor *pCur = (spellfix1_cursor *)cur;
  1714         -  if( pCur->iRow < pCur->nRow ) pCur->iRow++;
  1715         -  return SQLITE_OK;
         2511  +  int rc = SQLITE_OK;
         2512  +  if( pCur->iRow < pCur->nRow ){
         2513  +    if( pCur->pFullScan ){
         2514  +      rc = sqlite3_step(pCur->pFullScan);
         2515  +      if( rc!=SQLITE_ROW ) pCur->iRow = pCur->nRow;
         2516  +      if( rc==SQLITE_ROW || rc==SQLITE_DONE ) rc = SQLITE_OK;
         2517  +    }else{
         2518  +      pCur->iRow++;
         2519  +    }
         2520  +  }
         2521  +  return rc;
  1716   2522   }
  1717   2523   
  1718   2524   /*
  1719   2525   ** Return TRUE if we are at the end-of-file
  1720   2526   */
  1721   2527   static int spellfix1Eof(sqlite3_vtab_cursor *cur){
  1722   2528     spellfix1_cursor *pCur = (spellfix1_cursor *)cur;
  1723   2529     return pCur->iRow>=pCur->nRow;
  1724   2530   }
  1725   2531   
  1726   2532   /*
  1727   2533   ** Return columns from the current row.
  1728   2534   */
  1729         -static int spellfix1Column(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
         2535  +static int spellfix1Column(
         2536  +  sqlite3_vtab_cursor *cur,
         2537  +  sqlite3_context *ctx,
         2538  +  int i
         2539  +){
  1730   2540     spellfix1_cursor *pCur = (spellfix1_cursor*)cur;
         2541  +  if( pCur->pFullScan ){
         2542  +    if( i<=SPELLFIX_COL_LANGID ){
         2543  +      sqlite3_result_value(ctx, sqlite3_column_value(pCur->pFullScan, i));
         2544  +    }else{
         2545  +      sqlite3_result_null(ctx);
         2546  +    }
         2547  +    return SQLITE_OK;
         2548  +  }
  1731   2549     switch( i ){
  1732         -    case 0: {
         2550  +    case SPELLFIX_COL_WORD: {
  1733   2551         sqlite3_result_text(ctx, pCur->a[pCur->iRow].zWord, -1, SQLITE_STATIC);
  1734   2552         break;
  1735   2553       }
  1736         -    case 1: {
         2554  +    case SPELLFIX_COL_RANK: {
  1737   2555         sqlite3_result_int(ctx, pCur->a[pCur->iRow].iRank);
  1738   2556         break;
  1739   2557       }
  1740         -    case 2: {
         2558  +    case SPELLFIX_COL_DISTANCE: {
  1741   2559         sqlite3_result_int(ctx, pCur->a[pCur->iRow].iDistance);
  1742   2560         break;
  1743   2561       }
  1744         -    case 3: {
         2562  +    case SPELLFIX_COL_LANGID: {
  1745   2563         sqlite3_result_int(ctx, pCur->iLang);
  1746   2564         break;
  1747   2565       }
  1748         -    case 4: {
         2566  +    case SPELLFIX_COL_SCORE: {
  1749   2567         sqlite3_result_int(ctx, pCur->a[pCur->iRow].iScore);
  1750   2568         break;
  1751   2569       }
  1752         -    case 5: {
         2570  +    case SPELLFIX_COL_MATCHLEN: {
         2571  +      int iMatchlen = pCur->a[pCur->iRow].iMatchlen;
         2572  +      if( iMatchlen<0 ){
         2573  +        int nPattern = strlen(pCur->zPattern);
         2574  +        char *zWord = pCur->a[pCur->iRow].zWord;
         2575  +        int nWord = strlen(zWord);
         2576  +
         2577  +        if( nPattern>0 && pCur->zPattern[nPattern-1]=='*' ){
         2578  +          char *zTranslit;
         2579  +          int res;
         2580  +          zTranslit = (char *)transliterate((unsigned char *)zWord, nWord);
         2581  +          if( !zTranslit ) return SQLITE_NOMEM;
         2582  +          res = editdist1(pCur->zPattern, zTranslit, &iMatchlen);
         2583  +          sqlite3_free(zTranslit);
         2584  +          if( res<0 ) return SQLITE_NOMEM;
         2585  +          iMatchlen = translen_to_charlen(zWord, nWord, iMatchlen);
         2586  +        }else{
         2587  +          iMatchlen = utf8Charlen(zWord, nWord);
         2588  +        }
         2589  +      }
         2590  +
         2591  +      sqlite3_result_int(ctx, iMatchlen);
         2592  +      break;
         2593  +    }
         2594  +    case SPELLFIX_COL_PHONEHASH: {
         2595  +      sqlite3_result_text(ctx, pCur->a[pCur->iRow].zHash, -1, SQLITE_STATIC);
         2596  +      break;
         2597  +    }
         2598  +    case SPELLFIX_COL_TOP: {
  1753   2599         sqlite3_result_int(ctx, pCur->iTop);
  1754   2600         break;
  1755   2601       }
  1756         -    case 6: {
         2602  +    case SPELLFIX_COL_SCOPE: {
  1757   2603         sqlite3_result_int(ctx, pCur->iScope);
  1758   2604         break;
  1759   2605       }
  1760         -    case 7: {
         2606  +    case SPELLFIX_COL_SRCHCNT: {
  1761   2607         sqlite3_result_int(ctx, pCur->nSearch);
  1762   2608         break;
  1763   2609       }
  1764   2610       default: {
  1765   2611         sqlite3_result_null(ctx);
  1766   2612         break;
  1767   2613       }
................................................................................
  1770   2616   }
  1771   2617   
  1772   2618   /*
  1773   2619   ** The rowid.
  1774   2620   */
  1775   2621   static int spellfix1Rowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  1776   2622     spellfix1_cursor *pCur = (spellfix1_cursor*)cur;
  1777         -  *pRowid = pCur->a[pCur->iRow].iRowid;
         2623  +  if( pCur->pFullScan ){
         2624  +    *pRowid = sqlite3_column_int64(pCur->pFullScan, 4);
         2625  +  }else{
         2626  +    *pRowid = pCur->a[pCur->iRow].iRowid;
         2627  +  }
  1778   2628     return SQLITE_OK;
  1779   2629   }
  1780   2630   
  1781   2631   /*
  1782   2632   ** The xUpdate() method.
  1783   2633   */
  1784   2634   static int spellfix1Update(
................................................................................
  1795   2645     if( argc==1 ){
  1796   2646       /* A delete operation on the rowid given by argv[0] */
  1797   2647       rowid = *pRowid = sqlite3_value_int64(argv[0]);
  1798   2648       spellfix1DbExec(&rc, db, "DELETE FROM \"%w\".\"%w_vocab\" "
  1799   2649                              " WHERE id=%lld",
  1800   2650                     p->zDbName, p->zTableName, rowid);
  1801   2651     }else{
  1802         -    const unsigned char *zWord = sqlite3_value_text(argv[2]);
  1803         -    int nWord = sqlite3_value_bytes(argv[2]);
  1804         -    int iLang = sqlite3_value_int(argv[5]);
  1805         -    int iRank = sqlite3_value_int(argv[3]);
  1806         -    const unsigned char *zSoundslike = sqlite3_value_text(argv[10]);
  1807         -    int nSoundslike = sqlite3_value_bytes(argv[10]);
         2652  +    const unsigned char *zWord = sqlite3_value_text(argv[SPELLFIX_COL_WORD+2]);
         2653  +    int nWord = sqlite3_value_bytes(argv[SPELLFIX_COL_WORD+2]);
         2654  +    int iLang = sqlite3_value_int(argv[SPELLFIX_COL_LANGID+2]);
         2655  +    int iRank = sqlite3_value_int(argv[SPELLFIX_COL_RANK+2]);
         2656  +    const unsigned char *zSoundslike =
         2657  +           sqlite3_value_text(argv[SPELLFIX_COL_SOUNDSLIKE+2]);
         2658  +    int nSoundslike = sqlite3_value_bytes(argv[SPELLFIX_COL_SOUNDSLIKE+2]);
  1808   2659       char *zK1, *zK2;
  1809   2660       int i;
  1810   2661       char c;
  1811   2662   
  1812   2663       if( zWord==0 ){
  1813         -      pVTab->zErrMsg = sqlite3_mprintf("%w.word may not be NULL",
  1814         -                            p->zTableName);
  1815         -      return SQLITE_CONSTRAINT;
         2664  +      /* Inserts of the form:  INSERT INTO table(command) VALUES('xyzzy');
         2665  +      ** cause zWord to be NULL, so we look at the "command" column to see
         2666  +      ** what special actions to take */
         2667  +      const char *zCmd = 
         2668  +         (const char*)sqlite3_value_text(argv[SPELLFIX_COL_COMMAND+2]);
         2669  +      if( zCmd==0 ){
         2670  +        pVTab->zErrMsg = sqlite3_mprintf("%s.word may not be NULL",
         2671  +                                         p->zTableName);
         2672  +        return SQLITE_CONSTRAINT;
         2673  +      }
         2674  +      if( strcmp(zCmd,"reset")==0 ){
         2675  +        /* Reset the  edit cost table (if there is one). */
         2676  +        editDist3ConfigDelete(p->pConfig3);
         2677  +        p->pConfig3 = 0;
         2678  +        return SQLITE_OK;
         2679  +      }
         2680  +      pVTab->zErrMsg = sqlite3_mprintf("unknown value for %s.command: \"%w\"",
         2681  +                                       p->zTableName, zCmd);
         2682  +      return SQLITE_ERROR;
  1816   2683       }
  1817   2684       if( iRank<1 ) iRank = 1;
  1818   2685       if( zSoundslike ){
  1819   2686         zK1 = (char*)transliterate(zSoundslike, nSoundslike);
  1820   2687       }else{
  1821   2688         zK1 = (char*)transliterate(zWord, nWord);
  1822   2689       }
  1823   2690       if( zK1==0 ) return SQLITE_NOMEM;
  1824   2691       for(i=0; (c = zK1[i])!=0; i++){
  1825   2692          if( c>='A' && c<='Z' ) zK1[i] += 'a' - 'A';
  1826   2693       }
  1827         -    zK2 = (char*)characterClassString((const unsigned char*)zK1, i);
         2694  +    zK2 = (char*)phoneticHash((const unsigned char*)zK1, i);
  1828   2695       if( zK2==0 ){
  1829   2696         sqlite3_free(zK1);
  1830   2697         return SQLITE_NOMEM;
  1831   2698       }
  1832   2699       if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
  1833   2700         spellfix1DbExec(&rc, db,
  1834   2701                "INSERT INTO \"%w\".\"%w_vocab\"(rank,langid,word,k1,k2) "
................................................................................
  1837   2704                iRank, iLang, zWord, zK1, zK2
  1838   2705         );
  1839   2706         *pRowid = sqlite3_last_insert_rowid(db);
  1840   2707       }else{
  1841   2708         rowid = sqlite3_value_int64(argv[0]);
  1842   2709         newRowid = *pRowid = sqlite3_value_int64(argv[1]);
  1843   2710         spellfix1DbExec(&rc, db,
  1844         -             "UPDATE \"%w\".\"%w_vocab\" SET id=%lld, rank=%d, lang=%d,"
  1845         -             " word=%Q, rank=%d, k1=%Q, k2=%Q WHERE id=%lld",
         2711  +             "UPDATE \"%w\".\"%w_vocab\" SET id=%lld, rank=%d, langid=%d,"
         2712  +             " word=%Q, k1=%Q, k2=%Q WHERE id=%lld",
  1846   2713                p->zDbName, p->zTableName, newRowid, iRank, iLang,
  1847   2714                zWord, zK1, zK2, rowid
  1848   2715         );
  1849   2716       }
  1850   2717       sqlite3_free(zK1);
  1851   2718       sqlite3_free(zK2);
  1852   2719     }
................................................................................
  1867   2734     spellfix1DbExec(&rc, db, 
  1868   2735        "ALTER TABLE \"%w\".\"%w_vocab\" RENAME TO \"%w_vocab\"",
  1869   2736        p->zDbName, p->zTableName, zNewName
  1870   2737     );
  1871   2738     if( rc==SQLITE_OK ){
  1872   2739       sqlite3_free(p->zTableName);
  1873   2740       p->zTableName = zNewName;
         2741  +  }else{
         2742  +    sqlite3_free(zNewName);
  1874   2743     }
  1875   2744     return rc;
  1876   2745   }
  1877   2746   
  1878   2747   
  1879   2748   /*
  1880   2749   ** A virtual table module that provides fuzzy search.
................................................................................
  1902   2771     spellfix1Rename,         /* xRename */
  1903   2772   };
  1904   2773   
  1905   2774   /*
  1906   2775   ** Register the various functions and the virtual table.
  1907   2776   */
  1908   2777   static int spellfix1Register(sqlite3 *db){
  1909         -  int nErr = 0;
         2778  +  int rc = SQLITE_OK;
  1910   2779     int i;
  1911         -  nErr += sqlite3_create_function(db, "spellfix1_translit", 1, SQLITE_UTF8, 0,
         2780  +  rc = sqlite3_create_function(db, "spellfix1_translit", 1, SQLITE_UTF8, 0,
  1912   2781                                     transliterateSqlFunc, 0, 0);
  1913         -  nErr += sqlite3_create_function(db, "spellfix1_editdist", 2, SQLITE_UTF8, 0,
         2782  +  if( rc==SQLITE_OK ){
         2783  +    rc = sqlite3_create_function(db, "spellfix1_editdist", 2, SQLITE_UTF8, 0,
  1914   2784                                     editdistSqlFunc, 0, 0);
  1915         -  nErr += sqlite3_create_function(db, "spellfix1_charclass", 1, SQLITE_UTF8, 0,
  1916         -                                  characterClassSqlFunc, 0, 0);
  1917         -  nErr += sqlite3_create_function(db, "spellfix1_scriptcode", 1, SQLITE_UTF8, 0,
         2785  +  }
         2786  +  if( rc==SQLITE_OK ){
         2787  +    rc = sqlite3_create_function(db, "spellfix1_phonehash", 1, SQLITE_UTF8, 0,
         2788  +                                  phoneticHashSqlFunc, 0, 0);
         2789  +  }
         2790  +  if( rc==SQLITE_OK ){
         2791  +    rc = sqlite3_create_function(db, "spellfix1_scriptcode", 1, SQLITE_UTF8, 0,
  1918   2792                                     scriptCodeSqlFunc, 0, 0);
  1919         -  nErr += sqlite3_create_module(db, "spellfix1", &spellfix1Module, 0);
         2793  +  }
         2794  +  if( rc==SQLITE_OK ){
         2795  +    rc = sqlite3_create_module(db, "spellfix1", &spellfix1Module, 0);
         2796  +  }
         2797  +  if( rc==SQLITE_OK ){
         2798  +    rc = editDist3Install(db);
         2799  +  }
  1920   2800   
  1921   2801     /* Verify sanity of the translit[] table */
  1922   2802     for(i=0; i<sizeof(translit)/sizeof(translit[0])-1; i++){
  1923   2803       assert( translit[i].cFrom<translit[i+1].cFrom );
  1924         -  }  
         2804  +  }
  1925   2805   
  1926         -  return nErr ? SQLITE_ERROR : SQLITE_OK;
         2806  +  return rc;
  1927   2807   }
  1928   2808   
  1929   2809   #if SQLITE_CORE || defined(SQLITE_TEST)
  1930   2810   /*
  1931   2811   ** Register the spellfix1 virtual table and its associated functions.
  1932   2812   */
  1933   2813   int sqlite3Spellfix1Register(sqlite3 *db){

Changes to src/test_vfs.c.

   357    357   ){
   358    358     int rc = SQLITE_OK;
   359    359     TestvfsFd *pFd = tvfsGetFd(pFile);
   360    360     Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
   361    361   
   362    362     if( p->pScript && p->mask&TESTVFS_WRITE_MASK ){
   363    363       tvfsExecTcl(p, "xWrite", 
   364         -        Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0
          364  +        Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 
          365  +        Tcl_NewWideIntObj(iOfst)
   365    366       );
   366    367       tvfsResultCode(p, &rc);
   367    368     }
   368    369   
   369    370     if( rc==SQLITE_OK && tvfsInjectFullerr(p) ){
   370    371       rc = SQLITE_FULL;
   371    372     }

Changes to src/update.c.

   309    309       goto update_cleanup;
   310    310     }
   311    311   
   312    312     /* Begin the database scan
   313    313     */
   314    314     sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
   315    315     pWInfo = sqlite3WhereBegin(
   316         -      pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED
          316  +      pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0
   317    317     );
   318    318     if( pWInfo==0 ) goto update_cleanup;
   319    319     okOnePass = pWInfo->okOnePass;
   320    320   
   321    321     /* Remember the rowid of every item to be updated.
   322    322     */
   323    323     sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);

Changes to src/util.c.

   367    367   
   368    368       /* adjust the sign of significand */
   369    369       s = sign<0 ? -s : s;
   370    370   
   371    371       /* if exponent, scale significand as appropriate
   372    372       ** and store in result. */
   373    373       if( e ){
   374         -      double scale = 1.0;
          374  +      LONGDOUBLE_TYPE scale = 1.0;
   375    375         /* attempt to handle extremely small/large numbers better */
   376    376         if( e>307 && e<342 ){
   377    377           while( e%308 ) { scale *= 1.0e+1; e -= 1; }
   378    378           if( esign<0 ){
   379    379             result = s / scale;
   380    380             result /= 1.0e+308;
   381    381           }else{

Changes to src/vdbe.c.

  3116   3116     int p2;
  3117   3117     int iDb;
  3118   3118     int wrFlag;
  3119   3119     Btree *pX;
  3120   3120     VdbeCursor *pCur;
  3121   3121     Db *pDb;
  3122   3122   
         3123  +  assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
         3124  +  assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
         3125  +
  3123   3126     if( p->expired ){
  3124   3127       rc = SQLITE_ABORT;
  3125   3128       break;
  3126   3129     }
  3127   3130   
  3128   3131     nField = 0;
  3129   3132     pKeyInfo = 0;
................................................................................
  3139   3142       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  3140   3143       if( pDb->pSchema->file_format < p->minWriteFileFormat ){
  3141   3144         p->minWriteFileFormat = pDb->pSchema->file_format;
  3142   3145       }
  3143   3146     }else{
  3144   3147       wrFlag = 0;
  3145   3148     }
  3146         -  if( pOp->p5 ){
         3149  +  if( pOp->p5 & OPFLAG_P2ISREG ){
  3147   3150       assert( p2>0 );
  3148   3151       assert( p2<=p->nMem );
  3149   3152       pIn2 = &aMem[p2];
  3150   3153       assert( memIsValid(pIn2) );
  3151   3154       assert( (pIn2->flags & MEM_Int)!=0 );
  3152   3155       sqlite3VdbeMemIntegerify(pIn2);
  3153   3156       p2 = (int)pIn2->u.i;
................................................................................
  3170   3173     assert( pOp->p1>=0 );
  3171   3174     pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
  3172   3175     if( pCur==0 ) goto no_mem;
  3173   3176     pCur->nullRow = 1;
  3174   3177     pCur->isOrdered = 1;
  3175   3178     rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
  3176   3179     pCur->pKeyInfo = pKeyInfo;
         3180  +  assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
         3181  +  sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
  3177   3182   
  3178   3183     /* Since it performs no memory allocation or IO, the only value that
  3179   3184     ** sqlite3BtreeCursor() may return is SQLITE_OK. */
  3180   3185     assert( rc==SQLITE_OK );
  3181   3186   
  3182   3187     /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
  3183   3188     ** SQLite used to check if the root-page flags were sane at this point

Changes to src/vdbeInt.h.

   427    427   # define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
   428    428   # define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
   429    429   # define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
   430    430   # define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
   431    431   #else
   432    432   int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
   433    433   void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   434         -int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
   435         -int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
   436         -int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
   437         -int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
   438         -int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
          434  +int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
          435  +int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
          436  +int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
          437  +int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
          438  +int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int *);
   439    439   #endif
   440    440   
   441    441   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
   442    442     void sqlite3VdbeEnter(Vdbe*);
   443    443     void sqlite3VdbeLeave(Vdbe*);
   444    444   #else
   445    445   # define sqlite3VdbeEnter(X)

Changes to src/vdbeapi.c.

    74     74       rc = SQLITE_OK;
    75     75     }else{
    76     76       Vdbe *v = (Vdbe*)pStmt;
    77     77   #ifdef SQLITE_ENABLE_SQLRR
    78     78       SRRecFinalize(pStmt);
    79     79   #endif
    80     80       sqlite3 *db = v->db;
    81         -#if SQLITE_THREADSAFE
    82         -    sqlite3_mutex *mutex;
    83         -#endif
    84     81       if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
    85         -#if SQLITE_THREADSAFE
    86         -    mutex = v->db->mutex;
    87         -#endif
    88         -    sqlite3_mutex_enter(mutex);
           82  +    sqlite3_mutex_enter(db->mutex);
    89     83       rc = sqlite3VdbeFinalize(v);
    90     84       rc = sqlite3ApiExit(db, rc);
    91         -    sqlite3_mutex_leave(mutex);
           85  +    sqlite3LeaveMutexAndCloseZombie(db);
    92     86     }
    93     87     return rc;
    94     88   }
    95     89   
    96     90   /*
    97     91   ** Terminate the current execution of an SQL statement and reset it
    98     92   ** back to its starting state so that it can be reused. A success code from

Changes to src/vdbeaux.c.

  2467   2467   ** Delete an entire VDBE.
  2468   2468   */
  2469   2469   void sqlite3VdbeDelete(Vdbe *p){
  2470   2470     sqlite3 *db;
  2471   2471   
  2472   2472     if( NEVER(p==0) ) return;
  2473   2473     db = p->db;
         2474  +  assert( sqlite3_mutex_held(db->mutex) );
  2474   2475     if( p->pPrev ){
  2475   2476       p->pPrev->pNext = p->pNext;
  2476   2477     }else{
  2477   2478       assert( db->pVdbe==p );
  2478   2479       db->pVdbe = p->pNext;
  2479   2480     }
  2480   2481     if( p->pNext ){

Changes to src/vdbesort.c.

    18     18   #include "sqliteInt.h"
    19     19   #include "vdbeInt.h"
    20     20   
    21     21   #ifndef SQLITE_OMIT_MERGE_SORT
    22     22   
    23     23   typedef struct VdbeSorterIter VdbeSorterIter;
    24     24   typedef struct SorterRecord SorterRecord;
           25  +typedef struct FileWriter FileWriter;
    25     26   
    26     27   /*
    27     28   ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
    28     29   **
    29     30   ** As keys are added to the sorter, they are written to disk in a series
    30     31   ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
    31     32   ** the same as the cache-size allowed for temporary databases. In order
................................................................................
   115    116     i64 iReadOff;                   /* Current read offset */
   116    117     i64 iEof;                       /* 1 byte past EOF for this iterator */
   117    118     int nAlloc;                     /* Bytes of space at aAlloc */
   118    119     int nKey;                       /* Number of bytes in key */
   119    120     sqlite3_file *pFile;            /* File iterator is reading from */
   120    121     u8 *aAlloc;                     /* Allocated space */
   121    122     u8 *aKey;                       /* Pointer to current key */
          123  +  u8 *aBuffer;                    /* Current read buffer */
          124  +  int nBuffer;                    /* Size of read buffer in bytes */
          125  +};
          126  +
          127  +/*
          128  +** An instance of this structure is used to organize the stream of records
          129  +** being written to files by the merge-sort code into aligned, page-sized
          130  +** blocks.  Doing all I/O in aligned page-sized blocks helps I/O to go
          131  +** faster on many operating systems.
          132  +*/
          133  +struct FileWriter {
          134  +  int eFWErr;                     /* Non-zero if in an error state */
          135  +  u8 *aBuffer;                    /* Pointer to write buffer */
          136  +  int nBuffer;                    /* Size of write buffer in bytes */
          137  +  int iBufStart;                  /* First byte of buffer to write */
          138  +  int iBufEnd;                    /* Last byte of buffer to write */
          139  +  i64 iWriteOff;                  /* Offset of start of buffer in file */
          140  +  sqlite3_file *pFile;            /* File to write to */
   122    141   };
   123    142   
   124    143   /*
   125    144   ** A structure to store a single record. All in-memory records are connected
   126    145   ** together into a linked list headed at VdbeSorter.pRecord using the 
   127    146   ** SorterRecord.pNext pointer.
   128    147   */
................................................................................
   140    159   
   141    160   /*
   142    161   ** Free all memory belonging to the VdbeSorterIter object passed as the second
   143    162   ** argument. All structure fields are set to zero before returning.
   144    163   */
   145    164   static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
   146    165     sqlite3DbFree(db, pIter->aAlloc);
          166  +  sqlite3DbFree(db, pIter->aBuffer);
   147    167     memset(pIter, 0, sizeof(VdbeSorterIter));
   148    168   }
          169  +
          170  +/*
          171  +** Read nByte bytes of data from the stream of data iterated by object p.
          172  +** If successful, set *ppOut to point to a buffer containing the data
          173  +** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
          174  +** error code.
          175  +**
          176  +** The buffer indicated by *ppOut may only be considered valid until the
          177  +** next call to this function.
          178  +*/
          179  +static int vdbeSorterIterRead(
          180  +  sqlite3 *db,                    /* Database handle (for malloc) */
          181  +  VdbeSorterIter *p,              /* Iterator */
          182  +  int nByte,                      /* Bytes of data to read */
          183  +  u8 **ppOut                      /* OUT: Pointer to buffer containing data */
          184  +){
          185  +  int iBuf;                       /* Offset within buffer to read from */
          186  +  int nAvail;                     /* Bytes of data available in buffer */
          187  +  assert( p->aBuffer );
          188  +
          189  +  /* If there is no more data to be read from the buffer, read the next 
          190  +  ** p->nBuffer bytes of data from the file into it. Or, if there are less
          191  +  ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
          192  +  iBuf = p->iReadOff % p->nBuffer;
          193  +  if( iBuf==0 ){
          194  +    int nRead;                    /* Bytes to read from disk */
          195  +    int rc;                       /* sqlite3OsRead() return code */
          196  +
          197  +    /* Determine how many bytes of data to read. */
          198  +    nRead = (int)(p->iEof - p->iReadOff);
          199  +    if( nRead>p->nBuffer ) nRead = p->nBuffer;
          200  +    assert( nRead>0 );
          201  +
          202  +    /* Read data from the file. Return early if an error occurs. */
          203  +    rc = sqlite3OsRead(p->pFile, p->aBuffer, nRead, p->iReadOff);
          204  +    assert( rc!=SQLITE_IOERR_SHORT_READ );
          205  +    if( rc!=SQLITE_OK ) return rc;
          206  +  }
          207  +  nAvail = p->nBuffer - iBuf; 
          208  +
          209  +  if( nByte<=nAvail ){
          210  +    /* The requested data is available in the in-memory buffer. In this
          211  +    ** case there is no need to make a copy of the data, just return a 
          212  +    ** pointer into the buffer to the caller.  */
          213  +    *ppOut = &p->aBuffer[iBuf];
          214  +    p->iReadOff += nByte;
          215  +  }else{
          216  +    /* The requested data is not all available in the in-memory buffer.
          217  +    ** In this case, allocate space at p->aAlloc[] to copy the requested
          218  +    ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
          219  +    int nRem;                     /* Bytes remaining to copy */
          220  +
          221  +    /* Extend the p->aAlloc[] allocation if required. */
          222  +    if( p->nAlloc<nByte ){
          223  +      int nNew = p->nAlloc*2;
          224  +      while( nByte>nNew ) nNew = nNew*2;
          225  +      p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
          226  +      if( !p->aAlloc ) return SQLITE_NOMEM;
          227  +      p->nAlloc = nNew;
          228  +    }
          229  +
          230  +    /* Copy as much data as is available in the buffer into the start of
          231  +    ** p->aAlloc[].  */
          232  +    memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
          233  +    p->iReadOff += nAvail;
          234  +    nRem = nByte - nAvail;
          235  +
          236  +    /* The following loop copies up to p->nBuffer bytes per iteration into
          237  +    ** the p->aAlloc[] buffer.  */
          238  +    while( nRem>0 ){
          239  +      int rc;                     /* vdbeSorterIterRead() return code */
          240  +      int nCopy;                  /* Number of bytes to copy */
          241  +      u8 *aNext;                  /* Pointer to buffer to copy data from */
          242  +
          243  +      nCopy = nRem;
          244  +      if( nRem>p->nBuffer ) nCopy = p->nBuffer;
          245  +      rc = vdbeSorterIterRead(db, p, nCopy, &aNext);
          246  +      if( rc!=SQLITE_OK ) return rc;
          247  +      assert( aNext!=p->aAlloc );
          248  +      memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
          249  +      nRem -= nCopy;
          250  +    }
          251  +
          252  +    *ppOut = p->aAlloc;
          253  +  }
          254  +
          255  +  return SQLITE_OK;
          256  +}
          257  +
          258  +/*
          259  +** Read a varint from the stream of data accessed by p. Set *pnOut to
          260  +** the value read.
          261  +*/
          262  +static int vdbeSorterIterVarint(sqlite3 *db, VdbeSorterIter *p, u64 *pnOut){
          263  +  int iBuf;
          264  +
          265  +  iBuf = p->iReadOff % p->nBuffer;
          266  +  if( iBuf && (p->nBuffer-iBuf)>=9 ){
          267  +    p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
          268  +  }else{
          269  +    u8 aVarint[16], *a;
          270  +    int i = 0, rc;
          271  +    do{
          272  +      rc = vdbeSorterIterRead(db, p, 1, &a);
          273  +      if( rc ) return rc;
          274  +      aVarint[(i++)&0xf] = a[0];
          275  +    }while( (a[0]&0x80)!=0 );
          276  +    sqlite3GetVarint(aVarint, pnOut);
          277  +  }
          278  +
          279  +  return SQLITE_OK;
          280  +}
          281  +
   149    282   
   150    283   /*
   151    284   ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
   152    285   ** no error occurs, or an SQLite error code if one does.
   153    286   */
   154    287   static int vdbeSorterIterNext(
   155    288     sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
   156    289     VdbeSorterIter *pIter           /* Iterator to advance */
   157    290   ){
   158    291     int rc;                         /* Return Code */
   159         -  int nRead;                      /* Number of bytes read */
   160         -  int nRec = 0;                   /* Size of record in bytes */
   161         -  int iOff = 0;                   /* Size of serialized size varint in bytes */
          292  +  u64 nRec = 0;                   /* Size of record in bytes */
   162    293   
   163         -  assert( pIter->iEof>=pIter->iReadOff );
   164         -  if( pIter->iEof-pIter->iReadOff>5 ){
   165         -    nRead = 5;
   166         -  }else{
   167         -    nRead = (int)(pIter->iEof - pIter->iReadOff);
   168         -  }
   169         -  if( nRead<=0 ){
          294  +  if( pIter->iReadOff>=pIter->iEof ){
   170    295       /* This is an EOF condition */
   171    296       vdbeSorterIterZero(db, pIter);
   172    297       return SQLITE_OK;
   173    298     }
   174    299   
   175         -  rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
          300  +  rc = vdbeSorterIterVarint(db, pIter, &nRec);
   176    301     if( rc==SQLITE_OK ){
   177         -    iOff = getVarint32(pIter->aAlloc, nRec);
   178         -    if( (iOff+nRec)>nRead ){
   179         -      int nRead2;                   /* Number of extra bytes to read */
   180         -      if( (iOff+nRec)>pIter->nAlloc ){
   181         -        int nNew = pIter->nAlloc*2;
   182         -        while( (iOff+nRec)>nNew ) nNew = nNew*2;
   183         -        pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
   184         -        if( !pIter->aAlloc ) return SQLITE_NOMEM;
   185         -        pIter->nAlloc = nNew;
   186         -      }
   187         -  
   188         -      nRead2 = iOff + nRec - nRead;
   189         -      rc = sqlite3OsRead(
   190         -          pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
   191         -      );
   192         -    }
   193         -  }
   194         -
   195         -  assert( rc!=SQLITE_OK || nRec>0 );
   196         -  pIter->iReadOff += iOff+nRec;
   197         -  pIter->nKey = nRec;
   198         -  pIter->aKey = &pIter->aAlloc[iOff];
   199         -  return rc;
   200         -}
   201         -
   202         -/*
   203         -** Write a single varint, value iVal, to file-descriptor pFile. Return
   204         -** SQLITE_OK if successful, or an SQLite error code if some error occurs.
   205         -**
   206         -** The value of *piOffset when this function is called is used as the byte
   207         -** offset in file pFile to write to. Before returning, *piOffset is 
   208         -** incremented by the number of bytes written.
   209         -*/
   210         -static int vdbeSorterWriteVarint(
   211         -  sqlite3_file *pFile,            /* File to write to */
   212         -  i64 iVal,                       /* Value to write as a varint */
   213         -  i64 *piOffset                   /* IN/OUT: Write offset in file pFile */
   214         -){
   215         -  u8 aVarint[9];                  /* Buffer large enough for a varint */
   216         -  int nVarint;                    /* Number of used bytes in varint */
   217         -  int rc;                         /* Result of write() call */
   218         -
   219         -  nVarint = sqlite3PutVarint(aVarint, iVal);
   220         -  rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
   221         -  *piOffset += nVarint;
   222         -
   223         -  return rc;
   224         -}
   225         -
   226         -/*
   227         -** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
   228         -** successful, or an SQLite error code if some error occurs.
   229         -**
   230         -** The value of *piOffset when this function is called is used as the
   231         -** byte offset in file pFile from whence to read the varint. If successful
   232         -** (i.e. if no IO error occurs), then *piOffset is set to the offset of
   233         -** the first byte past the end of the varint before returning. *piVal is
   234         -** set to the integer value read. If an error occurs, the final values of
   235         -** both *piOffset and *piVal are undefined.
   236         -*/
   237         -static int vdbeSorterReadVarint(
   238         -  sqlite3_file *pFile,            /* File to read from */
   239         -  i64 *piOffset,                  /* IN/OUT: Read offset in pFile */
   240         -  i64 *piVal                      /* OUT: Value read from file */
   241         -){
   242         -  u8 aVarint[9];                  /* Buffer large enough for a varint */
   243         -  i64 iOff = *piOffset;           /* Offset in file to read from */
   244         -  int rc;                         /* Return code */
   245         -
   246         -  rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
   247         -  if( rc==SQLITE_OK ){
   248         -    *piOffset += getVarint(aVarint, (u64 *)piVal);
          302  +    pIter->nKey = (int)nRec;
          303  +    rc = vdbeSorterIterRead(db, pIter, (int)nRec, &pIter->aKey);
   249    304     }
   250    305   
   251    306     return rc;
   252    307   }
   253    308   
   254    309   /*
   255    310   ** Initialize iterator pIter to scan through the PMA stored in file pFile
   256    311   ** starting at offset iStart and ending at offset iEof-1. This function 
   257    312   ** leaves the iterator pointing to the first key in the PMA (or EOF if the 
   258    313   ** PMA is empty).
   259    314   */
   260    315   static int vdbeSorterIterInit(
   261    316     sqlite3 *db,                    /* Database handle */
   262         -  VdbeSorter *pSorter,            /* Sorter object */
          317  +  const VdbeSorter *pSorter,      /* Sorter object */
   263    318     i64 iStart,                     /* Start offset in pFile */
   264    319     VdbeSorterIter *pIter,          /* Iterator to populate */
   265    320     i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
   266    321   ){
   267         -  int rc;
          322  +  int rc = SQLITE_OK;
          323  +  int nBuf;
          324  +
          325  +  nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
   268    326   
   269    327     assert( pSorter->iWriteOff>iStart );
   270    328     assert( pIter->aAlloc==0 );
          329  +  assert( pIter->aBuffer==0 );
   271    330     pIter->pFile = pSorter->pTemp1;
   272    331     pIter->iReadOff = iStart;
   273    332     pIter->nAlloc = 128;
   274    333     pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
   275         -  if( !pIter->aAlloc ){
          334  +  pIter->nBuffer = nBuf;
          335  +  pIter->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
          336  +
          337  +  if( !pIter->aBuffer ){
   276    338       rc = SQLITE_NOMEM;
   277    339     }else{
   278         -    i64 nByte;                         /* Total size of PMA in bytes */
   279         -    rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
   280         -    *pnByte += nByte;
   281         -    pIter->iEof = pIter->iReadOff + nByte;
          340  +    int iBuf;
          341  +
          342  +    iBuf = iStart % nBuf;
          343  +    if( iBuf ){
          344  +      int nRead = nBuf - iBuf;
          345  +      if( (iStart + nRead) > pSorter->iWriteOff ){
          346  +        nRead = (int)(pSorter->iWriteOff - iStart);
          347  +      }
          348  +      rc = sqlite3OsRead(
          349  +          pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
          350  +      );
          351  +      assert( rc!=SQLITE_IOERR_SHORT_READ );
          352  +    }
          353  +
          354  +    if( rc==SQLITE_OK ){
          355  +      u64 nByte;                       /* Size of PMA in bytes */
          356  +      pIter->iEof = pSorter->iWriteOff;
          357  +      rc = vdbeSorterIterVarint(db, pIter, &nByte);
          358  +      pIter->iEof = pIter->iReadOff + nByte;
          359  +      *pnByte += nByte;
          360  +    }
   282    361     }
          362  +
   283    363     if( rc==SQLITE_OK ){
   284    364       rc = vdbeSorterIterNext(db, pIter);
   285    365     }
   286    366     return rc;
   287    367   }
   288    368   
   289    369   
................................................................................
   299    379   ** is true and key1 contains even a single NULL value, it is considered to
   300    380   ** be less than key2. Even if key2 also contains NULL values.
   301    381   **
   302    382   ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
   303    383   ** has been allocated and contains an unpacked record that is used as key2.
   304    384   */
   305    385   static void vdbeSorterCompare(
   306         -  VdbeCursor *pCsr,               /* Cursor object (for pKeyInfo) */
          386  +  const VdbeCursor *pCsr,         /* Cursor object (for pKeyInfo) */
   307    387     int bOmitRowid,                 /* Ignore rowid field at end of keys */
   308         -  void *pKey1, int nKey1,         /* Left side of comparison */
   309         -  void *pKey2, int nKey2,         /* Right side of comparison */
          388  +  const void *pKey1, int nKey1,   /* Left side of comparison */
          389  +  const void *pKey2, int nKey2,   /* Right side of comparison */
   310    390     int *pRes                       /* OUT: Result of comparison */
   311    391   ){
   312    392     KeyInfo *pKeyInfo = pCsr->pKeyInfo;
   313    393     VdbeSorter *pSorter = pCsr->pSorter;
   314    394     UnpackedRecord *r2 = pSorter->pUnpacked;
   315    395     int i;
   316    396   
................................................................................
   334    414   }
   335    415   
   336    416   /*
   337    417   ** This function is called to compare two iterator keys when merging 
   338    418   ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
   339    419   ** value to recalculate.
   340    420   */
   341         -static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
          421  +static int vdbeSorterDoCompare(const VdbeCursor *pCsr, int iOut){
   342    422     VdbeSorter *pSorter = pCsr->pSorter;
   343    423     int i1;
   344    424     int i2;
   345    425     int iRes;
   346    426     VdbeSorterIter *p1;
   347    427     VdbeSorterIter *p2;
   348    428   
................................................................................
   460    540   }
   461    541   
   462    542   /*
   463    543   ** Merge the two sorted lists p1 and p2 into a single list.
   464    544   ** Set *ppOut to the head of the new list.
   465    545   */
   466    546   static void vdbeSorterMerge(
   467         -  VdbeCursor *pCsr,               /* For pKeyInfo */
          547  +  const VdbeCursor *pCsr,         /* For pKeyInfo */
   468    548     SorterRecord *p1,               /* First list to merge */
   469    549     SorterRecord *p2,               /* Second list to merge */
   470    550     SorterRecord **ppOut            /* OUT: Head of merged list */
   471    551   ){
   472    552     SorterRecord *pFinal = 0;
   473    553     SorterRecord **pp = &pFinal;
   474    554     void *pVal2 = p2 ? p2->pVal : 0;
................................................................................
   494    574   }
   495    575   
   496    576   /*
   497    577   ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
   498    578   ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
   499    579   ** occurs.
   500    580   */
   501         -static int vdbeSorterSort(VdbeCursor *pCsr){
          581  +static int vdbeSorterSort(const VdbeCursor *pCsr){
   502    582     int i;
   503    583     SorterRecord **aSlot;
   504    584     SorterRecord *p;
   505    585     VdbeSorter *pSorter = pCsr->pSorter;
   506    586   
   507    587     aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
   508    588     if( !aSlot ){
................................................................................
   527    607     }
   528    608     pSorter->pRecord = p;
   529    609   
   530    610     sqlite3_free(aSlot);
   531    611     return SQLITE_OK;
   532    612   }
   533    613   
          614  +/*
          615  +** Initialize a file-writer object.
          616  +*/
          617  +static void fileWriterInit(
          618  +  sqlite3 *db,                    /* Database (for malloc) */
          619  +  sqlite3_file *pFile,            /* File to write to */
          620  +  FileWriter *p,                  /* Object to populate */
          621  +  i64 iStart                      /* Offset of pFile to begin writing at */
          622  +){
          623  +  int nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
          624  +
          625  +  memset(p, 0, sizeof(FileWriter));
          626  +  p->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
          627  +  if( !p->aBuffer ){
          628  +    p->eFWErr = SQLITE_NOMEM;
          629  +  }else{
          630  +    p->iBufEnd = p->iBufStart = (iStart % nBuf);
          631  +    p->iWriteOff = iStart - p->iBufStart;
          632  +    p->nBuffer = nBuf;
          633  +    p->pFile = pFile;
          634  +  }
          635  +}
          636  +
          637  +/*
          638  +** Write nData bytes of data to the file-write object. Return SQLITE_OK
          639  +** if successful, or an SQLite error code if an error occurs.
          640  +*/
          641  +static void fileWriterWrite(FileWriter *p, u8 *pData, int nData){
          642  +  int nRem = nData;
          643  +  while( nRem>0 && p->eFWErr==0 ){
          644  +    int nCopy = nRem;
          645  +    if( nCopy>(p->nBuffer - p->iBufEnd) ){
          646  +      nCopy = p->nBuffer - p->iBufEnd;
          647  +    }
          648  +
          649  +    memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
          650  +    p->iBufEnd += nCopy;
          651  +    if( p->iBufEnd==p->nBuffer ){
          652  +      p->eFWErr = sqlite3OsWrite(p->pFile, 
          653  +          &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
          654  +          p->iWriteOff + p->iBufStart
          655  +      );
          656  +      p->iBufStart = p->iBufEnd = 0;
          657  +      p->iWriteOff += p->nBuffer;
          658  +    }
          659  +    assert( p->iBufEnd<p->nBuffer );
          660  +
          661  +    nRem -= nCopy;
          662  +  }
          663  +}
          664  +
          665  +/*
          666  +** Flush any buffered data to disk and clean up the file-writer object.
          667  +** The results of using the file-writer after this call are undefined.
          668  +** Return SQLITE_OK if flushing the buffered data succeeds or is not 
          669  +** required. Otherwise, return an SQLite error code.
          670  +**
          671  +** Before returning, set *piEof to the offset immediately following the
          672  +** last byte written to the file.
          673  +*/
          674  +static int fileWriterFinish(sqlite3 *db, FileWriter *p, i64 *piEof){
          675  +  int rc;
          676  +  if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
          677  +    p->eFWErr = sqlite3OsWrite(p->pFile, 
          678  +        &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
          679  +        p->iWriteOff + p->iBufStart
          680  +    );
          681  +  }
          682  +  *piEof = (p->iWriteOff + p->iBufEnd);
          683  +  sqlite3DbFree(db, p->aBuffer);
          684  +  rc = p->eFWErr;
          685  +  memset(p, 0, sizeof(FileWriter));
          686  +  return rc;
          687  +}
          688  +
          689  +/*
          690  +** Write value iVal encoded as a varint to the file-write object. Return 
          691  +** SQLITE_OK if successful, or an SQLite error code if an error occurs.
          692  +*/
          693  +static void fileWriterWriteVarint(FileWriter *p, u64 iVal){
          694  +  int nByte; 
          695  +  u8 aByte[10];
          696  +  nByte = sqlite3PutVarint(aByte, iVal);
          697  +  fileWriterWrite(p, aByte, nByte);
          698  +}
   534    699   
   535    700   /*
   536    701   ** Write the current contents of the in-memory linked-list to a PMA. Return
   537    702   ** SQLITE_OK if successful, or an SQLite error code otherwise.
   538    703   **
   539    704   ** The format of a PMA is:
   540    705   **
................................................................................
   541    706   **     * A varint. This varint contains the total number of bytes of content
   542    707   **       in the PMA (not including the varint itself).
   543    708   **
   544    709   **     * One or more records packed end-to-end in order of ascending keys. 
   545    710   **       Each record consists of a varint followed by a blob of data (the 
   546    711   **       key). The varint is the number of bytes in the blob of data.
   547    712   */
   548         -static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
          713  +static int vdbeSorterListToPMA(sqlite3 *db, const VdbeCursor *pCsr){
   549    714     int rc = SQLITE_OK;             /* Return code */
   550    715     VdbeSorter *pSorter = pCsr->pSorter;
          716  +  FileWriter writer;
          717  +
          718  +  memset(&writer, 0, sizeof(FileWriter));
   551    719   
   552    720     if( pSorter->nInMemory==0 ){
   553    721       assert( pSorter->pRecord==0 );
   554    722       return rc;
   555    723     }
   556    724   
   557    725     rc = vdbeSorterSort(pCsr);
................................................................................
   561    729       rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
   562    730       assert( rc!=SQLITE_OK || pSorter->pTemp1 );
   563    731       assert( pSorter->iWriteOff==0 );
   564    732       assert( pSorter->nPMA==0 );
   565    733     }
   566    734   
   567    735     if( rc==SQLITE_OK ){
   568         -    i64 iOff = pSorter->iWriteOff;
   569    736       SorterRecord *p;
   570    737       SorterRecord *pNext = 0;
   571         -    static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
   572    738   
          739  +    fileWriterInit(db, pSorter->pTemp1, &writer, pSorter->iWriteOff);
   573    740       pSorter->nPMA++;
   574         -    rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
   575         -    for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
          741  +    fileWriterWriteVarint(&writer, pSorter->nInMemory);
          742  +    for(p=pSorter->pRecord; p; p=pNext){
   576    743         pNext = p->pNext;
   577         -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
   578         -
   579         -      if( rc==SQLITE_OK ){
   580         -        rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
   581         -        iOff += p->nVal;
   582         -      }
   583         -
          744  +      fileWriterWriteVarint(&writer, p->nVal);
          745  +      fileWriterWrite(&writer, p->pVal, p->nVal);
   584    746         sqlite3DbFree(db, p);
   585    747       }
   586         -
   587         -    /* This assert verifies that unless an error has occurred, the size of 
   588         -    ** the PMA on disk is the same as the expected size stored in
   589         -    ** pSorter->nInMemory. */ 
   590         -    assert( rc!=SQLITE_OK || pSorter->nInMemory==(
   591         -          iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
   592         -    ));
   593         -
   594         -    pSorter->iWriteOff = iOff;
   595         -    if( rc==SQLITE_OK ){
   596         -      /* Terminate each file with 8 extra bytes so that from any offset
   597         -      ** in the file we can always read 9 bytes without a SHORT_READ error */
   598         -      rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
   599         -    }
   600    748       pSorter->pRecord = p;
          749  +    rc = fileWriterFinish(db, &writer, &pSorter->iWriteOff);
   601    750     }
   602    751   
   603    752     return rc;
   604    753   }
   605    754   
   606    755   /*
   607    756   ** Add a record to the sorter.
   608    757   */
   609    758   int sqlite3VdbeSorterWrite(
   610    759     sqlite3 *db,                    /* Database handle */
   611         -  VdbeCursor *pCsr,               /* Sorter cursor */
          760  +  const VdbeCursor *pCsr,               /* Sorter cursor */
   612    761     Mem *pVal                       /* Memory cell containing record */
   613    762   ){
   614    763     VdbeSorter *pSorter = pCsr->pSorter;
   615    764     int rc = SQLITE_OK;             /* Return Code */
   616    765     SorterRecord *pNew;             /* New list element */
   617    766   
   618    767     assert( pSorter );
................................................................................
   638    787     **   * The total memory allocated for the in-memory list is greater 
   639    788     **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
   640    789     */
   641    790     if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
   642    791           (pSorter->nInMemory>pSorter->mxPmaSize)
   643    792        || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
   644    793     )){
          794  +#ifdef SQLITE_DEBUG
          795  +    i64 nExpect = pSorter->iWriteOff
          796  +                + sqlite3VarintLen(pSorter->nInMemory)
          797  +                + pSorter->nInMemory;
          798  +#endif
   645    799       rc = vdbeSorterListToPMA(db, pCsr);
   646    800       pSorter->nInMemory = 0;
          801  +    assert( rc!=SQLITE_OK || (nExpect==pSorter->iWriteOff) );
   647    802     }
   648    803   
   649    804     return rc;
   650    805   }
   651    806   
   652    807   /*
   653    808   ** Helper function for sqlite3VdbeSorterRewind(). 
   654    809   */
   655    810   static int vdbeSorterInitMerge(
   656    811     sqlite3 *db,                    /* Database handle */
   657         -  VdbeCursor *pCsr,               /* Cursor handle for this sorter */
          812  +  const VdbeCursor *pCsr,         /* Cursor handle for this sorter */
   658    813     i64 *pnByte                     /* Sum of bytes in all opened PMAs */
   659    814   ){
   660    815     VdbeSorter *pSorter = pCsr->pSorter;
   661    816     int rc = SQLITE_OK;             /* Return code */
   662    817     int i;                          /* Used to iterator through aIter[] */
   663    818     i64 nByte = 0;                  /* Total bytes in all opened PMAs */
   664    819   
................................................................................
   680    835     return rc;
   681    836   }
   682    837   
   683    838   /*
   684    839   ** Once the sorter has been populated, this function is called to prepare
   685    840   ** for iterating through its contents in sorted order.
   686    841   */
   687         -int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
          842  +int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
   688    843     VdbeSorter *pSorter = pCsr->pSorter;
   689    844     int rc;                         /* Return code */
   690    845     sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
   691    846     i64 iWrite2 = 0;                /* Write offset for pTemp2 */
   692    847     int nIter;                      /* Number of iterators used */
   693    848     int nByte;                      /* Bytes of space required for aIter/aTree */
   694    849     int N = 2;                      /* Power of 2 >= nIter */
................................................................................
   700    855     ** from the in-memory list.  */
   701    856     if( pSorter->nPMA==0 ){
   702    857       *pbEof = !pSorter->pRecord;
   703    858       assert( pSorter->aTree==0 );
   704    859       return vdbeSorterSort(pCsr);
   705    860     }
   706    861   
   707         -  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
          862  +  /* Write the current in-memory list to a PMA. */
   708    863     rc = vdbeSorterListToPMA(db, pCsr);
   709    864     if( rc!=SQLITE_OK ) return rc;
   710    865   
   711    866     /* Allocate space for aIter[] and aTree[]. */
   712    867     nIter = pSorter->nPMA;
   713    868     if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
   714    869     assert( nIter>0 );
................................................................................
   722    877     do {
   723    878       int iNew;                     /* Index of new, merged, PMA */
   724    879   
   725    880       for(iNew=0; 
   726    881           rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; 
   727    882           iNew++
   728    883       ){
          884  +      int rc2;                    /* Return code from fileWriterFinish() */
          885  +      FileWriter writer;          /* Object used to write to disk */
   729    886         i64 nWrite;                 /* Number of bytes in new PMA */
          887  +
          888  +      memset(&writer, 0, sizeof(FileWriter));
   730    889   
   731    890         /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
   732    891         ** initialize an iterator for each of them and break out of the loop.
   733    892         ** These iterators will be incrementally merged as the VDBE layer calls
   734    893         ** sqlite3VdbeSorterNext().
   735    894         **
   736    895         ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
................................................................................
   745    904   
   746    905         /* Open the second temp file, if it is not already open. */
   747    906         if( pTemp2==0 ){
   748    907           assert( iWrite2==0 );
   749    908           rc = vdbeSorterOpenTempFile(db, &pTemp2);
   750    909         }
   751    910   
   752         -      if( rc==SQLITE_OK ){
   753         -        rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
   754         -      }
   755         -
   756    911         if( rc==SQLITE_OK ){
   757    912           int bEof = 0;
          913  +        fileWriterInit(db, pTemp2, &writer, iWrite2);
          914  +        fileWriterWriteVarint(&writer, nWrite);
   758    915           while( rc==SQLITE_OK && bEof==0 ){
   759         -          int nToWrite;
   760    916             VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
   761    917             assert( pIter->pFile );
   762         -          nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
   763         -          rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
   764         -          iWrite2 += nToWrite;
   765         -          if( rc==SQLITE_OK ){
   766         -            rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
   767         -          }
          918  +
          919  +          fileWriterWriteVarint(&writer, pIter->nKey);
          920  +          fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
          921  +          rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
   768    922           }
          923  +        rc2 = fileWriterFinish(db, &writer, &iWrite2);
          924  +        if( rc==SQLITE_OK ) rc = rc2;
   769    925         }
   770    926       }
   771    927   
   772    928       if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
   773    929         break;
   774    930       }else{
   775    931         sqlite3_file *pTmp = pSorter->pTemp1;
................................................................................
   788    944     *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
   789    945     return rc;
   790    946   }
   791    947   
   792    948   /*
   793    949   ** Advance to the next element in the sorter.
   794    950   */
   795         -int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
          951  +int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
   796    952     VdbeSorter *pSorter = pCsr->pSorter;
   797    953     int rc;                         /* Return code */
   798    954   
   799    955     if( pSorter->aTree ){
   800    956       int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
   801    957       int i;                        /* Index of aTree[] to recalculate */
   802    958   
................................................................................
   818    974   }
   819    975   
   820    976   /*
   821    977   ** Return a pointer to a buffer owned by the sorter that contains the 
   822    978   ** current key.
   823    979   */
   824    980   static void *vdbeSorterRowkey(
   825         -  VdbeSorter *pSorter,            /* Sorter object */
          981  +  const VdbeSorter *pSorter,      /* Sorter object */
   826    982     int *pnKey                      /* OUT: Size of current key in bytes */
   827    983   ){
   828    984     void *pKey;
   829    985     if( pSorter->aTree ){
   830    986       VdbeSorterIter *pIter;
   831    987       pIter = &pSorter->aIter[ pSorter->aTree[1] ];
   832    988       *pnKey = pIter->nKey;
................................................................................
   837    993     }
   838    994     return pKey;
   839    995   }
   840    996   
   841    997   /*
   842    998   ** Copy the current sorter key into the memory cell pOut.
   843    999   */
   844         -int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
         1000  +int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
   845   1001     VdbeSorter *pSorter = pCsr->pSorter;
   846   1002     void *pKey; int nKey;           /* Sorter key to copy into pOut */
   847   1003   
   848   1004     pKey = vdbeSorterRowkey(pSorter, &nKey);
   849   1005     if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
   850   1006       return SQLITE_NOMEM;
   851   1007     }
................................................................................
   863   1019   **
   864   1020   ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
   865   1021   ** Otherwise, set *pRes to a negative, zero or positive value if the
   866   1022   ** key in pVal is smaller than, equal to or larger than the current sorter
   867   1023   ** key.
   868   1024   */
   869   1025   int sqlite3VdbeSorterCompare(
   870         -  VdbeCursor *pCsr,               /* Sorter cursor */
         1026  +  const VdbeCursor *pCsr,         /* Sorter cursor */
   871   1027     Mem *pVal,                      /* Value to compare to current sorter key */
   872   1028     int *pRes                       /* OUT: Result of comparison */
   873   1029   ){
   874   1030     VdbeSorter *pSorter = pCsr->pSorter;
   875   1031     void *pKey; int nKey;           /* Sorter key to compare pVal with */
   876   1032   
   877   1033     pKey = vdbeSorterRowkey(pSorter, &nKey);
   878   1034     vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
   879   1035     return SQLITE_OK;
   880   1036   }
   881   1037   
   882   1038   #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */

Changes to src/vdbetrace.c.

   168    168   /*
   169    169   ** Allocate a new Explain object
   170    170   */
   171    171   void sqlite3ExplainBegin(Vdbe *pVdbe){
   172    172     if( pVdbe ){
   173    173       Explain *p;
   174    174       sqlite3BeginBenignMalloc();
   175         -    p = sqlite3_malloc( sizeof(Explain) );
          175  +    p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
   176    176       if( p ){
   177         -      memset(p, 0, sizeof(*p));
   178    177         p->pVdbe = pVdbe;
   179    178         sqlite3_free(pVdbe->pExplain);
   180    179         pVdbe->pExplain = p;
   181    180         sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
   182    181                             SQLITE_MAX_LENGTH);
   183    182         p->str.useMalloc = 2;
   184    183       }else{

Changes to src/walker.c.

   121    121   ** If the Walker does not have an xSelectCallback() then this routine
   122    122   ** is a no-op returning WRC_Continue.
   123    123   */
   124    124   int sqlite3WalkSelect(Walker *pWalker, Select *p){
   125    125     int rc;
   126    126     if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
   127    127     rc = WRC_Continue;
   128         -  while( p  ){
          128  +  pWalker->walkerDepth++;
          129  +  while( p ){
   129    130       rc = pWalker->xSelectCallback(pWalker, p);
   130    131       if( rc ) break;
   131         -    if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
   132         -    if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
          132  +    if( sqlite3WalkSelectExpr(pWalker, p)
          133  +     || sqlite3WalkSelectFrom(pWalker, p)
          134  +    ){
          135  +      pWalker->walkerDepth--;
          136  +      return WRC_Abort;
          137  +    }
   133    138       p = p->pPrior;
   134    139     }
          140  +  pWalker->walkerDepth--;
   135    141     return rc & WRC_Abort;
   136    142   }

Changes to src/where.c.

  3618   3618     /* Evaluate the equality constraints
  3619   3619     */
  3620   3620     assert( pIdx->nColumn>=nEq );
  3621   3621     for(j=0; j<nEq; j++){
  3622   3622       int r1;
  3623   3623       int k = pIdx->aiColumn[j];
  3624   3624       pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
  3625         -    if( NEVER(pTerm==0) ) break;
         3625  +    if( pTerm==0 ) break;
  3626   3626       /* The following true for indices with redundant columns. 
  3627   3627       ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
  3628   3628       testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
  3629   3629       testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3630   3630       r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
  3631   3631       if( r1!=regBase+j ){
  3632   3632         if( nReg==1 ){
................................................................................
  4293   4293       **          Return     2                # Jump back to the Gosub
  4294   4294       **
  4295   4295       **       B: <after the loop>
  4296   4296       **
  4297   4297       */
  4298   4298       WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
  4299   4299       SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
         4300  +    Index *pCov = 0;             /* Potential covering index (or NULL) */
         4301  +    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
  4300   4302   
  4301   4303       int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
  4302   4304       int regRowset = 0;                        /* Register for RowSet object */
  4303   4305       int regRowid = 0;                         /* Register holding rowid */
  4304   4306       int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
  4305   4307       int iRetInit;                             /* Address of regReturn init */
  4306   4308       int untestedTerms = 0;             /* Some terms not completely tested */
................................................................................
  4311   4313       assert( pTerm!=0 );
  4312   4314       assert( pTerm->eOperator==WO_OR );
  4313   4315       assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
  4314   4316       pOrWc = &pTerm->u.pOrInfo->wc;
  4315   4317       pLevel->op = OP_Return;
  4316   4318       pLevel->p1 = regReturn;
  4317   4319   
  4318         -    /* Set up a new SrcList ni pOrTab containing the table being scanned
         4320  +    /* Set up a new SrcList in pOrTab containing the table being scanned
  4319   4321       ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
  4320   4322       ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
  4321   4323       */
  4322   4324       if( pWInfo->nLevel>1 ){
  4323   4325         int nNotReady;                 /* The number of notReady tables */
  4324   4326         struct SrcList_item *origSrc;     /* Original list of tables */
  4325   4327         nNotReady = pWInfo->nLevel - iLevel - 1;
................................................................................
  4388   4390           if( pAndExpr ){
  4389   4391             pAndExpr->pLeft = pOrExpr;
  4390   4392             pOrExpr = pAndExpr;
  4391   4393           }
  4392   4394           /* Loop through table entries that match term pOrTerm. */
  4393   4395           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
  4394   4396                           WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
  4395         -                        WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
         4397  +                        WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
         4398  +        assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
  4396   4399           if( pSubWInfo ){
         4400  +          WhereLevel *pLvl;
  4397   4401             explainOneScan(
  4398   4402                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  4399   4403             );
  4400   4404             if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  4401   4405               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
  4402   4406               int r;
  4403   4407               r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
................................................................................
  4409   4413   
  4410   4414             /* The pSubWInfo->untestedTerms flag means that this OR term
  4411   4415             ** contained one or more AND term from a notReady table.  The
  4412   4416             ** terms from the notReady table could not be tested and will
  4413   4417             ** need to be tested later.
  4414   4418             */
  4415   4419             if( pSubWInfo->untestedTerms ) untestedTerms = 1;
         4420  +
         4421  +          /* If all of the OR-connected terms are optimized using the same
         4422  +          ** index, and the index is opened using the same cursor number
         4423  +          ** by each call to sqlite3WhereBegin() made by this loop, it may
         4424  +          ** be possible to use that index as a covering index.
         4425  +          **
         4426  +          ** If the call to sqlite3WhereBegin() above resulted in a scan that
         4427  +          ** uses an index, and this is either the first OR-connected term
         4428  +          ** processed or the index is the same as that used by all previous
         4429  +          ** terms, set pCov to the candidate covering index. Otherwise, set 
         4430  +          ** pCov to NULL to indicate that no candidate covering index will 
         4431  +          ** be available.
         4432  +          */
         4433  +          pLvl = &pSubWInfo->a[0];
         4434  +          if( (pLvl->plan.wsFlags & WHERE_INDEXED)!=0
         4435  +           && (pLvl->plan.wsFlags & WHERE_TEMP_INDEX)==0
         4436  +           && (ii==0 || pLvl->plan.u.pIdx==pCov)
         4437  +          ){
         4438  +            assert( pLvl->iIdxCur==iCovCur );
         4439  +            pCov = pLvl->plan.u.pIdx;
         4440  +          }else{
         4441  +            pCov = 0;
         4442  +          }
  4416   4443   
  4417   4444             /* Finish the loop through table entries that match term pOrTerm. */
  4418   4445             sqlite3WhereEnd(pSubWInfo);
  4419   4446           }
  4420   4447         }
  4421   4448       }
         4449  +    pLevel->u.pCovidx = pCov;
         4450  +    pLevel->iIdxCur = iCovCur;
  4422   4451       if( pAndExpr ){
  4423   4452         pAndExpr->pLeft = 0;
  4424   4453         sqlite3ExprDelete(pParse->db, pAndExpr);
  4425   4454       }
  4426   4455       sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
  4427   4456       sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
  4428   4457       sqlite3VdbeResolveLabel(v, iLoopBody);
................................................................................
  4632   4661   */
  4633   4662   WhereInfo *sqlite3WhereBegin(
  4634   4663     Parse *pParse,        /* The parser context */
  4635   4664     SrcList *pTabList,    /* A list of all tables to be scanned */
  4636   4665     Expr *pWhere,         /* The WHERE clause */
  4637   4666     ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
  4638   4667     ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
  4639         -  u16 wctrlFlags        /* One of the WHERE_* flags defined in sqliteInt.h */
         4668  +  u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
         4669  +  int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
  4640   4670   ){
  4641   4671     int i;                     /* Loop counter */
  4642   4672     int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
  4643   4673     int nTabList;              /* Number of elements in pTabList */
  4644   4674     WhereInfo *pWInfo;         /* Will become the return value of this function */
  4645   4675     Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
  4646   4676     Bitmask notReady;          /* Cursors that are not yet positioned */
................................................................................
  4952   4982         pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
  4953   4983       }
  4954   4984       andFlags &= bestPlan.plan.wsFlags;
  4955   4985       pLevel->plan = bestPlan.plan;
  4956   4986       testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
  4957   4987       testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
  4958   4988       if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
  4959         -      pLevel->iIdxCur = pParse->nTab++;
         4989  +      if( (wctrlFlags & WHERE_ONETABLE_ONLY) 
         4990  +       && (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0 
         4991  +      ){
         4992  +        pLevel->iIdxCur = iIdxCur;
         4993  +      }else{
         4994  +        pLevel->iIdxCur = pParse->nTab++;
         4995  +      }
  4960   4996       }else{
  4961   4997         pLevel->iIdxCur = -1;
  4962   4998       }
  4963   4999       notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
  4964   5000       pLevel->iFrom = (u8)bestJ;
  4965   5001       if( bestPlan.plan.nRow>=(double)1 ){
  4966   5002         pParse->nQueryLoop *= bestPlan.plan.nRow;
................................................................................
  5204   5240     */
  5205   5241     sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
  5206   5242   
  5207   5243     /* Close all of the cursors that were opened by sqlite3WhereBegin.
  5208   5244     */
  5209   5245     assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
  5210   5246     for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
         5247  +    Index *pIdx = 0;
  5211   5248       struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
  5212   5249       Table *pTab = pTabItem->pTab;
  5213   5250       assert( pTab!=0 );
  5214   5251       if( (pTab->tabFlags & TF_Ephemeral)==0
  5215   5252        && pTab->pSelect==0
  5216   5253        && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
  5217   5254       ){
................................................................................
  5233   5270       ** 
  5234   5271       ** Calls to the code generator in between sqlite3WhereBegin and
  5235   5272       ** sqlite3WhereEnd will have created code that references the table
  5236   5273       ** directly.  This loop scans all that code looking for opcodes
  5237   5274       ** that reference the table and converts them into opcodes that
  5238   5275       ** reference the index.
  5239   5276       */
  5240         -    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
         5277  +    if( pLevel->plan.wsFlags & WHERE_INDEXED ){
         5278  +      pIdx = pLevel->plan.u.pIdx;
         5279  +    }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
         5280  +      pIdx = pLevel->u.pCovidx;
         5281  +    }
         5282  +    if( pIdx && !db->mallocFailed){
  5241   5283         int k, j, last;
  5242   5284         VdbeOp *pOp;
  5243         -      Index *pIdx = pLevel->plan.u.pIdx;
  5244   5285   
  5245         -      assert( pIdx!=0 );
  5246   5286         pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
  5247   5287         last = sqlite3VdbeCurrentAddr(v);
  5248   5288         for(k=pWInfo->iTop; k<last; k++, pOp++){
  5249   5289           if( pOp->p1!=pLevel->iTabCur ) continue;
  5250   5290           if( pOp->opcode==OP_Column ){
  5251   5291             for(j=0; j<pIdx->nColumn; j++){
  5252   5292               if( pOp->p2==pIdx->aiColumn[j] ){

Added test/aggnested.test.

            1  +# 2012 August 23
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.
           12  +#
           13  +# This file implements tests for processing aggregate queries with 
           14  +# subqueries in which the subqueries hold the aggregate functions
           15  +# or in which the subqueries are themselves aggregate queries
           16  +#
           17  +
           18  +set testdir [file dirname $argv0]
           19  +source $testdir/tester.tcl
           20  +
           21  +do_test aggnested-1.1 {
           22  +  db eval {
           23  +    CREATE TABLE t1(a1 INTEGER);
           24  +    INSERT INTO t1 VALUES(1), (2), (3);
           25  +    CREATE TABLE t2(b1 INTEGER);
           26  +    INSERT INTO t2 VALUES(4), (5);
           27  +    SELECT (SELECT group_concat(a1,'x') FROM t2) FROM t1;
           28  +  }
           29  +} {1x2x3}
           30  +do_test aggnested-1.2 {
           31  +  db eval {
           32  +    SELECT
           33  +     (SELECT group_concat(a1,'x') || '-' || group_concat(b1,'y') FROM t2)
           34  +    FROM t1;
           35  +  }
           36  +} {1x2x3-4y5}
           37  +do_test aggnested-1.3 {
           38  +  db eval {
           39  +    SELECT (SELECT group_concat(b1,a1) FROM t2) FROM t1;
           40  +  }
           41  +} {415 425 435}
           42  +do_test aggnested-1.4 {
           43  +  db eval {
           44  +    SELECT (SELECT group_concat(a1,b1) FROM t2) FROM t1;
           45  +  }
           46  +} {151 252 353}
           47  +
           48  +
           49  +# This test case is a copy of the one in
           50  +# http://www.mail-archive.com/sqlite-users@sqlite.org/msg70787.html
           51  +#
           52  +do_test aggnested-2.0 {
           53  +  sqlite3 db2 :memory:
           54  +  db2 eval {
           55  +    CREATE TABLE t1 (A1 INTEGER NOT NULL,A2 INTEGER NOT NULL,A3 INTEGER NOT 
           56  +    NULL,A4 INTEGER NOT NULL,PRIMARY KEY(A1));
           57  +    REPLACE INTO t1 VALUES(1,11,111,1111);
           58  +    REPLACE INTO t1 VALUES(2,22,222,2222);
           59  +    REPLACE INTO t1 VALUES(3,33,333,3333);
           60  +    CREATE TABLE t2 (B1 INTEGER NOT NULL,B2 INTEGER NOT NULL,B3 INTEGER NOT 
           61  +    NULL,B4 INTEGER NOT NULL,PRIMARY KEY(B1));
           62  +    REPLACE INTO t2 VALUES(1,88,888,8888);
           63  +    REPLACE INTO t2 VALUES(2,99,999,9999);
           64  +    SELECT (SELECT GROUP_CONCAT(CASE WHEN a1=1 THEN'A' ELSE 'B' END) FROM t2),
           65  +            t1.* 
           66  +    FROM t1;
           67  +  }
           68  +} {A,B,B 3 33 333 3333}
           69  +db2 close
           70  +
           71  +finish_test

Added test/atof1.test.

            1  +# 2012 June 18
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# 
           12  +# Tests of the sqlite3AtoF() function.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +
           18  +if {![info exists __GNUC__]} {
           19  +  finish_test
           20  +  return
           21  +}
           22  +
           23  +expr srand(1)
           24  +for {set i 1} {$i<20000} {incr i} {
           25  +  set pow [expr {int((rand()-0.5)*100)}]
           26  +  set x [expr {pow((rand()-0.5)*2*rand(),$pow)}]
           27  +  set xf [format %.32e $x]
           28  +
           29  +  # Verify that text->real conversions get exactly same ieee754 floating-
           30  +  # point value in SQLite as they do in TCL.
           31  +  #
           32  +  do_test atof1-1.$i.1 {
           33  +    set y [db eval "SELECT $xf=\$x"]
           34  +    if {!$y} {
           35  +      puts -nonewline \173[db eval "SELECT real2hex($xf), real2hex(\$x)"]\175
           36  +      db eval "SELECT $xf+0.0 AS a, \$x AS b" {
           37  +        puts [format "\n%.60e\n%.60e\n%.60e" $x $a $b]
           38  +      }
           39  +    }
           40  +    set y
           41  +  } {1}
           42  +
           43  +  # Verify that round-trip real->text->real conversions using the quote()
           44  +  # function preserve the bits of the numeric value exactly.
           45  +  #
           46  +  do_test atof1-1.$i.2 {
           47  +    set y [db eval {SELECT $x=CAST(quote($x) AS real)}]
           48  +    if {!$y} {
           49  +      db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {}
           50  +      puts "\nIN:    $a $xf"
           51  +      puts [format {QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]]
           52  +      db eval {SELECT CAST(quote($x) AS real) c} {}
           53  +      puts "OUT:   $b [format %.32e $c]"
           54  +    }
           55  +    set y
           56  +  } {1}
           57  +}
           58  +
           59  +
           60  +finish_test

Changes to test/backup.test.

   419    419   } {B}
   420    420   do_test backup-4.3.2 {
   421    421     db2 cache flush
   422    422     sqlite3_close db2
   423    423   } {SQLITE_BUSY}
   424    424   do_test backup-4.3.3 {
   425    425     sqlite3_errmsg db2
   426         -} {unable to close due to unfinished backup operation}
          426  +} {unable to close due to unfinalized statements or unfinished backups}
   427    427   do_test backup-4.3.4 {
   428    428     B step 50
   429    429   } {SQLITE_DONE}
   430    430   do_test backup-4.3.5 {
   431    431     B finish
   432    432   } {SQLITE_OK}
   433    433   

Changes to test/date.test.

   147    147   datetest 2.51 {datetime('2003-10-22 12:24','nonsense')} NULL
   148    148   
   149    149   datetest 3.1 {strftime('%d','2003-10-31 12:34:56.432')} 31
   150    150   datetest 3.2.1 {strftime('pre%fpost','2003-10-31 12:34:56.432')} pre56.432post
   151    151   datetest 3.2.2 {strftime('%f','2003-10-31 12:34:59.9999999')} 59.999
   152    152   datetest 3.3 {strftime('%H','2003-10-31 12:34:56.432')} 12
   153    153   datetest 3.4 {strftime('%j','2003-10-31 12:34:56.432')} 304
   154         -datetest 3.5 {strftime('%J','2003-10-31 12:34:56.432')} 2452944.02426426
          154  +datetest 3.5 {strftime('%J','2003-10-31 12:34:56.432')} 2452944.024264259
   155    155   datetest 3.6 {strftime('%m','2003-10-31 12:34:56.432')} 10
   156    156   datetest 3.7 {strftime('%M','2003-10-31 12:34:56.432')} 34
   157    157   datetest 3.8.1 {strftime('%s','2003-10-31 12:34:56.432')} 1067603696
   158    158   datetest 3.8.2 {strftime('%s','2038-01-19 03:14:07')} 2147483647
   159    159   datetest 3.8.3 {strftime('%s','2038-01-19 03:14:08')} 2147483648
   160    160   datetest 3.8.4 {strftime('%s','2201-04-09 12:00:00')} 7298164800
   161    161   datetest 3.8.5 {strftime('%s','9999-12-31 23:59:59')} 253402300799

Changes to test/fts3atoken.test.

   170    170   
   171    171     do_execsql_test 5.1 {
   172    172       CREATE VIRTUAL TABLE x1 USING fts3(name,TOKENIZE icu en_US);
   173    173       insert into x1 (name) values (NULL);
   174    174       insert into x1 (name) values (NULL);
   175    175       delete from x1;
   176    176     }
          177  +
          178  +  proc cp_to_str {codepoint_list} {
          179  +    set fmt [string repeat %c [llength $codepoint_list]]
          180  +    eval [list format $fmt] $codepoint_list
          181  +  }
          182  +
          183  +  do_test 5.2 {
          184  +    set str [cp_to_str {19968 26085 32822 32645 27874 23433 20986}]
          185  +    execsql { INSERT INTO x1 VALUES($str) }
          186  +  } {}
   177    187   }
   178    188   
   179    189   
   180    190   do_test fts3token-internal {
   181    191     execsql { SELECT fts3_tokenizer_internal_test() }
   182    192   } {ok}
   183    193   
   184    194   
   185    195   finish_test
   186    196   
   187    197   

Changes to test/fts3auto.test.

    63     63     set tbl   [lindex $args [expr $nArg-2]]
    64     64     set match [lindex $args [expr $nArg-1]]
    65     65     set deferred [list]
    66     66   
    67     67     foreach {k v} [lrange $args 0 [expr $nArg-3]] {
    68     68       switch -- $k {
    69     69         -deferred {
    70         -        set deferred $v
           70  +        ifcapable fts4_deferred { set deferred $v }
    71     71         }
    72     72         default {
    73     73           error "bad option \"$k\": must be -deferred"
    74     74         }
    75     75       }
    76     76     }
    77     77   
................................................................................
   505    505       INSERT INTO t1(docid, x) VALUES(6, 'c a b');
   506    506     }
   507    507   
   508    508     set limit [fts3_make_deferrable t1 c]
   509    509   
   510    510     do_fts3query_test 3.$tn.2.1 t1 {a OR c}
   511    511   
   512         -  do_test 3.$tn.3 { 
   513         -    fts3_zero_long_segments t1 $limit 
   514         -  } {1}
          512  +  ifcapable fts4_deferred {
          513  +    do_test 3.$tn.3 { fts3_zero_long_segments t1 $limit } {1}
          514  +  }
   515    515   
   516    516     foreach {tn2 expr def} {
   517    517       1     {a NEAR c}            {}
   518    518       2     {a AND c}             c
   519    519       3     {"a c"}               c
   520    520       4     {"c a"}               c
   521    521       5     {"a c" NEAR/1 g}      {}
................................................................................
   546    546     do_fts3query_test 4.$tn.1.3 t1 {one NEAR/1 five}
   547    547     do_fts3query_test 4.$tn.1.4 t1 {one NEAR/2 five}
   548    548     do_fts3query_test 4.$tn.1.5 t1 {one NEAR/3 five}
   549    549   
   550    550     do_test 4.$tn.2 { 
   551    551       set limit [fts3_make_deferrable t1 five]
   552    552       execsql { INSERT INTO t1(t1) VALUES('optimize') }
   553         -    expr {[fts3_zero_long_segments t1 $limit]>0}
          553  +    ifcapable fts4_deferred {
          554  +      expr {[fts3_zero_long_segments t1 $limit]>0}
          555  +    } else {
          556  +      expr 1
          557  +    }
   554    558     } {1}
   555    559   
   556    560     do_fts3query_test 4.$tn.3.1 -deferred five t1 {one AND five}
   557    561     do_fts3query_test 4.$tn.3.2 -deferred five t1 {one NEAR five}
   558    562     do_fts3query_test 4.$tn.3.3 -deferred five t1 {one NEAR/1 five}
   559    563     do_fts3query_test 4.$tn.3.4 -deferred five t1 {one NEAR/2 five}
   560    564   

Changes to test/fts3defer.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   
    12     12   set testdir [file dirname $argv0]
    13     13   source $testdir/tester.tcl
    14     14   source $testdir/malloc_common.tcl
    15     15   
    16         -ifcapable !fts3 {
           16  +ifcapable !fts3||!fts4_deferred {
    17     17     finish_test
    18     18     return
    19     19   }
    20     20   
    21     21   set sqlite_fts3_enable_parentheses 1
    22     22   
    23     23   set fts3_simple_deferred_tokens_only 1

Changes to test/fts3defer2.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12     12   
    13     13   set testdir [file dirname $argv0]
    14     14   source $testdir/tester.tcl
    15     15   source $testdir/malloc_common.tcl
    16         -ifcapable !fts3 { finish_test ; return }
           16  +ifcapable !fts3||!fts4_deferred {
           17  +  finish_test 
           18  +  return
           19  +}
    17     20   
    18     21   set testprefix fts3defer2
    19     22   
    20     23   proc mit {blob} {
    21     24     set scan(littleEndian) i*
    22     25     set scan(bigEndian) I*
    23     26     binary scan $blob $scan($::tcl_platform(byteOrder)) r

Changes to test/fts3matchinfo.test.

   271    271   
   272    272   do_matchinfo_test 4.3.2 t5 {t5 MATCH 'a b'}         { s {2} }
   273    273   do_matchinfo_test 4.3.3 t5 {t5 MATCH 'a b a'}       { s {3} }
   274    274   do_matchinfo_test 4.3.4 t5 {t5 MATCH 'a a a'}       { s {3 1} }
   275    275   do_matchinfo_test 4.3.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} }
   276    276   do_matchinfo_test 4.3.6 t5 {t5 MATCH 'a OR b'}      { s {1 2 1 1} }
   277    277   
   278         -do_execsql_test 4.4.0 {
   279         -  INSERT INTO t5(t5) VALUES('optimize');
   280         -  UPDATE t5_segments 
   281         -  SET block = zeroblob(length(block)) 
   282         -  WHERE length(block)>10000;
          278  +do_execsql_test 4.4.0.1 { INSERT INTO t5(t5) VALUES('optimize') }
          279  +
          280  +ifcapable fts4_deferred {
          281  +  do_execsql_test 4.4.0.2 {
          282  +    UPDATE t5_segments 
          283  +    SET block = zeroblob(length(block)) 
          284  +    WHERE length(block)>10000;
          285  +  }
   283    286   }
   284    287   
   285    288   do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'}         { s {2} }
   286    289   do_matchinfo_test 4.4.1 t5 {t5 MATCH 'a a'}         { s {2 1} }
   287    290   do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'}         { s {2} }
   288    291   do_matchinfo_test 4.4.3 t5 {t5 MATCH 'a b a'}       { s {3} }
   289    292   do_matchinfo_test 4.4.4 t5 {t5 MATCH 'a a a'}       { s {3 1} }

Changes to test/fts4aa.test.

  1651   1651     }
  1652   1652   } {}
  1653   1653   do_test fts4aa-1.8 {
  1654   1654     db eval {
  1655   1655       SELECT docid FROM t1_docsize EXCEPT SELECT docid FROM t1
  1656   1656     }
  1657   1657   } {}
  1658         -do_test fts4aa-1.9 {
  1659         -  # Note: Token 'in' is being deferred in the following query. 
  1660         -  db eval {
  1661         -    SELECT docid, mit(matchinfo(t1, 'pcxnal')) FROM t1
  1662         -     WHERE t1 MATCH 'joseph died in egypt'
  1663         -     ORDER BY docid;
  1664         -  }
  1665         -} {1050026 {4 1 1 1 1 1 1 1 2 1 1 1 1 1 1 23 23}}
         1658  +ifcapable fts4_deferred {
         1659  +  do_test fts4aa-1.9 {
         1660  +    # Note: Token 'in' is being deferred in the following query. 
         1661  +    db eval {
         1662  +      SELECT docid, mit(matchinfo(t1, 'pcxnal')) FROM t1
         1663  +       WHERE t1 MATCH 'joseph died in egypt'
         1664  +       ORDER BY docid;
         1665  +    }
         1666  +  } {1050026 {4 1 1 1 1 1 1 1 2 1 1 1 1 1 1 23 23}}
         1667  +}
  1666   1668   
  1667   1669   # Should get the same search results from FTS3
  1668   1670   #
  1669   1671   do_test fts4aa-2.0 {
  1670   1672     db eval {
  1671   1673       DROP TABLE t1;
  1672   1674       CREATE VIRTUAL TABLE t1 USING fts3(words, tokenize porter);

Changes to test/fts4unicode.test.

   321    321     "0 hello\u0301world hello\u0301world 1 helloworld helloworld"
   322    322   
   323    323   do_unicode_token_test3 5.11 "tokenchars=\u0301" \
   324    324     "remove_diacritics=0"                         \
   325    325     "hello\u0301world \u0301helloworld"           \
   326    326     "0 hello\u0301world hello\u0301world 1 helloworld helloworld"
   327    327   
          328  +
          329  +#-------------------------------------------------------------------------
          330  +
          331  +proc do_tokenize {tokenizer txt} {
          332  +  set res [list]
          333  +  foreach {a b c} [db one {SELECT fts3_tokenizer_test($tokenizer, $txt)}] {
          334  +    lappend res $b
          335  +  }
          336  +  set res
          337  +}
          338  +
          339  +# Argument $lCodepoint must be a list of codepoints (integers) that 
          340  +# correspond to whitespace characters. This command creates a string
          341  +# $W from the codepoints, then tokenizes "${W}hello{$W}world${W}" 
          342  +# using tokenizer $tokenizer. The test passes if the tokenizer successfully
          343  +# extracts the two 5 character tokens.
          344  +#
          345  +proc do_isspace_test {tn tokenizer lCp} {
          346  +  set whitespace [format [string repeat %c [llength $lCp]] {*}$lCp] 
          347  +  set txt "${whitespace}hello${whitespace}world${whitespace}"
          348  +  uplevel [list do_test $tn [list do_tokenize $tokenizer $txt] {hello world}]
          349  +}
          350  +
          351  +set tokenizers [list unicode61]
          352  +ifcapable icu { lappend tokenizers icu }
          353  +
          354  +# Some tests to check that the tokenizers can both identify white-space 
          355  +# codepoints. All codepoints tested below are of type "Zs" in the
          356  +# UnicodeData.txt file.
          357  +foreach T $tokenizers {
          358  +  do_isspace_test 6.$T.1 $T    32
          359  +  do_isspace_test 6.$T.2 $T    160
          360  +  do_isspace_test 6.$T.3 $T    5760
          361  +  do_isspace_test 6.$T.4 $T    6158
          362  +  do_isspace_test 6.$T.5 $T    8192
          363  +  do_isspace_test 6.$T.6 $T    8193
          364  +  do_isspace_test 6.$T.7 $T    8194
          365  +  do_isspace_test 6.$T.8 $T    8195
          366  +  do_isspace_test 6.$T.9 $T    8196
          367  +  do_isspace_test 6.$T.10 $T    8197
          368  +  do_isspace_test 6.$T.11 $T    8198
          369  +  do_isspace_test 6.$T.12 $T    8199
          370  +  do_isspace_test 6.$T.13 $T    8200
          371  +  do_isspace_test 6.$T.14 $T    8201
          372  +  do_isspace_test 6.$T.15 $T    8202
          373  +  do_isspace_test 6.$T.16 $T    8239
          374  +  do_isspace_test 6.$T.17 $T    8287
          375  +  do_isspace_test 6.$T.18 $T   12288
          376  +
          377  +  do_isspace_test 6.$T.19 $T   {32 160 5760 6158}
          378  +  do_isspace_test 6.$T.19 $T   {8192 8193 8194 8195}
          379  +  do_isspace_test 6.$T.19 $T   {8196 8197 8198 8199}
          380  +  do_isspace_test 6.$T.19 $T   {8200 8201 8202 8239}
          381  +  do_isspace_test 6.$T.19 $T   {8287 12288}
          382  +}
          383  +
   328    384   
   329    385   finish_test
          386  +
   330    387   

Changes to test/func.test.

   308    308     do_test func-4.36 {
   309    309       execsql {SELECT round(99999999999994.5);}
   310    310     } {99999999999995.0}
   311    311     do_test func-4.37 {
   312    312       execsql {SELECT round(9999999999999.55,1);}
   313    313     } {9999999999999.6}
   314    314     do_test func-4.38 {
   315         -    execsql {SELECT round(9999999999999.555,2);}
          315  +    execsql {SELECT round(9999999999999.556,2);}
   316    316     } {9999999999999.56}
   317    317   }
   318