/ Check-in [09d6c91d]
Login

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

Overview
Comment:Pull in all the changes from trunk up through the version 3.7.5 release candidate 1.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 09d6c91dcff0699daec02de0cfd1cdc4e61b4f6b
User & Date: drh 2011-01-27 18:48:02
Context
2011-02-10
01:49
This is a version of the SQLite 3.7.5 release with Apple's changes for MacOS. check-in: 55d2e55b user: drh tags: apple-osx
2011-01-27
18:48
Pull in all the changes from trunk up through the version 3.7.5 release candidate 1. check-in: 09d6c91d user: drh tags: apple-osx
14:35
SQLite version 3.7.5 release candidate 1 check-in: 35ac78f5 user: drh tags: trunk
2010-12-07
15:49
Merge in all changes to the trunk through version 3.7.4rc3. check-in: d2ccf7fc user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

    55     55   #
    56     56   LIBREADLINE = @TARGET_READLINE_LIBS@
    57     57   
    58     58   # Should the database engine be compiled threadsafe
    59     59   #
    60     60   TCC += -DSQLITE_THREADSAFE=@SQLITE_THREADSAFE@
    61     61   
    62         -# Do threads override each others locks by default (1), or do we test (-1)
    63         -#
    64         -TCC += -DSQLITE_THREAD_OVERRIDE_LOCK=@THREADSOVERRIDELOCKS@
    65         -
    66     62   # Any target libraries which libsqlite must be linked against
    67     63   # 
    68     64   TLIBS = @LIBS@
    69     65   
    70     66   # Flags controlling use of the in memory btree implementation
    71     67   #
    72     68   # SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to

Changes to VERSION.

     1         -3.7.4
            1  +3.7.5

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.4.
            3  +# Generated by GNU Autoconf 2.62 for sqlite 3.7.5.
     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.4'
   747         -PACKAGE_STRING='sqlite 3.7.4'
          746  +PACKAGE_VERSION='3.7.5'
          747  +PACKAGE_STRING='sqlite 3.7.5'
   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>
................................................................................
   865    865   program_prefix
   866    866   VERSION
   867    867   RELEASE
   868    868   VERSION_NUMBER
   869    869   BUILD_CC
   870    870   SQLITE_THREADSAFE
   871    871   XTHREADCONNECT
   872         -THREADSOVERRIDELOCKS
   873    872   ALLOWRELEASE
   874    873   TEMP_STORE
   875    874   BUILD_EXEEXT
   876    875   SQLITE_OS_UNIX
   877    876   SQLITE_OS_WIN
   878    877   SQLITE_OS_OS2
   879    878   TARGET_EXEEXT
................................................................................
   908    907   enable_fast_install
   909    908   with_gnu_ld
   910    909   enable_libtool_lock
   911    910   enable_largefile
   912    911   with_hints
   913    912   enable_threadsafe
   914    913   enable_cross_thread_connections
   915         -enable_threads_override_locks
   916    914   enable_releasemode
   917    915   enable_tempstore
   918    916   enable_tcl
   919    917   with_tcl
   920    918   enable_readline
   921    919   with_readline_lib
   922    920   with_readline_inc
................................................................................
  1483   1481   #
  1484   1482   # Report the --help message.
  1485   1483   #
  1486   1484   if test "$ac_init_help" = "long"; then
  1487   1485     # Omit some internal or obsolete options to make the list less imposing.
  1488   1486     # This message is too long to be a string in the A/UX 3.1 sh.
  1489   1487     cat <<_ACEOF
  1490         -\`configure' configures sqlite 3.7.4 to adapt to many kinds of systems.
         1488  +\`configure' configures sqlite 3.7.5 to adapt to many kinds of systems.
  1491   1489   
  1492   1490   Usage: $0 [OPTION]... [VAR=VALUE]...
  1493   1491   
  1494   1492   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1495   1493   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1496   1494   
  1497   1495   Defaults for the options are specified in brackets.
................................................................................
  1548   1546     --build=BUILD     configure for building on BUILD [guessed]
  1549   1547     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1550   1548   _ACEOF
  1551   1549   fi
  1552   1550   
  1553   1551   if test -n "$ac_init_help"; then
  1554   1552     case $ac_init_help in
  1555         -     short | recursive ) echo "Configuration of sqlite 3.7.4:";;
         1553  +     short | recursive ) echo "Configuration of sqlite 3.7.5:";;
  1556   1554      esac
  1557   1555     cat <<\_ACEOF
  1558   1556   
  1559   1557   Optional Features:
  1560   1558     --disable-option-checking  ignore unrecognized --enable/--with options
  1561   1559     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1562   1560     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1565   1563     --enable-fast-install[=PKGS]
  1566   1564                             optimize for fast installation [default=yes]
  1567   1565     --disable-libtool-lock  avoid locking (might break parallel builds)
  1568   1566     --disable-largefile     omit support for large files
  1569   1567     --enable-threadsafe     Support threadsafe operation
  1570   1568     --enable-cross-thread-connections
  1571   1569                             Allow connection sharing across threads
  1572         -  --enable-threads-override-locks
  1573         -                          Threads can override each others locks
  1574   1570     --enable-releasemode    Support libtool link to release mode
  1575   1571     --enable-tempstore      Use an in-ram database for temporary tables
  1576   1572                             (never,no,yes,always)
  1577   1573     --disable-tcl           do not build TCL extension
  1578   1574     --disable-readline      disable readline support [default=detect]
  1579   1575     --enable-debug          enable debugging & verbose explain
  1580   1576     --disable-amalgamation  Disable the amalgamation and instead build all files
................................................................................
  1666   1662       cd "$ac_pwd" || { ac_status=$?; break; }
  1667   1663     done
  1668   1664   fi
  1669   1665   
  1670   1666   test -n "$ac_init_help" && exit $ac_status
  1671   1667   if $ac_init_version; then
  1672   1668     cat <<\_ACEOF
  1673         -sqlite configure 3.7.4
         1669  +sqlite configure 3.7.5
  1674   1670   generated by GNU Autoconf 2.62
  1675   1671   
  1676   1672   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
  1677   1673   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  1678   1674   This configure script is free software; the Free Software Foundation
  1679   1675   gives unlimited permission to copy, distribute and modify it.
  1680   1676   _ACEOF
  1681   1677     exit
  1682   1678   fi
  1683   1679   cat >config.log <<_ACEOF
  1684   1680   This file contains any messages produced by compilers while
  1685   1681   running configure, to aid debugging if configure makes a mistake.
  1686   1682   
  1687         -It was created by sqlite $as_me 3.7.4, which was
         1683  +It was created by sqlite $as_me 3.7.5, which was
  1688   1684   generated by GNU Autoconf 2.62.  Invocation command line was
  1689   1685   
  1690   1686     $ $0 $@
  1691   1687   
  1692   1688   _ACEOF
  1693   1689   exec 5>>config.log
  1694   1690   {
................................................................................
  3734   3730   { $as_echo "$as_me:$LINENO: checking the name lister ($NM) interface" >&5
  3735   3731   $as_echo_n "checking the name lister ($NM) interface... " >&6; }
  3736   3732   if test "${lt_cv_nm_interface+set}" = set; then
  3737   3733     $as_echo_n "(cached) " >&6
  3738   3734   else
  3739   3735     lt_cv_nm_interface="BSD nm"
  3740   3736     echo "int some_variable = 0;" > conftest.$ac_ext
  3741         -  (eval echo "\"\$as_me:3741: $ac_compile\"" >&5)
         3737  +  (eval echo "\"\$as_me:3737: $ac_compile\"" >&5)
  3742   3738     (eval "$ac_compile" 2>conftest.err)
  3743   3739     cat conftest.err >&5
  3744         -  (eval echo "\"\$as_me:3744: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
         3740  +  (eval echo "\"\$as_me:3740: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
  3745   3741     (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
  3746   3742     cat conftest.err >&5
  3747         -  (eval echo "\"\$as_me:3747: output\"" >&5)
         3743  +  (eval echo "\"\$as_me:3743: output\"" >&5)
  3748   3744     cat conftest.out >&5
  3749   3745     if $GREP 'External.*some_variable' conftest.out > /dev/null; then
  3750   3746       lt_cv_nm_interface="MS dumpbin"
  3751   3747     fi
  3752   3748     rm -f conftest*
  3753   3749   fi
  3754   3750   { $as_echo "$as_me:$LINENO: result: $lt_cv_nm_interface" >&5
................................................................................
  4962   4958   	;;
  4963   4959       esac
  4964   4960     fi
  4965   4961     rm -rf conftest*
  4966   4962     ;;
  4967   4963   *-*-irix6*)
  4968   4964     # Find out which ABI we are using.
  4969         -  echo '#line 4969 "configure"' > conftest.$ac_ext
         4965  +  echo '#line 4965 "configure"' > conftest.$ac_ext
  4970   4966     if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
  4971   4967     (eval $ac_compile) 2>&5
  4972   4968     ac_status=$?
  4973   4969     $as_echo "$as_me:$LINENO: \$? = $ac_status" >&5
  4974   4970     (exit $ac_status); }; then
  4975   4971       if test "$lt_cv_prog_gnu_ld" = yes; then
  4976   4972         case `/usr/bin/file conftest.$ac_objext` in
................................................................................
  6831   6827      # Note that $ac_compile itself does not contain backslashes and begins
  6832   6828      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6833   6829      # The option is referenced via a variable to avoid confusing sed.
  6834   6830      lt_compile=`echo "$ac_compile" | $SED \
  6835   6831      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  6836   6832      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  6837   6833      -e 's:$: $lt_compiler_flag:'`
  6838         -   (eval echo "\"\$as_me:6838: $lt_compile\"" >&5)
         6834  +   (eval echo "\"\$as_me:6834: $lt_compile\"" >&5)
  6839   6835      (eval "$lt_compile" 2>conftest.err)
  6840   6836      ac_status=$?
  6841   6837      cat conftest.err >&5
  6842         -   echo "$as_me:6842: \$? = $ac_status" >&5
         6838  +   echo "$as_me:6838: \$? = $ac_status" >&5
  6843   6839      if (exit $ac_status) && test -s "$ac_outfile"; then
  6844   6840        # The compiler can only warn and ignore the option if not recognized
  6845   6841        # So say no if there are warnings other than the usual output.
  6846   6842        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  6847   6843        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  6848   6844        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  6849   6845          lt_cv_prog_compiler_rtti_exceptions=yes
................................................................................
  7170   7166      # Note that $ac_compile itself does not contain backslashes and begins
  7171   7167      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7172   7168      # The option is referenced via a variable to avoid confusing sed.
  7173   7169      lt_compile=`echo "$ac_compile" | $SED \
  7174   7170      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7175   7171      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7176   7172      -e 's:$: $lt_compiler_flag:'`
  7177         -   (eval echo "\"\$as_me:7177: $lt_compile\"" >&5)
         7173  +   (eval echo "\"\$as_me:7173: $lt_compile\"" >&5)
  7178   7174      (eval "$lt_compile" 2>conftest.err)
  7179   7175      ac_status=$?
  7180   7176      cat conftest.err >&5
  7181         -   echo "$as_me:7181: \$? = $ac_status" >&5
         7177  +   echo "$as_me:7177: \$? = $ac_status" >&5
  7182   7178      if (exit $ac_status) && test -s "$ac_outfile"; then
  7183   7179        # The compiler can only warn and ignore the option if not recognized
  7184   7180        # So say no if there are warnings other than the usual output.
  7185   7181        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  7186   7182        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  7187   7183        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  7188   7184          lt_cv_prog_compiler_pic_works=yes
................................................................................
  7275   7271      # (2) before a word containing "conftest.", or (3) at the end.
  7276   7272      # Note that $ac_compile itself does not contain backslashes and begins
  7277   7273      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7278   7274      lt_compile=`echo "$ac_compile" | $SED \
  7279   7275      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7280   7276      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7281   7277      -e 's:$: $lt_compiler_flag:'`
  7282         -   (eval echo "\"\$as_me:7282: $lt_compile\"" >&5)
         7278  +   (eval echo "\"\$as_me:7278: $lt_compile\"" >&5)
  7283   7279      (eval "$lt_compile" 2>out/conftest.err)
  7284   7280      ac_status=$?
  7285   7281      cat out/conftest.err >&5
  7286         -   echo "$as_me:7286: \$? = $ac_status" >&5
         7282  +   echo "$as_me:7282: \$? = $ac_status" >&5
  7287   7283      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7288   7284      then
  7289   7285        # The compiler can only warn and ignore the option if not recognized
  7290   7286        # So say no if there are warnings
  7291   7287        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7292   7288        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7293   7289        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  7330   7326      # (2) before a word containing "conftest.", or (3) at the end.
  7331   7327      # Note that $ac_compile itself does not contain backslashes and begins
  7332   7328      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7333   7329      lt_compile=`echo "$ac_compile" | $SED \
  7334   7330      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7335   7331      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7336   7332      -e 's:$: $lt_compiler_flag:'`
  7337         -   (eval echo "\"\$as_me:7337: $lt_compile\"" >&5)
         7333  +   (eval echo "\"\$as_me:7333: $lt_compile\"" >&5)
  7338   7334      (eval "$lt_compile" 2>out/conftest.err)
  7339   7335      ac_status=$?
  7340   7336      cat out/conftest.err >&5
  7341         -   echo "$as_me:7341: \$? = $ac_status" >&5
         7337  +   echo "$as_me:7337: \$? = $ac_status" >&5
  7342   7338      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7343   7339      then
  7344   7340        # The compiler can only warn and ignore the option if not recognized
  7345   7341        # So say no if there are warnings
  7346   7342        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7347   7343        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7348   7344        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
 10143  10139   else
 10144  10140     	  if test "$cross_compiling" = yes; then :
 10145  10141     lt_cv_dlopen_self=cross
 10146  10142   else
 10147  10143     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
 10148  10144     lt_status=$lt_dlunknown
 10149  10145     cat > conftest.$ac_ext <<_LT_EOF
 10150         -#line 10150 "configure"
        10146  +#line 10146 "configure"
 10151  10147   #include "confdefs.h"
 10152  10148   
 10153  10149   #if HAVE_DLFCN_H
 10154  10150   #include <dlfcn.h>
 10155  10151   #endif
 10156  10152   
 10157  10153   #include <stdio.h>
................................................................................
 10239  10235   else
 10240  10236     	  if test "$cross_compiling" = yes; then :
 10241  10237     lt_cv_dlopen_self_static=cross
 10242  10238   else
 10243  10239     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
 10244  10240     lt_status=$lt_dlunknown
 10245  10241     cat > conftest.$ac_ext <<_LT_EOF
 10246         -#line 10246 "configure"
        10242  +#line 10242 "configure"
 10247  10243   #include "confdefs.h"
 10248  10244   
 10249  10245   #if HAVE_DLFCN_H
 10250  10246   #include <dlfcn.h>
 10251  10247   #endif
 10252  10248   
 10253  10249   #include <stdio.h>
................................................................................
 12575  12571   $as_echo "no" >&6; }
 12576  12572   else
 12577  12573     XTHREADCONNECT='-DSQLITE_ALLOW_XTHREAD_CONNECT=1'
 12578  12574     { $as_echo "$as_me:$LINENO: result: yes" >&5
 12579  12575   $as_echo "yes" >&6; }
 12580  12576   fi
 12581  12577   
 12582         -
 12583         -##########
 12584         -# Do we want to set threadsOverrideEachOthersLocks variable to be 1 (true) by
 12585         -# default. Normally, a test at runtime is performed to determine the
 12586         -# appropriate value of this variable. Use this option only if you're sure that
 12587         -# threads can safely override each others locks in all runtime situations.
 12588         -#
 12589         -# Check whether --enable-threads-override-locks was given.
 12590         -if test "${enable_threads_override_locks+set}" = set; then
 12591         -  enableval=$enable_threads_override_locks;
 12592         -else
 12593         -  enable_threads_override_locks=no
 12594         -fi
 12595         -
 12596         -{ $as_echo "$as_me:$LINENO: checking whether threads can override each others locks" >&5
 12597         -$as_echo_n "checking whether threads can override each others locks... " >&6; }
 12598         -if test "$enable_threads_override_locks" = "no"; then
 12599         -  THREADSOVERRIDELOCKS='-1'
 12600         -  { $as_echo "$as_me:$LINENO: result: no" >&5
 12601         -$as_echo "no" >&6; }
 12602         -else
 12603         -  THREADSOVERRIDELOCKS='1'
 12604         -  { $as_echo "$as_me:$LINENO: result: yes" >&5
 12605         -$as_echo "yes" >&6; }
 12606         -fi
 12607         -
 12608  12578   
 12609  12579   ##########
 12610  12580   # Do we want to support release
 12611  12581   #
 12612  12582   # Check whether --enable-releasemode was given.
 12613  12583   if test "${enable_releasemode+set}" = set; then
 12614  12584     enableval=$enable_releasemode;
................................................................................
 13968  13938   
 13969  13939   exec 6>&1
 13970  13940   
 13971  13941   # Save the log message, to keep $[0] and so on meaningful, and to
 13972  13942   # report actual input values of CONFIG_FILES etc. instead of their
 13973  13943   # values after options handling.
 13974  13944   ac_log="
 13975         -This file was extended by sqlite $as_me 3.7.4, which was
        13945  +This file was extended by sqlite $as_me 3.7.5, which was
 13976  13946   generated by GNU Autoconf 2.62.  Invocation command line was
 13977  13947   
 13978  13948     CONFIG_FILES    = $CONFIG_FILES
 13979  13949     CONFIG_HEADERS  = $CONFIG_HEADERS
 13980  13950     CONFIG_LINKS    = $CONFIG_LINKS
 13981  13951     CONFIG_COMMANDS = $CONFIG_COMMANDS
 13982  13952     $ $0 $@
................................................................................
 14021  13991   $config_commands
 14022  13992   
 14023  13993   Report bugs to <bug-autoconf@gnu.org>."
 14024  13994   
 14025  13995   _ACEOF
 14026  13996   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 14027  13997   ac_cs_version="\\
 14028         -sqlite config.status 3.7.4
        13998  +sqlite config.status 3.7.5
 14029  13999   configured by $0, generated by GNU Autoconf 2.62,
 14030  14000     with options \\"`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"
 14031  14001   
 14032  14002   Copyright (C) 2008 Free Software Foundation, Inc.
 14033  14003   This config.status script is free software; the Free Software Foundation
 14034  14004   gives unlimited permission to copy, distribute and modify it."
 14035  14005   

Changes to configure.ac.

   264    264     AC_MSG_RESULT([no])
   265    265   else
   266    266     XTHREADCONNECT='-DSQLITE_ALLOW_XTHREAD_CONNECT=1'
   267    267     AC_MSG_RESULT([yes])
   268    268   fi
   269    269   AC_SUBST(XTHREADCONNECT)
   270    270   
   271         -##########
   272         -# Do we want to set threadsOverrideEachOthersLocks variable to be 1 (true) by
   273         -# default. Normally, a test at runtime is performed to determine the
   274         -# appropriate value of this variable. Use this option only if you're sure that
   275         -# threads can safely override each others locks in all runtime situations.
   276         -#
   277         -AC_ARG_ENABLE(threads-override-locks, 
   278         -AC_HELP_STRING([--enable-threads-override-locks],[Threads can override each others locks]),,enable_threads_override_locks=no)
   279         -AC_MSG_CHECKING([whether threads can override each others locks])
   280         -if test "$enable_threads_override_locks" = "no"; then
   281         -  THREADSOVERRIDELOCKS='-1'
   282         -  AC_MSG_RESULT([no])
   283         -else
   284         -  THREADSOVERRIDELOCKS='1'
   285         -  AC_MSG_RESULT([yes])
   286         -fi
   287         -AC_SUBST(THREADSOVERRIDELOCKS)
   288         -
   289    271   ##########
   290    272   # Do we want to support release
   291    273   #
   292    274   AC_ARG_ENABLE(releasemode, 
   293    275   AC_HELP_STRING([--enable-releasemode],[Support libtool link to release mode]),,enable_releasemode=no)
   294    276   AC_MSG_CHECKING([whether to support shared library linked as release mode or not])
   295    277   if test "$enable_releasemode" = "no"; then

Changes to ext/fts3/fts3.c.

  3102   3102     Fts3Expr *pExpr,                /* Access this expressions doclist */
  3103   3103     sqlite3_int64 iDocid,           /* Docid associated with requested pos-list */
  3104   3104     int iCol                        /* Column of requested pos-list */
  3105   3105   ){
  3106   3106     assert( pExpr->isLoaded );
  3107   3107     if( pExpr->aDoclist ){
  3108   3108       char *pEnd = &pExpr->aDoclist[pExpr->nDoclist];
  3109         -    char *pCsr = pExpr->pCurrent;
         3109  +    char *pCsr;
  3110   3110   
         3111  +    if( pExpr->pCurrent==0 ){
         3112  +      pExpr->pCurrent = pExpr->aDoclist;
         3113  +      pExpr->iCurrent = 0;
         3114  +      pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent,&pExpr->iCurrent);
         3115  +    }
         3116  +    pCsr = pExpr->pCurrent;
  3111   3117       assert( pCsr );
         3118  +
  3112   3119       while( pCsr<pEnd ){
  3113   3120         if( pExpr->iCurrent<iDocid ){
  3114   3121           fts3PoslistCopy(0, &pCsr);
  3115   3122           if( pCsr<pEnd ){
  3116   3123             fts3GetDeltaVarint(&pCsr, &pExpr->iCurrent);
  3117   3124           }
  3118   3125           pExpr->pCurrent = pCsr;

Changes to ext/fts3/fts3_snippet.c.

   222    222   }
   223    223   
   224    224   /*
   225    225   ** This is an fts3ExprIterate() callback used while loading the doclists
   226    226   ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
   227    227   ** fts3ExprLoadDoclists().
   228    228   */
   229         -static int fts3ExprLoadDoclistsCb1(Fts3Expr *pExpr, int iPhrase, void *ctx){
          229  +static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
   230    230     int rc = SQLITE_OK;
   231    231     LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
   232    232   
   233    233     UNUSED_PARAMETER(iPhrase);
   234    234   
   235    235     p->nPhrase++;
   236    236     p->nToken += pExpr->pPhrase->nToken;
................................................................................
   242    242         rc = fts3ExprNearTrim(pExpr);
   243    243       }
   244    244     }
   245    245   
   246    246     return rc;
   247    247   }
   248    248   
   249         -/*
   250         -** This is an fts3ExprIterate() callback used while loading the doclists
   251         -** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
   252         -** fts3ExprLoadDoclists().
   253         -*/
   254         -static int fts3ExprLoadDoclistsCb2(Fts3Expr *pExpr, int iPhrase, void *ctx){
   255         -  UNUSED_PARAMETER(iPhrase);
   256         -  UNUSED_PARAMETER(ctx);
   257         -  if( pExpr->aDoclist ){
   258         -    pExpr->pCurrent = pExpr->aDoclist;
   259         -    pExpr->iCurrent = 0;
   260         -    pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent, &pExpr->iCurrent);
   261         -  }
   262         -  return SQLITE_OK;
   263         -}
   264         -
   265    249   /*
   266    250   ** Load the doclists for each phrase in the query associated with FTS3 cursor
   267    251   ** pCsr. 
   268    252   **
   269    253   ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable 
   270    254   ** phrases in the expression (all phrases except those directly or 
   271    255   ** indirectly descended from the right-hand-side of a NOT operator). If 
................................................................................
   276    260     Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
   277    261     int *pnPhrase,                  /* OUT: Number of phrases in query */
   278    262     int *pnToken                    /* OUT: Number of tokens in query */
   279    263   ){
   280    264     int rc;                         /* Return Code */
   281    265     LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
   282    266     sCtx.pCsr = pCsr;
   283         -  rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb1, (void *)&sCtx);
   284         -  if( rc==SQLITE_OK ){
   285         -    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb2, 0);
   286         -  }
          267  +  rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
   287    268     if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
   288    269     if( pnToken ) *pnToken = sCtx.nToken;
   289    270     return rc;
   290    271   }
   291    272   
   292    273   static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
   293    274     (*(int *)ctx)++;
................................................................................
   971    952     sqlite3_int64 nDoc;
   972    953   
   973    954     if( !*ppStmt ){
   974    955       int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
   975    956       if( rc!=SQLITE_OK ) return rc;
   976    957     }
   977    958     pStmt = *ppStmt;
          959  +  assert( sqlite3_data_count(pStmt)==1 );
   978    960   
   979    961     a = sqlite3_column_blob(pStmt, 0);
   980    962     a += sqlite3Fts3GetVarint(a, &nDoc);
   981    963     *pnDoc = (u32)nDoc;
   982    964   
   983    965     if( paLen ) *paLen = a;
   984    966     return SQLITE_OK;

Changes to ext/fts3/fts3_write.c.

  1098   1098         ** varints, where nCol is the number of columns in the FTS3 table.
  1099   1099         ** The first varint is the number of documents currently stored in
  1100   1100         ** the table. The following nCol varints contain the total amount of
  1101   1101         ** data stored in all rows of each column of the table, from left
  1102   1102         ** to right.
  1103   1103         */
  1104   1104         sqlite3_stmt *pStmt;
  1105         -      rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0);
         1105  +      sqlite3_int64 nDoc = 0;
         1106  +      sqlite3_int64 nByte = 0;
         1107  +      const char *a;
         1108  +      rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
  1106   1109         if( rc ) return rc;
  1107         -      if( sqlite3_step(pStmt)==SQLITE_ROW ){
  1108         -        sqlite3_int64 nDoc = 0;
  1109         -        sqlite3_int64 nByte = 0;
  1110         -        const char *a = sqlite3_column_blob(pStmt, 0);
  1111         -        if( a ){
  1112         -          const char *pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
  1113         -          a += sqlite3Fts3GetVarint(a, &nDoc);
  1114         -          while( a<pEnd ){
  1115         -            a += sqlite3Fts3GetVarint(a, &nByte);
  1116         -          }
         1110  +      a = sqlite3_column_blob(pStmt, 0);
         1111  +      if( a ){
         1112  +        const char *pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
         1113  +        a += sqlite3Fts3GetVarint(a, &nDoc);
         1114  +        while( a<pEnd ){
         1115  +          a += sqlite3Fts3GetVarint(a, &nByte);
  1117   1116           }
         1117  +      }
         1118  +      if( nDoc==0 || nByte==0 ){
         1119  +        sqlite3_reset(pStmt);
         1120  +        return SQLITE_CORRUPT;
         1121  +      }
  1118   1122   
  1119         -        pCsr->nRowAvg = (int)(((nByte / nDoc) + pgsz - 1) / pgsz);
  1120         -      }
         1123  +      pCsr->nRowAvg = (int)(((nByte / nDoc) + pgsz) / pgsz);
         1124  +      assert( pCsr->nRowAvg>0 ); 
  1121   1125         rc = sqlite3_reset(pStmt);
  1122         -      if( rc!=SQLITE_OK || pCsr->nRowAvg==0 ) return rc;
         1126  +      if( rc!=SQLITE_OK ) return rc;
  1123   1127       }
  1124   1128   
  1125   1129       /* Assume that a blob flows over onto overflow pages if it is larger
  1126   1130       ** than (pgsz-35) bytes in size (the file-format documentation
  1127   1131       ** confirms this).
  1128   1132       */
  1129   1133       for(iBlock=pReader->iStartBlock; iBlock<=pReader->iLeafEndBlock; iBlock++){
................................................................................
  2236   2240             fts3ColumnFilter(pFilter->iCol, &pList, &nList);
  2237   2241           }
  2238   2242   
  2239   2243           if( !isIgnoreEmpty || nList>0 ){
  2240   2244             nByte = sqlite3Fts3VarintLen(iDocid-iPrev) + (isRequirePos?nList+1:0);
  2241   2245             if( nDoclist+nByte>nAlloc ){
  2242   2246               char *aNew;
  2243         -            nAlloc = nDoclist+nByte*2;
         2247  +            nAlloc = (nDoclist+nByte)*2;
  2244   2248               aNew = sqlite3_realloc(aBuffer, nAlloc);
  2245   2249               if( !aNew ){
  2246   2250                 rc = SQLITE_NOMEM;
  2247   2251                 goto finished;
  2248   2252               }
  2249   2253               aBuffer = aNew;
  2250   2254             }

Changes to src/analyze.c.

   229    229       ** the index b-tree.  */
   230    230       endOfLoop = sqlite3VdbeMakeLabel(v);
   231    231       sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
   232    232       topOfLoop = sqlite3VdbeCurrentAddr(v);
   233    233       sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
   234    234   
   235    235       for(i=0; i<nCol; i++){
          236  +      CollSeq *pColl;
   236    237         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
   237         -#ifdef SQLITE_ENABLE_STAT2
   238    238         if( i==0 ){
          239  +#ifdef SQLITE_ENABLE_STAT2
   239    240           /* Check if the record that cursor iIdxCur points to contains a
   240    241           ** value that should be stored in the sqlite_stat2 table. If so,
   241    242           ** store it.  */
   242    243           int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
   243    244           assert( regTabname+1==regIdxname 
   244    245                && regTabname+2==regSampleno
   245    246                && regTabname+3==regCol
................................................................................
   260    261           sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
   261    262           sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
   262    263           sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
   263    264           sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
   264    265   
   265    266           sqlite3VdbeJumpHere(v, ne);
   266    267           sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
   267         -      }
   268    268   #endif
   269    269   
   270         -      sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
   271         -      /**** TODO:  add collating sequence *****/
   272         -      sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
          270  +        /* Always record the very first row */
          271  +        sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
          272  +      }
          273  +      assert( pIdx->azColl!=0 );
          274  +      assert( pIdx->azColl[i]!=0 );
          275  +      pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
          276  +      sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
          277  +                       (char*)pColl, P4_COLLSEQ);
          278  +      sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
   273    279       }
   274    280       if( db->mallocFailed ){
   275    281         /* If a malloc failure has occurred, then the result of the expression 
   276    282         ** passed as the second argument to the call to sqlite3VdbeJumpHere() 
   277    283         ** below may be negative. Which causes an assert() to fail (or an
   278    284         ** out-of-bounds write if SQLITE_DEBUG is not defined).  */
   279    285         return;
   280    286       }
   281    287       sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
   282    288       for(i=0; i<nCol; i++){
   283         -      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-(nCol*2));
          289  +      int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
          290  +      if( i==0 ){
          291  +        sqlite3VdbeJumpHere(v, addr2-1);  /* Set jump dest for the OP_IfNot */
          292  +      }
          293  +      sqlite3VdbeJumpHere(v, addr2);      /* Set jump dest for the OP_Ne */
   284    294         sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
   285    295         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
   286    296       }
   287    297   
   288    298       /* End of the analysis loop. */
   289    299       sqlite3VdbeResolveLabel(v, endOfLoop);
   290    300       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
................................................................................
   622    632       }else{
   623    633         rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
   624    634         sqlite3DbFree(db, zSql);
   625    635       }
   626    636   
   627    637       if( rc==SQLITE_OK ){
   628    638         while( sqlite3_step(pStmt)==SQLITE_ROW ){
   629         -        char *zIndex = (char *)sqlite3_column_text(pStmt, 0);
   630         -        Index *pIdx = sqlite3FindIndex(db, zIndex, sInfo.zDatabase);
          639  +        char *zIndex;   /* Index name */
          640  +        Index *pIdx;    /* Pointer to the index object */
          641  +
          642  +        zIndex = (char *)sqlite3_column_text(pStmt, 0);
          643  +        pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
   631    644           if( pIdx ){
   632    645             int iSample = sqlite3_column_int(pStmt, 1);
   633    646             if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
   634    647               int eType = sqlite3_column_type(pStmt, 2);
   635    648   
   636    649               if( pIdx->aSample==0 ){
   637    650                 static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;

Changes to src/backup.c.

   112    112     if( i<0 ){
   113    113       sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
   114    114       return 0;
   115    115     }
   116    116   
   117    117     return pDb->aDb[i].pBt;
   118    118   }
          119  +
          120  +/*
          121  +** Attempt to set the page size of the destination to match the page size
          122  +** of the source.
          123  +*/
          124  +static int setDestPgsz(sqlite3_backup *p){
          125  +  int rc;
          126  +  rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
          127  +  return rc;
          128  +}
   119    129   
   120    130   /*
   121    131   ** Create an sqlite3_backup process to copy the contents of zSrcDb from
   122    132   ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
   123    133   ** a pointer to the new sqlite3_backup object.
   124    134   **
   125    135   ** If an error occurs, NULL is returned and an error code and error message
................................................................................
   166    176       p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
   167    177       p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
   168    178       p->pDestDb = pDestDb;
   169    179       p->pSrcDb = pSrcDb;
   170    180       p->iNext = 1;
   171    181       p->isAttached = 0;
   172    182   
   173         -    if( 0==p->pSrc || 0==p->pDest ){
   174         -      /* One (or both) of the named databases did not exist. An error has
   175         -      ** already been written into the pDestDb handle. All that is left
   176         -      ** to do here is free the sqlite3_backup structure.
          183  +    if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
          184  +      /* One (or both) of the named databases did not exist or an OOM
          185  +      ** error was hit.  The error has already been written into the
          186  +      ** pDestDb handle.  All that is left to do here is free the
          187  +      ** sqlite3_backup structure.
   177    188         */
   178    189         sqlite3_free(p);
   179    190         p = 0;
   180    191       }
   181    192     }
   182    193     if( p ){
   183    194       p->pSrc->nBackup++;
................................................................................
   426    437           **
   427    438           **   * Data stored on the pages immediately following the 
   428    439           **     pending-byte page in the source database may need to be
   429    440           **     copied into the destination database.
   430    441           */
   431    442           const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
   432    443           sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
          444  +        i64 iOff;
          445  +        i64 iEnd;
   433    446   
   434    447           assert( pFile );
   435    448           assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
   436    449                 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
   437    450              && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
   438    451           ));
   439         -        if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1))
   440         -         && SQLITE_OK==(rc = backupTruncateFile(pFile, iSize))
   441         -         && SQLITE_OK==(rc = sqlite3PagerSync(pDestPager))
          452  +
          453  +        /* This call ensures that all data required to recreate the original
          454  +        ** database has been stored in the journal for pDestPager and the
          455  +        ** journal synced to disk. So at this point we may safely modify
          456  +        ** the database file in any way, knowing that if a power failure
          457  +        ** occurs, the original database will be reconstructed from the 
          458  +        ** journal file.  */
          459  +        rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
          460  +
          461  +        /* Write the extra pages and truncate the database file as required. */
          462  +        iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
          463  +        for(
          464  +          iOff=PENDING_BYTE+pgszSrc; 
          465  +          rc==SQLITE_OK && iOff<iEnd; 
          466  +          iOff+=pgszSrc
   442    467           ){
   443         -          i64 iOff;
   444         -          i64 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
   445         -          for(
   446         -            iOff=PENDING_BYTE+pgszSrc; 
   447         -            rc==SQLITE_OK && iOff<iEnd; 
   448         -            iOff+=pgszSrc
   449         -          ){
   450         -            PgHdr *pSrcPg = 0;
   451         -            const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
   452         -            rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
   453         -            if( rc==SQLITE_OK ){
   454         -              u8 *zData = sqlite3PagerGetData(pSrcPg);
   455         -              rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
   456         -            }
   457         -            sqlite3PagerUnref(pSrcPg);
          468  +          PgHdr *pSrcPg = 0;
          469  +          const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
          470  +          rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
          471  +          if( rc==SQLITE_OK ){
          472  +            u8 *zData = sqlite3PagerGetData(pSrcPg);
          473  +            rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
   458    474             }
          475  +          sqlite3PagerUnref(pSrcPg);
          476  +        }
          477  +        if( rc==SQLITE_OK ){
          478  +          rc = backupTruncateFile(pFile, iSize);
          479  +        }
          480  +
          481  +        /* Sync the database file to disk. */
          482  +        if( rc==SQLITE_OK ){
          483  +          rc = sqlite3PagerSync(pDestPager);
   459    484           }
   460    485         }else{
   461    486           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
   462    487         }
   463    488     
   464    489         /* Finish committing the transaction to the destination database. */
   465    490         if( SQLITE_OK==rc

Changes to src/btree.c.

   914    914     pInfo->nHeader = n;
   915    915     testcase( nPayload==pPage->maxLocal );
   916    916     testcase( nPayload==pPage->maxLocal+1 );
   917    917     if( likely(nPayload<=pPage->maxLocal) ){
   918    918       /* This is the (easy) common case where the entire payload fits
   919    919       ** on the local page.  No overflow is required.
   920    920       */
   921         -    int nSize;          /* Total size of cell content in bytes */
   922         -    nSize = nPayload + n;
          921  +    if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
   923    922       pInfo->nLocal = (u16)nPayload;
   924    923       pInfo->iOverflow = 0;
   925         -    if( (nSize & ~3)==0 ){
   926         -      nSize = 4;        /* Minimum cell size is 4 */
   927         -    }
   928         -    pInfo->nSize = (u16)nSize;
   929    924     }else{
   930    925       /* If the payload will not fit completely on the local page, we have
   931    926       ** to decide how much to store locally and how much to spill onto
   932    927       ** overflow pages.  The strategy is to minimize the amount of unused
   933    928       ** space on overflow pages while keeping the amount of local storage
   934    929       ** in between minLocal and maxLocal.
   935    930       **
................................................................................
  2382   2377         pBt->usableSize = usableSize;
  2383   2378         pBt->pageSize = pageSize;
  2384   2379         freeTempSpace(pBt);
  2385   2380         rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
  2386   2381                                      pageSize-usableSize);
  2387   2382         return rc;
  2388   2383       }
  2389         -    if( nPageHeader>nPageFile ){
         2384  +    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPageHeader>nPageFile ){
  2390   2385         rc = SQLITE_CORRUPT_BKPT;
  2391   2386         goto page1_init_failed;
  2392   2387       }
  2393   2388       if( usableSize<480 ){
  2394   2389         goto page1_init_failed;
  2395   2390       }
  2396   2391       pBt->pageSize = pageSize;

Changes to src/btreeInt.h.

   410    410     u8 secureDelete;      /* True if secure_delete is enabled */
   411    411     u8 initiallyEmpty;    /* Database is empty at start of transaction */
   412    412     u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
   413    413   #ifndef SQLITE_OMIT_AUTOVACUUM
   414    414     u8 autoVacuum;        /* True if auto-vacuum is enabled */
   415    415     u8 incrVacuum;        /* True if incr-vacuum is enabled */
   416    416   #endif
          417  +  u8 inTransaction;     /* Transaction state */
          418  +  u8 doNotUseWAL;       /* If true, do not open write-ahead-log file */
   417    419     u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
   418    420     u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
   419    421     u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
   420    422     u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
   421         -  u8 inTransaction;     /* Transaction state */
   422         -  u8 doNotUseWAL;       /* If true, do not open write-ahead-log file */
   423    423     u32 pageSize;         /* Total number of bytes on a page */
   424    424     u32 usableSize;       /* Number of usable bytes on each page */
   425    425     int nTransaction;     /* Number of open transactions (read + write) */
   426    426     u32 nPage;            /* Number of pages in the database */
   427    427     void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
   428    428     void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
   429    429     sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
................................................................................
   442    442   /*
   443    443   ** An instance of the following structure is used to hold information
   444    444   ** about a cell.  The parseCellPtr() function fills in this structure
   445    445   ** based on information extract from the raw disk page.
   446    446   */
   447    447   typedef struct CellInfo CellInfo;
   448    448   struct CellInfo {
   449         -  u8 *pCell;     /* Pointer to the start of cell content */
   450    449     i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
          450  +  u8 *pCell;     /* Pointer to the start of cell content */
   451    451     u32 nData;     /* Number of bytes of data */
   452    452     u32 nPayload;  /* Total amount of payload */
   453    453     u16 nHeader;   /* Size of the cell content header in bytes */
   454    454     u16 nLocal;    /* Amount of payload held locally */
   455    455     u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
   456    456     u16 nSize;     /* Size of the cell content on the main b-tree page */
   457    457   };
................................................................................
   485    485     Btree *pBtree;            /* The Btree to which this cursor belongs */
   486    486     BtShared *pBt;            /* The BtShared this cursor points to */
   487    487     BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
   488    488     struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
   489    489     Pgno pgnoRoot;            /* The root page of this tree */
   490    490     sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
   491    491     CellInfo info;            /* A parse of the cell we are pointing at */
          492  +  i64 nKey;        /* Size of pKey, or last integer key */
          493  +  void *pKey;      /* Saved key that was cursor's last known position */
          494  +  int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
   492    495     u8 wrFlag;                /* True if writable */
   493    496     u8 atLast;                /* Cursor pointing to the last entry */
   494    497     u8 validNKey;             /* True if info.nKey is valid */
   495    498     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
   496         -  void *pKey;      /* Saved key that was cursor's last known position */
   497         -  i64 nKey;        /* Size of pKey, or last integer key */
   498         -  int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
   499    499   #ifndef SQLITE_OMIT_INCRBLOB
   500         -  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
   501    500     Pgno *aOverflow;          /* Cache of overflow page locations */
          501  +  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
   502    502   #endif
   503    503     i16 iPage;                            /* Index of current page in apPage */
   504         -  MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
   505    504     u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
          505  +  MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
   506    506   };
   507    507   
   508    508   /*
   509    509   ** Potential values for BtCursor.eState.
   510    510   **
   511    511   ** CURSOR_VALID:
   512    512   **   Cursor points to a valid entry. getPayload() etc. may be called.

Changes to src/ctime.c.

   169    169     "OMIT_AUTOINCREMENT",
   170    170   #endif
   171    171   #ifdef SQLITE_OMIT_AUTOINIT
   172    172     "OMIT_AUTOINIT",
   173    173   #endif
   174    174   #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
   175    175     "OMIT_AUTOMATIC_INDEX",
          176  +#endif
          177  +#ifdef SQLITE_OMIT_AUTORESET
          178  +  "OMIT_AUTORESET",
   176    179   #endif
   177    180   #ifdef SQLITE_OMIT_AUTOVACUUM
   178    181     "OMIT_AUTOVACUUM",
   179    182   #endif
   180    183   #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
   181    184     "OMIT_BETWEEN_OPTIMIZATION",
   182    185   #endif

Changes to src/func.c.

  1449   1449   void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
  1450   1450     struct compareInfo *pInfo;
  1451   1451     if( caseSensitive ){
  1452   1452       pInfo = (struct compareInfo*)&likeInfoAlt;
  1453   1453     }else{
  1454   1454       pInfo = (struct compareInfo*)&likeInfoNorm;
  1455   1455     }
  1456         -  sqlite3CreateFunc(db, "like", 2, SQLITE_ANY, pInfo, likeFunc, 0, 0, 0);
  1457         -  sqlite3CreateFunc(db, "like", 3, SQLITE_ANY, pInfo, likeFunc, 0, 0, 0);
  1458         -  sqlite3CreateFunc(db, "glob", 2, SQLITE_ANY, 
         1456  +  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
         1457  +  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
         1458  +  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
  1459   1459         (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
  1460   1460     setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
  1461   1461     setLikeOptFlag(db, "like", 
  1462   1462         caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
  1463   1463   }
  1464   1464   
  1465   1465   /*

Changes to src/main.c.

   819    819       /* SQLITE_BUSY        */ "database is locked",
   820    820       /* SQLITE_LOCKED      */ "database table is locked",
   821    821       /* SQLITE_NOMEM       */ "out of memory",
   822    822       /* SQLITE_READONLY    */ "attempt to write a readonly database",
   823    823       /* SQLITE_INTERRUPT   */ "interrupted",
   824    824       /* SQLITE_IOERR       */ "disk I/O error",
   825    825       /* SQLITE_CORRUPT     */ "database disk image is malformed",
   826         -    /* SQLITE_NOTFOUND    */ 0,
          826  +    /* SQLITE_NOTFOUND    */ "unknown operation",
   827    827       /* SQLITE_FULL        */ "database or disk is full",
   828    828       /* SQLITE_CANTOPEN    */ "unable to open database file",
   829    829       /* SQLITE_PROTOCOL    */ "locking protocol",
   830    830       /* SQLITE_EMPTY       */ "table contains no data",
   831    831       /* SQLITE_SCHEMA      */ "database schema has changed",
   832    832       /* SQLITE_TOOBIG      */ "string or blob too big",
   833    833       /* SQLITE_CONSTRAINT  */ "constraint failed",
................................................................................
  1831   1831   #endif
  1832   1832   #ifdef SQLITE_ENABLE_LOAD_EXTENSION
  1833   1833                    | SQLITE_LoadExtension
  1834   1834   #endif
  1835   1835   #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  1836   1836                    | SQLITE_RecTriggers
  1837   1837   #endif
         1838  +#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
         1839  +                 | SQLITE_ForeignKeys
         1840  +#endif
  1838   1841         ;
  1839   1842     sqlite3HashInit(&db->aCollSeq);
  1840   1843   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1841   1844     sqlite3HashInit(&db->aModule);
  1842   1845   #endif
  1843   1846   
  1844   1847     db->pVfs = sqlite3_vfs_find(zVfs);
................................................................................
  2382   2385         fd = sqlite3PagerFile(pPager);
  2383   2386         assert( fd!=0 );
  2384   2387         if( op==SQLITE_FCNTL_FILE_POINTER ){
  2385   2388           *(sqlite3_file**)pArg = fd;
  2386   2389           rc = SQLITE_OK;
  2387   2390         }else if( fd->pMethods ){
  2388   2391           rc = sqlite3OsFileControl(fd, op, pArg);
         2392  +      }else{
         2393  +        rc = SQLITE_NOTFOUND;
  2389   2394         }
  2390   2395         sqlite3BtreeLeave(pBtree);
  2391   2396       }
  2392   2397     }
  2393   2398     sqlite3_mutex_leave(db->mutex);
  2394   2399     return rc;   
  2395   2400   }

Changes to src/malloc.c.

   612    612     assert( db==0 || db->pnBytesFreed==0 );
   613    613   #ifndef SQLITE_OMIT_LOOKASIDE
   614    614     if( db ){
   615    615       LookasideSlot *pBuf;
   616    616       if( db->mallocFailed ){
   617    617         return 0;
   618    618       }
   619         -    if( db->lookaside.bEnabled && n<=db->lookaside.sz
   620         -         && (pBuf = db->lookaside.pFree)!=0 ){
   621         -      db->lookaside.pFree = pBuf->pNext;
   622         -      db->lookaside.nOut++;
   623         -      if( db->lookaside.nOut>db->lookaside.mxOut ){
   624         -        db->lookaside.mxOut = db->lookaside.nOut;
          619  +    if( db->lookaside.bEnabled ){
          620  +      if( n>db->lookaside.sz ){
          621  +        db->lookaside.anStat[1]++;
          622  +      }else if( (pBuf = db->lookaside.pFree)==0 ){
          623  +        db->lookaside.anStat[2]++;
          624  +      }else{
          625  +        db->lookaside.pFree = pBuf->pNext;
          626  +        db->lookaside.nOut++;
          627  +        db->lookaside.anStat[0]++;
          628  +        if( db->lookaside.nOut>db->lookaside.mxOut ){
          629  +          db->lookaside.mxOut = db->lookaside.nOut;
          630  +        }
          631  +        return (void*)pBuf;
   625    632         }
   626         -      return (void*)pBuf;
   627    633       }
   628    634     }
   629    635   #else
   630    636     if( db && db->mallocFailed ){
   631    637       return 0;
   632    638     }
   633    639   #endif

Changes to src/mutex_unix.c.

    95     95   ** <li>  SQLITE_MUTEX_FAST
    96     96   ** <li>  SQLITE_MUTEX_RECURSIVE
    97     97   ** <li>  SQLITE_MUTEX_STATIC_MASTER
    98     98   ** <li>  SQLITE_MUTEX_STATIC_MEM
    99     99   ** <li>  SQLITE_MUTEX_STATIC_MEM2
   100    100   ** <li>  SQLITE_MUTEX_STATIC_PRNG
   101    101   ** <li>  SQLITE_MUTEX_STATIC_LRU
   102         -** <li>  SQLITE_MUTEX_STATIC_LRU2
          102  +** <li>  SQLITE_MUTEX_STATIC_PMEM
   103    103   ** </ul>
   104    104   **
   105    105   ** The first two constants cause sqlite3_mutex_alloc() to create
   106    106   ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   107    107   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   108    108   ** The mutex implementation does not need to make a distinction
   109    109   ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does

Changes to src/mutex_w32.c.

   152    152   ** <li>  SQLITE_MUTEX_FAST
   153    153   ** <li>  SQLITE_MUTEX_RECURSIVE
   154    154   ** <li>  SQLITE_MUTEX_STATIC_MASTER
   155    155   ** <li>  SQLITE_MUTEX_STATIC_MEM
   156    156   ** <li>  SQLITE_MUTEX_STATIC_MEM2
   157    157   ** <li>  SQLITE_MUTEX_STATIC_PRNG
   158    158   ** <li>  SQLITE_MUTEX_STATIC_LRU
   159         -** <li>  SQLITE_MUTEX_STATIC_LRU2
          159  +** <li>  SQLITE_MUTEX_STATIC_PMEM
   160    160   ** </ul>
   161    161   **
   162    162   ** The first two constants cause sqlite3_mutex_alloc() to create
   163    163   ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   164    164   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   165    165   ** The mutex implementation does not need to make a distinction
   166    166   ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does

Changes to src/os_os2.c.

   529    529       case SQLITE_FCNTL_LOCKSTATE: {
   530    530         *(int*)pArg = ((os2File*)id)->locktype;
   531    531         OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
   532    532                   ((os2File*)id)->h, ((os2File*)id)->locktype ));
   533    533         return SQLITE_OK;
   534    534       }
   535    535     }
   536         -  return SQLITE_ERROR;
          536  +  return SQLITE_NOTFOUND;
   537    537   }
   538    538   
   539    539   /*
   540    540   ** Return the sector size in bytes of the underlying block device for
   541    541   ** the specified file. This is almost always 512 bytes, but may be
   542    542   ** larger for some devices.
   543    543   **

Changes to src/os_unix.c.

   115    115   #include <sys/types.h>
   116    116   #include <sys/stat.h>
   117    117   #include <fcntl.h>
   118    118   #include <unistd.h>
   119    119   #include <time.h>
   120    120   #include <sys/time.h>
   121    121   #include <errno.h>
          122  +#ifndef SQLITE_OMIT_WAL
   122    123   #include <sys/mman.h>
          124  +#endif
   123    125   
   124    126   #if SQLITE_ENABLE_LOCKING_STYLE
   125    127   # include <sys/ioctl.h>
   126    128   # if OS_VXWORKS
   127    129   #  include <semaphore.h>
   128    130   #  include <limits.h>
   129    131   # else
................................................................................
  3363   3365         }
  3364   3366         if( trc ){
  3365   3367           return trc;
  3366   3368         }
  3367   3369         return rc;
  3368   3370       }
  3369   3371   #endif /* (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__) */
         3372  +    case SQLITE_FCNTL_SYNC_OMITTED: {
         3373  +      return SQLITE_OK;  /* A no-op */
         3374  +    }
  3370   3375     }
  3371         -  return SQLITE_ERROR;
         3376  +  return SQLITE_NOTFOUND;
  3372   3377   }
  3373   3378   
  3374   3379   /*
  3375   3380   ** Return the sector size in bytes of the underlying block device for
  3376   3381   ** the specified file. This is almost always 512 bytes, but may be
  3377   3382   ** larger for some devices.
  3378   3383   **
................................................................................
  5665   5670   /* Not always defined in the headers as it ought to be */
  5666   5671   extern int gethostuuid(uuid_t id, const struct timespec *wait);
  5667   5672   
  5668   5673   /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
  5669   5674   ** bytes of writable memory.
  5670   5675   */
  5671   5676   static int proxyGetHostID(unsigned char *pHostID, int *pError){
  5672         -  struct timespec timeout = {1, 0}; /* 1 sec timeout */
  5673         -  
  5674   5677     assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
  5675   5678     memset(pHostID, 0, PROXY_HOSTIDLEN);
  5676   5679   #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
  5677   5680                  && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
  5678         -  if( gethostuuid(pHostID, &timeout) ){
  5679         -    int err = errno;
  5680         -    if( pError ){
  5681         -      *pError = err;
         5681  +  {
         5682  +    static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
         5683  +    if( gethostuuid(pHostID, &timeout) ){
         5684  +      int err = errno;
         5685  +      if( pError ){
         5686  +        *pError = err;
         5687  +      }
         5688  +      return SQLITE_IOERR;
  5682   5689       }
  5683         -    return SQLITE_IOERR;
  5684   5690     }
  5685   5691   #endif
  5686   5692   #ifdef SQLITE_TEST
  5687   5693     /* simulate multiple hosts by creating unique hostid file paths */
  5688   5694     if( sqlite3_hostid_num != 0){
  5689   5695       pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
  5690   5696     }

Changes to src/os_win.c.

  1179   1179       case SQLITE_FCNTL_SIZE_HINT: {
  1180   1180         sqlite3_int64 sz = *(sqlite3_int64*)pArg;
  1181   1181         SimulateIOErrorBenign(1);
  1182   1182         winTruncate(id, sz);
  1183   1183         SimulateIOErrorBenign(0);
  1184   1184         return SQLITE_OK;
  1185   1185       }
         1186  +    case SQLITE_FCNTL_SYNC_OMITTED: {
         1187  +      return SQLITE_OK;
         1188  +    }
  1186   1189     }
  1187         -  return SQLITE_ERROR;
         1190  +  return SQLITE_NOTFOUND;
  1188   1191   }
  1189   1192   
  1190   1193   /*
  1191   1194   ** Return the sector size in bytes of the underlying block device for
  1192   1195   ** the specified file. This is almost always 512 bytes, but may be
  1193   1196   ** larger for some devices.
  1194   1197   **

Changes to src/pager.c.

  2745   2745     pPager->changeCountDone = pPager->tempFile;
  2746   2746   
  2747   2747     if( rc==SQLITE_OK ){
  2748   2748       zMaster = pPager->pTmpSpace;
  2749   2749       rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
  2750   2750       testcase( rc!=SQLITE_OK );
  2751   2751     }
  2752         -  if( rc==SQLITE_OK && !pPager->noSync 
         2752  +  if( rc==SQLITE_OK
  2753   2753      && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
  2754   2754     ){
  2755         -    rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
         2755  +    rc = sqlite3PagerSync(pPager);
  2756   2756     }
  2757   2757     if( rc==SQLITE_OK ){
  2758   2758       rc = pager_end_transaction(pPager, zMaster[0]!='\0');
  2759   2759       testcase( rc!=SQLITE_OK );
  2760   2760     }
  2761   2761     if( rc==SQLITE_OK && zMaster[0] && res ){
  2762   2762       /* If there was a master journal and this routine will return success,
................................................................................
  2911   2911       rc = pagerUndoCallback((void *)pPager, pList->pgno);
  2912   2912       pList = pNext;
  2913   2913     }
  2914   2914   
  2915   2915     return rc;
  2916   2916   }
  2917   2917   
         2918  +
         2919  +/*
         2920  +** Update the value of the change-counter at offsets 24 and 92 in
         2921  +** the header and the sqlite version number at offset 96.
         2922  +**
         2923  +** This is an unconditional update.  See also the pager_incr_changecounter()
         2924  +** routine which only updates the change-counter if the update is actually
         2925  +** needed, as determined by the pPager->changeCountDone state variable.
         2926  +*/
         2927  +static void pager_write_changecounter(PgHdr *pPg){
         2928  +  u32 change_counter;
         2929  +
         2930  +  /* Increment the value just read and write it back to byte 24. */
         2931  +  change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
         2932  +  put32bits(((char*)pPg->pData)+24, change_counter);
         2933  +
         2934  +  /* Also store the SQLite version number in bytes 96..99 and in
         2935  +  ** bytes 92..95 store the change counter for which the version number
         2936  +  ** is valid. */
         2937  +  put32bits(((char*)pPg->pData)+92, change_counter);
         2938  +  put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
         2939  +}
         2940  +
  2918   2941   /*
  2919   2942   ** This function is a wrapper around sqlite3WalFrames(). As well as logging
  2920   2943   ** the contents of the list of pages headed by pList (connected by pDirty),
  2921   2944   ** this function notifies any active backup processes that the pages have
  2922         -** changed. 
         2945  +** changed.
         2946  +**
         2947  +** The list of pages passed into this routine is always sorted by page number.
         2948  +** Hence, if page 1 appears anywhere on the list, it will be the first page.
  2923   2949   */ 
  2924   2950   static int pagerWalFrames(
  2925   2951     Pager *pPager,                  /* Pager object */
  2926   2952     PgHdr *pList,                   /* List of frames to log */
  2927   2953     Pgno nTruncate,                 /* Database size after this commit */
  2928   2954     int isCommit,                   /* True if this is a commit */
  2929   2955     int syncFlags                   /* Flags to pass to OsSync() (or 0) */
  2930   2956   ){
  2931   2957     int rc;                         /* Return code */
         2958  +#if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
         2959  +  PgHdr *p;                       /* For looping over pages */
         2960  +#endif
  2932   2961   
  2933   2962     assert( pPager->pWal );
         2963  +#ifdef SQLITE_DEBUG
         2964  +  /* Verify that the page list is in accending order */
         2965  +  for(p=pList; p && p->pDirty; p=p->pDirty){
         2966  +    assert( p->pgno < p->pDirty->pgno );
         2967  +  }
         2968  +#endif
         2969  +
         2970  +  if( pList->pgno==1 ) pager_write_changecounter(pList);
  2934   2971     rc = sqlite3WalFrames(pPager->pWal, 
  2935   2972         pPager->pageSize, pList, nTruncate, isCommit, syncFlags
  2936   2973     );
  2937   2974     if( rc==SQLITE_OK && pPager->pBackup ){
  2938   2975       PgHdr *p;
  2939   2976       for(p=pList; p; p=p->pDirty){
  2940   2977         sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
  2941   2978       }
  2942   2979     }
  2943   2980   
  2944   2981   #ifdef SQLITE_CHECK_PAGES
  2945         -  {
  2946         -    PgHdr *p;
  2947         -    for(p=pList; p; p=p->pDirty) pager_set_pagehash(p);
         2982  +  for(p=pList; p; p=p->pDirty){
         2983  +    pager_set_pagehash(p);
  2948   2984     }
  2949   2985   #endif
  2950   2986   
  2951   2987     return rc;
  2952   2988   }
  2953   2989   
  2954   2990   /*
................................................................................
  3965   4001       ** set (set by sqlite3PagerDontWrite()).
  3966   4002       */
  3967   4003       if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
  3968   4004         i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
  3969   4005         char *pData;                                   /* Data to write */    
  3970   4006   
  3971   4007         assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
         4008  +      if( pList->pgno==1 ) pager_write_changecounter(pList);
  3972   4009   
  3973   4010         /* Encode the database */
  3974   4011         CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
  3975   4012   
  3976   4013         /* Write out the page data. */
  3977   4014         rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
  3978   4015   
................................................................................
  5485   5522       pager_set_pagehash(pPg);
  5486   5523     }
  5487   5524   }
  5488   5525   
  5489   5526   /*
  5490   5527   ** This routine is called to increment the value of the database file 
  5491   5528   ** change-counter, stored as a 4-byte big-endian integer starting at 
  5492         -** byte offset 24 of the pager file.
         5529  +** byte offset 24 of the pager file.  The secondary change counter at
         5530  +** 92 is also updated, as is the SQLite version number at offset 96.
         5531  +**
         5532  +** But this only happens if the pPager->changeCountDone flag is false.
         5533  +** To avoid excess churning of page 1, the update only happens once.
         5534  +** See also the pager_write_changecounter() routine that does an 
         5535  +** unconditional update of the change counters.
  5493   5536   **
  5494   5537   ** If the isDirectMode flag is zero, then this is done by calling 
  5495   5538   ** sqlite3PagerWrite() on page 1, then modifying the contents of the
  5496   5539   ** page data. In this case the file will be updated when the current
  5497   5540   ** transaction is committed.
  5498   5541   **
  5499   5542   ** The isDirectMode flag may only be non-zero if the library was compiled
................................................................................
  5526   5569     UNUSED_PARAMETER(isDirectMode);
  5527   5570   #else
  5528   5571   # define DIRECT_MODE isDirectMode
  5529   5572   #endif
  5530   5573   
  5531   5574     if( !pPager->changeCountDone && pPager->dbSize>0 ){
  5532   5575       PgHdr *pPgHdr;                /* Reference to page 1 */
  5533         -    u32 change_counter;           /* Initial value of change-counter field */
  5534   5576   
  5535   5577       assert( !pPager->tempFile && isOpen(pPager->fd) );
  5536   5578   
  5537   5579       /* Open page 1 of the file for writing. */
  5538   5580       rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
  5539   5581       assert( pPgHdr==0 || rc==SQLITE_OK );
  5540   5582   
................................................................................
  5544   5586       ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
  5545   5587       */
  5546   5588       if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
  5547   5589         rc = sqlite3PagerWrite(pPgHdr);
  5548   5590       }
  5549   5591   
  5550   5592       if( rc==SQLITE_OK ){
  5551         -      /* Increment the value just read and write it back to byte 24. */
  5552         -      change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
  5553         -      change_counter++;
  5554         -      put32bits(((char*)pPgHdr->pData)+24, change_counter);
  5555         -
  5556         -      /* Also store the SQLite version number in bytes 96..99 and in
  5557         -      ** bytes 92..95 store the change counter for which the version number
  5558         -      ** is valid. */
  5559         -      put32bits(((char*)pPgHdr->pData)+92, change_counter);
  5560         -      put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER);
         5593  +      /* Actually do the update of the change counter */
         5594  +      pager_write_changecounter(pPgHdr);
  5561   5595   
  5562   5596         /* If running in direct mode, write the contents of page 1 to the file. */
  5563   5597         if( DIRECT_MODE ){
  5564   5598           const void *zBuf;
  5565   5599           assert( pPager->dbFileSize>0 );
  5566   5600           CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
  5567   5601           if( rc==SQLITE_OK ){
................................................................................
  5585   5619   ** Sync the database file to disk. This is a no-op for in-memory databases
  5586   5620   ** or pages with the Pager.noSync flag set.
  5587   5621   **
  5588   5622   ** If successful, or if called on a pager for which it is a no-op, this
  5589   5623   ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
  5590   5624   */
  5591   5625   int sqlite3PagerSync(Pager *pPager){
  5592         -  int rc;                              /* Return code */
  5593         -  assert( !MEMDB );
  5594         -  if( pPager->noSync ){
  5595         -    rc = SQLITE_OK;
  5596         -  }else{
         5626  +  int rc = SQLITE_OK;
         5627  +  if( !pPager->noSync ){
         5628  +    assert( !MEMDB );
  5597   5629       rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
         5630  +  }else if( isOpen(pPager->fd) ){
         5631  +    assert( !MEMDB );
         5632  +    sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
  5598   5633     }
  5599   5634     return rc;
  5600   5635   }
  5601   5636   
  5602   5637   /*
  5603   5638   ** This function may only be called while a write-transaction is active in
  5604   5639   ** rollback. If the connection is in WAL mode, this call is a no-op. 
................................................................................
  5809   5844           Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
  5810   5845           assert( pPager->eState==PAGER_WRITER_DBMOD );
  5811   5846           rc = pager_truncate(pPager, nNew);
  5812   5847           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
  5813   5848         }
  5814   5849     
  5815   5850         /* Finally, sync the database file. */
  5816         -      if( !pPager->noSync && !noSync ){
  5817         -        rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
         5851  +      if( !noSync ){
         5852  +        rc = sqlite3PagerSync(pPager);
  5818   5853         }
  5819   5854         IOTRACE(("DBSYNC %p\n", pPager))
  5820   5855       }
  5821   5856     }
  5822   5857   
  5823   5858   commit_phase_one_exit:
  5824   5859     if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
................................................................................
  5922   5957   
  5923   5958     if( pagerUseWal(pPager) ){
  5924   5959       int rc2;
  5925   5960       rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
  5926   5961       rc2 = pager_end_transaction(pPager, pPager->setMaster);
  5927   5962       if( rc==SQLITE_OK ) rc = rc2;
  5928   5963     }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
         5964  +    int eState = pPager->eState;
  5929   5965       rc = pager_end_transaction(pPager, 0);
         5966  +    if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
         5967  +      /* This can happen using journal_mode=off. Move the pager to the error 
         5968  +      ** state to indicate that the contents of the cache may not be trusted.
         5969  +      ** Any active readers will get SQLITE_ABORT.
         5970  +      */
         5971  +      pPager->errCode = SQLITE_ABORT;
         5972  +      pPager->eState = PAGER_ERROR;
         5973  +      return rc;
         5974  +    }
  5930   5975     }else{
  5931   5976       rc = pager_playback(pPager, 0);
  5932   5977     }
  5933   5978   
  5934   5979     assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
  5935   5980     assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
  5936   5981   

Changes to src/pcache1.c.

    18     18   */
    19     19   
    20     20   #include "sqliteInt.h"
    21     21   
    22     22   typedef struct PCache1 PCache1;
    23     23   typedef struct PgHdr1 PgHdr1;
    24     24   typedef struct PgFreeslot PgFreeslot;
           25  +typedef struct PGroup PGroup;
           26  +
           27  +/* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
           28  +** of one or more PCaches that are able to recycle each others unpinned
           29  +** pages when they are under memory pressure.  A PGroup is an instance of
           30  +** the following object.
           31  +**
           32  +** This page cache implementation works in one of two modes:
           33  +**
           34  +**   (1)  Every PCache is the sole member of its own PGroup.  There is
           35  +**        one PGroup per PCache.
           36  +**
           37  +**   (2)  There is a single global PGroup that all PCaches are a member
           38  +**        of.
           39  +**
           40  +** Mode 1 uses more memory (since PCache instances are not able to rob
           41  +** unused pages from other PCaches) but it also operates without a mutex,
           42  +** and is therefore often faster.  Mode 2 requires a mutex in order to be
           43  +** threadsafe, but is able recycle pages more efficient.
           44  +**
           45  +** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
           46  +** PGroup which is the pcache1.grp global variable and its mutex is
           47  +** SQLITE_MUTEX_STATIC_LRU.
           48  +*/
           49  +struct PGroup {
           50  +  sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
           51  +  int nMaxPage;                  /* Sum of nMax for purgeable caches */
           52  +  int nMinPage;                  /* Sum of nMin for purgeable caches */
           53  +  int mxPinned;                  /* nMaxpage + 10 - nMinPage */
           54  +  int nCurrentPage;              /* Number of purgeable pages allocated */
           55  +  PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
           56  +};
    25     57   
    26     58   /* Each page cache is an instance of the following object.  Every
    27     59   ** open database file (including each in-memory database and each
    28     60   ** temporary or transient database) has a single page cache which
    29     61   ** is an instance of this object.
    30     62   **
    31     63   ** Pointers to structures of this type are cast and returned as 
    32     64   ** opaque sqlite3_pcache* handles.
    33     65   */
    34     66   struct PCache1 {
    35     67     /* Cache configuration parameters. Page size (szPage) and the purgeable
    36     68     ** flag (bPurgeable) are set when the cache is created. nMax may be 
    37     69     ** modified at any time by a call to the pcache1CacheSize() method.
    38         -  ** The global mutex must be held when accessing nMax.
           70  +  ** The PGroup mutex must be held when accessing nMax.
    39     71     */
           72  +  PGroup *pGroup;                     /* PGroup this cache belongs to */
    40     73     int szPage;                         /* Size of allocated pages in bytes */
    41     74     int bPurgeable;                     /* True if cache is purgeable */
    42     75     unsigned int nMin;                  /* Minimum number of pages reserved */
    43     76     unsigned int nMax;                  /* Configured "cache_size" value */
           77  +  unsigned int n90pct;                /* nMax*9/10 */
    44     78   
    45     79     /* Hash table of all pages. The following variables may only be accessed
    46         -  ** when the accessor is holding the global mutex (see pcache1EnterMutex() 
    47         -  ** and pcache1LeaveMutex()).
           80  +  ** when the accessor is holding the PGroup mutex.
    48     81     */
    49     82     unsigned int nRecyclable;           /* Number of pages in the LRU list */
    50     83     unsigned int nPage;                 /* Total number of pages in apHash */
    51     84     unsigned int nHash;                 /* Number of slots in apHash[] */
    52     85     PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
    53     86   
    54     87     unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
................................................................................
    76    109     PgFreeslot *pNext;  /* Next free slot */
    77    110   };
    78    111   
    79    112   /*
    80    113   ** Global data used by this cache.
    81    114   */
    82    115   static SQLITE_WSD struct PCacheGlobal {
    83         -  sqlite3_mutex *mutex;               /* static mutex MUTEX_STATIC_LRU */
          116  +  PGroup grp;                    /* The global PGroup for mode (2) */
    84    117   
    85         -  int nMaxPage;                       /* Sum of nMaxPage for purgeable caches */
    86         -  int nMinPage;                       /* Sum of nMinPage for purgeable caches */
    87         -  int nCurrentPage;                   /* Number of purgeable pages allocated */
    88         -  PgHdr1 *pLruHead, *pLruTail;        /* LRU list of unpinned pages */
    89         -
    90         -  /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
    91         -  int szSlot;                         /* Size of each free slot */
    92         -  int nSlot;                          /* The number of pcache slots */
    93         -  int nFreeSlot;                      /* Number of unused pcache slots */
    94         -  int nReserve;                       /* Try to keep nFreeSlot above this */
    95         -  void *pStart, *pEnd;                /* Bounds of pagecache malloc range */
    96         -  PgFreeslot *pFree;                  /* Free page blocks */
    97         -  int isInit;                         /* True if initialized */
          118  +  /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
          119  +  ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
          120  +  ** fixed at sqlite3_initialize() time and do not require mutex protection.
          121  +  ** The nFreeSlot and pFree values do require mutex protection.
          122  +  */
          123  +  int isInit;                    /* True if initialized */
          124  +  int szSlot;                    /* Size of each free slot */
          125  +  int nSlot;                     /* The number of pcache slots */
          126  +  int nReserve;                  /* Try to keep nFreeSlot above this */
          127  +  void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
          128  +  /* Above requires no mutex.  Use mutex below for variable that follow. */
          129  +  sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
          130  +  int nFreeSlot;                 /* Number of unused pcache slots */
          131  +  PgFreeslot *pFree;             /* Free page blocks */
          132  +  /* The following value requires a mutex to change.  We skip the mutex on
          133  +  ** reading because (1) most platforms read a 32-bit integer atomically and
          134  +  ** (2) even if an incorrect value is read, no great harm is done since this
          135  +  ** is really just an optimization. */
          136  +  int bUnderPressure;            /* True if low on PAGECACHE memory */
    98    137   } pcache1_g;
    99    138   
   100    139   /*
   101    140   ** All code in this file should access the global structure above via the
   102    141   ** alias "pcache1". This ensures that the WSD emulation is used when
   103    142   ** compiling for systems that do not support real WSD.
   104    143   */
................................................................................
   116    155   **
   117    156   **   assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
   118    157   */
   119    158   #define PGHDR1_TO_PAGE(p)    (void*)(((char*)p) - p->pCache->szPage)
   120    159   #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
   121    160   
   122    161   /*
   123         -** Macros to enter and leave the global LRU mutex.
          162  +** Macros to enter and leave the PCache LRU mutex.
   124    163   */
   125         -#define pcache1EnterMutex() sqlite3_mutex_enter(pcache1.mutex)
   126         -#define pcache1LeaveMutex() sqlite3_mutex_leave(pcache1.mutex)
          164  +#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
          165  +#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
   127    166   
   128    167   /******************************************************************************/
   129    168   /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
   130    169   
   131    170   /*
   132    171   ** This function is called during initialization if a static buffer is 
   133    172   ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
   134    173   ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
   135    174   ** enough to contain 'n' buffers of 'sz' bytes each.
          175  +**
          176  +** This routine is called from sqlite3_initialize() and so it is guaranteed
          177  +** to be serialized already.  There is no need for further mutexing.
   136    178   */
   137    179   void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
   138    180     if( pcache1.isInit ){
   139    181       PgFreeslot *p;
   140    182       sz = ROUNDDOWN8(sz);
   141    183       pcache1.szSlot = sz;
   142    184       pcache1.nSlot = pcache1.nFreeSlot = n;
   143    185       pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
   144    186       pcache1.pStart = pBuf;
   145    187       pcache1.pFree = 0;
          188  +    pcache1.bUnderPressure = 0;
   146    189       while( n-- ){
   147    190         p = (PgFreeslot*)pBuf;
   148    191         p->pNext = pcache1.pFree;
   149    192         pcache1.pFree = p;
   150    193         pBuf = (void*)&((char*)pBuf)[sz];
   151    194       }
   152    195       pcache1.pEnd = pBuf;
................................................................................
   154    197   }
   155    198   
   156    199   /*
   157    200   ** Malloc function used within this file to allocate space from the buffer
   158    201   ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
   159    202   ** such buffer exists or there is no space left in it, this function falls 
   160    203   ** back to sqlite3Malloc().
          204  +**
          205  +** Multiple threads can run this routine at the same time.  Global variables
          206  +** in pcache1 need to be protected via mutex.
   161    207   */
   162    208   static void *pcache1Alloc(int nByte){
   163         -  void *p;
   164         -  assert( sqlite3_mutex_held(pcache1.mutex) );
          209  +  void *p = 0;
          210  +  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
   165    211     sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
   166         -  if( nByte<=pcache1.szSlot && pcache1.pFree ){
   167         -    assert( pcache1.isInit );
          212  +  if( nByte<=pcache1.szSlot ){
          213  +    sqlite3_mutex_enter(pcache1.mutex);
   168    214       p = (PgHdr1 *)pcache1.pFree;
   169         -    pcache1.pFree = pcache1.pFree->pNext;
   170         -    pcache1.nFreeSlot--;
   171         -    assert( pcache1.nFreeSlot>=0 );
   172         -    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
   173         -  }else{
   174         -
   175         -    /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
   176         -    ** global pcache mutex and unlock the pager-cache object pCache. This is 
   177         -    ** so that if the attempt to allocate a new buffer causes the the 
   178         -    ** configured soft-heap-limit to be breached, it will be possible to
   179         -    ** reclaim memory from this pager-cache.
          215  +    if( p ){
          216  +      pcache1.pFree = pcache1.pFree->pNext;
          217  +      pcache1.nFreeSlot--;
          218  +      pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
          219  +      assert( pcache1.nFreeSlot>=0 );
          220  +      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
          221  +    }
          222  +    sqlite3_mutex_leave(pcache1.mutex);
          223  +  }
          224  +  if( p==0 ){
          225  +    /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
          226  +    ** it from sqlite3Malloc instead.
   180    227       */
   181         -    pcache1LeaveMutex();
   182    228       p = sqlite3Malloc(nByte);
   183         -    pcache1EnterMutex();
   184    229       if( p ){
   185    230         int sz = sqlite3MallocSize(p);
          231  +      sqlite3_mutex_enter(pcache1.mutex);
   186    232         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
          233  +      sqlite3_mutex_leave(pcache1.mutex);
   187    234       }
   188    235       sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
   189    236     }
   190    237     return p;
   191    238   }
   192    239   
   193    240   /*
   194    241   ** Free an allocated buffer obtained from pcache1Alloc().
   195    242   */
   196    243   static void pcache1Free(void *p){
   197         -  assert( sqlite3_mutex_held(pcache1.mutex) );
   198    244     if( p==0 ) return;
   199    245     if( p>=pcache1.pStart && p<pcache1.pEnd ){
   200    246       PgFreeslot *pSlot;
          247  +    sqlite3_mutex_enter(pcache1.mutex);
   201    248       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
   202    249       pSlot = (PgFreeslot*)p;
   203    250       pSlot->pNext = pcache1.pFree;
   204    251       pcache1.pFree = pSlot;
   205    252       pcache1.nFreeSlot++;
          253  +    pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
   206    254       assert( pcache1.nFreeSlot<=pcache1.nSlot );
          255  +    sqlite3_mutex_leave(pcache1.mutex);
   207    256     }else{
   208    257       int iSize;
   209    258       assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
   210    259       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   211    260       iSize = sqlite3MallocSize(p);
          261  +    sqlite3_mutex_enter(pcache1.mutex);
   212    262       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
          263  +    sqlite3_mutex_leave(pcache1.mutex);
   213    264       sqlite3_free(p);
   214    265     }
   215    266   }
   216    267   
   217    268   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   218    269   /*
   219    270   ** Return the size of a pcache allocation
   220    271   */
   221    272   static int pcache1MemSize(void *p){
   222         -  assert( sqlite3_mutex_held(pcache1.mutex) );
   223    273     if( p>=pcache1.pStart && p<pcache1.pEnd ){
   224    274       return pcache1.szSlot;
   225    275     }else{
   226    276       int iSize;
   227    277       assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
   228    278       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   229    279       iSize = sqlite3MallocSize(p);
................................................................................
   239    289   static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
   240    290     int nByte = sizeof(PgHdr1) + pCache->szPage;
   241    291     void *pPg = pcache1Alloc(nByte);
   242    292     PgHdr1 *p;
   243    293     if( pPg ){
   244    294       p = PAGE_TO_PGHDR1(pCache, pPg);
   245    295       if( pCache->bPurgeable ){
   246         -      pcache1.nCurrentPage++;
          296  +      pCache->pGroup->nCurrentPage++;
   247    297       }
   248    298     }else{
   249    299       p = 0;
   250    300     }
   251    301     return p;
   252    302   }
   253    303   
................................................................................
   256    306   **
   257    307   ** The pointer is allowed to be NULL, which is prudent.  But it turns out
   258    308   ** that the current implementation happens to never call this routine
   259    309   ** with a NULL pointer, so we mark the NULL test with ALWAYS().
   260    310   */
   261    311   static void pcache1FreePage(PgHdr1 *p){
   262    312     if( ALWAYS(p) ){
   263         -    if( p->pCache->bPurgeable ){
   264         -      pcache1.nCurrentPage--;
          313  +    PCache1 *pCache = p->pCache;
          314  +    if( pCache->bPurgeable ){
          315  +      pCache->pGroup->nCurrentPage--;
   265    316       }
   266    317       pcache1Free(PGHDR1_TO_PAGE(p));
   267    318     }
   268    319   }
   269    320   
   270    321   /*
   271    322   ** Malloc function used by SQLite to obtain space from the buffer configured
   272    323   ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
   273    324   ** exists, this function falls back to sqlite3Malloc().
   274    325   */
   275    326   void *sqlite3PageMalloc(int sz){
   276         -  void *p;
   277         -  pcache1EnterMutex();
   278         -  p = pcache1Alloc(sz);
   279         -  pcache1LeaveMutex();
   280         -  return p;
          327  +  return pcache1Alloc(sz);
   281    328   }
   282    329   
   283    330   /*
   284    331   ** Free an allocated buffer obtained from sqlite3PageMalloc().
   285    332   */
   286    333   void sqlite3PageFree(void *p){
   287         -  pcache1EnterMutex();
   288    334     pcache1Free(p);
   289         -  pcache1LeaveMutex();
   290    335   }
   291    336   
   292    337   
   293    338   /*
   294    339   ** Return true if it desirable to avoid allocating a new page cache
   295    340   ** entry.
   296    341   **
................................................................................
   303    348   **
   304    349   ** Or, the heap is used for all page cache memory put the heap is
   305    350   ** under memory pressure, then again it is desirable to avoid
   306    351   ** allocating a new page cache entry in order to avoid stressing
   307    352   ** the heap even further.
   308    353   */
   309    354   static int pcache1UnderMemoryPressure(PCache1 *pCache){
   310         -  assert( sqlite3_mutex_held(pcache1.mutex) );
   311    355     if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
   312         -    return pcache1.nFreeSlot<pcache1.nReserve;
          356  +    return pcache1.bUnderPressure;
   313    357     }else{
   314    358       return sqlite3HeapNearlyFull();
   315    359     }
   316    360   }
   317    361   
   318    362   /******************************************************************************/
   319    363   /******** General Implementation Functions ************************************/
   320    364   
   321    365   /*
   322    366   ** This function is used to resize the hash table used by the cache passed
   323    367   ** as the first argument.
   324    368   **
   325         -** The global mutex must be held when this function is called.
          369  +** The PCache mutex must be held when this function is called.
   326    370   */
   327    371   static int pcache1ResizeHash(PCache1 *p){
   328    372     PgHdr1 **apNew;
   329    373     unsigned int nNew;
   330    374     unsigned int i;
   331    375   
   332         -  assert( sqlite3_mutex_held(pcache1.mutex) );
          376  +  assert( sqlite3_mutex_held(p->pGroup->mutex) );
   333    377   
   334    378     nNew = p->nHash*2;
   335    379     if( nNew<256 ){
   336    380       nNew = 256;
   337    381     }
   338    382   
   339         -  pcache1LeaveMutex();
          383  +  pcache1LeaveMutex(p->pGroup);
   340    384     if( p->nHash ){ sqlite3BeginBenignMalloc(); }
   341    385     apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
   342    386     if( p->nHash ){ sqlite3EndBenignMalloc(); }
   343         -  pcache1EnterMutex();
          387  +  pcache1EnterMutex(p->pGroup);
   344    388     if( apNew ){
   345    389       memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
   346    390       for(i=0; i<p->nHash; i++){
   347    391         PgHdr1 *pPage;
   348    392         PgHdr1 *pNext = p->apHash[i];
   349    393         while( (pPage = pNext)!=0 ){
   350    394           unsigned int h = pPage->iKey % nNew;
................................................................................
   359    403     }
   360    404   
   361    405     return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
   362    406   }
   363    407   
   364    408   /*
   365    409   ** This function is used internally to remove the page pPage from the 
   366         -** global LRU list, if is part of it. If pPage is not part of the global
          410  +** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
   367    411   ** LRU list, then this function is a no-op.
   368    412   **
   369         -** The global mutex must be held when this function is called.
          413  +** The PGroup mutex must be held when this function is called.
          414  +**
          415  +** If pPage is NULL then this routine is a no-op.
   370    416   */
   371    417   static void pcache1PinPage(PgHdr1 *pPage){
   372         -  assert( sqlite3_mutex_held(pcache1.mutex) );
   373         -  if( pPage && (pPage->pLruNext || pPage==pcache1.pLruTail) ){
          418  +  PCache1 *pCache;
          419  +  PGroup *pGroup;
          420  +
          421  +  if( pPage==0 ) return;
          422  +  pCache = pPage->pCache;
          423  +  pGroup = pCache->pGroup;
          424  +  assert( sqlite3_mutex_held(pGroup->mutex) );
          425  +  if( pPage->pLruNext || pPage==pGroup->pLruTail ){
   374    426       if( pPage->pLruPrev ){
   375    427         pPage->pLruPrev->pLruNext = pPage->pLruNext;
   376    428       }
   377    429       if( pPage->pLruNext ){
   378    430         pPage->pLruNext->pLruPrev = pPage->pLruPrev;
   379    431       }
   380         -    if( pcache1.pLruHead==pPage ){
   381         -      pcache1.pLruHead = pPage->pLruNext;
          432  +    if( pGroup->pLruHead==pPage ){
          433  +      pGroup->pLruHead = pPage->pLruNext;
   382    434       }
   383         -    if( pcache1.pLruTail==pPage ){
   384         -      pcache1.pLruTail = pPage->pLruPrev;
          435  +    if( pGroup->pLruTail==pPage ){
          436  +      pGroup->pLruTail = pPage->pLruPrev;
   385    437       }
   386    438       pPage->pLruNext = 0;
   387    439       pPage->pLruPrev = 0;
   388    440       pPage->pCache->nRecyclable--;
   389    441     }
   390    442   }
   391    443   
   392    444   
   393    445   /*
   394    446   ** Remove the page supplied as an argument from the hash table 
   395    447   ** (PCache1.apHash structure) that it is currently stored in.
   396    448   **
   397         -** The global mutex must be held when this function is called.
          449  +** The PGroup mutex must be held when this function is called.
   398    450   */
   399    451   static void pcache1RemoveFromHash(PgHdr1 *pPage){
   400    452     unsigned int h;
   401    453     PCache1 *pCache = pPage->pCache;
   402    454     PgHdr1 **pp;
   403    455   
          456  +  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
   404    457     h = pPage->iKey % pCache->nHash;
   405    458     for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
   406    459     *pp = (*pp)->pNext;
   407    460   
   408    461     pCache->nPage--;
   409    462   }
   410    463   
   411    464   /*
   412         -** If there are currently more than pcache.nMaxPage pages allocated, try
   413         -** to recycle pages to reduce the number allocated to pcache.nMaxPage.
          465  +** If there are currently more than nMaxPage pages allocated, try
          466  +** to recycle pages to reduce the number allocated to nMaxPage.
   414    467   */
   415         -static void pcache1EnforceMaxPage(void){
   416         -  assert( sqlite3_mutex_held(pcache1.mutex) );
   417         -  while( pcache1.nCurrentPage>pcache1.nMaxPage && pcache1.pLruTail ){
   418         -    PgHdr1 *p = pcache1.pLruTail;
          468  +static void pcache1EnforceMaxPage(PGroup *pGroup){
          469  +  assert( sqlite3_mutex_held(pGroup->mutex) );
          470  +  while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
          471  +    PgHdr1 *p = pGroup->pLruTail;
          472  +    assert( p->pCache->pGroup==pGroup );
   419    473       pcache1PinPage(p);
   420    474       pcache1RemoveFromHash(p);
   421    475       pcache1FreePage(p);
   422    476     }
   423    477   }
   424    478   
   425    479   /*
   426    480   ** Discard all pages from cache pCache with a page number (key value) 
   427    481   ** greater than or equal to iLimit. Any pinned pages that meet this 
   428    482   ** criteria are unpinned before they are discarded.
   429    483   **
   430         -** The global mutex must be held when this function is called.
          484  +** The PCache mutex must be held when this function is called.
   431    485   */
   432    486   static void pcache1TruncateUnsafe(
   433         -  PCache1 *pCache, 
   434         -  unsigned int iLimit 
          487  +  PCache1 *pCache,             /* The cache to truncate */
          488  +  unsigned int iLimit          /* Drop pages with this pgno or larger */
   435    489   ){
   436         -  TESTONLY( unsigned int nPage = 0; )      /* Used to assert pCache->nPage is correct */
          490  +  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
   437    491     unsigned int h;
   438         -  assert( sqlite3_mutex_held(pcache1.mutex) );
          492  +  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
   439    493     for(h=0; h<pCache->nHash; h++){
   440    494       PgHdr1 **pp = &pCache->apHash[h]; 
   441    495       PgHdr1 *pPage;
   442    496       while( (pPage = *pp)!=0 ){
   443    497         if( pPage->iKey>=iLimit ){
   444    498           pCache->nPage--;
   445    499           *pp = pPage->pNext;
................................................................................
   461    515   ** Implementation of the sqlite3_pcache.xInit method.
   462    516   */
   463    517   static int pcache1Init(void *NotUsed){
   464    518     UNUSED_PARAMETER(NotUsed);
   465    519     assert( pcache1.isInit==0 );
   466    520     memset(&pcache1, 0, sizeof(pcache1));
   467    521     if( sqlite3GlobalConfig.bCoreMutex ){
   468         -    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
          522  +    pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
          523  +    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
   469    524     }
          525  +  pcache1.grp.mxPinned = 10;
   470    526     pcache1.isInit = 1;
   471    527     return SQLITE_OK;
   472    528   }
   473    529   
   474    530   /*
   475    531   ** Implementation of the sqlite3_pcache.xShutdown method.
   476    532   ** Note that the static mutex allocated in xInit does 
................................................................................
   484    540   
   485    541   /*
   486    542   ** Implementation of the sqlite3_pcache.xCreate method.
   487    543   **
   488    544   ** Allocate a new cache.
   489    545   */
   490    546   static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
   491         -  PCache1 *pCache;
          547  +  PCache1 *pCache;      /* The newly created page cache */
          548  +  PGroup *pGroup;       /* The group the new page cache will belong to */
          549  +  int sz;               /* Bytes of memory required to allocate the new cache */
   492    550   
   493         -  pCache = (PCache1 *)sqlite3_malloc(sizeof(PCache1));
          551  +  /*
          552  +  ** The seperateCache variable is true if each PCache has its own private
          553  +  ** PGroup.  In other words, separateCache is true for mode (1) where no
          554  +  ** mutexing is required.
          555  +  **
          556  +  **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
          557  +  **
          558  +  **   *  Always use a unified cache in single-threaded applications
          559  +  **
          560  +  **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
          561  +  **      use separate caches (mode-1)
          562  +  */
          563  +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
          564  +  const int separateCache = 0;
          565  +#else
          566  +  int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
          567  +#endif
          568  +
          569  +  sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
          570  +  pCache = (PCache1 *)sqlite3_malloc(sz);
   494    571     if( pCache ){
   495         -    memset(pCache, 0, sizeof(PCache1));
          572  +    memset(pCache, 0, sz);
          573  +    if( separateCache ){
          574  +      pGroup = (PGroup*)&pCache[1];
          575  +      pGroup->mxPinned = 10;
          576  +    }else{
          577  +      pGroup = &pcache1_g.grp;
          578  +    }
          579  +    pCache->pGroup = pGroup;
   496    580       pCache->szPage = szPage;
   497    581       pCache->bPurgeable = (bPurgeable ? 1 : 0);
   498    582       if( bPurgeable ){
   499    583         pCache->nMin = 10;
   500         -      pcache1EnterMutex();
   501         -      pcache1.nMinPage += pCache->nMin;
   502         -      pcache1LeaveMutex();
          584  +      pcache1EnterMutex(pGroup);
          585  +      pGroup->nMinPage += pCache->nMin;
          586  +      pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
          587  +      pcache1LeaveMutex(pGroup);
   503    588       }
   504    589     }
   505    590     return (sqlite3_pcache *)pCache;
   506    591   }
   507    592   
   508    593   /*
   509    594   ** Implementation of the sqlite3_pcache.xCachesize method. 
   510    595   **
   511    596   ** Configure the cache_size limit for a cache.
   512    597   */
   513    598   static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
   514    599     PCache1 *pCache = (PCache1 *)p;
   515    600     if( pCache->bPurgeable ){
   516         -    pcache1EnterMutex();
   517         -    pcache1.nMaxPage += (nMax - pCache->nMax);
          601  +    PGroup *pGroup = pCache->pGroup;
          602  +    pcache1EnterMutex(pGroup);
          603  +    pGroup->nMaxPage += (nMax - pCache->nMax);
          604  +    pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
   518    605       pCache->nMax = nMax;
   519         -    pcache1EnforceMaxPage();
   520         -    pcache1LeaveMutex();
          606  +    pCache->n90pct = pCache->nMax*9/10;
          607  +    pcache1EnforceMaxPage(pGroup);
          608  +    pcache1LeaveMutex(pGroup);
   521    609     }
   522    610   }
   523    611   
   524    612   /*
   525    613   ** Implementation of the sqlite3_pcache.xPagecount method. 
   526    614   */
   527    615   static int pcache1Pagecount(sqlite3_pcache *p){
   528    616     int n;
   529         -  pcache1EnterMutex();
   530         -  n = ((PCache1 *)p)->nPage;
   531         -  pcache1LeaveMutex();
          617  +  PCache1 *pCache = (PCache1*)p;
          618  +  pcache1EnterMutex(pCache->pGroup);
          619  +  n = pCache->nPage;
          620  +  pcache1LeaveMutex(pCache->pGroup);
   532    621     return n;
   533    622   }
   534    623   
   535    624   /*
   536    625   ** Implementation of the sqlite3_pcache.xFetch method. 
   537    626   **
   538    627   ** Fetch a page by key value.
................................................................................
   583    672   **      then attempt to recycle a page from the LRU list. If it is the right
   584    673   **      size, return the recycled buffer. Otherwise, free the buffer and
   585    674   **      proceed to step 5. 
   586    675   **
   587    676   **   5. Otherwise, allocate and return a new page buffer.
   588    677   */
   589    678   static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
   590         -  unsigned int nPinned;
          679  +  int nPinned;
   591    680     PCache1 *pCache = (PCache1 *)p;
          681  +  PGroup *pGroup;
   592    682     PgHdr1 *pPage = 0;
   593    683   
   594    684     assert( pCache->bPurgeable || createFlag!=1 );
   595         -  pcache1EnterMutex();
   596         -  if( createFlag==1 ) sqlite3BeginBenignMalloc();
          685  +  assert( pCache->bPurgeable || pCache->nMin==0 );
          686  +  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
          687  +  assert( pCache->nMin==0 || pCache->bPurgeable );
          688  +  pcache1EnterMutex(pGroup = pCache->pGroup);
   597    689   
   598         -  /* Search the hash table for an existing entry. */
          690  +  /* Step 1: Search the hash table for an existing entry. */
   599    691     if( pCache->nHash>0 ){
   600    692       unsigned int h = iKey % pCache->nHash;
   601    693       for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
   602    694     }
   603    695   
          696  +  /* Step 2: Abort if no existing page is found and createFlag is 0 */
   604    697     if( pPage || createFlag==0 ){
   605    698       pcache1PinPage(pPage);
   606    699       goto fetch_out;
   607    700     }
   608    701   
   609         -  /* Step 3 of header comment. */
          702  +  /* The pGroup local variable will normally be initialized by the
          703  +  ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
          704  +  ** then pcache1EnterMutex() is a no-op, so we have to initialize the
          705  +  ** local variable here.  Delaying the initialization of pGroup is an
          706  +  ** optimization:  The common case is to exit the module before reaching
          707  +  ** this point.
          708  +  */
          709  +#ifdef SQLITE_MUTEX_OMIT
          710  +  pGroup = pCache->pGroup;
          711  +#endif
          712  +
          713  +
          714  +  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
   610    715     nPinned = pCache->nPage - pCache->nRecyclable;
          716  +  assert( nPinned>=0 );
          717  +  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
          718  +  assert( pCache->n90pct == pCache->nMax*9/10 );
   611    719     if( createFlag==1 && (
   612         -        nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage)
   613         -     || nPinned>=(pCache->nMax * 9 / 10)
          720  +        nPinned>=pGroup->mxPinned
          721  +     || nPinned>=(int)pCache->n90pct
   614    722        || pcache1UnderMemoryPressure(pCache)
   615    723     )){
   616    724       goto fetch_out;
   617    725     }
   618    726   
   619    727     if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
   620    728       goto fetch_out;
   621    729     }
   622    730   
   623         -  /* Step 4. Try to recycle a page buffer if appropriate. */
   624         -  if( pCache->bPurgeable && pcache1.pLruTail && (
          731  +  /* Step 4. Try to recycle a page. */
          732  +  if( pCache->bPurgeable && pGroup->pLruTail && (
   625    733            (pCache->nPage+1>=pCache->nMax)
   626         -      || pcache1.nCurrentPage>=pcache1.nMaxPage
          734  +      || pGroup->nCurrentPage>=pGroup->nMaxPage
   627    735         || pcache1UnderMemoryPressure(pCache)
   628    736     )){
   629         -    pPage = pcache1.pLruTail;
          737  +    PCache1 *pOtherCache;
          738  +    pPage = pGroup->pLruTail;
   630    739       pcache1RemoveFromHash(pPage);
   631    740       pcache1PinPage(pPage);
   632         -    if( pPage->pCache->szPage!=pCache->szPage ){
          741  +    if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
   633    742         pcache1FreePage(pPage);
   634    743         pPage = 0;
   635    744       }else{
   636         -      pcache1.nCurrentPage -= (pPage->pCache->bPurgeable - pCache->bPurgeable);
          745  +      pGroup->nCurrentPage -= 
          746  +               (pOtherCache->bPurgeable - pCache->bPurgeable);
   637    747       }
   638    748     }
   639    749   
   640    750     /* Step 5. If a usable page buffer has still not been found, 
   641    751     ** attempt to allocate a new one. 
   642    752     */
   643    753     if( !pPage ){
          754  +    if( createFlag==1 ) sqlite3BeginBenignMalloc();
          755  +    pcache1LeaveMutex(pGroup);
   644    756       pPage = pcache1AllocPage(pCache);
          757  +    pcache1EnterMutex(pGroup);
          758  +    if( createFlag==1 ) sqlite3EndBenignMalloc();
   645    759     }
   646    760   
   647    761     if( pPage ){
   648    762       unsigned int h = iKey % pCache->nHash;
   649    763       pCache->nPage++;
   650    764       pPage->iKey = iKey;
   651    765       pPage->pNext = pCache->apHash[h];
................................................................................
   656    770       pCache->apHash[h] = pPage;
   657    771     }
   658    772   
   659    773   fetch_out:
   660    774     if( pPage && iKey>pCache->iMaxKey ){
   661    775       pCache->iMaxKey = iKey;
   662    776     }
   663         -  if( createFlag==1 ) sqlite3EndBenignMalloc();
   664         -  pcache1LeaveMutex();
          777  +  pcache1LeaveMutex(pGroup);
   665    778     return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
   666    779   }
   667    780   
   668    781   
   669    782   /*
   670    783   ** Implementation of the sqlite3_pcache.xUnpin method.
   671    784   **
   672    785   ** Mark a page as unpinned (eligible for asynchronous recycling).
   673    786   */
   674    787   static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
   675    788     PCache1 *pCache = (PCache1 *)p;
   676    789     PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
          790  +  PGroup *pGroup = pCache->pGroup;
   677    791    
   678    792     assert( pPage->pCache==pCache );
   679         -  pcache1EnterMutex();
          793  +  pcache1EnterMutex(pGroup);
   680    794   
   681    795     /* It is an error to call this function if the page is already 
   682         -  ** part of the global LRU list.
          796  +  ** part of the PGroup LRU list.
   683    797     */
   684    798     assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
   685         -  assert( pcache1.pLruHead!=pPage && pcache1.pLruTail!=pPage );
          799  +  assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
   686    800   
   687         -  if( reuseUnlikely || pcache1.nCurrentPage>pcache1.nMaxPage ){
          801  +  if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
   688    802       pcache1RemoveFromHash(pPage);
   689    803       pcache1FreePage(pPage);
   690    804     }else{
   691         -    /* Add the page to the global LRU list. Normally, the page is added to
   692         -    ** the head of the list (last page to be recycled). However, if the 
   693         -    ** reuseUnlikely flag passed to this function is true, the page is added
   694         -    ** to the tail of the list (first page to be recycled).
   695         -    */
   696         -    if( pcache1.pLruHead ){
   697         -      pcache1.pLruHead->pLruPrev = pPage;
   698         -      pPage->pLruNext = pcache1.pLruHead;
   699         -      pcache1.pLruHead = pPage;
          805  +    /* Add the page to the PGroup LRU list. */
          806  +    if( pGroup->pLruHead ){
          807  +      pGroup->pLruHead->pLruPrev = pPage;
          808  +      pPage->pLruNext = pGroup->pLruHead;
          809  +      pGroup->pLruHead = pPage;
   700    810       }else{
   701         -      pcache1.pLruTail = pPage;
   702         -      pcache1.pLruHead = pPage;
          811  +      pGroup->pLruTail = pPage;
          812  +      pGroup->pLruHead = pPage;
   703    813       }
   704    814       pCache->nRecyclable++;
   705    815     }
   706    816   
   707         -  pcache1LeaveMutex();
          817  +  pcache1LeaveMutex(pCache->pGroup);
   708    818   }
   709    819   
   710    820   /*
   711    821   ** Implementation of the sqlite3_pcache.xRekey method. 
   712    822   */
   713    823   static void pcache1Rekey(
   714    824     sqlite3_pcache *p,
................................................................................
   719    829     PCache1 *pCache = (PCache1 *)p;
   720    830     PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
   721    831     PgHdr1 **pp;
   722    832     unsigned int h; 
   723    833     assert( pPage->iKey==iOld );
   724    834     assert( pPage->pCache==pCache );
   725    835   
   726         -  pcache1EnterMutex();
          836  +  pcache1EnterMutex(pCache->pGroup);
   727    837   
   728    838     h = iOld%pCache->nHash;
   729    839     pp = &pCache->apHash[h];
   730    840     while( (*pp)!=pPage ){
   731    841       pp = &(*pp)->pNext;
   732    842     }
   733    843     *pp = pPage->pNext;
................................................................................
   736    846     pPage->iKey = iNew;
   737    847     pPage->pNext = pCache->apHash[h];
   738    848     pCache->apHash[h] = pPage;
   739    849     if( iNew>pCache->iMaxKey ){
   740    850       pCache->iMaxKey = iNew;
   741    851     }
   742    852   
   743         -  pcache1LeaveMutex();
          853  +  pcache1LeaveMutex(pCache->pGroup);
   744    854   }
   745    855   
   746    856   /*
   747    857   ** Implementation of the sqlite3_pcache.xTruncate method. 
   748    858   **
   749    859   ** Discard all unpinned pages in the cache with a page number equal to
   750    860   ** or greater than parameter iLimit. Any pinned pages with a page number
   751    861   ** equal to or greater than iLimit are implicitly unpinned.
   752    862   */
   753    863   static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
   754    864     PCache1 *pCache = (PCache1 *)p;
   755         -  pcache1EnterMutex();
          865  +  pcache1EnterMutex(pCache->pGroup);
   756    866     if( iLimit<=pCache->iMaxKey ){
   757    867       pcache1TruncateUnsafe(pCache, iLimit);
   758    868       pCache->iMaxKey = iLimit-1;
   759    869     }
   760         -  pcache1LeaveMutex();
          870  +  pcache1LeaveMutex(pCache->pGroup);
   761    871   }
   762    872   
   763    873   /*
   764    874   ** Implementation of the sqlite3_pcache.xDestroy method. 
   765    875   **
   766    876   ** Destroy a cache allocated using pcache1Create().
   767    877   */
   768    878   static void pcache1Destroy(sqlite3_pcache *p){
   769    879     PCache1 *pCache = (PCache1 *)p;
          880  +  PGroup *pGroup = pCache->pGroup;
   770    881     assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
   771         -  pcache1EnterMutex();
          882  +  pcache1EnterMutex(pGroup);
   772    883     pcache1TruncateUnsafe(pCache, 0);
   773         -  pcache1.nMaxPage -= pCache->nMax;
   774         -  pcache1.nMinPage -= pCache->nMin;
   775         -  pcache1EnforceMaxPage();
   776         -  pcache1LeaveMutex();
          884  +  pGroup->nMaxPage -= pCache->nMax;
          885  +  pGroup->nMinPage -= pCache->nMin;
          886  +  pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
          887  +  pcache1EnforceMaxPage(pGroup);
          888  +  pcache1LeaveMutex(pGroup);
   777    889     sqlite3_free(pCache->apHash);
   778    890     sqlite3_free(pCache);
   779    891   }
   780    892   
   781    893   /*
   782    894   ** This function is called during initialization (sqlite3_initialize()) to
   783    895   ** install the default pluggable cache module, assuming the user has not
................................................................................
   808    920   **
   809    921   ** nReq is the number of bytes of memory required. Once this much has
   810    922   ** been released, the function returns. The return value is the total number 
   811    923   ** of bytes of memory released.
   812    924   */
   813    925   int sqlite3PcacheReleaseMemory(int nReq){
   814    926     int nFree = 0;
          927  +  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
          928  +  assert( sqlite3_mutex_notheld(pcache1.mutex) );
   815    929     if( pcache1.pStart==0 ){
   816    930       PgHdr1 *p;
   817         -    pcache1EnterMutex();
   818         -    while( (nReq<0 || nFree<nReq) && ((p=pcache1.pLruTail)!=0) ){
          931  +    pcache1EnterMutex(&pcache1.grp);
          932  +    while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
   819    933         nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
   820    934         pcache1PinPage(p);
   821    935         pcache1RemoveFromHash(p);
   822    936         pcache1FreePage(p);
   823    937       }
   824         -    pcache1LeaveMutex();
          938  +    pcache1LeaveMutex(&pcache1.grp);
   825    939     }
   826    940     return nFree;
   827    941   }
   828    942   #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
   829    943   
   830    944   #ifdef SQLITE_TEST
   831    945   /*
................................................................................
   836    950     int *pnCurrent,      /* OUT: Total number of pages cached */
   837    951     int *pnMax,          /* OUT: Global maximum cache size */
   838    952     int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
   839    953     int *pnRecyclable    /* OUT: Total number of pages available for recycling */
   840    954   ){
   841    955     PgHdr1 *p;
   842    956     int nRecyclable = 0;
   843         -  for(p=pcache1.pLruHead; p; p=p->pLruNext){
          957  +  for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
   844    958       nRecyclable++;
   845    959     }
   846         -  *pnCurrent = pcache1.nCurrentPage;
   847         -  *pnMax = pcache1.nMaxPage;
   848         -  *pnMin = pcache1.nMinPage;
          960  +  *pnCurrent = pcache1.grp.nCurrentPage;
          961  +  *pnMax = pcache1.grp.nMaxPage;
          962  +  *pnMin = pcache1.grp.nMinPage;
   849    963     *pnRecyclable = nRecyclable;
   850    964   }
   851    965   #endif

Changes to src/prepare.c.

   789    789   
   790    790   #ifndef SQLITE_OMIT_UTF16
   791    791   /*
   792    792   ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
   793    793   */
   794    794   static int sqlite3Prepare16(
   795    795     sqlite3 *db,              /* Database handle. */ 
   796         -  const void *zSql,         /* UTF-8 encoded SQL statement. */
          796  +  const void *zSql,         /* UTF-16 encoded SQL statement. */
   797    797     int nBytes,               /* Length of zSql in bytes. */
   798    798     int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
   799    799     sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   800    800     const void **pzTail       /* OUT: End of parsed string */
   801    801   ){
   802    802     /* This function currently works by first transforming the UTF-16
   803    803     ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
................................................................................
   841    841   ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
   842    842   ** sqlite3_step().  In the new version, the original SQL text is retained
   843    843   ** and the statement is automatically recompiled if an schema change
   844    844   ** occurs.
   845    845   */
   846    846   int sqlite3_prepare16(
   847    847     sqlite3 *db,              /* Database handle. */ 
   848         -  const void *zSql,         /* UTF-8 encoded SQL statement. */
          848  +  const void *zSql,         /* UTF-16 encoded SQL statement. */
   849    849     int nBytes,               /* Length of zSql in bytes. */
   850    850     sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   851    851     const void **pzTail       /* OUT: End of parsed string */
   852    852   ){
   853    853     int rc;
   854    854     rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
   855    855     assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
   856    856     return rc;
   857    857   }
   858    858   int sqlite3_prepare16_v2(
   859    859     sqlite3 *db,              /* Database handle. */ 
   860         -  const void *zSql,         /* UTF-8 encoded SQL statement. */
          860  +  const void *zSql,         /* UTF-16 encoded SQL statement. */
   861    861     int nBytes,               /* Length of zSql in bytes. */
   862    862     sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   863    863     const void **pzTail       /* OUT: End of parsed string */
   864    864   ){
   865    865     int rc;
   866    866     rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
   867    867     assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
   868    868     return rc;
   869    869   }
   870    870   
   871    871   #endif /* SQLITE_OMIT_UTF16 */

Changes to src/printf.c.

   759    759       if( !p->useMalloc ){
   760    760         p->tooBig = 1;
   761    761         N = p->nAlloc - p->nChar - 1;
   762    762         if( N<=0 ){
   763    763           return;
   764    764         }
   765    765       }else{
          766  +      char *zOld = (p->zText==p->zBase ? 0 : p->zText);
   766    767         i64 szNew = p->nChar;
   767    768         szNew += N + 1;
   768    769         if( szNew > p->mxAlloc ){
   769    770           sqlite3StrAccumReset(p);
   770    771           p->tooBig = 1;
   771    772           return;
   772    773         }else{
   773    774           p->nAlloc = (int)szNew;
   774    775         }
   775    776         if( p->useMalloc==1 ){
   776         -        zNew = sqlite3DbMallocRaw(p->db, p->nAlloc );
          777  +        zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   777    778         }else{
   778         -        zNew = sqlite3_malloc(p->nAlloc);
          779  +        zNew = sqlite3_realloc(zOld, p->nAlloc);
   779    780         }
   780    781         if( zNew ){
   781         -        memcpy(zNew, p->zText, p->nChar);
   782         -        sqlite3StrAccumReset(p);
          782  +        if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
   783    783           p->zText = zNew;
   784    784         }else{
   785    785           p->mallocFailed = 1;
   786    786           sqlite3StrAccumReset(p);
   787    787           return;
   788    788         }
   789    789       }
................................................................................
   930    930   }
   931    931   
   932    932   /*
   933    933   ** sqlite3_snprintf() works like snprintf() except that it ignores the
   934    934   ** current locale settings.  This is important for SQLite because we
   935    935   ** are not able to use a "," as the decimal point in place of "." as
   936    936   ** specified by some locales.
          937  +**
          938  +** Oops:  The first two arguments of sqlite3_snprintf() are backwards
          939  +** from the snprintf() standard.  Unfortunately, it is too late to change
          940  +** this without breaking compatibility, so we just have to live with the
          941  +** mistake.
          942  +**
          943  +** sqlite3_vsnprintf() is the varargs version.
   937    944   */
          945  +char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
          946  +  StrAccum acc;
          947  +  if( n<=0 ) return zBuf;
          948  +  sqlite3StrAccumInit(&acc, zBuf, n, 0);
          949  +  acc.useMalloc = 0;
          950  +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
          951  +  return sqlite3StrAccumFinish(&acc);
          952  +}
   938    953   char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
   939    954     char *z;
   940    955     va_list ap;
   941         -  StrAccum acc;
   942         -
   943         -  if( n<=0 ){
   944         -    return zBuf;
   945         -  }
   946         -  sqlite3StrAccumInit(&acc, zBuf, n, 0);
   947         -  acc.useMalloc = 0;
   948    956     va_start(ap,zFormat);
   949         -  sqlite3VXPrintf(&acc, 0, zFormat, ap);
          957  +  z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
   950    958     va_end(ap);
   951         -  z = sqlite3StrAccumFinish(&acc);
   952    959     return z;
   953    960   }
   954    961   
   955    962   /*
   956    963   ** This is the routine that actually formats the sqlite3_log() message.
   957    964   ** We house it in a separate routine from sqlite3_log() to avoid using
   958    965   ** stack space on small-stack systems when logging is disabled.

Changes to src/shell.c.

    34     34   # include <sys/types.h>
    35     35   #endif
    36     36   
    37     37   #ifdef __OS2__
    38     38   # include <unistd.h>
    39     39   #endif
    40     40   
           41  +#ifdef HAVE_EDITLINE
           42  +# include <editline/editline.h>
           43  +#endif
    41     44   #if defined(HAVE_READLINE) && HAVE_READLINE==1
    42     45   # include <readline/readline.h>
    43     46   # include <readline/history.h>
    44         -#else
           47  +#endif
           48  +#if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1)
    45     49   # define readline(p) local_getline(p,stdin)
    46     50   # define add_history(X)
    47     51   # define read_history(X)
    48     52   # define write_history(X)
    49     53   # define stifle_history(X)
    50     54   #endif
    51     55   
................................................................................
   976    980     if( pArg && pArg->out ){
   977    981       
   978    982       iHiwtr = iCur = -1;
   979    983       sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
   980    984       fprintf(pArg->out, "Memory Used:                         %d (max %d) bytes\n", iCur, iHiwtr);
   981    985       iHiwtr = iCur = -1;
   982    986       sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
   983         -    fprintf(pArg->out, "Number of Allocations:               %d (max %d)\n", iCur, iHiwtr);
          987  +    fprintf(pArg->out, "Number of Outstanding Allocations:   %d (max %d)\n", iCur, iHiwtr);
   984    988   /*
   985    989   ** Not currently used by the CLI.
   986    990   **    iHiwtr = iCur = -1;
   987    991   **    sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
   988    992   **    fprintf(pArg->out, "Number of Pcache Pages Used:         %d (max %d) pages\n", iCur, iHiwtr);
   989    993   */
   990    994       iHiwtr = iCur = -1;
................................................................................
  1015   1019   #endif
  1016   1020     }
  1017   1021   
  1018   1022     if( pArg && pArg->out && db ){
  1019   1023       iHiwtr = iCur = -1;
  1020   1024       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
  1021   1025       fprintf(pArg->out, "Lookaside Slots Used:                %d (max %d)\n", iCur, iHiwtr);
         1026  +    sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
         1027  +    fprintf(pArg->out, "Successful lookaside attempts:       %d\n", iHiwtr);
         1028  +    sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
         1029  +    fprintf(pArg->out, "Lookaside failures due to size:      %d\n", iHiwtr);
         1030  +    sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
         1031  +    fprintf(pArg->out, "Lookaside failures due to OOM:       %d\n", iHiwtr);
  1022   1032       iHiwtr = iCur = -1;
  1023   1033       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
  1024   1034       fprintf(pArg->out, "Pager Heap Usage:                    %d bytes\n", iCur); 
  1025   1035       iHiwtr = iCur = -1;
  1026   1036       sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
  1027   1037       fprintf(pArg->out, "Schema Heap Usage:                   %d bytes\n", iCur); 
  1028   1038       iHiwtr = iCur = -1;
................................................................................
  2534   2544     */
  2535   2545   #ifdef SIGINT
  2536   2546     signal(SIGINT, interrupt_handler);
  2537   2547   #endif
  2538   2548   
  2539   2549     /* Do an initial pass through the command-line argument to locate
  2540   2550     ** the name of the database file, the name of the initialization file,
         2551  +  ** the size of the alternative malloc heap,
  2541   2552     ** and the first command to execute.
  2542   2553     */
  2543   2554     for(i=1; i<argc-1; i++){
  2544   2555       char *z;
  2545   2556       if( argv[i][0]!='-' ) break;
  2546   2557       z = argv[i];
  2547   2558       if( z[0]=='-' && z[1]=='-' ) z++;
................................................................................
  2552   2563         zInitFile = argv[i];
  2553   2564       /* Need to check for batch mode here to so we can avoid printing
  2554   2565       ** informational messages (like from process_sqliterc) before 
  2555   2566       ** we do the actual processing of arguments later in a second pass.
  2556   2567       */
  2557   2568       }else if( strcmp(argv[i],"-batch")==0 ){
  2558   2569         stdin_is_interactive = 0;
         2570  +    }else if( strcmp(argv[i],"-heap")==0 ){
         2571  +      int j, c;
         2572  +      const char *zSize;
         2573  +      sqlite3_int64 szHeap;
         2574  +
         2575  +      zSize = argv[++i];
         2576  +      szHeap = atoi(zSize);
         2577  +      for(j=0; (c = zSize[j])!=0; j++){
         2578  +        if( c=='M' ){ szHeap *= 1000000; break; }
         2579  +        if( c=='K' ){ szHeap *= 1000; break; }
         2580  +        if( c=='G' ){ szHeap *= 1000000000; break; }
         2581  +      }
         2582  +      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
         2583  +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
         2584  +      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
         2585  +#endif
  2559   2586       }
  2560   2587     }
  2561   2588     if( i<argc ){
  2562   2589   #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
  2563   2590       data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
  2564   2591   #else
  2565   2592       data.zDbFilename = argv[i++];
................................................................................
  2658   2685       }else if( strcmp(z,"-version")==0 ){
  2659   2686         printf("%s\n", sqlite3_libversion());
  2660   2687         return 0;
  2661   2688       }else if( strcmp(z,"-interactive")==0 ){
  2662   2689         stdin_is_interactive = 1;
  2663   2690       }else if( strcmp(z,"-batch")==0 ){
  2664   2691         stdin_is_interactive = 0;
         2692  +    }else if( strcmp(z,"-heap")==0 ){
         2693  +      i++;
  2665   2694       }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
  2666   2695         usage(1);
  2667   2696       }else{
  2668   2697         fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
  2669   2698         fprintf(stderr,"Use -help for a list of options.\n");
  2670   2699         return 1;
  2671   2700       }
................................................................................
  2719   2748         free(zHome);
  2720   2749       }else{
  2721   2750         rc = process_input(&data, stdin);
  2722   2751       }
  2723   2752     }
  2724   2753     set_table_name(&data, 0);
  2725   2754     if( data.db ){
  2726         -    if( sqlite3_close(data.db)!=SQLITE_OK ){
  2727         -      fprintf(stderr,"Error: cannot close database \"%s\"\n",
  2728         -              sqlite3_errmsg(db));
  2729         -      rc++;
  2730         -    }
         2755  +    sqlite3_close(data.db);
  2731   2756     }
  2732   2757     return rc;
  2733   2758   }

Changes to src/sqlite.h.in.

   381    381   #define SQLITE_BUSY         5   /* The database file is locked */
   382    382   #define SQLITE_LOCKED       6   /* A table in the database is locked */
   383    383   #define SQLITE_NOMEM        7   /* A malloc() failed */
   384    384   #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
   385    385   #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
   386    386   #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
   387    387   #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
   388         -#define SQLITE_NOTFOUND    12   /* NOT USED. Table or record not found */
          388  +#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
   389    389   #define SQLITE_FULL        13   /* Insertion failed because database is full */
   390    390   #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
   391    391   #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
   392    392   #define SQLITE_EMPTY       16   /* Database is empty */
   393    393   #define SQLITE_SCHEMA      17   /* The database schema changed */
   394    394   #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
   395    395   #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
................................................................................
   613    613   ** write return values.  Potential uses for xFileControl() might be
   614    614   ** functions to enable blocking locks with timeouts, to change the
   615    615   ** locking strategy (for example to use dot-file locks), to inquire
   616    616   ** about the status of a lock, or to break stale locks.  The SQLite
   617    617   ** core reserves all opcodes less than 100 for its own use.
   618    618   ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
   619    619   ** Applications that define a custom xFileControl method should use opcodes
   620         -** greater than 100 to avoid conflicts.
          620  +** greater than 100 to avoid conflicts.  VFS implementations should
          621  +** return [SQLITE_NOTFOUND] for file control opcodes that they do not
          622  +** recognize.
   621    623   **
   622    624   ** The xSectorSize() method returns the sector size of the
   623    625   ** device that underlies the file.  The sector size is the
   624    626   ** minimum write that can be performed without disturbing
   625    627   ** other bytes in the file.  The xDeviceCharacteristics()
   626    628   ** method returns a bit vector describing behaviors of the
   627    629   ** underlying device:
................................................................................
   706    708   ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
   707    709   ** extends and truncates the database file in chunks of a size specified
   708    710   ** by the user. The fourth argument to [sqlite3_file_control()] should 
   709    711   ** point to an integer (type int) containing the new chunk-size to use
   710    712   ** for the nominated database. Allocating database file space in large
   711    713   ** chunks (say 1MB at a time), may reduce file-system fragmentation and
   712    714   ** improve performance on some systems.
          715  +**
          716  +** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
          717  +** to the [sqlite3_file] object associated with a particular database
          718  +** connection.  See the [sqlite3_file_control()] documentation for
          719  +** additional information.
          720  +**
          721  +** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
          722  +** SQLite and sent to all VFSes in place of a call to the xSync method
          723  +** when the database connection has [PRAGMA synchronous] set to OFF.)^
          724  +** Some specialized VFSes need this signal in order to operate correctly
          725  +** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
          726  +** VFSes do not need this signal and should silently ignore this opcode.
          727  +** Applications should not call [sqlite3_file_control()] with this
          728  +** opcode as doing so may disrupt the operation of the specilized VFSes
          729  +** that do require it.  
   713    730   */
   714    731   #define SQLITE_FCNTL_LOCKSTATE        1
   715    732   #define SQLITE_GET_LOCKPROXYFILE      2
   716    733   #define SQLITE_SET_LOCKPROXYFILE      3
   717    734   #define SQLITE_LAST_ERRNO             4
   718    735   #define SQLITE_FCNTL_SIZE_HINT        5
   719    736   #define SQLITE_FCNTL_CHUNK_SIZE       6
   720    737   #define SQLITE_FCNTL_FILE_POINTER     7
          738  +#define SQLITE_FCNTL_SYNC_OMITTED     8
   721    739   
   722    740   
   723    741   /*
   724    742   ** CAPI3REF: Mutex Handle
   725    743   **
   726    744   ** The mutex module within SQLite defines [sqlite3_mutex] to be an
   727    745   ** abstract type for a mutex object.  The SQLite core never looks
................................................................................
  1833   1851   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  1834   1852   ** results into memory obtained from [sqlite3_malloc()].
  1835   1853   ** The strings returned by these two routines should be
  1836   1854   ** released by [sqlite3_free()].  ^Both routines return a
  1837   1855   ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
  1838   1856   ** memory to hold the resulting string.
  1839   1857   **
  1840         -** ^(In sqlite3_snprintf() routine is similar to "snprintf()" from
         1858  +** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  1841   1859   ** the standard C library.  The result is written into the
  1842   1860   ** buffer supplied as the second parameter whose size is given by
  1843   1861   ** the first parameter. Note that the order of the
  1844   1862   ** first two parameters is reversed from snprintf().)^  This is an
  1845   1863   ** historical accident that cannot be fixed without breaking
  1846   1864   ** backwards compatibility.  ^(Note also that sqlite3_snprintf()
  1847   1865   ** returns a pointer to its buffer instead of the number of
................................................................................
  1851   1869   ** now without breaking compatibility.
  1852   1870   **
  1853   1871   ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
  1854   1872   ** guarantees that the buffer is always zero-terminated.  ^The first
  1855   1873   ** parameter "n" is the total size of the buffer, including space for
  1856   1874   ** the zero terminator.  So the longest string that can be completely
  1857   1875   ** written will be n-1 characters.
         1876  +**
         1877  +** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  1858   1878   **
  1859   1879   ** These routines all implement some additional formatting
  1860   1880   ** options that are useful for constructing SQL statements.
  1861   1881   ** All of the usual printf() formatting options apply.  In addition, there
  1862   1882   ** is are "%q", "%Q", and "%z" options.
  1863   1883   **
  1864   1884   ** ^(The %q option works like %s in that it substitutes a null-terminated
................................................................................
  1915   1935   ** ^(The "%z" formatting option works like "%s" but with the
  1916   1936   ** addition that after the string has been read and copied into
  1917   1937   ** the result, [sqlite3_free()] is called on the input string.)^
  1918   1938   */
  1919   1939   char *sqlite3_mprintf(const char*,...);
  1920   1940   char *sqlite3_vmprintf(const char*, va_list);
  1921   1941   char *sqlite3_snprintf(int,char*,const char*, ...);
         1942  +char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  1922   1943   
  1923   1944   /*
  1924   1945   ** CAPI3REF: Memory Allocation Subsystem
  1925   1946   **
  1926   1947   ** The SQLite core uses these three routines for all of its own
  1927   1948   ** internal memory allocation needs. "Core" in the previous sentence
  1928   1949   ** does not include operating-system specific VFS implementation.  The
................................................................................
  2292   2313   **
  2293   2314   ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
  2294   2315   ** <dd>The database is opened for reading and writing if possible, or reading
  2295   2316   ** only if the file is write protected by the operating system.  In either
  2296   2317   ** case the database must already exist, otherwise an error is returned.</dd>)^
  2297   2318   **
  2298   2319   ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
  2299         -** <dd>The database is opened for reading and writing, and is creates it if
         2320  +** <dd>The database is opened for reading and writing, and is created if
  2300   2321   ** it does not already exist. This is the behavior that is always used for
  2301   2322   ** sqlite3_open() and sqlite3_open16().</dd>)^
  2302   2323   ** </dl>
  2303   2324   **
  2304   2325   ** If the 3rd parameter to sqlite3_open_v2() is not one of the
  2305   2326   ** combinations shown above or one of the combinations shown above combined
  2306   2327   ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX],
................................................................................
  2641   2662   ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  2642   2663   */
  2643   2664   const char *sqlite3_sql(sqlite3_stmt *pStmt);
  2644   2665   
  2645   2666   /*
  2646   2667   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  2647   2668   **
  2648         -** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  2649         -** the [prepared statement] X is [SELECT] statement and false (zero) if
  2650         -** X is an [INSERT], [UPDATE], [DELETE], CREATE, DROP, [ANALYZE],
  2651         -** [ALTER], or [REINDEX] statement.
  2652         -** If X is a NULL pointer or any other kind of statement, including but
  2653         -** not limited to [ATTACH], [DETACH], [COMMIT], [ROLLBACK], [RELEASE],
  2654         -** [SAVEPOINT], [PRAGMA], or [VACUUM] the result of sqlite3_stmt_readonly(X) is
  2655         -** undefined.
         2669  +** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if 
         2670  +** and only if the [prepared statement] X makes no direct changes to
         2671  +** the content of the database file.
         2672  +**
         2673  +** Note that [application-defined SQL functions] or
         2674  +** [virtual tables] might change the database indirectly as a side effect.  
         2675  +** ^(For example, if an application defines a function "eval()" that 
         2676  +** calls [sqlite3_exec()], then the following SQL statement would
         2677  +** change the database file through side-effects:
         2678  +**
         2679  +** <blockquote><pre>
         2680  +**    SELECT eval('DELETE FROM t1') FROM t2;
         2681  +** </pre></blockquote>
         2682  +**
         2683  +** But because the [SELECT] statement does not change the database file
         2684  +** directly, sqlite3_stmt_readonly() would still return true.)^
         2685  +**
         2686  +** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
         2687  +** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
         2688  +** since the statements themselves do not actually modify the database but
         2689  +** rather they control the timing of when other statements modify the 
         2690  +** database.  ^The [ATTACH] and [DETACH] statements also cause
         2691  +** sqlite3_stmt_readonly() to return true since, while those statements
         2692  +** change the configuration of a database connection, they do not make 
         2693  +** changes to the content of the database files on disk.
  2656   2694   */
  2657   2695   int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  2658   2696   
  2659   2697   /*
  2660   2698   ** CAPI3REF: Dynamically Typed Value Object
  2661   2699   ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
  2662   2700   **
................................................................................
  3042   3080   ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
  3043   3081   ** Perhaps it was called on a [prepared statement] that has
  3044   3082   ** already been [sqlite3_finalize | finalized] or on one that had
  3045   3083   ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
  3046   3084   ** be the case that the same database connection is being used by two or
  3047   3085   ** more threads at the same moment in time.
  3048   3086   **
  3049         -** For all versions of SQLite up to and including 3.6.23.1, it was required
  3050         -** after sqlite3_step() returned anything other than [SQLITE_ROW] that
  3051         -** [sqlite3_reset()] be called before any subsequent invocation of
  3052         -** sqlite3_step().  Failure to invoke [sqlite3_reset()] in this way would
  3053         -** result in an [SQLITE_MISUSE] return from sqlite3_step().  But after
  3054         -** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()] 
  3055         -** automatically in this circumstance rather than returning [SQLITE_MISUSE].  
         3087  +** For all versions of SQLite up to and including 3.6.23.1, a call to
         3088  +** [sqlite3_reset()] was required after sqlite3_step() returned anything
         3089  +** other than [SQLITE_ROW] before any subsequent invocation of
         3090  +** sqlite3_step().  Failure to reset the prepared statement using 
         3091  +** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
         3092  +** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
         3093  +** calling [sqlite3_reset()] automatically in this circumstance rather
         3094  +** than returning [SQLITE_MISUSE].  This is not considered a compatibility
         3095  +** break because any application that ever receives an SQLITE_MISUSE error
         3096  +** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
         3097  +** can be used to restore the legacy behavior.
  3056   3098   **
  3057   3099   ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
  3058   3100   ** API always returns a generic error code, [SQLITE_ERROR], following any
  3059   3101   ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
  3060   3102   ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
  3061   3103   ** specific [error codes] that better describes the error.
  3062   3104   ** We admit that this is a goofy design.  The problem has been fixed
................................................................................
  3385   3427   ** will pick the one that involves the least amount of data conversion.
  3386   3428   ** If there is only a single implementation which does not care what text
  3387   3429   ** encoding is used, then the fourth argument should be [SQLITE_ANY].
  3388   3430   **
  3389   3431   ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
  3390   3432   ** function can gain access to this pointer using [sqlite3_user_data()].)^
  3391   3433   **
  3392         -** ^The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are
         3434  +** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
  3393   3435   ** pointers to C-language functions that implement the SQL function or
  3394   3436   ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
  3395   3437   ** callback only; NULL pointers must be passed as the xStep and xFinal
  3396   3438   ** parameters. ^An aggregate SQL function requires an implementation of xStep
  3397   3439   ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
  3398   3440   ** SQL function or aggregate, pass NULL poiners for all three function
  3399   3441   ** callbacks.
  3400   3442   **
  3401         -** ^(If the tenth parameter to sqlite3_create_function_v2() is not NULL,
         3443  +** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
  3402   3444   ** then it is destructor for the application data pointer. 
  3403   3445   ** The destructor is invoked when the function is deleted, either by being
  3404   3446   ** overloaded or when the database connection closes.)^
  3405   3447   ** ^The destructor is also invoked if the call to
  3406   3448   ** sqlite3_create_function_v2() fails.
  3407   3449   ** ^When the destructor callback of the tenth parameter is invoked, it
  3408   3450   ** is passed a single argument which is a copy of the application data 
................................................................................
  3498   3540   ** The C-language implementation of SQL functions and aggregates uses
  3499   3541   ** this set of interface routines to access the parameter values on
  3500   3542   ** the function or aggregate.
  3501   3543   **
  3502   3544   ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  3503   3545   ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
  3504   3546   ** define callbacks that implement the SQL functions and aggregates.
  3505         -** The 4th parameter to these callbacks is an array of pointers to
         3547  +** The 3rd parameter to these callbacks is an array of pointers to
  3506   3548   ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
  3507   3549   ** each parameter to the SQL function.  These routines are used to
  3508   3550   ** extract values from the [sqlite3_value] objects.
  3509   3551   **
  3510   3552   ** These routines work only with [protected sqlite3_value] objects.
  3511   3553   ** Any attempt to use these routines on an [unprotected sqlite3_value]
  3512   3554   ** object results in undefined behavior.
................................................................................
  5226   5268   #define SQLITE_MUTEX_RECURSIVE        1
  5227   5269   #define SQLITE_MUTEX_STATIC_MASTER    2
  5228   5270   #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
  5229   5271   #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
  5230   5272   #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
  5231   5273   #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
  5232   5274   #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
  5233         -#define SQLITE_MUTEX_STATIC_LRU2      7  /* lru page list */
         5275  +#define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
         5276  +#define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  5234   5277   
  5235   5278   /*
  5236   5279   ** CAPI3REF: Retrieve the mutex for a database connection
  5237   5280   **
  5238   5281   ** ^This interface returns a pointer the [sqlite3_mutex] object that 
  5239   5282   ** serializes access to the [database connection] given in the argument
  5240   5283   ** when the [threading mode] is Serialized.
................................................................................
  5377   5420   ** <dd>This parameter records the largest memory allocation request
  5378   5421   ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
  5379   5422   ** internal equivalents).  Only the value returned in the
  5380   5423   ** *pHighwater parameter to [sqlite3_status()] is of interest.  
  5381   5424   ** The value written into the *pCurrent parameter is undefined.</dd>)^
  5382   5425   **
  5383   5426   ** ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
  5384         -** <dd>This parameter records the number of separate memory allocations.</dd>)^
         5427  +** <dd>This parameter records the number of separate memory allocations
         5428  +** currently checked out.</dd>)^
  5385   5429   **
  5386   5430   ** ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
  5387   5431   ** <dd>This parameter returns the number of pages used out of the
  5388   5432   ** [pagecache memory allocator] that was configured using 
  5389   5433   ** [SQLITE_CONFIG_PAGECACHE].  The
  5390   5434   ** value returned is in pages, not in bytes.</dd>)^
  5391   5435   **
................................................................................
  5482   5526   ** The [sqlite3_db_status()] interface will return a non-zero error code
  5483   5527   ** if a discontinued or unsupported verb is invoked.
  5484   5528   **
  5485   5529   ** <dl>
  5486   5530   ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
  5487   5531   ** <dd>This parameter returns the number of lookaside memory slots currently
  5488   5532   ** checked out.</dd>)^
         5533  +**
         5534  +** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
         5535  +** <dd>This parameter returns the number malloc attempts that were 
         5536  +** satisfied using lookaside memory. Only the high-water value is meaningful;
         5537  +** the current value is always zero.
         5538  +** checked out.</dd>)^
         5539  +**
         5540  +** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
         5541  +** <dd>This parameter returns the number malloc attempts that might have
         5542  +** been satisfied using lookaside memory but failed due to the amount of
         5543  +** memory requested being larger than the lookaside slot size.
         5544  +** Only the high-water value is meaningful;
         5545  +** the current value is always zero.
         5546  +** checked out.</dd>)^
         5547  +**
         5548  +** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
         5549  +** <dd>This parameter returns the number malloc attempts that might have
         5550  +** been satisfied using lookaside memory but failed due to all lookaside
         5551  +** memory already being in use.
         5552  +** Only the high-water value is meaningful;
         5553  +** the current value is always zero.
         5554  +** checked out.</dd>)^
  5489   5555   **
  5490   5556   ** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
  5491   5557   ** <dd>This parameter returns the approximate number of of bytes of heap
  5492   5558   ** memory used by all pager caches associated with the database connection.)^
  5493   5559   ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
  5494   5560   **
  5495   5561   ** ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
................................................................................
  5505   5571   ** <dd>This parameter returns the approximate number of of bytes of heap
  5506   5572   ** and lookaside memory used by all prepared statements associated with
  5507   5573   ** the database connection.)^
  5508   5574   ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  5509   5575   ** </dd>
  5510   5576   ** </dl>
  5511   5577   */
  5512         -#define SQLITE_DBSTATUS_LOOKASIDE_USED     0
  5513         -#define SQLITE_DBSTATUS_CACHE_USED         1
  5514         -#define SQLITE_DBSTATUS_SCHEMA_USED        2
  5515         -#define SQLITE_DBSTATUS_STMT_USED          3
  5516         -#define SQLITE_DBSTATUS_MAX                3   /* Largest defined DBSTATUS */
         5578  +#define SQLITE_DBSTATUS_LOOKASIDE_USED       0
         5579  +#define SQLITE_DBSTATUS_CACHE_USED           1
         5580  +#define SQLITE_DBSTATUS_SCHEMA_USED          2
         5581  +#define SQLITE_DBSTATUS_STMT_USED            3
         5582  +#define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
         5583  +#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
         5584  +#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
         5585  +#define SQLITE_DBSTATUS_MAX                  6   /* Largest defined DBSTATUS */
  5517   5586   
  5518   5587   
  5519   5588   /*
  5520   5589   ** CAPI3REF: Prepared Statement Status
  5521   5590   **
  5522   5591   ** ^(Each prepared statement maintains various
  5523   5592   ** [SQLITE_STMTSTATUS_SORT | counters] that measure the number
................................................................................
  5637   5706   **
  5638   5707   ** ^SQLite invokes the xCreate() method to construct a new cache instance.
  5639   5708   ** SQLite will typically create one cache instance for each open database file,
  5640   5709   ** though this is not guaranteed. ^The
  5641   5710   ** first parameter, szPage, is the size in bytes of the pages that must
  5642   5711   ** be allocated by the cache.  ^szPage will not be a power of two.  ^szPage
  5643   5712   ** will the page size of the database file that is to be cached plus an
  5644         -** increment (here called "R") of about 100 or 200.  SQLite will use the
         5713  +** increment (here called "R") of less than 250.  SQLite will use the
  5645   5714   ** extra R bytes on each page to store metadata about the underlying
  5646   5715   ** database page on disk.  The value of R depends
  5647   5716   ** on the SQLite version, the target platform, and how SQLite was compiled.
  5648         -** ^R is constant for a particular build of SQLite.  ^The second argument to
         5717  +** ^(R is constant for a particular build of SQLite. Except, there are two
         5718  +** distinct values of R when SQLite is compiled with the proprietary
         5719  +** ZIPVFS extension.)^  ^The second argument to
  5649   5720   ** xCreate(), bPurgeable, is true if the cache being created will
  5650   5721   ** be used to cache database pages of a file stored on disk, or
  5651   5722   ** false if it is used for an in-memory database. The cache implementation
  5652   5723   ** does not have to do anything special based with the value of bPurgeable;
  5653   5724   ** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
  5654   5725   ** never invoke xUnpin() except to deliberately delete a page.
  5655   5726   ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
................................................................................
  5673   5744   ** 8-byte boundary. The page to be fetched is determined by the key. ^The
  5674   5745   ** mimimum key value is 1.  After it has been retrieved using xFetch, the page 
  5675   5746   ** is considered to be "pinned".
  5676   5747   **
  5677   5748   ** If the requested page is already in the page cache, then the page cache
  5678   5749   ** implementation must return a pointer to the page buffer with its content
  5679   5750   ** intact.  If the requested page is not already in the cache, then the
  5680         -** behavior of the cache implementation should use the value of the createFlag
         5751  +** cache implementation should use the value of the createFlag
  5681   5752   ** parameter to help it determined what action to take:
  5682   5753   **
  5683   5754   ** <table border=1 width=85% align=center>
  5684   5755   ** <tr><th> createFlag <th> Behaviour when page is not already in cache
  5685   5756   ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
  5686   5757   ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
  5687   5758   **                 Otherwise return NULL.
................................................................................
  5757   5828   **
  5758   5829   ** The backup API copies the content of one database into another.
  5759   5830   ** It is useful either for creating backups of databases or
  5760   5831   ** for copying in-memory databases to or from persistent files. 
  5761   5832   **
  5762   5833   ** See Also: [Using the SQLite Online Backup API]
  5763   5834   **
  5764         -** ^Exclusive access is required to the destination database for the 
  5765         -** duration of the operation. ^However the source database is only
  5766         -** read-locked while it is actually being read; it is not locked
  5767         -** continuously for the entire backup operation. ^Thus, the backup may be
  5768         -** performed on a live source database without preventing other users from
         5835  +** ^SQLite holds a write transaction open on the destination database file
         5836  +** for the duration of the backup operation.
         5837  +** ^The source database is read-locked only while it is being read;
         5838  +** it is not locked continuously for the entire backup operation.
         5839  +** ^Thus, the backup may be performed on a live source database without
         5840  +** preventing other database connections from
  5769   5841   ** reading or writing to the source database while the backup is underway.
  5770   5842   ** 
  5771   5843   ** ^(To perform a backup operation: 
  5772   5844   **   <ol>
  5773   5845   **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
  5774   5846   **         backup, 
  5775   5847   **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer 
................................................................................
  5788   5860   ** ^The database name is "main" for the main database, "temp" for the
  5789   5861   ** temporary database, or the name specified after the AS keyword in
  5790   5862   ** an [ATTACH] statement for an attached database.
  5791   5863   ** ^The S and M arguments passed to 
  5792   5864   ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
  5793   5865   ** and database name of the source database, respectively.
  5794   5866   ** ^The source and destination [database connections] (parameters S and D)
  5795         -** must be different or else sqlite3_backup_init(D,N,S,M) will file with
         5867  +** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
  5796   5868   ** an error.
  5797   5869   **
  5798   5870   ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
  5799         -** returned and an error code and error message are store3d in the
         5871  +** returned and an error code and error message are stored in the
  5800   5872   ** destination [database connection] D.
  5801   5873   ** ^The error code and message for the failed call to sqlite3_backup_init()
  5802   5874   ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
  5803   5875   ** [sqlite3_errmsg16()] functions.
  5804   5876   ** ^A successful call to sqlite3_backup_init() returns a pointer to an
  5805   5877   ** [sqlite3_backup] object.
  5806   5878   ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
................................................................................
  5809   5881   **
  5810   5882   ** <b>sqlite3_backup_step()</b>
  5811   5883   **
  5812   5884   ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between 
  5813   5885   ** the source and destination databases specified by [sqlite3_backup] object B.
  5814   5886   ** ^If N is negative, all remaining source pages are copied. 
  5815   5887   ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
  5816         -** are still more pages to be copied, then the function resturns [SQLITE_OK].
         5888  +** are still more pages to be copied, then the function returns [SQLITE_OK].
  5817   5889   ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
  5818   5890   ** from source to destination, then it returns [SQLITE_DONE].
  5819   5891   ** ^If an error occurs while running sqlite3_backup_step(B,N),
  5820   5892   ** then an [error code] is returned. ^As well as [SQLITE_OK] and
  5821   5893   ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
  5822   5894   ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
  5823   5895   ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
  5824   5896   **
  5825   5897   ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
  5826   5898   ** <ol>
  5827   5899   ** <li> the destination database was opened read-only, or
  5828   5900   ** <li> the destination database is using write-ahead-log journaling
  5829   5901   ** and the destination and source page sizes differ, or
  5830         -** <li> The destination database is an in-memory database and the
         5902  +** <li> the destination database is an in-memory database and the
  5831   5903   ** destination and source page sizes differ.
  5832   5904   ** </ol>)^
  5833   5905   **
  5834   5906   ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
  5835   5907   ** the [sqlite3_busy_handler | busy-handler function]
  5836   5908   ** is invoked (if one is specified). ^If the 
  5837   5909   ** busy-handler returns non-zero before the lock is available, then 
................................................................................
  6154   6226   ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
  6155   6227   ** configured by this function.
  6156   6228   **
  6157   6229   ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
  6158   6230   ** from SQL.
  6159   6231   **
  6160   6232   ** ^Every new [database connection] defaults to having the auto-checkpoint
  6161         -** enabled with a threshold of 1000 pages.  The use of this interface
         6233  +** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
         6234  +** pages.  The use of this interface
  6162   6235   ** is only necessary if the default setting is found to be suboptimal
  6163   6236   ** for a particular application.
  6164   6237   */
  6165   6238   int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  6166   6239   
  6167   6240   /*
  6168   6241   ** CAPI3REF: Checkpoint a database

Changes to src/sqliteInt.h.

   733    733   */
   734    734   struct Lookaside {
   735    735     u16 sz;                 /* Size of each buffer in bytes */
   736    736     u8 bEnabled;            /* False to disable new lookaside allocations */
   737    737     u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
   738    738     int nOut;               /* Number of buffers currently checked out */
   739    739     int mxOut;              /* Highwater mark for nOut */
          740  +  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
   740    741     LookasideSlot *pFree;   /* List of available buffers */
   741    742     void *pStart;           /* First byte of available memory space */
   742    743     void *pEnd;             /* First byte past end of available space */
   743    744   };
   744    745   struct LookasideSlot {
   745    746     LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
   746    747   };
................................................................................
   811    812       u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
   812    813     } init;
   813    814     int nExtension;               /* Number of loaded extensions */
   814    815     void **aExtension;            /* Array of shared library handles */
   815    816     struct Vdbe *pVdbe;           /* List of active virtual machines */
   816    817     int activeVdbeCnt;            /* Number of VDBEs currently executing */
   817    818     int writeVdbeCnt;             /* Number of active VDBEs that are writing */
          819  +  int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */
   818    820     void (*xTrace)(void*,const char*);        /* Trace function */
   819    821     void *pTraceArg;                          /* Argument to the trace function */
   820    822     void (*xProfile)(void*,const char*,u64);  /* Profiling function */
   821    823     void *pProfileArg;                        /* Argument to profile function */
   822    824     void *pCommitArg;                 /* Argument to xCommitCallback() */   
   823    825     int (*xCommitCallback)(void*);    /* Invoked at every commit. */
   824    826     void *pRollbackArg;               /* Argument to xRollbackCallback() */   

Changes to src/status.c.

   111    111         *pCurrent = db->lookaside.nOut;
   112    112         *pHighwater = db->lookaside.mxOut;
   113    113         if( resetFlag ){
   114    114           db->lookaside.mxOut = db->lookaside.nOut;
   115    115         }
   116    116         break;
   117    117       }
          118  +
          119  +    case SQLITE_DBSTATUS_LOOKASIDE_HIT:
          120  +    case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
          121  +    case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
          122  +      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
          123  +      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
          124  +      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
          125  +      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
          126  +      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
          127  +      *pCurrent = 0;
          128  +      *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
          129  +      if( resetFlag ){
          130  +        db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
          131  +      }
          132  +      break;
          133  +    }
   118    134   
   119    135       /* 
   120    136       ** Return an approximation for the amount of memory currently used
   121    137       ** by all pagers associated with the given database connection.  The
   122    138       ** highwater mark is meaningless and is returned as zero.
   123    139       */
   124    140       case SQLITE_DBSTATUS_CACHE_USED: {

Changes to src/tclsqlite.c.

  3577   3577       extern int Sqlitetestintarray_Init(Tcl_Interp*);
  3578   3578       extern int Sqlitetestvfs_Init(Tcl_Interp *);
  3579   3579       extern int SqlitetestStat_Init(Tcl_Interp*);
  3580   3580       extern int Sqlitetestrtree_Init(Tcl_Interp*);
  3581   3581       extern int Sqlitequota_Init(Tcl_Interp*);
  3582   3582       extern int Sqlitemultiplex_Init(Tcl_Interp*);
  3583   3583       extern int SqliteSuperlock_Init(Tcl_Interp*);
         3584  +
         3585  +#ifdef SQLITE_ENABLE_ZIPVFS
         3586  +    extern int Zipvfs_Init(Tcl_Interp*);
         3587  +    Zipvfs_Init(interp);
         3588  +#endif
  3584   3589   
  3585   3590       Sqliteconfig_Init(interp);
  3586   3591       Sqlitetest1_Init(interp);
  3587   3592       Sqlitetest2_Init(interp);
  3588   3593       Sqlitetest3_Init(interp);
  3589   3594       Sqlitetest4_Init(interp);
  3590   3595       Sqlitetest5_Init(interp);

Changes to src/test1.c.

  4815   4815     if( objc!=2 ){
  4816   4816       Tcl_AppendResult(interp, "wrong # args: should be \"",
  4817   4817           Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
  4818   4818       return TCL_ERROR;
  4819   4819     }
  4820   4820     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  4821   4821     rc = sqlite3_file_control(db, 0, 0, &iArg);
  4822         -  assert( rc==SQLITE_ERROR );
         4822  +  assert( rc==SQLITE_NOTFOUND );
  4823   4823     rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
  4824   4824     assert( rc==SQLITE_ERROR );
  4825   4825     rc = sqlite3_file_control(db, "main", -1, &iArg);
  4826         -  assert( rc==SQLITE_ERROR );
         4826  +  assert( rc==SQLITE_NOTFOUND );
  4827   4827     rc = sqlite3_file_control(db, "temp", -1, &iArg);
  4828         -  assert( rc==SQLITE_ERROR );
         4828  +  assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR );
  4829   4829   
  4830   4830     return TCL_OK;
  4831   4831   }
  4832   4832   
  4833   4833   
  4834   4834   /*
  4835   4835   ** tclcmd:   file_control_lasterrno_test DB

Changes to src/test_config.c.

   128    128   #endif
   129    129   
   130    130   #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
   131    131     Tcl_SetVar2(interp, "sqlite_options", "autoindex", "0", TCL_GLOBAL_ONLY);
   132    132   #else
   133    133     Tcl_SetVar2(interp, "sqlite_options", "autoindex", "1", TCL_GLOBAL_ONLY);
   134    134   #endif
          135  +
          136  +#ifdef SQLITE_OMIT_AUTORESET
          137  +  Tcl_SetVar2(interp, "sqlite_options", "autoreset", "0", TCL_GLOBAL_ONLY);
          138  +#else
          139  +  Tcl_SetVar2(interp, "sqlite_options", "autoreset", "1", TCL_GLOBAL_ONLY);
          140  +#endif
   135    141   
   136    142   #ifdef SQLITE_OMIT_AUTOVACUUM
   137    143     Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "0", TCL_GLOBAL_ONLY);
   138    144   #else
   139    145     Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "1", TCL_GLOBAL_ONLY);
   140    146   #endif /* SQLITE_OMIT_AUTOVACUUM */
   141    147   #if !defined(SQLITE_DEFAULT_AUTOVACUUM)

Changes to src/test_malloc.c.

  1286   1286     const char *zOpName;
  1287   1287     sqlite3 *db;
  1288   1288     int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
  1289   1289     static const struct {
  1290   1290       const char *zName;
  1291   1291       int op;
  1292   1292     } aOp[] = {
  1293         -    { "SQLITE_DBSTATUS_LOOKASIDE_USED",    SQLITE_DBSTATUS_LOOKASIDE_USED   },
  1294         -    { "SQLITE_DBSTATUS_CACHE_USED",        SQLITE_DBSTATUS_CACHE_USED       },
  1295         -    { "SQLITE_DBSTATUS_SCHEMA_USED",       SQLITE_DBSTATUS_SCHEMA_USED      },
  1296         -    { "SQLITE_DBSTATUS_STMT_USED",         SQLITE_DBSTATUS_STMT_USED        }
         1293  +    { "LOOKASIDE_USED",      SQLITE_DBSTATUS_LOOKASIDE_USED      },
         1294  +    { "CACHE_USED",          SQLITE_DBSTATUS_CACHE_USED          },
         1295  +    { "SCHEMA_USED",         SQLITE_DBSTATUS_SCHEMA_USED         },
         1296  +    { "STMT_USED",           SQLITE_DBSTATUS_STMT_USED           },
         1297  +    { "LOOKASIDE_HIT",       SQLITE_DBSTATUS_LOOKASIDE_HIT       },
         1298  +    { "LOOKASIDE_MISS_SIZE", SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE },
         1299  +    { "LOOKASIDE_MISS_FULL", SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL }
  1297   1300     };
  1298   1301     Tcl_Obj *pResult;
  1299   1302     if( objc!=4 ){
  1300   1303       Tcl_WrongNumArgs(interp, 1, objv, "PARAMETER RESETFLAG");
  1301   1304       return TCL_ERROR;
  1302   1305     }
  1303   1306     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1304   1307     zOpName = Tcl_GetString(objv[2]);
         1308  +  if( memcmp(zOpName, "SQLITE_", 7)==0 ) zOpName += 7;
         1309  +  if( memcmp(zOpName, "DBSTATUS_", 9)==0 ) zOpName += 9;
  1305   1310     for(i=0; i<ArraySize(aOp); i++){
  1306   1311       if( strcmp(aOp[i].zName, zOpName)==0 ){
  1307   1312         op = aOp[i].op;
  1308   1313         break;
  1309   1314       }
  1310   1315     }
  1311   1316     if( i>=ArraySize(aOp) ){

Changes to src/test_mutex.c.

   243    243     int objc,
   244    244     Tcl_Obj *CONST objv[]
   245    245   ){
   246    246     Tcl_Obj *pRet;
   247    247     int ii;
   248    248     char *aName[8] = {
   249    249       "fast",        "recursive",   "static_master", "static_mem", 
   250         -    "static_open", "static_prng", "static_lru",    "static_lru2"
          250  +    "static_open", "static_prng", "static_lru",    "static_pmem"
   251    251     };
   252    252   
   253    253     if( objc!=1 ){
   254    254       Tcl_WrongNumArgs(interp, 1, objv, "");
   255    255       return TCL_ERROR;
   256    256     }
   257    257   

Changes to src/test_quota.c.

    26     26   ** callback does enlarge the quota such that the total size of all
    27     27   ** files within the group is less than the new quota, then the write
    28     28   ** continues as if nothing had happened.
    29     29   */
    30     30   #include "sqlite3.h"
    31     31   #include <string.h>
    32     32   #include <assert.h>
           33  +
           34  +/*
           35  +** For an build without mutexes, no-op the mutex calls.
           36  +*/
           37  +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
           38  +#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
           39  +#define sqlite3_mutex_free(X)
           40  +#define sqlite3_mutex_enter(X)
           41  +#define sqlite3_mutex_try(X)      SQLITE_OK
           42  +#define sqlite3_mutex_leave(X)
           43  +#define sqlite3_mutex_held(X)     ((void)(X),1)
           44  +#define sqlite3_mutex_notheld(X)  ((void)(X),1)
           45  +#endif /* SQLITE_THREADSAFE==0 */
           46  +
    33     47   
    34     48   /************************ Object Definitions ******************************/
    35     49   
    36     50   /* Forward declaration of all object types */
    37     51   typedef struct quotaGroup quotaGroup;
    38     52   typedef struct quotaConn quotaConn;
    39     53   typedef struct quotaFile quotaFile;

Changes to src/test_superlock.c.

    29     29   struct SuperlockBusy {
    30     30     int (*xBusy)(void*,int);        /* Pointer to busy-handler function */
    31     31     void *pBusyArg;                 /* First arg to pass to xBusy */
    32     32     int nBusy;                      /* Number of times xBusy has been invoked */
    33     33   };
    34     34   typedef struct SuperlockBusy SuperlockBusy;
    35     35   
           36  +/*
           37  +** An instance of the following structure is allocated for each active
           38  +** superlock. The opaque handle returned by sqlite3demo_superlock() is
           39  +** actually a pointer to an instance of this structure.
           40  +*/
           41  +struct Superlock {
           42  +  sqlite3 *db;                    /* Database handle used to lock db */
           43  +  int bWal;                       /* True if db is a WAL database */
           44  +};
           45  +typedef struct Superlock Superlock;
           46  +
    36     47   /*
    37     48   ** The pCtx pointer passed to this function is actually a pointer to a
    38     49   ** SuperlockBusy structure. Invoke the busy-handler function encapsulated
    39     50   ** by the structure and return the result.
    40     51   */
    41     52   static int superlockBusyHandler(void *pCtx, int UNUSED){
    42     53     SuperlockBusy *pBusy = (SuperlockBusy *)pCtx;
................................................................................
    49     60   ** connection db is open in WAL mode or not. If no error occurs and the
    50     61   ** database file is in WAL mode, set *pbWal to true and return SQLITE_OK.
    51     62   ** If it is not in WAL mode, set *pbWal to false.
    52     63   **
    53     64   ** If an error occurs, return an SQLite error code. The value of *pbWal
    54     65   ** is undefined in this case.
    55     66   */
    56         -static int superlockIsWal(sqlite3 *db, int *pbWal){
           67  +static int superlockIsWal(Superlock *pLock){
    57     68     int rc;                         /* Return Code */
    58     69     sqlite3_stmt *pStmt;            /* Compiled PRAGMA journal_mode statement */
    59     70   
    60         -  rc = sqlite3_prepare(db, "PRAGMA main.journal_mode", -1, &pStmt, 0);
           71  +  rc = sqlite3_prepare(pLock->db, "PRAGMA main.journal_mode", -1, &pStmt, 0);
    61     72     if( rc!=SQLITE_OK ) return rc;
    62     73   
    63         -  *pbWal = 0;
           74  +  pLock->bWal = 0;
    64     75     if( SQLITE_ROW==sqlite3_step(pStmt) ){
    65     76       const char *zMode = (const char *)sqlite3_column_text(pStmt, 0);
    66     77       if( zMode && strlen(zMode)==3 && sqlite3_strnicmp("wal", zMode, 3)==0 ){
    67         -      *pbWal = 1;
           78  +      pLock->bWal = 1;
    68     79       }
    69     80     }
    70     81   
    71     82     return sqlite3_finalize(pStmt);
    72     83   }
    73     84   
    74     85   /*
................................................................................
   127    138     /* Obtain exclusive locks on all the "read-lock" slots. Once these locks
   128    139     ** are held, it is guaranteed that there are no active reader, writer or 
   129    140     ** checkpointer clients.
   130    141     */
   131    142     rc = superlockShmLock(fd, 3, SQLITE_SHM_NLOCK-3, pBusy);
   132    143     return rc;
   133    144   }
          145  +
          146  +/*
          147  +** Release a superlock held on a database file. The argument passed to 
          148  +** this function must have been obtained from a successful call to
          149  +** sqlite3demo_superlock().
          150  +*/
          151  +void sqlite3demo_superunlock(void *pLock){
          152  +  Superlock *p = (Superlock *)pLock;
          153  +  if( p->bWal ){
          154  +    int rc;                         /* Return code */
          155  +    int flags = SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE;
          156  +    sqlite3_file *fd = 0;
          157  +    rc = sqlite3_file_control(p->db, "main", SQLITE_FCNTL_FILE_POINTER, (void *)&fd);
          158  +    if( rc==SQLITE_OK ){
          159  +      fd->pMethods->xShmLock(fd, 2, 1, flags);
          160  +      fd->pMethods->xShmLock(fd, 3, SQLITE_SHM_NLOCK-3, flags);
          161  +    }
          162  +  }
          163  +  sqlite3_close(p->db);
          164  +  sqlite3_free(p);
          165  +}
   134    166   
   135    167   /*
   136    168   ** Obtain a superlock on the database file identified by zPath, using the
   137    169   ** locking primitives provided by VFS zVfs. If successful, SQLITE_OK is
   138    170   ** returned and output variable *ppLock is populated with an opaque handle
   139    171   ** that may be used with sqlite3demo_superunlock() to release the lock.
   140    172   **
................................................................................
   150    182   int sqlite3demo_superlock(
   151    183     const char *zPath,              /* Path to database file to lock */
   152    184     const char *zVfs,               /* VFS to use to access database file */
   153    185     int (*xBusy)(void*,int),        /* Busy handler callback */
   154    186     void *pBusyArg,                 /* Context arg for busy handler */
   155    187     void **ppLock                   /* OUT: Context to pass to superunlock() */
   156    188   ){
   157         -  sqlite3 *db = 0;                /* Database handle open on zPath */
   158    189     SuperlockBusy busy = {0, 0, 0}; /* Busy handler wrapper object */
   159    190     int rc;                         /* Return code */
          191  +  Superlock *pLock;
          192  +
          193  +  pLock = sqlite3_malloc(sizeof(Superlock));
          194  +  if( !pLock ) return SQLITE_NOMEM;
          195  +  memset(pLock, 0, sizeof(Superlock));
   160    196   
   161    197     /* Open a database handle on the file to superlock. */
   162    198     rc = sqlite3_open_v2(
   163         -      zPath, &db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs
          199  +      zPath, &pLock->db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs
   164    200     );
   165    201   
   166    202     /* Install a busy-handler and execute a BEGIN EXCLUSIVE. If this is not
   167    203     ** a WAL database, this is all we need to do.  
   168    204     **
   169    205     ** A wrapper function is used to invoke the busy-handler instead of
   170    206     ** registering the busy-handler function supplied by the user directly
................................................................................
   175    211     ** represents the total number of busy-handler invocations made within
   176    212     ** this call to sqlite3demo_superlock(), including any made during the
   177    213     ** "BEGIN EXCLUSIVE".
   178    214     */
   179    215     if( rc==SQLITE_OK ){
   180    216       busy.xBusy = xBusy;
   181    217       busy.pBusyArg = pBusyArg;
   182         -    sqlite3_busy_handler(db, superlockBusyHandler, (void *)&busy);
   183         -    rc = sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
          218  +    sqlite3_busy_handler(pLock->db, superlockBusyHandler, (void *)&busy);
          219  +    rc = sqlite3_exec(pLock->db, "BEGIN EXCLUSIVE", 0, 0, 0);
   184    220     }
   185    221   
   186    222     /* If the BEGIN EXCLUSIVE was executed successfully and this is a WAL
   187    223     ** database, call superlockWalLock() to obtain the extra locks required
   188    224     ** to prevent readers, writers and/or checkpointers from accessing the
   189    225     ** db while this process is holding the superlock.
   190    226     **
   191    227     ** Before attempting any WAL locks, commit the transaction started above
   192    228     ** to drop the WAL read and write locks currently held. Otherwise, the
   193    229     ** new WAL locks may conflict with the old.
   194    230     */
   195    231     if( rc==SQLITE_OK ){
   196         -    int bWal;                     /* True for a WAL database, false otherwise */
   197         -    if( SQLITE_OK==(rc = superlockIsWal(db, &bWal)) && bWal ){
   198         -      rc = sqlite3_exec(db, "COMMIT", 0, 0, 0);
          232  +    if( SQLITE_OK==(rc = superlockIsWal(pLock)) && pLock->bWal ){
          233  +      rc = sqlite3_exec(pLock->db, "COMMIT", 0, 0, 0);
   199    234         if( rc==SQLITE_OK ){
   200         -        rc = superlockWalLock(db, &busy);
          235  +        rc = superlockWalLock(pLock->db, &busy);
   201    236         }
   202    237       }
   203    238     }
   204    239   
   205    240     if( rc!=SQLITE_OK ){
   206         -    sqlite3_close(db);
          241  +    sqlite3demo_superunlock(pLock);
   207    242       *ppLock = 0;
   208    243     }else{
   209         -    *ppLock = (void *)db;
          244  +    *ppLock = pLock;
   210    245     }
   211    246   
   212    247     return rc;
   213    248   }
   214    249   
   215         -/*
   216         -** Release a superlock held on a database file. The argument passed to 
   217         -** this function must have been obtained from a successful call to
   218         -** sqlite3demo_superlock().
   219         -*/
   220         -void sqlite3demo_superunlock(void *pLock){
   221         -  sqlite3_close((sqlite3 *)pLock);
   222         -}
   223         -
   224    250   /*
   225    251   ** End of example code. Everything below here is the test harness.
   226    252   **************************************************************************
   227    253   **************************************************************************
   228    254   *************************************************************************/
   229    255   
   230    256   

Changes to src/test_vfs.c.

   536    536     pFd = (TestvfsFd *)ckalloc(sizeof(TestvfsFd) + PARENTVFS(pVfs)->szOsFile);
   537    537     memset(pFd, 0, sizeof(TestvfsFd) + PARENTVFS(pVfs)->szOsFile);
   538    538     pFd->pShm = 0;
   539    539     pFd->pShmId = 0;
   540    540     pFd->zFilename = zName;
   541    541     pFd->pVfs = pVfs;
   542    542     pFd->pReal = (sqlite3_file *)&pFd[1];
          543  +  memset(pTestfile, 0, sizeof(TestvfsFile));
   543    544     pTestfile->pFd = pFd;
   544    545   
   545    546     /* Evaluate the Tcl script: 
   546    547     **
   547    548     **   SCRIPT xOpen FILENAME
   548    549     **
   549    550     ** If the script returns an SQLite error code other than SQLITE_OK, an

Changes to src/vdbeInt.h.

    35     35   ** The cursor can seek to a BTree entry with a particular key, or
    36     36   ** loop over all entries of the Btree.  You can also insert new BTree
    37     37   ** entries or retrieve the key or data from the entry that the cursor
    38     38   ** is currently pointing to.
    39     39   ** 
    40     40   ** Every cursor that the virtual machine has open is represented by an
    41     41   ** instance of the following structure.
    42         -**
    43         -** If the VdbeCursor.isTriggerRow flag is set it means that this cursor is
    44         -** really a single row that represents the NEW or OLD pseudo-table of
    45         -** a row trigger.  The data for the row is stored in VdbeCursor.pData and
    46         -** the rowid is in VdbeCursor.iKey.
    47     42   */
    48     43   struct VdbeCursor {
    49     44     BtCursor *pCursor;    /* The cursor structure of the backend */
           45  +  Btree *pBt;           /* Separate file holding temporary table */
           46  +  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
    50     47     int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
    51         -  i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
           48  +  int pseudoTableReg;   /* Register holding pseudotable content. */
           49  +  int nField;           /* Number of fields in the header */
    52     50     Bool zeroed;          /* True if zeroed out and ready for reuse */
    53     51     Bool rowidIsValid;    /* True if lastRowid is valid */
    54     52     Bool atFirst;         /* True if pointing to first entry */
    55     53     Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
    56     54     Bool nullRow;         /* True if pointing to a row with no data */
    57     55     Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
    58     56     Bool isTable;         /* True if a table requiring integer keys */
    59     57     Bool isIndex;         /* True if an index containing keys only - no data */
    60     58     Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
    61         -  i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    62         -  Btree *pBt;           /* Separate file holding temporary table */
    63         -  int pseudoTableReg;   /* Register holding pseudotable content. */
    64         -  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
    65         -  int nField;           /* Number of fields in the header */
    66         -  i64 seqCount;         /* Sequence counter */
    67     59     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    68     60     const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
           61  +  i64 seqCount;         /* Sequence counter */
           62  +  i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
           63  +  i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
    69     64   
    70     65     /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
    71     66     ** OP_IsUnique opcode on this cursor. */
    72     67     int seekResult;
    73     68   
    74     69     /* Cached information about the header for the data record that the
    75     70     ** cursor is currently pointing to.  Only valid if cacheStatus matches
................................................................................
   133    128   ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
   134    129   */
   135    130   #define CACHE_STALE 0
   136    131   
   137    132   /*
   138    133   ** Internally, the vdbe manipulates nearly all SQL values as Mem
   139    134   ** structures. Each Mem struct may cache multiple representations (string,
   140         -** integer etc.) of the same value.  A value (and therefore Mem structure)
   141         -** has the following properties:
   142         -**
   143         -** Each value has a manifest type. The manifest type of the value stored
   144         -** in a Mem struct is returned by the MemType(Mem*) macro. The type is
   145         -** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or
   146         -** SQLITE_BLOB.
          135  +** integer etc.) of the same value.
   147    136   */
   148    137   struct Mem {
          138  +  sqlite3 *db;        /* The associated database connection */
          139  +  char *z;            /* String or BLOB value */
          140  +  double r;           /* Real value */
   149    141     union {
   150         -    i64 i;              /* Integer value. */
          142  +    i64 i;              /* Integer value used when MEM_Int is set in flags */
   151    143       int nZero;          /* Used when bit MEM_Zero is set in flags */
   152    144       FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   153    145       RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   154    146       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   155    147     } u;
   156         -  double r;           /* Real value */
   157         -  sqlite3 *db;        /* The associated database connection */
   158         -  char *z;            /* String or BLOB value */
   159    148     int n;              /* Number of characters in string value, excluding '\0' */
   160    149     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   161    150     u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
   162    151     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   163    152   #ifdef SQLITE_DEBUG
   164    153     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   165    154     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
................................................................................
   175    164   ** No other flags may be set in this case.
   176    165   **
   177    166   ** If the MEM_Str flag is set then Mem.z points at a string representation.
   178    167   ** Usually this is encoded in the same unicode encoding as the main
   179    168   ** database (see below for exceptions). If the MEM_Term flag is also
   180    169   ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
   181    170   ** flags may coexist with the MEM_Str flag.
   182         -**
   183         -** Multiple of these values can appear in Mem.flags.  But only one
   184         -** at a time can appear in Mem.type.
   185    171   */
   186    172   #define MEM_Null      0x0001   /* Value is NULL */
   187    173   #define MEM_Str       0x0002   /* Value is a string */
   188    174   #define MEM_Int       0x0004   /* Value is an integer */
   189    175   #define MEM_Real      0x0008   /* Value is a real number */
   190    176   #define MEM_Blob      0x0010   /* Value is a BLOB */
   191    177   #define MEM_RowSet    0x0020   /* Value is a RowSet object */
................................................................................
   260    246     VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
   261    247     Mem s;                /* The return value is stored here */
   262    248     Mem *pMem;            /* Memory cell used to store aggregate context */
   263    249     int isError;          /* Error code returned by the function. */
   264    250     CollSeq *pColl;       /* Collating sequence */
   265    251   };
   266    252   
   267         -/*
   268         -** A Set structure is used for quick testing to see if a value
   269         -** is part of a small set.  Sets are used to implement code like
   270         -** this:
   271         -**            x.y IN ('hi','hoo','hum')
   272         -*/
   273         -typedef struct Set Set;
   274         -struct Set {
   275         -  Hash hash;             /* A set is just a hash table */
   276         -  HashElem *prev;        /* Previously accessed hash elemen */
   277         -};
   278         -
   279    253   /*
   280    254   ** An instance of the virtual machine.  This structure contains the complete
   281    255   ** state of the virtual machine.
   282    256   **
   283         -** The "sqlite3_stmt" structure pointer that is returned by sqlite3_compile()
          257  +** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
   284    258   ** is really a pointer to an instance of this structure.
   285    259   **
   286    260   ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
   287    261   ** any virtual table method invocations made by the vdbe program. It is
   288    262   ** set to 2 for xDestroy method calls and 1 for all other methods. This
   289    263   ** variable is used for two purposes: to allow xDestroy methods to execute
   290    264   ** "DROP TABLE" statements and to prevent some nasty side effects of
   291    265   ** malloc failure when SQLite is invoked recursively by a virtual table 
   292    266   ** method function.
   293    267   */
   294    268   struct Vdbe {
   295    269     sqlite3 *db;            /* The database connection that owns this statement */
   296         -  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
   297         -  int nOp;                /* Number of instructions in the program */
   298         -  int nOpAlloc;           /* Number of slots allocated for aOp[] */
   299    270     Op *aOp;                /* Space to hold the virtual machine's program */
   300         -  int nLabel;             /* Number of labels used */
   301         -  int nLabelAlloc;        /* Number of slots allocated in aLabel[] */
   302         -  int *aLabel;            /* Space to hold the labels */
          271  +  Mem *aMem;              /* The memory locations */
   303    272     Mem **apArg;            /* Arguments to currently executing user function */
   304    273     Mem *aColName;          /* Column names to return */
   305    274     Mem *pResultSet;        /* Pointer to an array of results */
          275  +  int nMem;               /* Number of memory locations currently allocated */
          276  +  int nOp;                /* Number of instructions in the program */
          277  +  int nOpAlloc;           /* Number of slots allocated for aOp[] */
          278  +  int nLabel;             /* Number of labels used */
          279  +  int nLabelAlloc;        /* Number of slots allocated in aLabel[] */
          280  +  int *aLabel;            /* Space to hold the labels */
   306    281     u16 nResColumn;         /* Number of columns in one row of the result set */
   307    282     u16 nCursor;            /* Number of slots in apCsr[] */
          283  +  u32 magic;              /* Magic number for sanity checking */
          284  +  char *zErrMsg;          /* Error message written here */
          285  +  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
   308    286     VdbeCursor **apCsr;     /* One element of this array for each open cursor */
   309         -  u8 errorAction;         /* Recovery action to do in case of an error */
   310         -  u8 okVar;               /* True if azVar[] has been initialized */
   311         -  ynVar nVar;             /* Number of entries in aVar[] */
   312    287     Mem *aVar;              /* Values for the OP_Variable opcode. */
   313    288     char **azVar;           /* Name of variables */
   314         -  u32 magic;              /* Magic number for sanity checking */
   315         -  int nMem;               /* Number of memory locations currently allocated */
   316         -  Mem *aMem;              /* The memory locations */
          289  +  ynVar nVar;             /* Number of entries in aVar[] */
   317    290     u32 cacheCtr;           /* VdbeCursor row cache generation counter */
   318    291     int pc;                 /* The program counter */
   319    292     int rc;                 /* Value to return */
   320         -  char *zErrMsg;          /* Error message written here */
          293  +  u8 errorAction;         /* Recovery action to do in case of an error */
          294  +  u8 okVar;               /* True if azVar[] has been initialized */
   321    295     u8 explain;             /* True if EXPLAIN present on SQL command */
   322    296     u8 changeCntOn;         /* True to update the change-counter */
   323    297     u8 expired;             /* True if the VM needs to be recompiled */
   324    298     u8 runOnlyOnce;         /* Automatically expire on reset */
   325    299     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   326    300     u8 inVtabMethod;        /* See comments above */
   327    301     u8 usesStmtJournal;     /* True if uses a statement journal */
   328    302     u8 readOnly;            /* True for read-only statements */
   329    303     u8 isPrepareV2;         /* True if prepared with prepare_v2() */
   330    304     int nChange;            /* Number of db changes made since last reset */
   331    305     int btreeMask;          /* Bitmask of db->aDb[] entries referenced */
   332         -  i64 startTime;          /* Time when query started - used for profiling */
   333         -  BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
          306  +  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
   334    307     int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
   335         -  char *zSql;             /* Text of the SQL statement that generated this */
   336         -  void *pFree;            /* Free this when deleting the vdbe */
          308  +  BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
          309  +#ifndef SQLITE_OMIT_TRACE
          310  +  i64 startTime;          /* Time when query started - used for profiling */
          311  +#endif
   337    312     i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
   338    313     i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
   339         -  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
          314  +  char *zSql;             /* Text of the SQL statement that generated this */
          315  +  void *pFree;            /* Free this when deleting the vdbe */
   340    316   #ifdef SQLITE_DEBUG
   341    317     FILE *trace;            /* Write an execution trace here, if not NULL */
   342    318   #endif
   343    319     VdbeFrame *pFrame;      /* Parent frame */
   344    320     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
   345    321     int nFrame;             /* Number of frames in pFrame list */
   346    322     u32 expmask;            /* Binding to these vars invalidates VM */

Changes to src/vdbeapi.c.

   358    358   static int sqlite3Step(Vdbe *p){
   359    359     sqlite3 *db;
   360    360     int rc;
   361    361   
   362    362     assert(p);
   363    363     if( p->magic!=VDBE_MAGIC_RUN ){
   364    364       /* We used to require that sqlite3_reset() be called before retrying
   365         -    ** sqlite3_step() after any error.  But after 3.6.23, we changed this
   366         -    ** so that sqlite3_reset() would be called automatically instead of
   367         -    ** throwing the error.
          365  +    ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
          366  +    ** with version 3.7.0, we changed this so that sqlite3_reset() would
          367  +    ** be called automatically instead of throwing the SQLITE_MISUSE error.
          368  +    ** This "automatic-reset" change is not technically an incompatibility, 
          369  +    ** since any application that receives an SQLITE_MISUSE is broken by
          370  +    ** definition.
          371  +    **
          372  +    ** Nevertheless, some published applications that were originally written
          373  +    ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 
          374  +    ** returns, and the so were broken by the automatic-reset change.  As a
          375  +    ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
          376  +    ** legacy behavior of returning SQLITE_MISUSE for cases where the 
          377  +    ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
          378  +    ** or SQLITE_BUSY error.
   368    379       */
          380  +#ifdef SQLITE_OMIT_AUTORESET
          381  +    if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
          382  +      sqlite3_reset((sqlite3_stmt*)p);
          383  +    }else{
          384  +      return SQLITE_MISUSE_BKPT;
          385  +    }
          386  +#else
   369    387       sqlite3_reset((sqlite3_stmt*)p);
          388  +#endif
   370    389     }
   371    390   
   372    391     /* Check that malloc() has not failed. If it has, return early. */
   373    392     db = p->db;
   374    393     if( db->mallocFailed ){
   375    394       p->rc = SQLITE_NOMEM;
   376    395       return SQLITE_NOMEM;
................................................................................
   404    423     }
   405    424   #ifndef SQLITE_OMIT_EXPLAIN
   406    425     if( p->explain ){
   407    426       rc = sqlite3VdbeList(p);
   408    427     }else
   409    428   #endif /* SQLITE_OMIT_EXPLAIN */
   410    429     {
          430  +    db->vdbeExecCnt++;
   411    431       rc = sqlite3VdbeExec(p);
          432  +    db->vdbeExecCnt--;
   412    433     }
   413    434   
   414    435   #ifndef SQLITE_OMIT_TRACE
   415    436     /* Invoke the profile callback if there is one
   416    437     */
   417    438     if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
   418    439       sqlite3_int64 iNow;
................................................................................
   698    719       ** this assert() from failing, when building with SQLITE_DEBUG defined
   699    720       ** using gcc, force nullMem to be 8-byte aligned using the magical
   700    721       ** __attribute__((aligned(8))) macro.  */
   701    722       static const Mem nullMem 
   702    723   #if defined(SQLITE_DEBUG) && defined(__GNUC__)
   703    724         __attribute__((aligned(8))) 
   704    725   #endif
   705         -      = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
          726  +      = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
   706    727   
   707    728       if( pVm && ALWAYS(pVm->db) ){
   708    729         sqlite3_mutex_enter(pVm->db->mutex);
   709    730         sqlite3Error(pVm->db, SQLITE_RANGE, 0);
   710    731       }
   711    732       pOut = (Mem*)&nullMem;
   712    733     }

Changes to src/vdbeaux.c.

   404    404     p->readOnly = 1;
   405    405     for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
   406    406       u8 opcode = pOp->opcode;
   407    407   
   408    408       pOp->opflags = sqlite3OpcodeProperty[opcode];
   409    409       if( opcode==OP_Function || opcode==OP_AggStep ){
   410    410         if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
   411         -    }else if( opcode==OP_Transaction && pOp->p2!=0 ){
          411  +    }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
   412    412         p->readOnly = 0;
   413    413   #ifndef SQLITE_OMIT_VIRTUALTABLE
   414    414       }else if( opcode==OP_VUpdate ){
   415    415         if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
   416    416       }else if( opcode==OP_VFilter ){
   417    417         int n;
   418    418         assert( p->nOp - i >= 3 );

Changes to src/vdbetrace.c.

    40     40       nTotal += n;
    41     41       zSql += n;
    42     42     }
    43     43     return nTotal;
    44     44   }
    45     45   
    46     46   /*
    47         -** Return a pointer to a string in memory obtained form sqlite3DbMalloc() which
    48         -** holds a copy of zRawSql but with host parameters expanded to their
    49         -** current bindings.
           47  +** This function returns a pointer to a nul-terminated string in memory
           48  +** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
           49  +** string contains a copy of zRawSql but with host parameters expanded to 
           50  +** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1, 
           51  +** then the returned string holds a copy of zRawSql with "-- " prepended
           52  +** to each line of text.
    50     53   **
    51     54   ** The calling function is responsible for making sure the memory returned
    52     55   ** is eventually freed.
    53     56   **
    54     57   ** ALGORITHM:  Scan the input string looking for host parameters in any of
    55     58   ** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
    56     59   ** string literals, quoted identifier names, and comments.  For text forms,
................................................................................
    73     76     StrAccum out;            /* Accumulate the output here */
    74     77     char zBase[100];         /* Initial working space */
    75     78   
    76     79     db = p->db;
    77     80     sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
    78     81                         db->aLimit[SQLITE_LIMIT_LENGTH]);
    79     82     out.db = db;
    80         -  while( zRawSql[0] ){
    81         -    n = findNextHostParameter(zRawSql, &nToken);
    82         -    assert( n>0 );
    83         -    sqlite3StrAccumAppend(&out, zRawSql, n);
    84         -    zRawSql += n;
    85         -    assert( zRawSql[0] || nToken==0 );
    86         -    if( nToken==0 ) break;
    87         -    if( zRawSql[0]=='?' ){
    88         -      if( nToken>1 ){
    89         -        assert( sqlite3Isdigit(zRawSql[1]) );
    90         -        sqlite3GetInt32(&zRawSql[1], &idx);
           83  +  if( db->vdbeExecCnt>1 ){
           84  +    while( *zRawSql ){
           85  +      const char *zStart = zRawSql;
           86  +      while( *(zRawSql++)!='\n' && *zRawSql );
           87  +      sqlite3StrAccumAppend(&out, "-- ", 3);
           88  +      sqlite3StrAccumAppend(&out, zStart, zRawSql-zStart);
           89  +    }
           90  +  }else{
           91  +    while( zRawSql[0] ){
           92  +      n = findNextHostParameter(zRawSql, &nToken);
           93  +      assert( n>0 );
           94  +      sqlite3StrAccumAppend(&out, zRawSql, n);
           95  +      zRawSql += n;
           96  +      assert( zRawSql[0] || nToken==0 );
           97  +      if( nToken==0 ) break;
           98  +      if( zRawSql[0]=='?' ){
           99  +        if( nToken>1 ){
          100  +          assert( sqlite3Isdigit(zRawSql[1]) );
          101  +          sqlite3GetInt32(&zRawSql[1], &idx);
          102  +        }else{
          103  +          idx = nextIndex;
          104  +        }
    91    105         }else{
    92         -        idx = nextIndex;
          106  +        assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
          107  +        testcase( zRawSql[0]==':' );
          108  +        testcase( zRawSql[0]=='$' );
          109  +        testcase( zRawSql[0]=='@' );
          110  +        idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
          111  +        assert( idx>0 );
    93    112         }
    94         -    }else{
    95         -      assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
    96         -      testcase( zRawSql[0]==':' );
    97         -      testcase( zRawSql[0]=='$' );
    98         -      testcase( zRawSql[0]=='@' );
    99         -      idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
   100         -      assert( idx>0 );
   101         -    }
   102         -    zRawSql += nToken;
   103         -    nextIndex = idx + 1;
   104         -    assert( idx>0 && idx<=p->nVar );
   105         -    pVar = &p->aVar[idx-1];
   106         -    if( pVar->flags & MEM_Null ){
   107         -      sqlite3StrAccumAppend(&out, "NULL", 4);
   108         -    }else if( pVar->flags & MEM_Int ){
   109         -      sqlite3XPrintf(&out, "%lld", pVar->u.i);
   110         -    }else if( pVar->flags & MEM_Real ){
   111         -      sqlite3XPrintf(&out, "%!.15g", pVar->r);
   112         -    }else if( pVar->flags & MEM_Str ){
          113  +      zRawSql += nToken;
          114  +      nextIndex = idx + 1;
          115  +      assert( idx>0 && idx<=p->nVar );
          116  +      pVar = &p->aVar[idx-1];
          117  +      if( pVar->flags & MEM_Null ){
          118  +        sqlite3StrAccumAppend(&out, "NULL", 4);
          119  +      }else if( pVar->flags & MEM_Int ){
          120  +        sqlite3XPrintf(&out, "%lld", pVar->u.i);
          121  +      }else if( pVar->flags & MEM_Real ){
          122  +        sqlite3XPrintf(&out, "%!.15g", pVar->r);
          123  +      }else if( pVar->flags & MEM_Str ){
   113    124   #ifndef SQLITE_OMIT_UTF16
   114         -      u8 enc = ENC(db);
   115         -      if( enc!=SQLITE_UTF8 ){
   116         -        Mem utf8;
   117         -        memset(&utf8, 0, sizeof(utf8));
   118         -        utf8.db = db;
   119         -        sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
   120         -        sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
   121         -        sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
   122         -        sqlite3VdbeMemRelease(&utf8);
   123         -      }else
          125  +        u8 enc = ENC(db);
          126  +        if( enc!=SQLITE_UTF8 ){
          127  +          Mem utf8;
          128  +          memset(&utf8, 0, sizeof(utf8));
          129  +          utf8.db = db;
          130  +          sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
          131  +          sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
          132  +          sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
          133  +          sqlite3VdbeMemRelease(&utf8);
          134  +        }else
   124    135   #endif
   125         -      {
   126         -        sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
          136  +        {
          137  +          sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
          138  +        }
          139  +      }else if( pVar->flags & MEM_Zero ){
          140  +        sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
          141  +      }else{
          142  +        assert( pVar->flags & MEM_Blob );
          143  +        sqlite3StrAccumAppend(&out, "x'", 2);
          144  +        for(i=0; i<pVar->n; i++){
          145  +          sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
          146  +        }
          147  +        sqlite3StrAccumAppend(&out, "'", 1);
   127    148         }
   128         -    }else if( pVar->flags & MEM_Zero ){
   129         -      sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
   130         -    }else{
   131         -      assert( pVar->flags & MEM_Blob );
   132         -      sqlite3StrAccumAppend(&out, "x'", 2);
   133         -      for(i=0; i<pVar->n; i++){
   134         -        sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
   135         -      }
   136         -      sqlite3StrAccumAppend(&out, "'", 1);
   137    149       }
   138    150     }
   139    151     return sqlite3StrAccumFinish(&out);
   140    152   }
   141    153   
   142    154   #endif /* #ifndef SQLITE_OMIT_TRACE */

Changes to src/wal.c.

   454    454   **   walIteratorNext() - Step an iterator,
   455    455   **   walIteratorFree() - Free an iterator.
   456    456   **
   457    457   ** This functionality is used by the checkpoint code (see walCheckpoint()).
   458    458   */
   459    459   struct WalIterator {
   460    460     int iPrior;                     /* Last result returned from the iterator */
   461         -  int nSegment;                   /* Size of the aSegment[] array */
          461  +  int nSegment;                   /* Number of entries in aSegment[] */
   462    462     struct WalSegment {
   463    463       int iNext;                    /* Next slot in aIndex[] not yet returned */
   464    464       ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
   465    465       u32 *aPgno;                   /* Array of page numbers. */
   466         -    int nEntry;                   /* Max size of aPgno[] and aIndex[] arrays */
          466  +    int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
   467    467       int iZero;                    /* Frame number associated with aPgno[0] */
   468         -  } aSegment[1];                  /* One for every 32KB page in the WAL */
          468  +  } aSegment[1];                  /* One for every 32KB page in the wal-index */
   469    469   };
   470    470   
   471    471   /*
   472    472   ** Define the parameters of the hash tables in the wal-index file. There
   473    473   ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
   474    474   ** wal-index.
   475    475   **
................................................................................
  1325   1325   
  1326   1326     *piPage = p->iPrior = iRet;
  1327   1327     return (iRet==0xFFFFFFFF);
  1328   1328   }
  1329   1329   
  1330   1330   /*
  1331   1331   ** This function merges two sorted lists into a single sorted list.
         1332  +**
         1333  +** aLeft[] and aRight[] are arrays of indices.  The sort key is
         1334  +** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
         1335  +** is guaranteed for all J<K:
         1336  +**
         1337  +**        aContent[aLeft[J]] < aContent[aLeft[K]]
         1338  +**        aContent[aRight[J]] < aContent[aRight[K]]
         1339  +**
         1340  +** This routine overwrites aRight[] with a new (probably longer) sequence
         1341  +** of indices such that the aRight[] contains every index that appears in
         1342  +** either aLeft[] or the old aRight[] and such that the second condition
         1343  +** above is still met.
         1344  +**
         1345  +** The aContent[aLeft[X]] values will be unique for all X.  And the
         1346  +** aContent[aRight[X]] values will be unique too.  But there might be
         1347  +** one or more combinations of X and Y such that
         1348  +**
         1349  +**      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
         1350  +**
         1351  +** When that happens, omit the aLeft[X] and use the aRight[Y] index.
  1332   1352   */
  1333   1353   static void walMerge(
  1334         -  u32 *aContent,                  /* Pages in wal */
         1354  +  const u32 *aContent,            /* Pages in wal - keys for the sort */
  1335   1355     ht_slot *aLeft,                 /* IN: Left hand input list */
  1336   1356     int nLeft,                      /* IN: Elements in array *paLeft */
  1337   1357     ht_slot **paRight,              /* IN/OUT: Right hand input list */
  1338   1358     int *pnRight,                   /* IN/OUT: Elements in *paRight */
  1339   1359     ht_slot *aTmp                   /* Temporary buffer */
  1340   1360   ){
  1341   1361     int iLeft = 0;                  /* Current index in aLeft */
................................................................................
  1367   1387   
  1368   1388     *paRight = aLeft;
  1369   1389     *pnRight = iOut;
  1370   1390     memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
  1371   1391   }
  1372   1392   
  1373   1393   /*
  1374         -** Sort the elements in list aList, removing any duplicates.
         1394  +** Sort the elements in list aList using aContent[] as the sort key.
         1395  +** Remove elements with duplicate keys, preferring to keep the
         1396  +** larger aList[] values.
         1397  +**
         1398  +** The aList[] entries are indices into aContent[].  The values in
         1399  +** aList[] are to be sorted so that for all J<K:
         1400  +**
         1401  +**      aContent[aList[J]] < aContent[aList[K]]
         1402  +**
         1403  +** For any X and Y such that
         1404  +**
         1405  +**      aContent[aList[X]] == aContent[aList[Y]]
         1406  +**
         1407  +** Keep the larger of the two values aList[X] and aList[Y] and discard
         1408  +** the smaller.
  1375   1409   */
  1376   1410   static void walMergesort(
  1377         -  u32 *aContent,                  /* Pages in wal */
         1411  +  const u32 *aContent,            /* Pages in wal */
  1378   1412     ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
  1379   1413     ht_slot *aList,                 /* IN/OUT: List to sort */
  1380   1414     int *pnList                     /* IN/OUT: Number of elements in aList[] */
  1381   1415   ){
  1382   1416     struct Sublist {
  1383   1417       int nList;                    /* Number of elements in aList */
  1384   1418       ht_slot *aList;               /* Pointer to sub-list content */
................................................................................
  1435   1469   static void walIteratorFree(WalIterator *p){
  1436   1470     sqlite3ScratchFree(p);
  1437   1471   }
  1438   1472   
  1439   1473   /*
  1440   1474   ** Construct a WalInterator object that can be used to loop over all 
  1441   1475   ** pages in the WAL in ascending order. The caller must hold the checkpoint
         1476  +** lock.
  1442   1477   **
  1443   1478   ** On success, make *pp point to the newly allocated WalInterator object
  1444   1479   ** return SQLITE_OK. Otherwise, return an error code. If this routine
  1445   1480   ** returns an error, the value of *pp is undefined.
  1446   1481   **
  1447   1482   ** The calling routine should invoke walIteratorFree() to destroy the
  1448   1483   ** WalIterator object when it has finished with it.
................................................................................
  1569   1604     u32 mxPage;                     /* Max database page to write */
  1570   1605     int i;                          /* Loop counter */
  1571   1606     volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
  1572   1607   
  1573   1608     szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
  1574   1609     testcase( szPage<=32768 );
  1575   1610     testcase( szPage>=65536 );
  1576         -  if( pWal->hdr.mxFrame==0 ) return SQLITE_OK;
         1611  +  pInfo = walCkptInfo(pWal);
         1612  +  if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
  1577   1613   
  1578   1614     /* Allocate the iterator */
  1579   1615     rc = walIteratorInit(pWal, &pIter);
  1580   1616     if( rc!=SQLITE_OK ){
  1581   1617       return rc;
  1582   1618     }
  1583   1619     assert( pIter );
................................................................................
  1591   1627     /* Compute in mxSafeFrame the index of the last frame of the WAL that is
  1592   1628     ** safe to write into the database.  Frames beyond mxSafeFrame might
  1593   1629     ** overwrite database pages that are in use by active readers and thus
  1594   1630     ** cannot be backfilled from the WAL.
  1595   1631     */
  1596   1632     mxSafeFrame = pWal->hdr.mxFrame;
  1597   1633     mxPage = pWal->hdr.nPage;
  1598         -  pInfo = walCkptInfo(pWal);
  1599   1634     for(i=1; i<WAL_NREADER; i++){
  1600   1635       u32 y = pInfo->aReadMark[i];
  1601   1636       if( mxSafeFrame>=y ){
  1602   1637         assert( y<=pWal->hdr.mxFrame );
  1603   1638         rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
  1604   1639         if( rc==SQLITE_OK ){
  1605   1640           pInfo->aReadMark[i] = READMARK_NOT_USED;

Changes to src/where.c.

  2311   2311   #ifdef SQLITE_ENABLE_STAT2
  2312   2312   static int valueFromExpr(
  2313   2313     Parse *pParse, 
  2314   2314     Expr *pExpr, 
  2315   2315     u8 aff, 
  2316   2316     sqlite3_value **pp
  2317   2317   ){
  2318         -  /* The evalConstExpr() function will have already converted any TK_VARIABLE
  2319         -  ** expression involved in an comparison into a TK_REGISTER. */
  2320         -  assert( pExpr->op!=TK_VARIABLE );
  2321         -  if( pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE ){
         2318  +  if( pExpr->op==TK_VARIABLE
         2319  +   || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
         2320  +  ){
  2322   2321       int iVar = pExpr->iColumn;
  2323   2322       sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */
  2324   2323       *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
  2325   2324       return SQLITE_OK;
  2326   2325     }
  2327   2326     return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
  2328   2327   }

Added test/analyze4.test.

            1  +# 2011 January 04
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file implements regression tests for SQLite library. This file 
           13  +# implements tests for ANALYZE to verify that multiple rows containing
           14  +# a NULL value count as distinct rows for the purposes of analyze 
           15  +# statistics.
           16  +#
           17  +# Also include test cases for collating sequences on indices.
           18  +#
           19  +
           20  +set testdir [file dirname $argv0]
           21  +source $testdir/tester.tcl
           22  +
           23  +do_test analyze4-1.0 {
           24  +  db eval {
           25  +    CREATE TABLE t1(a,b);
           26  +    CREATE INDEX t1a ON t1(a);
           27  +    CREATE INDEX t1b ON t1(b);
           28  +    INSERT INTO t1 VALUES(1,NULL);
           29  +    INSERT INTO t1 SELECT a+1, b FROM t1;
           30  +    INSERT INTO t1 SELECT a+2, b FROM t1;
           31  +    INSERT INTO t1 SELECT a+4, b FROM t1;
           32  +    INSERT INTO t1 SELECT a+8, b FROM t1;
           33  +    INSERT INTO t1 SELECT a+16, b FROM t1;
           34  +    INSERT INTO t1 SELECT a+32, b FROM t1;
           35  +    INSERT INTO t1 SELECT a+64, b FROM t1;
           36  +    ANALYZE;
           37  +  }
           38  +
           39  +  # Should choose the t1a index since it is more specific than t1b.
           40  +  db eval {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=5 AND b IS NULL}
           41  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           42  +
           43  +# Verify that the t1b index shows that it does not narrow down the
           44  +# search any at all.
           45  +#
           46  +do_test analyze4-1.1 {
           47  +  db eval {
           48  +    SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t1' ORDER BY idx;
           49  +  }
           50  +} {t1a {128 1} t1b {128 128}}
           51  +
           52  +# Change half of the b values from NULL to a constant.  Verify
           53  +# that the number of rows selected in stat1 is half the total 
           54  +# number of rows.
           55  +#
           56  +do_test analyze4-1.2 {
           57  +  db eval {
           58  +    UPDATE t1 SET b='x' WHERE a%2;
           59  +    ANALYZE;
           60  +    SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t1' ORDER BY idx;
           61  +  }
           62  +} {t1a {128 1} t1b {128 64}}
           63  +
           64  +# Change the t1.b values all back to NULL.  Add columns t1.c and t1.d.
           65  +# Create a multi-column indices using t1.b and verify that ANALYZE 
           66  +# processes them correctly.
           67  +#
           68  +do_test analyze4-1.3 {
           69  +  db eval {
           70  +    UPDATE t1 SET b=NULL;
           71  +    ALTER TABLE t1 ADD COLUMN c;
           72  +    ALTER TABLE t1 ADD COLUMN d;
           73  +    UPDATE t1 SET c=a/4, d=a/2;
           74  +    CREATE INDEX t1bcd ON t1(b,c,d);
           75  +    CREATE INDEX t1cdb ON t1(c,d,b);
           76  +    CREATE INDEX t1cbd ON t1(c,b,d);
           77  +    ANALYZE;
           78  +    SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t1' ORDER BY idx;
           79  +  }
           80  +} {t1a {128 1} t1b {128 128} t1bcd {128 128 4 2} t1cbd {128 4 4 2} t1cdb {128 4 2 2}}
           81  +
           82  +# Verify that collating sequences are taken into account when computing
           83  +# ANALYZE statistics.
           84  +#
           85  +do_test analyze4-2.0 {
           86  +  db eval {
           87  +    CREATE TABLE t2(
           88  +      x INTEGER PRIMARY KEY,
           89  +      a TEXT COLLATE nocase,
           90  +      b TEXT COLLATE rtrim,
           91  +      c TEXT COLLATE binary
           92  +    );
           93  +    CREATE INDEX t2a ON t2(a);
           94  +    CREATE INDEX t2b ON t2(b);
           95  +    CREATE INDEX t2c ON t2(c);
           96  +    CREATE INDEX t2c2 ON t2(c COLLATE nocase);
           97  +    CREATE INDEX t2c3 ON t2(c COLLATE rtrim);
           98  +    INSERT INTO t2 VALUES(1, 'abc', 'abc', 'abc');
           99  +    INSERT INTO t2 VALUES(2, 'abC', 'abC', 'abC');
          100  +    INSERT INTO t2 VALUES(3, 'abc ', 'abc ', 'abc ');
          101  +    INSERT INTO t2 VALUES(4, 'abC ', 'abC ', 'abC ');
          102  +    INSERT INTO t2 VALUES(5, 'aBc', 'aBc', 'aBc');
          103  +    INSERT INTO t2 VALUES(6, 'aBC', 'aBC', 'aBC');
          104  +    INSERT INTO t2 VALUES(7, 'aBc ', 'aBc ', 'aBc ');
          105  +    INSERT INTO t2 VALUES(8, 'aBC ', 'aBC ', 'aBC ');
          106  +    ANALYZE;
          107  +    SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t2' ORDER BY idx;
          108  +  }
          109  +} {t2a {8 4} t2b {8 2} t2c {8 1} t2c2 {8 4} t2c3 {8 2}}
          110  +
          111  +finish_test

Changes to test/capi2.test.

    70     70   } {SQLITE_DONE}
    71     71   do_test capi2-1.7 {
    72     72     list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
    73     73   } {2 {} {name rowid text INTEGER}}
    74     74   
    75     75   # This used to be SQLITE_MISUSE.  But now we automatically reset prepared
    76     76   # statements.
    77         -do_test capi2-1.8 {
    78         -  sqlite3_step $VM
    79         -} {SQLITE_ROW}
           77  +ifcapable autoreset {
           78  +  do_test capi2-1.8 {
           79  +    sqlite3_step $VM
           80  +  } {SQLITE_ROW}
           81  +} else {
           82  +  do_test capi2-1.8 {
           83  +    sqlite3_step $VM
           84  +  } {SQLITE_MISUSE}
           85  +}
    80     86   
    81     87   # Update: In v2, once SQLITE_MISUSE is returned the statement handle cannot
    82     88   # be interrogated for more information. However in v3, since the column
    83     89   # count, names and types are determined at compile time, these are still
    84     90   # accessible after an SQLITE_MISUSE error.
    85     91   do_test capi2-1.9 {
    86     92     sqlite3_reset $VM

Changes to test/exclusive2.test.

   159    159   }
   160    160   do_test exclusive2-1.10 {
   161    161     pagerChangeCounter test.db 2
   162    162   } {2}
   163    163   do_test exclusive2-1.11 {
   164    164     expr {[t1sig] eq $::sig}
   165    165   } {0}
          166  +db2 close
   166    167   
   167    168   #--------------------------------------------------------------------
   168    169   # These tests - exclusive2-2.X - are similar to exclusive2-1.X, 
   169    170   # except that they are run with locking_mode=EXCLUSIVE.
   170    171   #
   171    172   # 1-3:   Build a database with exclusive-access connection 1, 
   172    173   #        calculate a signature.
................................................................................
   248    249   # These tests - exclusive2-3.X - verify that the pager change-counter
   249    250   # is only incremented by the first change when in exclusive access
   250    251   # mode. In normal mode, the change-counter is incremented once
   251    252   # per write-transaction.
   252    253   #
   253    254   
   254    255   db close
   255         -db2 close
   256    256   catch {close $::fd}
   257    257   file delete -force test.db
   258    258   file delete -force test.db-journal
   259    259   
   260    260   do_test exclusive2-3.0 {
   261    261     sqlite3 db test.db
   262    262     execsql {
   263    263       BEGIN;
   264    264       CREATE TABLE t1(a UNIQUE);
   265         -    INSERT INTO t1 VALUES(randstr(10, 400));
   266         -    INSERT INTO t1 VALUES(randstr(10, 400));
          265  +    INSERT INTO t1 VALUES(randstr(200, 200));
          266  +    INSERT INTO t1 VALUES(randstr(200, 200));
   267    267       COMMIT;
   268    268     }
   269    269     readPagerChangeCounter test.db
   270    270   } {1}
   271    271   do_test exclusive2-3.1 {
   272    272     execsql {
   273         -    INSERT INTO t1 VALUES(randstr(10, 400));
          273  +    INSERT INTO t1 VALUES(randstr(200, 200));
   274    274     }
   275    275     readPagerChangeCounter test.db
   276    276   } {2}
   277    277   do_test exclusive2-3.2 {
   278    278     execsql {
   279         -    INSERT INTO t1 VALUES(randstr(10, 400));
          279  +    INSERT INTO t1 VALUES(randstr(200, 200));
   280    280     }
   281    281     readPagerChangeCounter test.db
   282    282   } {3}
   283    283   do_test exclusive2-3.3 {
   284    284     execsql {
   285    285       PRAGMA locking_mode = exclusive;
   286         -    INSERT INTO t1 VALUES(randstr(10, 400));
          286  +    INSERT INTO t1 VALUES(randstr(200, 200));
   287    287     }
   288    288     readPagerChangeCounter test.db
   289    289   } {4}
   290    290   do_test exclusive2-3.4 {
          291  +breakpoint
   291    292     execsql {
   292         -    INSERT INTO t1 VALUES(randstr(10, 400));
          293  +    INSERT INTO t1 VALUES(randstr(200, 200));
   293    294     }
   294    295     readPagerChangeCounter test.db
   295    296   } {4}
   296    297   do_test exclusive2-3.5 {
   297    298     execsql {
   298    299       PRAGMA locking_mode = normal;
   299         -    INSERT INTO t1 VALUES(randstr(10, 400));
          300  +    INSERT INTO t1 VALUES(randstr(200, 200));
   300    301     }
   301    302     readPagerChangeCounter test.db
   302    303   } {4}
   303    304   do_test exclusive2-3.6 {
   304    305     execsql {
   305         -    INSERT INTO t1 VALUES(randstr(10, 400));
          306  +    INSERT INTO t1 VALUES(randstr(200, 200));
   306    307     }
   307    308     readPagerChangeCounter test.db
   308    309   } {5}
   309    310   sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
   310    311   
   311    312   finish_test

Changes to test/fkey2.test.

  1410   1410       INSERT INTO one VALUES(1, 2, 3);
  1411   1411     }
  1412   1412   } {1}
  1413   1413   do_test fkey2-17.1.2 {
  1414   1414     set STMT [sqlite3_prepare_v2 db "INSERT INTO two VALUES(4, 5, 6)" -1 dummy]
  1415   1415     sqlite3_step $STMT
  1416   1416   } {SQLITE_CONSTRAINT}
  1417         -do_test fkey2-17.1.3 {
  1418         -  sqlite3_step $STMT
  1419         -} {SQLITE_CONSTRAINT}
         1417  +ifcapable autoreset {
         1418  +  do_test fkey2-17.1.3 {
         1419  +    sqlite3_step $STMT
         1420  +  } {SQLITE_CONSTRAINT}
         1421  +} else {
         1422  +  do_test fkey2-17.1.3 {
         1423  +    sqlite3_step $STMT
         1424  +  } {SQLITE_MISUSE}
         1425  +}
  1420   1426   do_test fkey2-17.1.4 {
  1421   1427     sqlite3_finalize $STMT
  1422   1428   } {SQLITE_CONSTRAINT}
  1423   1429   do_test fkey2-17.1.5 {
  1424   1430     execsql {
  1425   1431       INSERT INTO one VALUES(2, 3, 4);
  1426   1432       INSERT INTO one VALUES(3, 4, 5);

Changes to test/fts3matchinfo.test.

   333    333     SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10 WHERE docid=1;
   334    334   } {blob 0}
   335    335   do_execsql_test 7.4 {
   336    336     SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) 
   337    337     FROM t10 WHERE t10 MATCH 'record'
   338    338   } {blob 20 blob 20}
   339    339   
          340  +#-------------------------------------------------------------------------
          341  +# Test a special case - matchinfo('nxa') with many zero length documents. 
          342  +# Special because "x" internally uses a statement used by both "n" and "a". 
          343  +# This was causing a problem at one point in the obscure case where the
          344  +# total number of bytes of data stored in an fts3 table was greater than
          345  +# the number of rows. i.e. when the following query returns true:
          346  +#
          347  +#   SELECT sum(length(content)) < count(*) FROM fts4table;
          348  +#
          349  +do_execsql_test 8.1 {
          350  +  CREATE VIRTUAL TABLE t11 USING fts4;
          351  +  INSERT INTO t11(t11) VALUES('nodesize=24');
          352  +  INSERT INTO t11 VALUES('quitealongstringoftext');
          353  +  INSERT INTO t11 VALUES('anotherquitealongstringoftext');
          354  +  INSERT INTO t11 VALUES('athirdlongstringoftext');
          355  +  INSERT INTO t11 VALUES('andonemoreforgoodluck');
          356  +}
          357  +do_test 8.2 {
          358  +  for {set i 0} {$i < 200} {incr i} {
          359  +    execsql { INSERT INTO t11 VALUES('') }
          360  +  }
          361  +  execsql { INSERT INTO t11(t11) VALUES('optimize') }
          362  +} {}
          363  +do_execsql_test 8.3 {
          364  +  SELECT mit(matchinfo(t11, 'nxa')) FROM t11 WHERE t11 MATCH 'a*'
          365  +} {{204 1 3 3 0} {204 1 3 3 0} {204 1 3 3 0}}
          366  +
          367  +# Corruption related tests.
          368  +do_execsql_test  8.4.1.1 { UPDATE t11_stat SET value = X'0000'; }
          369  +do_catchsql_test 8.5.1.2 {
          370  +  SELECT mit(matchinfo(t11, 'nxa')) FROM t11 WHERE t11 MATCH 'a*'
          371  +} {1 {database disk image is malformed}}
          372  +
          373  +do_execsql_test  8.4.2.1 { UPDATE t11_stat SET value = X'00'; }
          374  +do_catchsql_test 8.5.2.2 {
          375  +  SELECT mit(matchinfo(t11, 'nxa')) FROM t11 WHERE t11 MATCH 'a*'
          376  +} {1 {database disk image is malformed}}
          377  +
          378  +do_execsql_test  8.4.3.1 { UPDATE t11_stat SET value = NULL; }
          379  +do_catchsql_test 8.5.3.2 {
          380  +  SELECT mit(matchinfo(t11, 'nxa')) FROM t11 WHERE t11 MATCH 'a*'
          381  +} {1 {database disk image is malformed}}
   340    382   
   341    383   finish_test
   342    384   

Changes to test/jrnlmode3.test.

    41     41   do_test jrnlmode3-1.2 {
    42     42     db eval {
    43     43       BEGIN;
    44     44       INSERT INTO t1 VALUES(2);
    45     45       ROLLBACK;
    46     46       SELECT * FROM t1;
    47     47     }
    48         -} {1 2}
           48  +} {1}
    49     49   
    50     50   db close
    51     51   file delete -force test.db test.db-journal
    52     52   sqlite3 db test.db
    53     53   
    54     54   do_test jrnlmode3-2.1 {
    55     55     db eval {
................................................................................
    63     63   do_test jrnlmode3-2.2 {
    64     64     db eval {
    65     65       BEGIN;
    66     66       INSERT INTO t1 VALUES(2);
    67     67       ROLLBACK;
    68     68       SELECT * FROM t1;
    69     69     }
    70         -} {1 2}
           70  +} {1}
    71     71   
    72     72   # Test cases to verify that we can move from any journal_mode
    73     73   # to any other, as long as we are not in a transaction.  Verify
    74     74   # that we cannot change journal_mode while a transaction is active.
    75     75   #
    76     76   set all_journal_modes {delete persist truncate memory off}
    77     77   set cnt 0
................................................................................
   108    108           CREATE TABLE t1(x);
   109    109           BEGIN;
   110    110           INSERT INTO t1 VALUES($cnt);
   111    111         }
   112    112         db eval "PRAGMA journal_mode=$tojmode"
   113    113       } $fromjmode
   114    114   
   115         -    # Rollback the transaction.  Verify that the rollback occurred
   116         -    # if journal_mode!=OFF.
          115  +    # Rollback the transaction.  
   117    116       #
   118    117       do_test jrnlmode3-3.$cnt.4 {
   119    118         db eval {
   120    119           ROLLBACK;
   121    120           SELECT * FROM t1;
   122    121         }
   123         -    } [expr {$fromjmode=="off"?$cnt:""}]
          122  +    } {}
   124    123   
   125    124       # Now change the journal mode again.  This time the new mode
   126    125       # should take.
   127    126       #
   128    127       do_test jrnlmode3-3.$cnt.5 {
   129    128         db eval "PRAGMA journal_mode=$tojmode"
   130    129       } $tojmode
................................................................................
   139    138           BEGIN;
   140    139           INSERT INTO t1 VALUES(1);
   141    140         }
   142    141         db eval ROLLBACK
   143    142         db eval {
   144    143           SELECT * FROM t1;
   145    144         }
   146         -    } [expr {$tojmode=="off"?"1":""}]
          145  +    } {}
   147    146     }
   148    147   }
   149    148   
   150    149   finish_test

Changes to test/lookaside.test.

    42     42   do_test lookaside-1.1 {
    43     43     catch {sqlite3_config_error db}
    44     44   } {0}
    45     45   
    46     46   do_test lookaside-1.2 {
    47     47     sqlite3_db_config_lookaside db 1 18 18
    48     48   } {0}
    49         -do_test lookaside-1.3 {
    50         -  sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 0
           49  +do_test lookaside-1.3.1 {
           50  +  sqlite3_db_status db DBSTATUS_LOOKASIDE_USED 0
           51  +} {0 0 0}
           52  +do_test lookaside-1.3.2 {
           53  +  sqlite3_db_status db DBSTATUS_LOOKASIDE_HIT 0
           54  +} {0 0 0}
           55  +do_test lookaside-1.3.3 {
           56  +  sqlite3_db_status db DBSTATUS_LOOKASIDE_MISS_SIZE 0
           57  +} {0 0 0}
           58  +do_test lookaside-1.3.4 {
           59  +  sqlite3_db_status db DBSTATUS_LOOKASIDE_MISS_FULL 0
    51     60   } {0 0 0}
    52     61   
    53     62   do_test lookaside-1.4 {
    54     63     db eval {CREATE TABLE t1(w,x,y,z);}
    55         -  foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 0] break
    56         -  expr {$x==0 && $y<$z && $z==18}
           64  +  foreach {x y z} [sqlite3_db_status db DBSTATUS_LOOKASIDE_USED 0] break
           65  +  set p [lindex [sqlite3_db_status db DBSTATUS_LOOKASIDE_HIT 0] 2]
           66  +  set q [lindex [sqlite3_db_status db DBSTATUS_LOOKASIDE_MISS_SIZE 0] 2]
           67  +  set r [lindex [sqlite3_db_status db DBSTATUS_LOOKASIDE_MISS_FULL 0] 2]
           68  +  expr {$x==0 && $y<$z && $z==18 && $p>0 && $q>0 && $r>0}
    57     69   } {0}
    58     70   do_test lookaside-1.5 {
    59         -  foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 1] break
           71  +  foreach {x y z} [sqlite3_db_status db DBSTATUS_LOOKASIDE_USED 1] break
    60     72     expr {$x==0 && $y<$z && $z==18}
    61     73   } {0}
    62     74   do_test lookaside-1.6 {
    63         -  foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 0] break
           75  +  foreach {x y z} [sqlite3_db_status db DBSTATUS_LOOKASIDE_USED 0] break
    64     76     expr {$x==0 && $y==$z && $y<18}
    65     77   } {1}
    66     78   do_test lookaside-1.7 {
    67     79     db cache flush
    68         -  foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 0] break
           80  +  foreach {x y z} [sqlite3_db_status db DBSTATUS_LOOKASIDE_USED 0] break
    69     81     expr {$x==0 && $y==0 && $z<18}
    70     82   } {1}
    71     83   do_test lookaside-1.8 {
    72     84     db cache flush
    73         -  foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 1] break
           85  +  foreach {x y z} [sqlite3_db_status db DBSTATUS_LOOKASIDE_USED 1] break
    74     86     expr {$x==0 && $y==0 && $z<18}
    75     87   } {1}
    76     88   do_test lookaside-1.9 {
    77     89     db cache flush
    78         -  sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 0
           90  +  sqlite3_db_status db DBSTATUS_LOOKASIDE_USED 0
    79     91   } {0 0 0}
    80     92   
    81     93   do_test lookaside-2.1 {
    82     94     sqlite3_db_config_lookaside db 0 100 1000
    83     95   } {0}
    84     96   do_test lookaside-2.2 {
    85     97     db eval {CREATE TABLE t2(x);}
    86         -  foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 0] break
           98  +  foreach {x y z} [sqlite3_db_status db DBSTATUS_LOOKASIDE_USED 0] break
    87     99     expr {$x==0 && $y<$z && $z>10 && $z<100}
    88    100   } {1}
    89    101   do_test lookaside-2.3 {
    90    102     sqlite3_db_config_lookaside db 0 50 50
    91    103   } {5}  ;# SQLITE_BUSY
    92    104   do_test lookaside-2.4 {
    93    105     db cache flush

Changes to test/malloc_common.tcl.

   102    102   #
   103    103   #     -prep             Script to execute before -body.
   104    104   #
   105    105   #     -body             Script to execute (with fault injection).
   106    106   #
   107    107   #     -test             Script to execute after -body.
   108    108   #
          109  +#     -install          Script to execute after faultsim -injectinstall
          110  +#
          111  +#     -uninstall        Script to execute after faultsim -uninjectinstall
          112  +#
   109    113   proc do_faultsim_test {name args} {
   110    114     global FAULTSIM
   111    115     
   112    116     set DEFAULT(-faults)        [array names FAULTSIM]
   113    117     set DEFAULT(-prep)          ""
   114    118     set DEFAULT(-body)          ""
   115    119     set DEFAULT(-test)          ""
          120  +  set DEFAULT(-install)          ""
          121  +  set DEFAULT(-uninstall)          ""
   116    122   
   117    123     fix_testname name
   118    124   
   119    125     array set O [array get DEFAULT]
   120    126     array set O $args
   121    127     foreach o [array names O] {
   122    128       if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
................................................................................
   125    131     set faultlist [list]
   126    132     foreach f $O(-faults) {
   127    133       set flist [array names FAULTSIM $f]
   128    134       if {[llength $flist]==0} { error "unknown fault: $f" }
   129    135       set faultlist [concat $faultlist $flist]
   130    136     }
   131    137   
   132         -  set testspec [list -prep $O(-prep) -body $O(-body) -test $O(-test)]
          138  +  set testspec [list -prep $O(-prep) -body $O(-body) \
          139  +      -test $O(-test) -install $O(-install) -uninstall $O(-uninstall)
          140  +  ]
   133    141     foreach f [lsort -unique $faultlist] {
   134    142       eval do_one_faultsim_test "$name-$f" $FAULTSIM($f) $testspec
   135    143     }
   136    144   }
   137    145   
   138    146   
   139    147   #-------------------------------------------------------------------------
................................................................................
   290    298     set DEFAULT(-injectstop)      "expr 0"
   291    299     set DEFAULT(-injecterrlist)   [list]
   292    300     set DEFAULT(-injectinstall)   ""
   293    301     set DEFAULT(-injectuninstall) ""
   294    302     set DEFAULT(-prep)            ""
   295    303     set DEFAULT(-body)            ""
   296    304     set DEFAULT(-test)            ""
          305  +  set DEFAULT(-install)         ""
          306  +  set DEFAULT(-uninstall)       ""
   297    307   
   298    308     array set O [array get DEFAULT]
   299    309     array set O $args
   300    310     foreach o [array names O] {
   301    311       if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
   302    312     }
   303    313   
   304    314     proc faultsim_test_proc {testrc testresult testnfail} $O(-test)
   305    315     proc faultsim_test_result {args} "
   306    316       uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)]
   307    317     "
   308    318   
   309    319     eval $O(-injectinstall)
          320  +  eval $O(-install)
   310    321   
   311    322     set stop 0
   312    323     for {set iFail 1} {!$stop} {incr iFail} {
   313    324   
   314    325       # Evaluate the -prep script.
   315    326       #
   316    327       eval $O(-prep)
................................................................................
   334    345   
   335    346       # If no faults where injected this trial, don't bother running
   336    347       # any more. This test is finished.
   337    348       #
   338    349       if {$nfail==0} { set stop 1 }
   339    350     }
   340    351   
          352  +  eval $O(-uninstall)
   341    353     eval $O(-injectuninstall)
   342    354   }
   343    355   
   344    356   # Usage: do_malloc_test <test number> <options...>
   345    357   #
   346    358   # The first argument, <test number>, is an integer used to name the
   347    359   # tests executed by this proc. Options are as follows:

Changes to test/memsubsys1.test.

    92     92   db close
    93     93   sqlite3_shutdown
    94     94   sqlite3_config_pagecache [expr 1024+$xtra_size] 20
    95     95   sqlite3_initialize
    96     96   reset_highwater_marks
    97     97   build_test_db memsubsys1-2 {PRAGMA page_size=1024}
    98     98   #show_memstats
           99  +set MEMORY_MANAGEMENT $sqlite_options(memorymanage)
    99    100   do_test memsubsys1-2.3 {
   100    101     set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
   101         -} [expr ($AUTOVACUUM+$TEMP_STORE>=2)*1024]
          102  +} [expr ($TEMP_STORE>1 || $MEMORY_MANAGEMENT==0)*1024]
   102    103   do_test memsubsys1-2.4 {
   103    104     set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
   104    105   } 20
   105    106   do_test memsubsys1-2.5 {
   106    107     set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
   107    108   } 0
   108    109   

Changes to test/mutex1.test.

    97     97   #   * Multi-threaded mode,
    98     98   #   * Single-threaded mode.
    99     99   #
   100    100   ifcapable threadsafe&&shared_cache {
   101    101     set enable_shared_cache [sqlite3_enable_shared_cache 1]
   102    102     foreach {mode mutexes} {
   103    103       singlethread {}
   104         -    multithread  {fast static_lru static_master static_mem static_open static_prng }
   105         -    serialized  {fast recursive static_lru static_master static_mem static_open static_prng}
          104  +    multithread  {
          105  +      fast static_lru static_master static_mem static_open static_prng 
          106  +      static_pmem
          107  +    }
          108  +    serialized  {
          109  +      fast recursive static_lru static_master static_mem static_open 
          110  +      static_prng static_pmem
          111  +    }
   106    112     } {
   107    113   
   108    114       do_test mutex1.2.$mode.1 {
   109    115         catch {db close}
   110    116         sqlite3_shutdown
   111    117         sqlite3_config $mode
   112    118       } SQLITE_OK
................................................................................
   116    122         clear_mutex_counters
   117    123         sqlite3 db test.db -nomutex 0 -fullmutex 0
   118    124         catchsql { CREATE TABLE abc(a, b, c) }
   119    125         db eval {
   120    126           INSERT INTO abc VALUES(1, 2, 3);
   121    127         }
   122    128       } {}
   123         -  
          129  +    ifcapable !memorymanage {
          130  +      regsub { static_lru} $mutexes {} mutexes
          131  +    }
   124    132       do_test mutex1.2.$mode.3 {
   125    133         mutex_counters counters
   126    134     
   127    135         set res [list]
   128    136         foreach {key value} [array get counters] {
   129    137           if {$key ne "total" && $value > 0} {
   130    138             lappend res $key

Changes to test/pager1.test.

  1610   1610   do_catchsql_test pager1-14.1.2 {
  1611   1611     BEGIN;
  1612   1612       INSERT INTO t1 VALUES(3, 4);
  1613   1613     ROLLBACK;
  1614   1614   } {0 {}}
  1615   1615   do_execsql_test pager1-14.1.3 {
  1616   1616     SELECT * FROM t1;
  1617         -} {1 2 3 4}
         1617  +} {1 2}
  1618   1618   do_catchsql_test pager1-14.1.4 {
  1619   1619     BEGIN;
  1620   1620       INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
  1621   1621       INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
  1622   1622   } {1 {PRIMARY KEY must be unique}}
  1623   1623   do_execsql_test pager1-14.1.5 {
  1624   1624     COMMIT;
  1625   1625     SELECT * FROM t1;
  1626         -} {1 2 3 4 2 2 4 4}
         1626  +} {1 2 2 2}
  1627   1627   
  1628   1628   #-------------------------------------------------------------------------
  1629   1629   # Test opening and closing the pager sub-system with different values
  1630   1630   # for the sqlite3_vfs.szOsFile variable.
  1631   1631   #
  1632   1632   faultsim_delete_and_reopen
  1633   1633   do_execsql_test pager1-15.0 {

Changes to test/pager2.test.

   129    129       CREATE TABLE t1(a, b);
   130    130       PRAGMA journal_mode = off;
   131    131       BEGIN;
   132    132         INSERT INTO t1 VALUES(1, 2);
   133    133       ROLLBACK;
   134    134       SELECT * FROM t1;
   135    135     }
   136         -} {off 1 2}
          136  +} {off}
   137    137   do_test pager2-2.2 {
   138    138     faultsim_delete_and_reopen
   139    139     execsql {
   140    140       PRAGMA auto_vacuum = incremental;
   141    141       PRAGMA page_size = 1024;
   142    142       PRAGMA journal_mode = off;
   143    143       CREATE TABLE t1(a, b);

Changes to test/pcache.test.

    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   # Do not use a codec for tests in this file, as the database file is
    20     20   # manipulated directly using tcl scripts (using the [hexio_write] command).
    21     21   #
    22     22   do_not_use_codec
           23  +
           24  +# Only works with a mode-2 pcache where all pcaches share a single set
           25  +# of pages.
           26  +#
           27  +ifcapable {!memorymanage && threadsafe} {
           28  +  finish_test
           29  +  return
           30  +}
    23     31   
    24     32   # The pcache module limits the number of pages available to purgeable
    25     33   # caches to the sum of the 'cache_size' values for the set of open
    26     34   # caches. This block of tests, pcache-1.*, test that the library behaves
    27     35   # corrctly when it is forced to exceed this limit.
    28     36   #
    29     37   do_test pcache-1.1 {

Changes to test/savepoint.test.

   901    901           INSERT INTO t1 VALUES(13, 14);
   902    902           SAVEPOINT s1;
   903    903             INSERT INTO t1 VALUES(15, 16);
   904    904           ROLLBACK TO s1;
   905    905         ROLLBACK;
   906    906         SELECT * FROM t1;
   907    907       }
   908         -  } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
          908  +  } {1 2 3 4 5 6 7 8 9 10 11 12}
   909    909   }
   910    910   
   911    911   db close
   912    912   file delete test.db
   913    913   do_multiclient_test tn {
   914    914     do_test savepoint-14.$tn.1 {
   915    915       sql1 {

Changes to test/superlock.test.

   139    139       list [catch {sqlite3demo_superlock unlock test.db} msg] $msg
   140    140     } {1 {database is locked}}
   141    141     do_test 5.$tn.12 { 
   142    142       sql3 COMMIT
   143    143       list [catch {sqlite3demo_superlock unlock test.db} msg] $msg
   144    144     } {0 unlock}
   145    145     unlock
          146  +
          147  +
          148  +  do_test 5.$tn.13 { sql1 { SELECT * FROM t1 } } {1 2 3 4 5 6}
          149  +  do_test 5.$tn.14 { sql2 { SELECT * FROM t1 } } {1 2 3 4 5 6}
          150  +  do_test 5.$tn.15 { sqlite3demo_superlock unlock test.db } {unlock}
          151  +  do_test 5.$tn.16 { unlock } {}
          152  +  do_test 5.$tn.17 { sql2 { SELECT * FROM t1 } } {1 2 3 4 5 6}
          153  +  do_test 5.$tn.18 { sql1 { SELECT * FROM t1 } } {1 2 3 4 5 6}
          154  +  do_test 5.$tn.19 { sql2 { SELECT * FROM t1 } } {1 2 3 4 5 6}
   146    155   }
   147    156   
   148    157   proc read_content {file} {
   149    158     if {[file exists $file]==0} {return ""}
   150    159     set fd [open $file]
   151    160     fconfigure $fd -encoding binary -translation binary
   152    161     set content [read $fd]

Added test/tkt-5d863f876e.test.

            1  +# 2011 January 15
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.
           12  +#
           13  +# This file implements tests to verify that ticket [5d863f876e] has been
           14  +# fixed.  
           15  +#
           16  +
           17  +set testdir [file dirname $argv0]
           18  +source $testdir/tester.tcl
           19  +source $testdir/lock_common.tcl
           20  +
           21  +do_multiclient_test tn {
           22  +  do_test $tn.1 {
           23  +    sql1 {
           24  +      CREATE TABLE t1(a, b);
           25  +      CREATE INDEX i1 ON t1(a, b);
           26  +      INSERT INTO t1 VALUES(1, 2);
           27  +      INSERT INTO t1 VALUES(3, 4);
           28  +      PRAGMA journal_mode = WAL;
           29  +      VACUUM;
           30  +      PRAGMA journal_mode = DELETE;
           31  +    }
           32  +  } {wal delete}
           33  +
           34  +  do_test $tn.2 {
           35  +    sql2 { SELECT * FROM t1 } 
           36  +  } {1 2 3 4}
           37  +
           38  +  do_test $tn.3 {
           39  +    sql1 {
           40  +      INSERT INTO t1 VALUES(5, 6);
           41  +      PRAGMA journal_mode = WAL;
           42  +      VACUUM;
           43  +      PRAGMA journal_mode = DELETE;
           44  +    }
           45  +  } {wal delete}
           46  +
           47  +  do_test $tn.2 {
           48  +    sql2 { PRAGMA integrity_check }
           49  +  } {ok}
           50  +}
           51  +
           52  +
           53  +finish_test

Added test/trace2.test.

            1  +# 2011 Jan 21
            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 the "sqlite3_trace()" API. Specifically,
           14  +# it tests the special handling of nested SQL statements (those executed
           15  +# by virtual table or user function callbacks). These statements are treated
           16  +# differently in two respects:
           17  +#
           18  +#   1. Each line of the statement is prefixed with "-- " to turn it into
           19  +#      an SQL comment.
           20  +#
           21  +#   2. Parameter expansion is not performed.
           22  +#
           23  +
           24  +set testdir [file dirname $argv0]
           25  +source $testdir/tester.tcl
           26  +ifcapable !trace { finish_test ; return }
           27  +set ::testprefix trace2
           28  +
           29  +proc sql {zSql} { db one $zSql }
           30  +proc trace {zSql} { lappend ::trace $zSql }
           31  +
           32  +db func sql sql
           33  +db trace trace
           34  +
           35  +proc do_trace_test {tn sql expected} {
           36  +  # Test that the list of string passed to the trace callback when $sql
           37  +  # is executed is equivalent to the list of strings in $expected.
           38  +  #
           39  +  set ::trace [list]
           40  +  execsql $sql
           41  +  uplevel do_test $tn [list {set ::trace}] [list [list {*}$expected]]
           42  +}
           43  +
           44  +proc do_trace_select_test {tn sql expected} {
           45  +
           46  +  uplevel [list do_trace_test ${tn}.a $sql $expected]
           47  +
           48  +  # Now execute each SQL statement passed to the trace callback in the
           49  +  # block above. Check that this causes the same set of strings to be
           50  +  # passed to the trace callback again. i.e. that executing the output
           51  +  # of the trace callback is equivalent to the SQL script in $sql.
           52  +  #
           53  +  set sqllist $::trace
           54  +  set ::trace [list]
           55  +  foreach item $sqllist { execsql $item }
           56  +  uplevel do_test $tn.b [list {set ::trace}] [list $sqllist]
           57  +}
           58  +
           59  +do_trace_select_test 1.1  {
           60  +  SELECT 1, 2, 3;
           61  +} {
           62  +  "SELECT 1, 2, 3;"
           63  +}
           64  +
           65  +do_trace_select_test 1.2  {
           66  +  SELECT sql('SELECT 1, 2, 3');
           67  +} {
           68  +  "SELECT sql('SELECT 1, 2, 3');"
           69  +  "-- SELECT 1, 2, 3"
           70  +}
           71  +
           72  +do_trace_select_test 1.3  {
           73  +  SELECT sql('SELECT 1, 
           74  +    2, 
           75  +    3'
           76  +  );
           77  +} {
           78  +  "SELECT sql('SELECT 1, 
           79  +    2, 
           80  +    3'
           81  +  );"
           82  +  "-- SELECT 1, 
           83  +--     2, 
           84  +--     3"
           85  +}
           86  +
           87  +do_trace_select_test 1.4  {
           88  +  SELECT sql('SELECT 1, 
           89  +
           90  +
           91  +    3'
           92  +  );
           93  +} {
           94  +  "SELECT sql('SELECT 1, 
           95  +
           96  +
           97  +    3'
           98  +  );"
           99  +  "-- SELECT 1, 
          100  +-- 
          101  +-- 
          102  +--     3"
          103  +}
          104  +
          105  +do_trace_select_test 1.5  {
          106  +  SELECT $var, sql('SELECT 1, 
          107  +    $var, 
          108  +    3'
          109  +  );
          110  +} {
          111  +  "SELECT NULL, sql('SELECT 1, 
          112  +    $var, 
          113  +    3'
          114  +  );"
          115  +  "-- SELECT 1, 
          116  +--     $var, 
          117  +--     3"
          118  +}
          119  +
          120  +ifcapable fts3 {
          121  +  do_execsql_test 2.1 {
          122  +    CREATE VIRTUAL TABLE x1 USING fts4;
          123  +    INSERT INTO x1 VALUES('Cloudy, with a high near 16');
          124  +    INSERT INTO x1 VALUES('Wind chill values as low as -13');
          125  +  }
          126  +
          127  +  do_trace_test 2.2 {
          128  +    INSERT INTO x1 VALUES('North northwest wind between 8 and 14 mph');
          129  +  } {
          130  +    "INSERT INTO x1 VALUES('North northwest wind between 8 and 14 mph');" 
          131  +    "-- INSERT INTO 'main'.'x1_content' VALUES(?,?)" 
          132  +    "-- REPLACE INTO 'main'.'x1_docsize' VALUES(?,?)" 
          133  +    "-- SELECT value FROM 'main'.'x1_stat' WHERE id=0" 
          134  +    "-- REPLACE INTO 'main'.'x1_stat' VALUES(0,?)" 
          135  +    "-- SELECT (SELECT max(idx) FROM 'main'.'x1_segdir' WHERE level = ?) + 1" 
          136  +    "-- SELECT coalesce((SELECT max(blockid) FROM 'main'.'x1_segments') + 1, 1)"
          137  +    "-- INSERT INTO 'main'.'x1_segdir' VALUES(?,?,?,?,?,?)"
          138  +  }
          139  +
          140  +  do_trace_test 2.3 {
          141  +    INSERT INTO x1(x1) VALUES('optimize');
          142  +  } {
          143  +    "INSERT INTO x1(x1) VALUES('optimize');"
          144  +    "-- SELECT count(*), max(level) FROM 'main'.'x1_segdir'"
          145  +    "-- SELECT idx, start_block, leaves_end_block, end_block, root FROM 'main'.'x1_segdir' ORDER BY level DESC, idx ASC"
          146  +    "-- SELECT coalesce((SELECT max(blockid) FROM 'main'.'x1_segments') + 1, 1)"
          147  +    "-- DELETE FROM 'main'.'x1_segdir'"
          148  +    "-- INSERT INTO 'main'.'x1_segdir' VALUES(?,?,?,?,?,?)"
          149  +  }
          150  +}
          151  +
          152  +finish_test

Changes to tool/mksqlite3h.tcl.

    49     49   
    50     50   # Get the fossil-scm check-in date from the "D" card of $TOP/manifest.
    51     51   #
    52     52   set in [open $TOP/manifest]
    53     53   set zDate {}
    54     54   while {![eof $in]} {
    55     55     set line [gets $in]
    56         -  if {[regexp {^D (2.*[0-9])} $line all date]} {
           56  +  if {[regexp {^D (2[-0-9T:]+)} $line all date]} {
    57     57       set zDate [string map {T { }} $date]
    58     58       break
    59     59     }
    60     60   }
    61     61   close $in
    62     62   
    63     63   # Set up patterns for recognizing API declarations.

Changes to tool/shell1.test.

   196    196     list $rc \
   197    197          [regexp {Error: missing argument for option: -nullvalue} $res]
   198    198   } {1 1}
   199    199   
   200    200   # -version             show SQLite version
   201    201   do_test shell1-1.16.1 {
   202    202     catchcmd "-version test.db" "" 
   203         -} {0 3.7.3}
          203  +} {0 3.7.5}
   204    204   
   205    205   #----------------------------------------------------------------------------
   206    206   # Test cases shell1-2.*: Basic "dot" command token parsing.
   207    207   #
   208    208   
   209    209   # check first token handling
   210    210   do_test shell1-2.1.1 {

Changes to tool/showdb.c.

    53     53   ** Read content from the file.
    54     54   **
    55     55   ** Space to hold the content is obtained from malloc() and needs to be
    56     56   ** freed by the caller.
    57     57   */
    58     58   static unsigned char *getContent(int ofst, int nByte){
    59     59     unsigned char *aData;
    60         -  aData = malloc(nByte);
           60  +  aData = malloc(nByte+32);
    61     61     if( aData==0 ) out_of_memory();
           62  +  memset(aData, 0, nByte+32);
    62     63     lseek(db, ofst, SEEK_SET);
    63     64     read(db, aData, nByte);
    64     65     return aData;
    65     66   }
    66     67   
    67     68   /*
    68     69   ** Print a range of bytes as hex and as ascii.
................................................................................
   176    177     print_decode_line(aData, 80, 4, "meta[10]");
   177    178     print_decode_line(aData, 84, 4, "meta[11]");
   178    179     print_decode_line(aData, 88, 4, "meta[12]");
   179    180     print_decode_line(aData, 92, 4, "Change counter for version number");
   180    181     print_decode_line(aData, 96, 4, "SQLite version number");
   181    182   }
   182    183   
          184  +/*
          185  +** Describe cell content.
          186  +*/
          187  +static int describeContent(
          188  +  unsigned char *a,       /* Cell content */
          189  +  int nLocal,             /* Bytes in a[] */
          190  +  char *zDesc             /* Write description here */
          191  +){
          192  +  int nDesc = 0;
          193  +  int n, i, j;
          194  +  i64 x, v;
          195  +  const unsigned char *pData;
          196  +  const unsigned char *pLimit;
          197  +  char sep = ' ';
          198  +
          199  +  pLimit = &a[nLocal];
          200  +  n = decodeVarint(a, &x);
          201  +  pData = &a[x];
          202  +  a += n;
          203  +  i = x - n;
          204  +  while( i>0 && pData<=pLimit ){
          205  +    n = decodeVarint(a, &x);
          206  +    a += n;
          207  +    i -= n;
          208  +    nLocal -= n;
          209  +    zDesc[0] = sep;
          210  +    sep = ',';
          211  +    nDesc++;
          212  +    zDesc++;
          213  +    if( x==0 ){
          214  +      sprintf(zDesc, "*");     /* NULL is a "*" */
          215  +    }else if( x>=1 && x<=6 ){
          216  +      v = (signed char)pData[0];
          217  +      pData++;
          218  +      switch( x ){
          219  +        case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
          220  +        case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
          221  +        case 4:  v = (v<<8) + pData[0];  pData++;
          222  +        case 3:  v = (v<<8) + pData[0];  pData++;
          223  +        case 2:  v = (v<<8) + pData[0];  pData++;
          224  +      }
          225  +      sprintf(zDesc, "%lld", v);
          226  +    }else if( x==7 ){
          227  +      sprintf(zDesc, "real");
          228  +      pData += 8;
          229  +    }else if( x==8 ){
          230  +      sprintf(zDesc, "0");
          231  +    }else if( x==9 ){
          232  +      sprintf(zDesc, "1");
          233  +    }else if( x>=12 ){
          234  +      int size = (x-12)/2;
          235  +      if( (x&1)==0 ){
          236  +        sprintf(zDesc, "blob(%d)", size);
          237  +      }else{
          238  +        sprintf(zDesc, "txt(%d)", size);
          239  +      }
          240  +      pData += size;
          241  +    }
          242  +    j = strlen(zDesc);
          243  +    zDesc += j;
          244  +    nDesc += j;
          245  +  }
          246  +  return nDesc;
          247  +}
          248  +
          249  +/*
          250  +** Compute the local payload size given the total payload size and
          251  +** the page size.
          252  +*/
          253  +static int localPayload(i64 nPayload, char cType){
          254  +  int maxLocal;
          255  +  int minLocal;
          256  +  int surplus;
          257  +  int nLocal;
          258  +  if( cType==13 ){
          259  +    /* Table leaf */
          260  +    maxLocal = pagesize-35;
          261  +    minLocal = (pagesize-12)*32/255-23;
          262  +  }else{
          263  +    maxLocal = (pagesize-12)*64/255-23;
          264  +    minLocal = (pagesize-12)*32/255-23;
          265  +  }
          266  +  if( nPayload>maxLocal ){
          267  +    surplus = minLocal + (nPayload-minLocal)%(pagesize-4);
          268  +    if( surplus<=maxLocal ){
          269  +      nLocal = surplus;
          270  +    }else{
          271  +      nLocal = minLocal;
          272  +    }
          273  +  }else{
          274  +    nLocal = nPayload;
          275  +  }
          276  +  return nLocal;
          277  +}
          278  +  
          279  +
   183    280   /*
   184    281   ** Create a description for a single cell.
          282  +**
          283  +** The return value is the local cell size.
   185    284   */
   186         -static int describeCell(unsigned char cType, unsigned char *a, char **pzDesc){
          285  +static int describeCell(
          286  +  unsigned char cType,    /* Page type */
          287  +  unsigned char *a,       /* Cell content */
          288  +  int showCellContent,    /* Show cell content if true */
          289  +  char **pzDesc           /* Store description here */
          290  +){
   187    291     int i;
   188    292     int nDesc = 0;
   189    293     int n = 0;
   190    294     int leftChild;
   191    295     i64 nPayload;
   192    296     i64 rowid;
   193         -  static char zDesc[100];
          297  +  int nLocal;
          298  +  static char zDesc[1000];
   194    299     i = 0;
   195    300     if( cType<=5 ){
   196    301       leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
   197    302       a += 4;
   198    303       n += 4;
   199         -    sprintf(zDesc, "left-child: %d ", leftChild);
          304  +    sprintf(zDesc, "lx: %d ", leftChild);
   200    305       nDesc = strlen(zDesc);
   201    306     }
   202    307     if( cType!=5 ){
   203    308       i = decodeVarint(a, &nPayload);
   204    309       a += i;
   205    310       n += i;
   206         -    sprintf(&zDesc[nDesc], "sz: %lld ", nPayload);
          311  +    sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
   207    312       nDesc += strlen(&zDesc[nDesc]);
          313  +    nLocal = localPayload(nPayload, cType);
          314  +  }else{
          315  +    nPayload = nLocal = 0;
   208    316     }
   209    317     if( cType==5 || cType==13 ){
   210    318       i = decodeVarint(a, &rowid);
   211    319       a += i;
   212    320       n += i;
   213         -    sprintf(&zDesc[nDesc], "rowid: %lld ", rowid);
          321  +    sprintf(&zDesc[nDesc], "r: %lld ", rowid);
          322  +    nDesc += strlen(&zDesc[nDesc]);
          323  +  }
          324  +  if( nLocal<nPayload ){
          325  +    int ovfl;
          326  +    unsigned char *b = &a[nLocal];
          327  +    ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
          328  +    sprintf(&zDesc[nDesc], "ov: %d ", ovfl);
   214    329       nDesc += strlen(&zDesc[nDesc]);
          330  +    n += 4;
          331  +  }
          332  +  if( showCellContent && cType!=5 ){
          333  +    nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
   215    334     }
   216    335     *pzDesc = zDesc;
   217         -  return n;
          336  +  return nLocal+n;
   218    337   }
   219    338   
   220    339   /*
   221    340   ** Decode a btree page
   222    341   */
   223         -static void decode_btree_page(unsigned char *a, int pgno, int hdrSize){
          342  +static void decode_btree_page(
          343  +  unsigned char *a,   /* Page content */
          344  +  int pgno,           /* Page number */
          345  +  int hdrSize,        /* Size of the page header.  0 or 100 */
          346  +  char *zArgs         /* Flags to control formatting */
          347  +){
   224    348     const char *zType = "unknown";
   225    349     int nCell;
   226         -  int i;
          350  +  int i, j;
   227    351     int iCellPtr;
          352  +  int showCellContent = 0;
          353  +  int showMap = 0;
          354  +  char *zMap = 0;
   228    355     switch( a[0] ){
   229    356       case 2:  zType = "index interior node";  break;
   230    357       case 5:  zType = "table interior node";  break;
   231    358       case 10: zType = "index leaf";           break;
   232    359       case 13: zType = "table leaf";           break;
          360  +  }
          361  +  while( zArgs[0] ){
          362  +    switch( zArgs[0] ){
          363  +      case 'c': showCellContent = 1;  break;
          364  +      case 'm': showMap = 1;          break;
          365  +    }
          366  +    zArgs++;
   233    367     }
   234    368     printf("Decode of btree page %d:\n", pgno);
   235    369     print_decode_line(a, 0, 1, zType);
   236    370     print_decode_line(a, 1, 2, "Offset to first freeblock");
   237    371     print_decode_line(a, 3, 2, "Number of cells on this page");
   238    372     nCell = a[3]*256 + a[4];
   239    373     print_decode_line(a, 5, 2, "Offset to cell content area");
................................................................................
   240    374     print_decode_line(a, 7, 1, "Fragmented byte count");
   241    375     if( a[0]==2 || a[0]==5 ){
   242    376       print_decode_line(a, 8, 4, "Right child");
   243    377       iCellPtr = 12;
   244    378     }else{
   245    379       iCellPtr = 8;
   246    380     }
          381  +  if( nCell>0 ){
          382  +    printf(" key: lx=left-child n=payload-size r=rowid\n");
          383  +  }
          384  +  if( showMap ){
          385  +    zMap = malloc(pagesize);
          386  +    memset(zMap, '.', pagesize);
          387  +    memset(zMap, '1', hdrSize);
          388  +    memset(&zMap[hdrSize], 'H', iCellPtr);
          389  +    memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
          390  +  }
   247    391     for(i=0; i<nCell; i++){
   248    392       int cofst = iCellPtr + i*2;
   249    393       char *zDesc;
          394  +    int n;
          395  +
   250    396       cofst = a[cofst]*256 + a[cofst+1];
   251         -    describeCell(a[0], &a[cofst-hdrSize], &zDesc);
          397  +    n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
          398  +    if( showMap ){
          399  +      char zBuf[30];
          400  +      memset(&zMap[cofst], '*', n);
          401  +      zMap[cofst] = '[';
          402  +      zMap[cofst+n-1] = ']';
          403  +      sprintf(zBuf, "%d", i);
          404  +      j = strlen(zBuf);
          405  +      if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
          406  +    }
   252    407       printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
   253    408     }
          409  +  if( showMap ){
          410  +    for(i=0; i<pagesize; i+=64){
          411  +      printf(" %03x: %.64s\n", i, &zMap[i]);
          412  +    }
          413  +    free(zMap);
          414  +  }  
   254    415   }
   255    416   
   256    417   /*
   257    418   ** Decode a freelist trunk page.
   258    419   */
   259    420   static void decode_trunk_page(
   260    421     int pgno,             /* The page number */
................................................................................
   296    457     fprintf(stderr, "Usage %s FILENAME ?args...?\n\n", argv0);
   297    458     fprintf(stderr,
   298    459       "args:\n"
   299    460       "    dbheader        Show database header\n"
   300    461       "    NNN..MMM        Show hex of pages NNN through MMM\n"
   301    462       "    NNN..end        Show hex of pages NNN through end of file\n"
   302    463       "    NNNb            Decode btree page NNN\n"
          464  +    "    NNNbc           Decode btree page NNN and show content\n"
          465  +    "    NNNbm           Decode btree page NNN and show a layout map\n"
   303    466       "    NNNt            Decode freelist trunk page NNN\n"
   304    467       "    NNNtd           Show leave freelist pages on the decode\n"
   305    468       "    NNNtr           Recurisvely decode freelist starting at NNN\n"
   306    469     );
   307    470   }
   308    471   
   309    472   int main(int argc, char **argv){
................................................................................
   357    520             nByte = pagesize-100;
   358    521           }else{
   359    522             hdrSize = 0;
   360    523             ofst = (iStart-1)*pagesize;
   361    524             nByte = pagesize;
   362    525           }
   363    526           a = getContent(ofst, nByte);
   364         -        decode_btree_page(a, iStart, hdrSize);
          527  +        decode_btree_page(a, iStart, hdrSize, &zLeft[1]);
   365    528           free(a);
   366    529           continue;
   367    530         }else if( zLeft && zLeft[0]=='t' ){
   368    531           unsigned char *a;
   369    532           int detail = 0;
   370    533           int recursive = 0;
   371    534           int i;