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

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

Overview
Comment:Merge updates from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | asciiMode
Files: files | file ages | folders
SHA1: 51f33cf1290cf767c1c6ba0228f4f30e4059c994
User & Date: mistachkin 2014-09-01 01:15:22
Context
2014-12-11
02:28
Merge updates from trunk. check-in: 5b5d3e4d user: mistachkin tags: asciiMode
2014-09-01
01:15
Merge updates from trunk. check-in: 51f33cf1 user: mistachkin tags: asciiMode
2014-08-30
15:49
In the command-line shell, added options --lookaside, --pagecache, and --scratch used to configure auxiliary memories. check-in: f61db04b user: drh tags: trunk
2014-07-24
22:51
Correct help text and make consistent use of snprintf. check-in: 9c424a5c user: mistachkin tags: asciiMode
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to VERSION.

     1         -3.8.6
            1  +3.8.7

Changes to autoconf/tea/Makefile.in.

    69     69   
    70     70   srcdir		= @srcdir@
    71     71   prefix		= @prefix@
    72     72   exec_prefix	= @exec_prefix@
    73     73   
    74     74   bindir		= @bindir@
    75     75   libdir		= @libdir@
           76  +datarootdir	= @datarootdir@
    76     77   datadir		= @datadir@
    77     78   mandir		= @mandir@
    78     79   includedir	= @includedir@
    79     80   
    80     81   DESTDIR		=
    81     82   
    82     83   PKG_DIR		= $(PACKAGE_NAME)$(PACKAGE_VERSION)

Changes to autoconf/tea/configure.in.

   162    162   AC_DEFINE(USE_TCL_STUBS, 1, [Use Tcl stubs])
   163    163   #AC_DEFINE(USE_TK_STUBS, 1, [Use Tk stubs])
   164    164   
   165    165   
   166    166   #--------------------------------------------------------------------
   167    167   # Redefine fdatasync as fsync on systems that lack fdatasync
   168    168   #--------------------------------------------------------------------
   169         -
   170         -AC_CHECK_FUNC(fdatasync, , AC_DEFINE(fdatasync, fsync))
          169  +#
          170  +#AC_CHECK_FUNC(fdatasync, , AC_DEFINE(fdatasync, fsync))
          171  +# Check for library functions that SQLite can optionally use.
          172  +AC_CHECK_FUNCS([fdatasync usleep fullfsync localtime_r gmtime_r])
   171    173   
   172    174   AC_FUNC_STRERROR_R
   173    175   
   174    176   
   175    177   #--------------------------------------------------------------------
   176    178   # This macro generates a line to use when building a library.  It
   177    179   # depends on values set by the TEA_ENABLE_SHARED, TEA_ENABLE_SYMBOLS,

Changes to autoconf/tea/tclconfig/tcl.m4.

  1637   1637   	    ])
  1638   1638   	    ;;
  1639   1639   	FreeBSD-*)
  1640   1640   	    # This configuration from FreeBSD Ports.
  1641   1641   	    SHLIB_CFLAGS="-fPIC"
  1642   1642   	    SHLIB_LD="${CC} -shared"
  1643   1643   	    TCL_SHLIB_LD_EXTRAS="-Wl,-soname=\$[@]"
         1644  +	    TK_SHLIB_LD_EXTRAS="-Wl,-soname,\$[@]"
  1644   1645   	    SHLIB_SUFFIX=".so"
  1645   1646   	    LDFLAGS=""
  1646   1647   	    AS_IF([test $doRpath = yes], [
  1647   1648   		CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}'
  1648   1649   		LD_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}'])
  1649   1650   	    AS_IF([test "${TCL_THREADS}" = "1"], [
  1650   1651   		# The -pthread needs to go in the LDFLAGS, not LIBS
  1651   1652   		LIBS=`echo $LIBS | sed s/-pthread//`
  1652   1653   		CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  1653   1654   		LDFLAGS="$LDFLAGS $PTHREAD_LIBS"])
  1654         -	    # Version numbers are dot-stripped by system policy.
  1655         -	    TCL_TRIM_DOTS=`echo ${PACKAGE_VERSION} | tr -d .`
  1656         -	    UNSHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.a'
  1657         -	    SHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}\$\{DBGX\}.so.1'
  1658         -	    TCL_LIB_VERSIONS_OK=nodots
         1655  +	    case $system in
         1656  +	    FreeBSD-3.*)
         1657  +		# Version numbers are dot-stripped by system policy.
         1658  +		TCL_TRIM_DOTS=`echo ${VERSION} | tr -d .`
         1659  +		UNSHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.a'
         1660  +		SHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.so'
         1661  +		TCL_LIB_VERSIONS_OK=nodots
         1662  +		;;
         1663  +	    esac
  1659   1664   	    ;;
  1660   1665   	Darwin-*)
  1661   1666   	    CFLAGS_OPTIMIZE="-Os"
  1662   1667   	    SHLIB_CFLAGS="-fno-common"
  1663   1668   	    # To avoid discrepancies between what headers configure sees during
  1664   1669   	    # preprocessing tests and compiling tests, move any -isysroot and
  1665   1670   	    # -mmacosx-version-min flags from CFLAGS to CPPFLAGS:
................................................................................
  1822   1827   	    LD_SEARCH_FLAGS=""
  1823   1828   	    ;;
  1824   1829   	SCO_SV-3.2*)
  1825   1830   	    AS_IF([test "$GCC" = yes], [
  1826   1831   		SHLIB_CFLAGS="-fPIC -melf"
  1827   1832   		LDFLAGS="$LDFLAGS -melf -Wl,-Bexport"
  1828   1833   	    ], [
  1829         -	       SHLIB_CFLAGS="-Kpic -belf"
  1830         -	       LDFLAGS="$LDFLAGS -belf -Wl,-Bexport"
         1834  +		SHLIB_CFLAGS="-Kpic -belf"
         1835  +		LDFLAGS="$LDFLAGS -belf -Wl,-Bexport"
  1831   1836   	    ])
  1832   1837   	    SHLIB_LD="ld -G"
  1833   1838   	    SHLIB_LD_LIBS=""
  1834   1839   	    SHLIB_SUFFIX=".so"
  1835   1840   	    CC_SEARCH_FLAGS=""
  1836   1841   	    LD_SEARCH_FLAGS=""
  1837   1842   	    ;;
................................................................................
  4154   4159   	    no_celib=
  4155   4160   	    CELIB_DIR=${ac_cv_c_celibconfig}
  4156   4161   	    CELIB_DIR=`echo "$CELIB_DIR" | sed -e 's!\\\!/!g'`
  4157   4162   	    AC_MSG_RESULT([found $CELIB_DIR])
  4158   4163   	fi
  4159   4164       fi
  4160   4165   ])
  4161         -
  4162         -
  4163   4166   # Local Variables:
  4164   4167   # mode: autoconf
  4165   4168   # End:

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.8.6.
            3  +# Generated by GNU Autoconf 2.62 for sqlite 3.8.7.
     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.8.6'
   747         -PACKAGE_STRING='sqlite 3.8.6'
          746  +PACKAGE_VERSION='3.8.7'
          747  +PACKAGE_STRING='sqlite 3.8.7'
   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>
................................................................................
  1479   1479   #
  1480   1480   # Report the --help message.
  1481   1481   #
  1482   1482   if test "$ac_init_help" = "long"; then
  1483   1483     # Omit some internal or obsolete options to make the list less imposing.
  1484   1484     # This message is too long to be a string in the A/UX 3.1 sh.
  1485   1485     cat <<_ACEOF
  1486         -\`configure' configures sqlite 3.8.6 to adapt to many kinds of systems.
         1486  +\`configure' configures sqlite 3.8.7 to adapt to many kinds of systems.
  1487   1487   
  1488   1488   Usage: $0 [OPTION]... [VAR=VALUE]...
  1489   1489   
  1490   1490   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1491   1491   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1492   1492   
  1493   1493   Defaults for the options are specified in brackets.
................................................................................
  1544   1544     --build=BUILD     configure for building on BUILD [guessed]
  1545   1545     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1546   1546   _ACEOF
  1547   1547   fi
  1548   1548   
  1549   1549   if test -n "$ac_init_help"; then
  1550   1550     case $ac_init_help in
  1551         -     short | recursive ) echo "Configuration of sqlite 3.8.6:";;
         1551  +     short | recursive ) echo "Configuration of sqlite 3.8.7:";;
  1552   1552      esac
  1553   1553     cat <<\_ACEOF
  1554   1554   
  1555   1555   Optional Features:
  1556   1556     --disable-option-checking  ignore unrecognized --enable/--with options
  1557   1557     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1558   1558     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1660   1660       cd "$ac_pwd" || { ac_status=$?; break; }
  1661   1661     done
  1662   1662   fi
  1663   1663   
  1664   1664   test -n "$ac_init_help" && exit $ac_status
  1665   1665   if $ac_init_version; then
  1666   1666     cat <<\_ACEOF
  1667         -sqlite configure 3.8.6
         1667  +sqlite configure 3.8.7
  1668   1668   generated by GNU Autoconf 2.62
  1669   1669   
  1670   1670   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
  1671   1671   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  1672   1672   This configure script is free software; the Free Software Foundation
  1673   1673   gives unlimited permission to copy, distribute and modify it.
  1674   1674   _ACEOF
  1675   1675     exit
  1676   1676   fi
  1677   1677   cat >config.log <<_ACEOF
  1678   1678   This file contains any messages produced by compilers while
  1679   1679   running configure, to aid debugging if configure makes a mistake.
  1680   1680   
  1681         -It was created by sqlite $as_me 3.8.6, which was
         1681  +It was created by sqlite $as_me 3.8.7, which was
  1682   1682   generated by GNU Autoconf 2.62.  Invocation command line was
  1683   1683   
  1684   1684     $ $0 $@
  1685   1685   
  1686   1686   _ACEOF
  1687   1687   exec 5>>config.log
  1688   1688   {
................................................................................
 14017  14017   
 14018  14018   exec 6>&1
 14019  14019   
 14020  14020   # Save the log message, to keep $[0] and so on meaningful, and to
 14021  14021   # report actual input values of CONFIG_FILES etc. instead of their
 14022  14022   # values after options handling.
 14023  14023   ac_log="
 14024         -This file was extended by sqlite $as_me 3.8.6, which was
        14024  +This file was extended by sqlite $as_me 3.8.7, which was
 14025  14025   generated by GNU Autoconf 2.62.  Invocation command line was
 14026  14026   
 14027  14027     CONFIG_FILES    = $CONFIG_FILES
 14028  14028     CONFIG_HEADERS  = $CONFIG_HEADERS
 14029  14029     CONFIG_LINKS    = $CONFIG_LINKS
 14030  14030     CONFIG_COMMANDS = $CONFIG_COMMANDS
 14031  14031     $ $0 $@
................................................................................
 14070  14070   $config_commands
 14071  14071   
 14072  14072   Report bugs to <bug-autoconf@gnu.org>."
 14073  14073   
 14074  14074   _ACEOF
 14075  14075   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 14076  14076   ac_cs_version="\\
 14077         -sqlite config.status 3.8.6
        14077  +sqlite config.status 3.8.7
 14078  14078   configured by $0, generated by GNU Autoconf 2.62,
 14079  14079     with options \\"`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"
 14080  14080   
 14081  14081   Copyright (C) 2008 Free Software Foundation, Inc.
 14082  14082   This config.status script is free software; the Free Software Foundation
 14083  14083   gives unlimited permission to copy, distribute and modify it."
 14084  14084   

Changes to ext/fts3/fts3_unicode.c.

   314    314     int *pnToken,                   /* OUT: Number of bytes at *paToken */
   315    315     int *piStart,                   /* OUT: Starting offset of token */
   316    316     int *piEnd,                     /* OUT: Ending offset of token */
   317    317     int *piPos                      /* OUT: Position integer of token */
   318    318   ){
   319    319     unicode_cursor *pCsr = (unicode_cursor *)pC;
   320    320     unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
   321         -  int iCode;
          321  +  int iCode = 0;
   322    322     char *zOut;
   323    323     const unsigned char *z = &pCsr->aInput[pCsr->iOff];
   324    324     const unsigned char *zStart = z;
   325    325     const unsigned char *zEnd;
   326    326     const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
   327    327   
   328    328     /* Scan past any delimiter characters before the start of the next token.

Changes to ext/fts3/fts3_unicode2.c.

    35     35     ** The most significant 22 bits in each 32-bit value contain the first 
    36     36     ** codepoint in the range. The least significant 10 bits are used to store
    37     37     ** the size of the range (always at least 1). In other words, the value 
    38     38     ** ((C<<22) + N) represents a range of N codepoints starting with codepoint 
    39     39     ** C. It is not possible to represent a range larger than 1023 codepoints 
    40     40     ** using this format.
    41     41     */
    42         -  const static unsigned int aEntry[] = {
           42  +  static const unsigned int aEntry[] = {
    43     43       0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
    44     44       0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
    45     45       0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
    46     46       0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
    47     47       0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
    48     48       0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
    49     49       0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
................................................................................
   127    127       0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
   128    128     };
   129    129   
   130    130     if( c<128 ){
   131    131       return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
   132    132     }else if( c<(1<<22) ){
   133    133       unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
   134         -    int iRes;
          134  +    int iRes = 0;
   135    135       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   136    136       int iLo = 0;
   137    137       while( iHi>=iLo ){
   138    138         int iTest = (iHi + iLo) / 2;
   139    139         if( key >= aEntry[iTest] ){
   140    140           iRes = iTest;
   141    141           iLo = iTest+1;
................................................................................
   198    198         iLo = iTest+1;
   199    199       }else{
   200    200         iHi = iTest-1;
   201    201       }
   202    202     }
   203    203     assert( key>=aDia[iRes] );
   204    204     return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
   205         -};
          205  +}
   206    206   
   207    207   
   208    208   /*
   209    209   ** Return true if the argument interpreted as a unicode codepoint
   210    210   ** is a diacritical modifier character.
   211    211   */
   212    212   int sqlite3FtsUnicodeIsdiacritic(int c){

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

   156    156         iLo = iTest+1;
   157    157       }else{
   158    158         iHi = iTest-1;
   159    159       }
   160    160     }
   161    161     assert( key>=aDia[iRes] );
   162    162     return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);}
   163         -  puts "\};"
          163  +  puts "\}"
   164    164   }
   165    165   
   166    166   proc print_isdiacritic {zFunc map} {
   167    167   
   168    168     set lCode [list]
   169    169     foreach m $map {
   170    170       foreach {code char} $m {}
................................................................................
   294    294     ** codepoint in the range. The least significant 10 bits are used to store
   295    295     ** the size of the range (always at least 1). In other words, the value 
   296    296     ** ((C<<22) + N) represents a range of N codepoints starting with codepoint 
   297    297     ** C. It is not possible to represent a range larger than 1023 codepoints 
   298    298     ** using this format.
   299    299     */
   300    300     }]
   301         -  puts -nonewline "  const static unsigned int aEntry\[\] = \{"
          301  +  puts -nonewline "  static const unsigned int aEntry\[\] = \{"
   302    302     set i 0
   303    303     foreach range $lRange {
   304    304       foreach {iFirst nRange} $range {}
   305    305       set u32 [format "0x%08X" [expr ($iFirst<<10) + $nRange]]
   306    306   
   307    307       if {($i % 5)==0} {puts "" ; puts -nonewline "   "}
   308    308       puts -nonewline " $u32,"
................................................................................
   345    345     an_print_range_array $lRange
   346    346     an_print_ascii_bitmap $lRange
   347    347     puts {
   348    348     if( c<128 ){
   349    349       return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
   350    350     }else if( c<(1<<22) ){
   351    351       unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
   352         -    int iRes;
          352  +    int iRes = 0;
   353    353       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   354    354       int iLo = 0;
   355    355       while( iHi>=iLo ){
   356    356         int iTest = (iHi + iLo) / 2;
   357    357         if( key >= aEntry[iTest] ){
   358    358           iRes = iTest;
   359    359           iLo = iTest+1;
................................................................................
   728    728   */
   729    729   
   730    730   /*
   731    731   ** DO NOT EDIT THIS MACHINE GENERATED FILE.
   732    732   */
   733    733     }]
   734    734     puts ""
   735         -  puts "#if defined(SQLITE_ENABLE_FTS4_UNICODE61)"
          735  +  puts "#ifndef SQLITE_DISABLE_FTS3_UNICODE"
   736    736     puts "#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)"
   737    737     puts ""
   738    738     puts "#include <assert.h>"
   739    739     puts ""
   740    740   }
   741    741   
   742    742   proc print_test_main {} {
................................................................................
   804    804   if {$::generate_test_code} {
   805    805     print_test_isalnum sqlite3FtsUnicodeIsalnum $lRange
   806    806     print_fold_test sqlite3FtsUnicodeFold $mappings
   807    807     print_test_main 
   808    808   }
   809    809   
   810    810   puts "#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */"
   811         -puts "#endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */"
          811  +puts "#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */"

Changes to ext/misc/fileio.c.

    57     57   static void writefileFunc(
    58     58     sqlite3_context *context,
    59     59     int argc,
    60     60     sqlite3_value **argv
    61     61   ){
    62     62     FILE *out;
    63     63     const char *z;
    64         -  int n;
    65     64     sqlite3_int64 rc;
    66     65     const char *zFile;
    67     66   
    68     67     zFile = (const char*)sqlite3_value_text(argv[0]);
    69     68     if( zFile==0 ) return;
    70     69     out = fopen(zFile, "wb");
    71     70     if( out==0 ) return;
    72     71     z = (const char*)sqlite3_value_blob(argv[1]);
    73     72     if( z==0 ){
    74         -    n = 0;
    75     73       rc = 0;
    76     74     }else{
    77         -    n = sqlite3_value_bytes(argv[1]);
    78         -    rc = fwrite(z, 1, n, out);
           75  +    rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
    79     76     }
    80     77     fclose(out);
    81     78     sqlite3_result_int64(context, rc);
    82     79   }
    83     80   
    84     81   
    85     82   #ifdef _WIN32

Changes to ext/misc/spellfix.c.

  2732   2732       }
  2733   2733       zK2 = (char*)phoneticHash((const unsigned char*)zK1, i);
  2734   2734       if( zK2==0 ){
  2735   2735         sqlite3_free(zK1);
  2736   2736         return SQLITE_NOMEM;
  2737   2737       }
  2738   2738       if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
  2739         -      spellfix1DbExec(&rc, db,
  2740         -             "INSERT INTO \"%w\".\"%w_vocab\"(rank,langid,word,k1,k2) "
  2741         -             "VALUES(%d,%d,%Q,%Q,%Q)",
  2742         -             p->zDbName, p->zTableName,
  2743         -             iRank, iLang, zWord, zK1, zK2
  2744         -      );
         2739  +      if( sqlite3_value_type(argv[1])==SQLITE_NULL ){
         2740  +        spellfix1DbExec(&rc, db,
         2741  +               "INSERT INTO \"%w\".\"%w_vocab\"(rank,langid,word,k1,k2) "
         2742  +               "VALUES(%d,%d,%Q,%Q,%Q)",
         2743  +               p->zDbName, p->zTableName,
         2744  +               iRank, iLang, zWord, zK1, zK2
         2745  +        );
         2746  +      }else{
         2747  +        newRowid = sqlite3_value_int64(argv[1]);
         2748  +        spellfix1DbExec(&rc, db,
         2749  +               "INSERT INTO \"%w\".\"%w_vocab\"(id,rank,langid,word,k1,k2) "
         2750  +               "VALUES(%lld,%d,%d,%Q,%Q,%Q)",
         2751  +               p->zDbName, p->zTableName,
         2752  +               newRowid, iRank, iLang, zWord, zK1, zK2
         2753  +        );
         2754  +      }
  2745   2755         *pRowid = sqlite3_last_insert_rowid(db);
  2746   2756       }else{
  2747   2757         rowid = sqlite3_value_int64(argv[0]);
  2748   2758         newRowid = *pRowid = sqlite3_value_int64(argv[1]);
  2749   2759         spellfix1DbExec(&rc, db,
  2750   2760                "UPDATE \"%w\".\"%w_vocab\" SET id=%lld, rank=%d, langid=%d,"
  2751   2761                " word=%Q, k1=%Q, k2=%Q WHERE id=%lld",

Changes to ext/rtree/rtree.c.

  1529   1529     RtreeNode *pRoot = 0;
  1530   1530     int ii;
  1531   1531     int rc = SQLITE_OK;
  1532   1532     int iCell = 0;
  1533   1533   
  1534   1534     rtreeReference(pRtree);
  1535   1535   
         1536  +  /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
  1536   1537     freeCursorConstraints(pCsr);
         1538  +  sqlite3_free(pCsr->aPoint);
         1539  +  memset(pCsr, 0, sizeof(RtreeCursor));
         1540  +  pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
         1541  +
  1537   1542     pCsr->iStrategy = idxNum;
  1538         -
  1539   1543     if( idxNum==1 ){
  1540   1544       /* Special case - lookup by rowid. */
  1541   1545       RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
  1542   1546       RtreeSearchPoint *p;     /* Search point for the the leaf */
  1543   1547       i64 iRowid = sqlite3_value_int64(argv[0]);
  1544   1548       i64 iNode = 0;
  1545   1549       rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);

Changes to ext/rtree/rtree1.test.

    29     29   #   rtree-4.*: Test INSERT
    30     30   #   rtree-5.*: Test DELETE
    31     31   #   rtree-6.*: Test UPDATE
    32     32   #   rtree-7.*: Test renaming an r-tree table.
    33     33   #   rtree-8.*: Test constrained scans of r-tree data.
    34     34   #
    35     35   #   rtree-12.*: Test that on-conflict clauses are supported.
           36  +#   rtree-13.*: Test that bug [d2889096e7bdeac6d] has been fixed.
    36     37   #
    37     38   
    38     39   ifcapable !rtree {
    39     40     finish_test
    40     41     return
    41     42   }
    42     43   
................................................................................
   509    510       do_test $testname.2 [list sql_uses_stmt db $sql] $uses
   510    511       do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
   511    512   
   512    513       do_test $testname.4 { rtree_check db t1 } 0
   513    514       db close
   514    515     }
   515    516   }
          517  +
          518  +#-------------------------------------------------------------------------
          519  +# Test that bug [d2889096e7bdeac6d] has been fixed.
          520  +#
          521  +reset_db
          522  +do_execsql_test 13.1 {
          523  +  CREATE VIRTUAL TABLE t9 USING rtree(id, xmin, xmax);
          524  +  INSERT INTO t9 VALUES(1,0,0);            
          525  +  INSERT INTO t9 VALUES(2,0,0);
          526  +  SELECT * FROM t9 WHERE id IN (1, 2);
          527  +} {1 0.0 0.0 2 0.0 0.0}
          528  +
          529  +do_execsql_test 13.2 {
          530  +  WITH r(x) AS (
          531  +    SELECT 1 UNION ALL
          532  +    SELECT 2 UNION ALL
          533  +    SELECT 3
          534  +  )
          535  +  SELECT * FROM r CROSS JOIN t9 WHERE id=x;
          536  +} {1 1 0.0 0.0 2 2 0.0 0.0}
          537  +
   516    538   finish_test

Added ext/rtree/rtreeF.test.

            1  +# 2014-08-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 contains tests for the r-tree module.
           12  +#
           13  +# This file contains test cases for the ticket
           14  +# [369d57fb8e5ccdff06f197a37147a88f9de95cda] (2014-08-21)
           15  +#
           16  +#  The following SQL causes an assertion fault while running
           17  +#  sqlite3_prepare() on the DELETE statement:
           18  +#
           19  +#     CREATE TABLE t1(x);
           20  +#     CREATE TABLE t2(y);
           21  +#     CREATE VIRTUAL TABLE t3 USING rtree(a,b,c);
           22  +#     CREATE TRIGGER t2del AFTER DELETE ON t2 WHEN (SELECT 1 from t1) BEGIN 
           23  +#       DELETE FROM t3 WHERE a=old.y; 
           24  +#     END;
           25  +#     DELETE FROM t2 WHERE y=1;
           26  +# 
           27  +
           28  +if {![info exists testdir]} {
           29  +  set testdir [file join [file dirname [info script]] .. .. test]
           30  +} 
           31  +source $testdir/tester.tcl
           32  +ifcapable !rtree { finish_test ; return }
           33  +
           34  +do_execsql_test rtreeF-1.1 {
           35  +  CREATE TABLE t1(x);
           36  +  CREATE TABLE t2(y);
           37  +  CREATE VIRTUAL TABLE t3 USING rtree(a,b,c);
           38  +  CREATE TRIGGER t2dwl AFTER DELETE ON t2 WHEN (SELECT 1 from t1) BEGIN 
           39  +    DELETE FROM t3 WHERE a=old.y; 
           40  +  END;
           41  +
           42  +  INSERT INTO t1(x) VALUES(999);
           43  +  INSERT INTO t2(y) VALUES(1),(2),(3),(4),(5);
           44  +  INSERT INTO t3(a,b,c) VALUES(1,2,3),(2,3,4),(3,4,5),(4,5,6),(5,6,7);
           45  +
           46  +  SELECT a FROM t3 ORDER BY a;
           47  +  SELECT '|';
           48  +  SELECT y FROM t2 ORDER BY y;
           49  +} {1 2 3 4 5 | 1 2 3 4 5}
           50  +do_execsql_test rtreeF-1.2 {
           51  +  DELETE FROM t2 WHERE y=3;
           52  +
           53  +  SELECT a FROM t3 ORDER BY a;
           54  +  SELECT '|';
           55  +  SELECT y FROM t2 ORDER BY y;
           56  +} {1 2 4 5 | 1 2 4 5}
           57  +do_execsql_test rtreeF-1.3 {
           58  +  DELETE FROM t1;
           59  +  DELETE FROM t2 WHERE y=5;
           60  +
           61  +  SELECT a FROM t3 ORDER BY a;
           62  +  SELECT '|';
           63  +  SELECT y FROM t2 ORDER BY y;
           64  +} {1 2 4 5 | 1 2 4}
           65  +do_execsql_test rtreeF-1.4 {
           66  +  INSERT INTO t1 DEFAULT VALUES;
           67  +  DELETE FROM t2 WHERE y=5;
           68  +
           69  +  SELECT a FROM t3 ORDER BY a;
           70  +  SELECT '|';
           71  +  SELECT y FROM t2 ORDER BY y;
           72  +} {1 2 4 5 | 1 2 4}
           73  +do_execsql_test rtreeF-1.5 {
           74  +  DELETE FROM t2 WHERE y=2;
           75  +
           76  +  SELECT a FROM t3 ORDER BY a;
           77  +  SELECT '|';
           78  +  SELECT y FROM t2 ORDER BY y;
           79  +} {1 4 5 | 1 4}
           80  +
           81  +finish_test

Changes to src/analyze.c.

   367    367   #endif
   368    368     sqlite3DbFree(p->db, p);
   369    369   }
   370    370   
   371    371   /*
   372    372   ** Implementation of the stat_init(N,K,C) SQL function. The three parameters
   373    373   ** are:
   374         -**     N:    The number of columns in the index including the rowid/pk
   375         -**     K:    The number of columns in the index excluding the rowid/pk
   376         -**     C:    The number of rows in the index
          374  +**     N:    The number of columns in the index including the rowid/pk (note 1)
          375  +**     K:    The number of columns in the index excluding the rowid/pk.
          376  +**     C:    The number of rows in the index (note 2)
   377    377   **
   378         -** C is only used for STAT3 and STAT4.
          378  +** Note 1:  In the special case of the covering index that implements a
          379  +** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
          380  +** total number of columns in the table.
   379    381   **
   380         -** For ordinary rowid tables, N==K+1.  But for WITHOUT ROWID tables,
   381         -** N=K+P where P is the number of columns in the primary key.  For the
   382         -** covering index that implements the original WITHOUT ROWID table, N==K.
          382  +** Note 2:  C is only used for STAT3 and STAT4.
          383  +**
          384  +** For indexes on ordinary rowid tables, N==K+1.  But for indexes on
          385  +** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
          386  +** PRIMARY KEY of the table.  The covering index that implements the
          387  +** original WITHOUT ROWID table as N==K as a special case.
   383    388   **
   384    389   ** This routine allocates the Stat4Accum object in heap memory. The return 
   385    390   ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e. 
   386    391   ** the size of the blob is sizeof(void*) bytes). 
   387    392   */
   388    393   static void statInit(
   389    394     sqlite3_context *context,
................................................................................
   685    690   ** Arguments:
   686    691   **
   687    692   **    P     Pointer to the Stat4Accum object created by stat_init()
   688    693   **    C     Index of left-most column to differ from previous row
   689    694   **    R     Rowid for the current row.  Might be a key record for
   690    695   **          WITHOUT ROWID tables.
   691    696   **
   692         -** The SQL function always returns NULL.
          697  +** This SQL function always returns NULL.  It's purpose it to accumulate
          698  +** statistical data and/or samples in the Stat4Accum object about the
          699  +** index being analyzed.  The stat_get() SQL function will later be used to
          700  +** extract relevant information for constructing the sqlite_statN tables.
   693    701   **
   694    702   ** The R parameter is only used for STAT3 and STAT4
   695    703   */
   696    704   static void statPush(
   697    705     sqlite3_context *context,
   698    706     int argc,
   699    707     sqlite3_value **argv
................................................................................
   779    787   #define STAT_GET_ROWID 1          /* "rowid" column of stat[34] entry */
   780    788   #define STAT_GET_NEQ   2          /* "neq" column of stat[34] entry */
   781    789   #define STAT_GET_NLT   3          /* "nlt" column of stat[34] entry */
   782    790   #define STAT_GET_NDLT  4          /* "ndlt" column of stat[34] entry */
   783    791   
   784    792   /*
   785    793   ** Implementation of the stat_get(P,J) SQL function.  This routine is
   786         -** used to query the results.  Content is returned for parameter J
          794  +** used to query statistical information that has been gathered into
          795  +** the Stat4Accum object by prior calls to stat_push().  The P parameter
          796  +** is a BLOB which is decoded into a pointer to the Stat4Accum objects.
          797  +** The content to returned is determined by the parameter J
   787    798   ** which is one of the STAT_GET_xxxx values defined above.
   788    799   **
   789    800   ** If neither STAT3 nor STAT4 are enabled, then J is always
   790    801   ** STAT_GET_STAT1 and is hence omitted and this routine becomes
   791    802   ** a one-parameter function, stat_get(P), that always returns the
   792    803   ** stat1 table entry information.
   793    804   */
................................................................................
   998   1009     iTabCur = iTab++;
   999   1010     iIdxCur = iTab++;
  1000   1011     pParse->nTab = MAX(pParse->nTab, iTab);
  1001   1012     sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
  1002   1013     sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
  1003   1014   
  1004   1015     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1005         -    int nCol;                     /* Number of columns indexed by pIdx */
  1006         -    int *aGotoChng;               /* Array of jump instruction addresses */
         1016  +    int nCol;                     /* Number of columns in pIdx. "N" */
  1007   1017       int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
  1008         -    int addrGotoChng0;            /* Address of "Goto addr_chng_0" */
  1009   1018       int addrNextRow;              /* Address of "next_row:" */
  1010   1019       const char *zIdxName;         /* Name of the index */
         1020  +    int nColTest;                 /* Number of columns to test for changes */
  1011   1021   
  1012   1022       if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
  1013   1023       if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
  1014   1024       if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
  1015   1025         nCol = pIdx->nKeyCol;
  1016   1026         zIdxName = pTab->zName;
         1027  +      nColTest = nCol - 1;
  1017   1028       }else{
  1018   1029         nCol = pIdx->nColumn;
  1019   1030         zIdxName = pIdx->zName;
         1031  +      nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
  1020   1032       }
  1021         -    aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
  1022         -    if( aGotoChng==0 ) continue;
  1023   1033   
  1024   1034       /* Populate the register containing the index name. */
  1025   1035       sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
  1026   1036       VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
  1027   1037   
  1028   1038       /*
  1029   1039       ** Pseudo-code for loop that calls stat_push():
................................................................................
  1044   1054       **
  1045   1055       **  chng_addr_0:
  1046   1056       **   regPrev(0) = idx(0)
  1047   1057       **  chng_addr_1:
  1048   1058       **   regPrev(1) = idx(1)
  1049   1059       **  ...
  1050   1060       **
  1051         -    **  chng_addr_N:
         1061  +    **  endDistinctTest:
  1052   1062       **   regRowid = idx(rowid)
  1053   1063       **   stat_push(P, regChng, regRowid)
  1054   1064       **   Next csr
  1055   1065       **   if !eof(csr) goto next_row;
  1056   1066       **
  1057   1067       **  end_of_scan:
  1058   1068       */
  1059   1069   
  1060   1070       /* Make sure there are enough memory cells allocated to accommodate 
  1061   1071       ** the regPrev array and a trailing rowid (the rowid slot is required
  1062   1072       ** when building a record to insert into the sample column of 
  1063   1073       ** the sqlite_stat4 table.  */
  1064         -    pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
         1074  +    pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
  1065   1075   
  1066   1076       /* Open a read-only cursor on the index being analyzed. */
  1067   1077       assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
  1068   1078       sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
  1069   1079       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1070   1080       VdbeComment((v, "%s", pIdx->zName));
  1071   1081   
  1072   1082       /* Invoke the stat_init() function. The arguments are:
  1073   1083       ** 
  1074         -    **    (1) the number of columns in the index including the rowid,
  1075         -    **    (2) the number of rows in the index,
         1084  +    **    (1) the number of columns in the index including the rowid
         1085  +    **        (or for a WITHOUT ROWID table, the number of PK columns),
         1086  +    **    (2) the number of columns in the key without the rowid/pk
         1087  +    **    (3) the number of rows in the index,
         1088  +    **
  1076   1089       **
  1077         -    ** The second argument is only used for STAT3 and STAT4
         1090  +    ** The third argument is only used for STAT3 and STAT4
  1078   1091       */
  1079   1092   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1080   1093       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
  1081   1094   #endif
  1082   1095       sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
  1083   1096       sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
  1084   1097       sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
................................................................................
  1092   1105       **   regChng = 0
  1093   1106       **   goto next_push_0;
  1094   1107       **
  1095   1108       */
  1096   1109       addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
  1097   1110       VdbeCoverage(v);
  1098   1111       sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
  1099         -    addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
  1100         -
  1101         -    /*
  1102         -    **  next_row:
  1103         -    **   regChng = 0
  1104         -    **   if( idx(0) != regPrev(0) ) goto chng_addr_0
  1105         -    **   regChng = 1
  1106         -    **   if( idx(1) != regPrev(1) ) goto chng_addr_1
  1107         -    **   ...
  1108         -    **   regChng = N
  1109         -    **   goto chng_addr_N
  1110         -    */
  1111   1112       addrNextRow = sqlite3VdbeCurrentAddr(v);
  1112         -    for(i=0; i<nCol-1; i++){
  1113         -      char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
  1114         -      sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
  1115         -      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
  1116         -      aGotoChng[i] = 
  1117         -      sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
  1118         -      sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
  1119         -      VdbeCoverage(v);
  1120         -    }
  1121         -    sqlite3VdbeAddOp2(v, OP_Integer, nCol-1, regChng);
  1122         -    aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
  1123         -
  1124         -    /*
  1125         -    **  chng_addr_0:
  1126         -    **   regPrev(0) = idx(0)
  1127         -    **  chng_addr_1:
  1128         -    **   regPrev(1) = idx(1)
  1129         -    **  ...
  1130         -    */
  1131         -    sqlite3VdbeJumpHere(v, addrGotoChng0);
  1132         -    for(i=0; i<nCol-1; i++){
  1133         -      sqlite3VdbeJumpHere(v, aGotoChng[i]);
  1134         -      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
  1135         -    }
  1136         -
         1113  +
         1114  +    if( nColTest>0 ){
         1115  +      int endDistinctTest = sqlite3VdbeMakeLabel(v);
         1116  +      int *aGotoChng;               /* Array of jump instruction addresses */
         1117  +      aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
         1118  +      if( aGotoChng==0 ) continue;
         1119  +
         1120  +      /*
         1121  +      **  next_row:
         1122  +      **   regChng = 0
         1123  +      **   if( idx(0) != regPrev(0) ) goto chng_addr_0
         1124  +      **   regChng = 1
         1125  +      **   if( idx(1) != regPrev(1) ) goto chng_addr_1
         1126  +      **   ...
         1127  +      **   regChng = N
         1128  +      **   goto endDistinctTest
         1129  +      */
         1130  +      sqlite3VdbeAddOp0(v, OP_Goto);
         1131  +      addrNextRow = sqlite3VdbeCurrentAddr(v);
         1132  +      if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
         1133  +        /* For a single-column UNIQUE index, once we have found a non-NULL
         1134  +        ** row, we know that all the rest will be distinct, so skip 
         1135  +        ** subsequent distinctness tests. */
         1136  +        sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
         1137  +        VdbeCoverage(v);
         1138  +      }
         1139  +      for(i=0; i<nColTest; i++){
         1140  +        char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
         1141  +        sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
         1142  +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
         1143  +        aGotoChng[i] = 
         1144  +        sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
         1145  +        sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
         1146  +        VdbeCoverage(v);
         1147  +      }
         1148  +      sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
         1149  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, endDistinctTest);
         1150  +  
         1151  +  
         1152  +      /*
         1153  +      **  chng_addr_0:
         1154  +      **   regPrev(0) = idx(0)
         1155  +      **  chng_addr_1:
         1156  +      **   regPrev(1) = idx(1)
         1157  +      **  ...
         1158  +      */
         1159  +      sqlite3VdbeJumpHere(v, addrNextRow-1);
         1160  +      for(i=0; i<nColTest; i++){
         1161  +        sqlite3VdbeJumpHere(v, aGotoChng[i]);
         1162  +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
         1163  +      }
         1164  +      sqlite3VdbeResolveLabel(v, endDistinctTest);
         1165  +      sqlite3DbFree(db, aGotoChng);
         1166  +    }
         1167  +  
  1137   1168       /*
  1138   1169       **  chng_addr_N:
  1139   1170       **   regRowid = idx(rowid)            // STAT34 only
  1140   1171       **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
  1141   1172       **   Next csr
  1142   1173       **   if !eof(csr) goto next_row;
  1143   1174       */
  1144         -    sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
  1145   1175   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1146   1176       assert( regRowid==(regStat4+2) );
  1147   1177       if( HasRowid(pTab) ){
  1148   1178         sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
  1149   1179       }else{
  1150   1180         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
  1151   1181         int j, k, regKey;
................................................................................
  1215   1245         sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
  1216   1246         sqlite3VdbeJumpHere(v, addrIsNull);
  1217   1247       }
  1218   1248   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1219   1249   
  1220   1250       /* End of analysis */
  1221   1251       sqlite3VdbeJumpHere(v, addrRewind);
  1222         -    sqlite3DbFree(db, aGotoChng);
  1223   1252     }
  1224   1253   
  1225   1254   
  1226   1255     /* Create a single sqlite_stat1 entry containing NULL as the index
  1227   1256     ** name and the row count as the content.
  1228   1257     */
  1229   1258     if( pOnlyIdx==0 && needTableCnt ){

Changes to src/backup.c.

    83     83     int i = sqlite3FindDbName(pDb, zDb);
    84     84   
    85     85     if( i==1 ){
    86     86       Parse *pParse;
    87     87       int rc = 0;
    88     88       pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
    89     89       if( pParse==0 ){
    90         -      sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
           90  +      sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory");
    91     91         rc = SQLITE_NOMEM;
    92     92       }else{
    93     93         pParse->db = pDb;
    94     94         if( sqlite3OpenTempDatabase(pParse) ){
    95         -        sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
           95  +        sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
    96     96           rc = SQLITE_ERROR;
    97     97         }
    98     98         sqlite3DbFree(pErrorDb, pParse->zErrMsg);
    99     99         sqlite3ParserReset(pParse);
   100    100         sqlite3StackFree(pErrorDb, pParse);
   101    101       }
   102    102       if( rc ){
   103    103         return 0;
   104    104       }
   105    105     }
   106    106   
   107    107     if( i<0 ){
   108         -    sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
          108  +    sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
   109    109       return 0;
   110    110     }
   111    111   
   112    112     return pDb->aDb[i].pBt;
   113    113   }
   114    114   
   115    115   /*
................................................................................
   146    146     ** database connection while a backup is in progress may cause
   147    147     ** a malfunction or a deadlock.
   148    148     */
   149    149     sqlite3_mutex_enter(pSrcDb->mutex);
   150    150     sqlite3_mutex_enter(pDestDb->mutex);
   151    151   
   152    152     if( pSrcDb==pDestDb ){
   153         -    sqlite3Error(
          153  +    sqlite3ErrorWithMsg(
   154    154           pDestDb, SQLITE_ERROR, "source and destination must be distinct"
   155    155       );
   156    156       p = 0;
   157    157     }else {
   158    158       /* Allocate space for a new sqlite3_backup object...
   159    159       ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
   160    160       ** call to sqlite3_backup_init() and is destroyed by a call to
   161    161       ** sqlite3_backup_finish(). */
   162    162       p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
   163    163       if( !p ){
   164         -      sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
          164  +      sqlite3Error(pDestDb, SQLITE_NOMEM);
   165    165       }
   166    166     }
   167    167   
   168    168     /* If the allocation succeeded, populate the new object. */
   169    169     if( p ){
   170    170       p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
   171    171       p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
................................................................................
   598    598   
   599    599     /* If a transaction is still open on the Btree, roll it back. */
   600    600     sqlite3BtreeRollback(p->pDest, SQLITE_OK);
   601    601   
   602    602     /* Set the error code of the destination database handle. */
   603    603     rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
   604    604     if( p->pDestDb ){
   605         -    sqlite3Error(p->pDestDb, rc, 0);
          605  +    sqlite3Error(p->pDestDb, rc);
   606    606   
   607    607       /* Exit the mutexes and free the backup context structure. */
   608    608       sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
   609    609     }
   610    610     sqlite3BtreeLeave(p->pSrc);
   611    611     if( p->pDestDb ){
   612    612       /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a

Changes to src/btmutex.c.

    34     34     p->locked = 1;
    35     35   }
    36     36   
    37     37   /*
    38     38   ** Release the BtShared mutex associated with B-Tree handle p and
    39     39   ** clear the p->locked boolean.
    40     40   */
    41         -static void unlockBtreeMutex(Btree *p){
           41  +static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
    42     42     BtShared *pBt = p->pBt;
    43     43     assert( p->locked==1 );
    44     44     assert( sqlite3_mutex_held(pBt->mutex) );
    45     45     assert( sqlite3_mutex_held(p->db->mutex) );
    46     46     assert( p->db==pBt->db );
    47     47   
    48     48     sqlite3_mutex_leave(pBt->mutex);
    49     49     p->locked = 0;
    50     50   }
    51     51   
           52  +/* Forward reference */
           53  +static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
           54  +
    52     55   /*
    53     56   ** Enter a mutex on the given BTree object.
    54     57   **
    55     58   ** If the object is not sharable, then no mutex is ever required
    56     59   ** and this routine is a no-op.  The underlying mutex is non-recursive.
    57     60   ** But we keep a reference count in Btree.wantToLock so the behavior
    58     61   ** of this interface is recursive.
................................................................................
    62     65   ** Btrees belonging to the same database connection as the p Btree
    63     66   ** which need to be locked after p.  If we cannot get a lock on
    64     67   ** p, then first unlock all of the others on p->pNext, then wait
    65     68   ** for the lock to become available on p, then relock all of the
    66     69   ** subsequent Btrees that desire a lock.
    67     70   */
    68     71   void sqlite3BtreeEnter(Btree *p){
    69         -  Btree *pLater;
    70         -
    71     72     /* Some basic sanity checking on the Btree.  The list of Btrees
    72     73     ** connected by pNext and pPrev should be in sorted order by
    73     74     ** Btree.pBt value. All elements of the list should belong to
    74     75     ** the same connection. Only shared Btrees are on the list. */
    75     76     assert( p->pNext==0 || p->pNext->pBt>p->pBt );
    76     77     assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
    77     78     assert( p->pNext==0 || p->pNext->db==p->db );
................................................................................
    88     89     /* Unless the database is sharable and unlocked, then BtShared.db
    89     90     ** should already be set correctly. */
    90     91     assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
    91     92   
    92     93     if( !p->sharable ) return;
    93     94     p->wantToLock++;
    94     95     if( p->locked ) return;
           96  +  btreeLockCarefully(p);
           97  +}
           98  +
           99  +/* This is a helper function for sqlite3BtreeLock(). By moving
          100  +** complex, but seldom used logic, out of sqlite3BtreeLock() and
          101  +** into this routine, we avoid unnecessary stack pointer changes
          102  +** and thus help the sqlite3BtreeLock() routine to run much faster
          103  +** in the common case.
          104  +*/
          105  +static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
          106  +  Btree *pLater;
    95    107   
    96    108     /* In most cases, we should be able to acquire the lock we
    97    109     ** want without having to go throught the ascending lock
    98    110     ** procedure that follows.  Just be sure not to block.
    99    111     */
   100    112     if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
   101    113       p->pBt->db = p->db;
................................................................................
   119    131     lockBtreeMutex(p);
   120    132     for(pLater=p->pNext; pLater; pLater=pLater->pNext){
   121    133       if( pLater->wantToLock ){
   122    134         lockBtreeMutex(pLater);
   123    135       }
   124    136     }
   125    137   }
          138  +
   126    139   
   127    140   /*
   128    141   ** Exit the recursive mutex on a Btree.
   129    142   */
   130    143   void sqlite3BtreeLeave(Btree *p){
   131    144     if( p->sharable ){
   132    145       assert( p->wantToLock>0 );

Changes to src/btree.c.

   158    158     }
   159    159   
   160    160     /* If the client is reading  or writing an index and the schema is
   161    161     ** not loaded, then it is too difficult to actually check to see if
   162    162     ** the correct locks are held.  So do not bother - just return true.
   163    163     ** This case does not come up very often anyhow.
   164    164     */
   165         -  if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
          165  +  if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
   166    166       return 1;
   167    167     }
   168    168   
   169    169     /* Figure out the root-page that the lock should be held on. For table
   170    170     ** b-trees, this is just the root page of the b-tree being read or
   171    171     ** written. For index b-trees, it is the root page of the associated
   172    172     ** table.  */
................................................................................
   625    625       pCur->eState = CURSOR_REQUIRESEEK;
   626    626     }
   627    627   
   628    628     invalidateOverflowCache(pCur);
   629    629     return rc;
   630    630   }
   631    631   
          632  +/* Forward reference */
          633  +static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
          634  +
   632    635   /*
   633    636   ** Save the positions of all cursors (except pExcept) that are open on
   634         -** the table  with root-page iRoot. Usually, this is called just before cursor
   635         -** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
          637  +** the table with root-page iRoot.  "Saving the cursor position" means that
          638  +** the location in the btree is remembered in such a way that it can be
          639  +** moved back to the same spot after the btree has been modified.  This
          640  +** routine is called just before cursor pExcept is used to modify the
          641  +** table, for example in BtreeDelete() or BtreeInsert().
          642  +**
          643  +** Implementation note:  This routine merely checks to see if any cursors
          644  +** need to be saved.  It calls out to saveCursorsOnList() in the (unusual)
          645  +** event that cursors are in need to being saved.
   636    646   */
   637    647   static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
   638    648     BtCursor *p;
   639    649     assert( sqlite3_mutex_held(pBt->mutex) );
   640    650     assert( pExcept==0 || pExcept->pBt==pBt );
   641    651     for(p=pBt->pCursor; p; p=p->pNext){
          652  +    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
          653  +  }
          654  +  return p ? saveCursorsOnList(p, iRoot, pExcept) : SQLITE_OK;
          655  +}
          656  +
          657  +/* This helper routine to saveAllCursors does the actual work of saving
          658  +** the cursors if and when a cursor is found that actually requires saving.
          659  +** The common case is that no cursors need to be saved, so this routine is
          660  +** broken out from its caller to avoid unnecessary stack pointer movement.
          661  +*/
          662  +static int SQLITE_NOINLINE saveCursorsOnList(
          663  +  BtCursor *p,           /* The first cursor that needs saving */
          664  +  Pgno iRoot,            /* Only save cursor with this iRoot.  Save all if zero */
          665  +  BtCursor *pExcept      /* Do not save this cursor */
          666  +){
          667  +  do{
   642    668       if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
   643    669         if( p->eState==CURSOR_VALID ){
   644    670           int rc = saveCursorPosition(p);
   645    671           if( SQLITE_OK!=rc ){
   646    672             return rc;
   647    673           }
   648    674         }else{
   649    675           testcase( p->iPage>0 );
   650    676           btreeReleaseAllCursorPages(p);
   651    677         }
   652    678       }
   653         -  }
          679  +    p = p->pNext;
          680  +  }while( p );
   654    681     return SQLITE_OK;
   655    682   }
   656    683   
   657    684   /*
   658    685   ** Clear the current cursor position.
   659    686   */
   660    687   void sqlite3BtreeClearCursor(BtCursor *pCur){
................................................................................
   731    758   
   732    759   #define restoreCursorPosition(p) \
   733    760     (p->eState>=CURSOR_REQUIRESEEK ? \
   734    761            btreeRestoreCursorPosition(p) : \
   735    762            SQLITE_OK)
   736    763   
   737    764   /*
   738         -** Determine whether or not a cursor has moved from the position it
   739         -** was last placed at.  Cursors can move when the row they are pointing
   740         -** at is deleted out from under them.
   741         -**
   742         -** This routine returns an error code if something goes wrong.  The
   743         -** integer *pHasMoved is set as follows:
   744         -**
   745         -**    0:   The cursor is unchanged
   746         -**    1:   The cursor is still pointing at the same row, but the pointers
   747         -**         returned by sqlite3BtreeKeyFetch() or sqlite3BtreeDataFetch()
   748         -**         might now be invalid because of a balance() or other change to the
   749         -**         b-tree.
   750         -**    2:   The cursor is no longer pointing to the row.  The row might have
   751         -**         been deleted out from under the cursor.
   752         -*/
   753         -int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
   754         -  int rc;
   755         -
   756         -  if( pCur->eState==CURSOR_VALID ){
   757         -    *pHasMoved = 0;
   758         -    return SQLITE_OK;
   759         -  }
          765  +** Determine whether or not a cursor has moved from the position where
          766  +** it was last placed, or has been invalidated for any other reason.
          767  +** Cursors can move when the row they are pointing at is deleted out
          768  +** from under them, for example.  Cursor might also move if a btree
          769  +** is rebalanced.
          770  +**
          771  +** Calling this routine with a NULL cursor pointer returns false.
          772  +**
          773  +** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
          774  +** back to where it ought to be if this routine returns true.
          775  +*/
          776  +int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
          777  +  return pCur && pCur->eState!=CURSOR_VALID;
          778  +}
          779  +
          780  +/*
          781  +** This routine restores a cursor back to its original position after it
          782  +** has been moved by some outside activity (such as a btree rebalance or
          783  +** a row having been deleted out from under the cursor).  
          784  +**
          785  +** On success, the *pDifferentRow parameter is false if the cursor is left
          786  +** pointing at exactly the same row.  *pDifferntRow is the row the cursor
          787  +** was pointing to has been deleted, forcing the cursor to point to some
          788  +** nearby row.
          789  +**
          790  +** This routine should only be called for a cursor that just returned
          791  +** TRUE from sqlite3BtreeCursorHasMoved().
          792  +*/
          793  +int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
          794  +  int rc;
          795  +
          796  +  assert( pCur!=0 );
          797  +  assert( pCur->eState!=CURSOR_VALID );
   760    798     rc = restoreCursorPosition(pCur);
   761    799     if( rc ){
   762         -    *pHasMoved = 2;
          800  +    *pDifferentRow = 1;
   763    801       return rc;
   764    802     }
   765    803     if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
   766         -    *pHasMoved = 2;
          804  +    *pDifferentRow = 1;
   767    805     }else{
   768         -    *pHasMoved = 1;
          806  +    *pDifferentRow = 0;
   769    807     }
   770    808     return SQLITE_OK;
   771    809   }
   772    810   
   773    811   #ifndef SQLITE_OMIT_AUTOVACUUM
   774    812   /*
   775    813   ** Given a page number of a regular database page, return the page
................................................................................
  1193   1231   ** the first two bytes past the cell pointer area since presumably this
  1194   1232   ** allocation is being made in order to insert a new cell, so we will
  1195   1233   ** also end up needing a new cell pointer.
  1196   1234   */
  1197   1235   static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
  1198   1236     const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
  1199   1237     u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
  1200         -  int nFrag;                           /* Number of fragmented bytes on pPage */
  1201   1238     int top;                             /* First byte of cell content area */
  1202   1239     int gap;        /* First byte of gap between cell pointers and cell content */
  1203   1240     int rc;         /* Integer return code */
  1204   1241     int usableSize; /* Usable size of the page */
  1205   1242     
  1206   1243     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  1207   1244     assert( pPage->pBt );
................................................................................
  1208   1245     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1209   1246     assert( nByte>=0 );  /* Minimum cell size is 4 */
  1210   1247     assert( pPage->nFree>=nByte );
  1211   1248     assert( pPage->nOverflow==0 );
  1212   1249     usableSize = pPage->pBt->usableSize;
  1213   1250     assert( nByte < usableSize-8 );
  1214   1251   
  1215         -  nFrag = data[hdr+7];
  1216   1252     assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
  1217   1253     gap = pPage->cellOffset + 2*pPage->nCell;
  1218         -  top = get2byteNotZero(&data[hdr+5]);
  1219         -  if( gap>top ) return SQLITE_CORRUPT_BKPT;
         1254  +  assert( gap<=65536 );
         1255  +  top = get2byte(&data[hdr+5]);
         1256  +  if( gap>top ){
         1257  +    if( top==0 ){
         1258  +      top = 65536;
         1259  +    }else{
         1260  +      return SQLITE_CORRUPT_BKPT;
         1261  +    }
         1262  +  }
         1263  +
         1264  +  /* If there is enough space between gap and top for one more cell pointer
         1265  +  ** array entry offset, and if the freelist is not empty, then search the
         1266  +  ** freelist looking for a free slot big enough to satisfy the request.
         1267  +  */
  1220   1268     testcase( gap+2==top );
  1221   1269     testcase( gap+1==top );
  1222   1270     testcase( gap==top );
  1223         -
  1224         -  if( nFrag>=60 ){
  1225         -    /* Always defragment highly fragmented pages */
  1226         -    rc = defragmentPage(pPage);
  1227         -    if( rc ) return rc;
  1228         -    top = get2byteNotZero(&data[hdr+5]);
  1229         -  }else if( gap+2<=top ){
  1230         -    /* Search the freelist looking for a free slot big enough to satisfy 
  1231         -    ** the request. The allocation is made from the first free slot in 
  1232         -    ** the list that is large enough to accommodate it.
  1233         -    */
         1271  +  if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){
  1234   1272       int pc, addr;
  1235   1273       for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
  1236   1274         int size;            /* Size of the free slot */
  1237   1275         if( pc>usableSize-4 || pc<addr+4 ){
  1238   1276           return SQLITE_CORRUPT_BKPT;
  1239   1277         }
  1240   1278         size = get2byte(&data[pc+2]);
  1241   1279         if( size>=nByte ){
  1242   1280           int x = size - nByte;
  1243   1281           testcase( x==4 );
  1244   1282           testcase( x==3 );
  1245   1283           if( x<4 ){
         1284  +          if( data[hdr+7]>=60 ) goto defragment_page;
  1246   1285             /* Remove the slot from the free-list. Update the number of
  1247   1286             ** fragmented bytes within the page. */
  1248   1287             memcpy(&data[addr], &data[pc], 2);
  1249         -          data[hdr+7] = (u8)(nFrag + x);
         1288  +          data[hdr+7] += (u8)x;
  1250   1289           }else if( size+pc > usableSize ){
  1251   1290             return SQLITE_CORRUPT_BKPT;
  1252   1291           }else{
  1253   1292             /* The slot remains on the free-list. Reduce its size to account
  1254   1293             ** for the portion used by the new allocation. */
  1255   1294             put2byte(&data[pc+2], x);
  1256   1295           }
  1257   1296           *pIdx = pc + x;
  1258   1297           return SQLITE_OK;
  1259   1298         }
  1260   1299       }
  1261   1300     }
  1262   1301   
  1263         -  /* Check to make sure there is enough space in the gap to satisfy
  1264         -  ** the allocation.  If not, defragment.
         1302  +  /* The request could not be fulfilled using a freelist slot.  Check
         1303  +  ** to see if defragmentation is necessary.
  1265   1304     */
  1266   1305     testcase( gap+2+nByte==top );
  1267   1306     if( gap+2+nByte>top ){
         1307  +defragment_page:
         1308  +    testcase( pPage->nCell==0 );
  1268   1309       rc = defragmentPage(pPage);
  1269   1310       if( rc ) return rc;
  1270   1311       top = get2byteNotZero(&data[hdr+5]);
  1271   1312       assert( gap+nByte<=top );
  1272   1313     }
  1273   1314   
  1274   1315   
................................................................................
  1283   1324     assert( top+nByte <= (int)pPage->pBt->usableSize );
  1284   1325     *pIdx = top;
  1285   1326     return SQLITE_OK;
  1286   1327   }
  1287   1328   
  1288   1329   /*
  1289   1330   ** Return a section of the pPage->aData to the freelist.
  1290         -** The first byte of the new free block is pPage->aDisk[start]
  1291         -** and the size of the block is "size" bytes.
         1331  +** The first byte of the new free block is pPage->aData[iStart]
         1332  +** and the size of the block is iSize bytes.
  1292   1333   **
  1293         -** Most of the effort here is involved in coalesing adjacent
  1294         -** free blocks into a single big free block.
         1334  +** Adjacent freeblocks are coalesced.
         1335  +**
         1336  +** Note that even though the freeblock list was checked by btreeInitPage(),
         1337  +** that routine will not detect overlap between cells or freeblocks.  Nor
         1338  +** does it detect cells or freeblocks that encrouch into the reserved bytes
         1339  +** at the end of the page.  So do additional corruption checks inside this
         1340  +** routine and return SQLITE_CORRUPT if any problems are found.
  1295   1341   */
  1296         -static int freeSpace(MemPage *pPage, int start, int size){
  1297         -  int addr, pbegin, hdr;
  1298         -  int iLast;                        /* Largest possible freeblock offset */
  1299         -  unsigned char *data = pPage->aData;
         1342  +static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
         1343  +  u16 iPtr;                             /* Address of pointer to next freeblock */
         1344  +  u16 iFreeBlk;                         /* Address of the next freeblock */
         1345  +  u8 hdr;                               /* Page header size.  0 or 100 */
         1346  +  u8 nFrag = 0;                         /* Reduction in fragmentation */
         1347  +  u16 iOrigSize = iSize;                /* Original value of iSize */
         1348  +  u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
         1349  +  u32 iEnd = iStart + iSize;            /* First byte past the iStart buffer */
         1350  +  unsigned char *data = pPage->aData;   /* Page content */
  1300   1351   
  1301   1352     assert( pPage->pBt!=0 );
  1302   1353     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  1303         -  assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
  1304         -  assert( (start + size) <= (int)pPage->pBt->usableSize );
         1354  +  assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
         1355  +  assert( iEnd <= pPage->pBt->usableSize );
  1305   1356     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1306         -  assert( size>=0 );   /* Minimum cell size is 4 */
         1357  +  assert( iSize>=4 );   /* Minimum cell size is 4 */
         1358  +  assert( iStart<=iLast );
  1307   1359   
         1360  +  /* Overwrite deleted information with zeros when the secure_delete
         1361  +  ** option is enabled */
  1308   1362     if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
  1309         -    /* Overwrite deleted information with zeros when the secure_delete
  1310         -    ** option is enabled */
  1311         -    memset(&data[start], 0, size);
         1363  +    memset(&data[iStart], 0, iSize);
  1312   1364     }
  1313   1365   
  1314         -  /* Add the space back into the linked list of freeblocks.  Note that
  1315         -  ** even though the freeblock list was checked by btreeInitPage(),
  1316         -  ** btreeInitPage() did not detect overlapping cells or
  1317         -  ** freeblocks that overlapped cells.   Nor does it detect when the
  1318         -  ** cell content area exceeds the value in the page header.  If these
  1319         -  ** situations arise, then subsequent insert operations might corrupt
  1320         -  ** the freelist.  So we do need to check for corruption while scanning
  1321         -  ** the freelist.
         1366  +  /* The list of freeblocks must be in ascending order.  Find the 
         1367  +  ** spot on the list where iStart should be inserted.
  1322   1368     */
  1323   1369     hdr = pPage->hdrOffset;
  1324         -  addr = hdr + 1;
  1325         -  iLast = pPage->pBt->usableSize - 4;
  1326         -  assert( start<=iLast );
  1327         -  while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
  1328         -    if( pbegin<addr+4 ){
  1329         -      return SQLITE_CORRUPT_BKPT;
  1330         -    }
  1331         -    addr = pbegin;
  1332         -  }
  1333         -  if( pbegin>iLast ){
  1334         -    return SQLITE_CORRUPT_BKPT;
  1335         -  }
  1336         -  assert( pbegin>addr || pbegin==0 );
  1337         -  put2byte(&data[addr], start);
  1338         -  put2byte(&data[start], pbegin);
  1339         -  put2byte(&data[start+2], size);
  1340         -  pPage->nFree = pPage->nFree + (u16)size;
  1341         -
  1342         -  /* Coalesce adjacent free blocks */
  1343         -  addr = hdr + 1;
  1344         -  while( (pbegin = get2byte(&data[addr]))>0 ){
  1345         -    int pnext, psize, x;
  1346         -    assert( pbegin>addr );
  1347         -    assert( pbegin <= (int)pPage->pBt->usableSize-4 );
  1348         -    pnext = get2byte(&data[pbegin]);
  1349         -    psize = get2byte(&data[pbegin+2]);
  1350         -    if( pbegin + psize + 3 >= pnext && pnext>0 ){
  1351         -      int frag = pnext - (pbegin+psize);
  1352         -      if( (frag<0) || (frag>(int)data[hdr+7]) ){
  1353         -        return SQLITE_CORRUPT_BKPT;
  1354         -      }
  1355         -      data[hdr+7] -= (u8)frag;
  1356         -      x = get2byte(&data[pnext]);
  1357         -      put2byte(&data[pbegin], x);
  1358         -      x = pnext + get2byte(&data[pnext+2]) - pbegin;
  1359         -      put2byte(&data[pbegin+2], x);
  1360         -    }else{
  1361         -      addr = pbegin;
  1362         -    }
  1363         -  }
  1364         -
  1365         -  /* If the cell content area begins with a freeblock, remove it. */
  1366         -  if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
  1367         -    int top;
  1368         -    pbegin = get2byte(&data[hdr+1]);
  1369         -    memcpy(&data[hdr+1], &data[pbegin], 2);
  1370         -    top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
  1371         -    put2byte(&data[hdr+5], top);
  1372         -  }
  1373         -  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
         1370  +  iPtr = hdr + 1;
         1371  +  if( data[iPtr+1]==0 && data[iPtr]==0 ){
         1372  +    iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
         1373  +  }else{
         1374  +    while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk<iStart ){
         1375  +      if( iFreeBlk<iPtr+4 ) return SQLITE_CORRUPT_BKPT;
         1376  +      iPtr = iFreeBlk;
         1377  +    }
         1378  +    if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
         1379  +    assert( iFreeBlk>iPtr || iFreeBlk==0 );
         1380  +  
         1381  +    /* At this point:
         1382  +    **    iFreeBlk:   First freeblock after iStart, or zero if none
         1383  +    **    iPtr:       The address of a pointer iFreeBlk
         1384  +    **
         1385  +    ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
         1386  +    */
         1387  +    if( iFreeBlk && iEnd+3>=iFreeBlk ){
         1388  +      nFrag = iFreeBlk - iEnd;
         1389  +      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
         1390  +      iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
         1391  +      iSize = iEnd - iStart;
         1392  +      iFreeBlk = get2byte(&data[iFreeBlk]);
         1393  +    }
         1394  +  
         1395  +    /* If iPtr is another freeblock (that is, if iPtr is not the freelist pointer
         1396  +    ** in the page header) then check to see if iStart should be coalesced 
         1397  +    ** onto the end of iPtr.
         1398  +    */
         1399  +    if( iPtr>hdr+1 ){
         1400  +      int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
         1401  +      if( iPtrEnd+3>=iStart ){
         1402  +        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
         1403  +        nFrag += iStart - iPtrEnd;
         1404  +        iSize = iEnd - iPtr;
         1405  +        iStart = iPtr;
         1406  +      }
         1407  +    }
         1408  +    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT;
         1409  +    data[hdr+7] -= nFrag;
         1410  +  }
         1411  +  if( iStart==get2byte(&data[hdr+5]) ){
         1412  +    /* The new freeblock is at the beginning of the cell content area,
         1413  +    ** so just extend the cell content area rather than create another
         1414  +    ** freelist entry */
         1415  +    if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT;
         1416  +    put2byte(&data[hdr+1], iFreeBlk);
         1417  +    put2byte(&data[hdr+5], iEnd);
         1418  +  }else{
         1419  +    /* Insert the new freeblock into the freelist */
         1420  +    put2byte(&data[iPtr], iStart);
         1421  +    put2byte(&data[iStart], iFreeBlk);
         1422  +    put2byte(&data[iStart+2], iSize);
         1423  +  }
         1424  +  pPage->nFree += iOrigSize;
  1374   1425     return SQLITE_OK;
  1375   1426   }
  1376   1427   
  1377   1428   /*
  1378   1429   ** Decode the flags byte (the first byte of the header) for a page
  1379   1430   ** and initialize fields of the MemPage structure accordingly.
  1380   1431   **
................................................................................
  1628   1679   */
  1629   1680   static Pgno btreePagecount(BtShared *pBt){
  1630   1681     return pBt->nPage;
  1631   1682   }
  1632   1683   u32 sqlite3BtreeLastPage(Btree *p){
  1633   1684     assert( sqlite3BtreeHoldsMutex(p) );
  1634   1685     assert( ((p->pBt->nPage)&0x8000000)==0 );
  1635         -  return (int)btreePagecount(p->pBt);
         1686  +  return btreePagecount(p->pBt);
  1636   1687   }
  1637   1688   
  1638   1689   /*
  1639   1690   ** Get a page from the pager and initialize it.  This routine is just a
  1640   1691   ** convenience wrapper around separate calls to btreeGetPage() and 
  1641   1692   ** btreeInitPage().
  1642   1693   **
................................................................................
  5737   5788     int cellOffset;   /* Address of first cell pointer in data[] */
  5738   5789     u8 *data;         /* The content of the whole page */
  5739   5790     int nSkip = (iChild ? 4 : 0);
  5740   5791   
  5741   5792     if( *pRC ) return;
  5742   5793   
  5743   5794     assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  5744         -  assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
         5795  +  assert( MX_CELL(pPage->pBt)<=10921 );
         5796  +  assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
  5745   5797     assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
  5746   5798     assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
  5747   5799     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  5748   5800     /* The cell should normally be sized correctly.  However, when moving a
  5749   5801     ** malformed cell from a leaf page to an interior page, if the cell size
  5750   5802     ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
  5751   5803     ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence

Changes to src/btree.h.

   165    165   int sqlite3BtreeMovetoUnpacked(
   166    166     BtCursor*,
   167    167     UnpackedRecord *pUnKey,
   168    168     i64 intKey,
   169    169     int bias,
   170    170     int *pRes
   171    171   );
   172         -int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
          172  +int sqlite3BtreeCursorHasMoved(BtCursor*);
          173  +int sqlite3BtreeCursorRestore(BtCursor*, int*);
   173    174   int sqlite3BtreeDelete(BtCursor*);
   174    175   int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
   175    176                                     const void *pData, int nData,
   176    177                                     int nZero, int bias, int seekResult);
   177    178   int sqlite3BtreeFirst(BtCursor*, int *pRes);
   178    179   int sqlite3BtreeLast(BtCursor*, int *pRes);
   179    180   int sqlite3BtreeNext(BtCursor*, int *pRes);

Changes to src/build.c.

   282    282   ** auxiliary databases added using the ATTACH command.
   283    283   **
   284    284   ** See also sqlite3LocateTable().
   285    285   */
   286    286   Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
   287    287     Table *p = 0;
   288    288     int i;
   289         -  int nName;
   290    289     assert( zName!=0 );
   291         -  nName = sqlite3Strlen30(zName);
   292    290     /* All mutexes are required for schema access.  Make sure we hold them. */
   293    291     assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
   294    292     for(i=OMIT_TEMPDB; i<db->nDb; i++){
   295    293       int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
   296    294       if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
   297    295       assert( sqlite3SchemaMutexHeld(db, j, 0) );
   298         -    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
          296  +    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
   299    297       if( p ) break;
   300    298     }
   301    299     return p;
   302    300   }
   303    301   
   304    302   /*
   305    303   ** Locate the in-memory structure that describes a particular database
................................................................................
   374    372   ** for duplicate index names is done.)  The search order is
   375    373   ** TEMP first, then MAIN, then any auxiliary databases added
   376    374   ** using the ATTACH command.
   377    375   */
   378    376   Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
   379    377     Index *p = 0;
   380    378     int i;
   381         -  int nName = sqlite3Strlen30(zName);
   382    379     /* All mutexes are required for schema access.  Make sure we hold them. */
   383    380     assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
   384    381     for(i=OMIT_TEMPDB; i<db->nDb; i++){
   385    382       int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
   386    383       Schema *pSchema = db->aDb[j].pSchema;
   387    384       assert( pSchema );
   388    385       if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
   389    386       assert( sqlite3SchemaMutexHeld(db, j, 0) );
   390         -    p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
          387  +    p = sqlite3HashFind(&pSchema->idxHash, zName);
   391    388       if( p ) break;
   392    389     }
   393    390     return p;
   394    391   }
   395    392   
   396    393   /*
   397    394   ** Reclaim the memory used by an index
................................................................................
   411    408   ** For the index called zIdxName which is found in the database iDb,
   412    409   ** unlike that index from its Table then remove the index from
   413    410   ** the index hash table and free all memory structures associated
   414    411   ** with the index.
   415    412   */
   416    413   void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
   417    414     Index *pIndex;
   418         -  int len;
   419    415     Hash *pHash;
   420    416   
   421    417     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   422    418     pHash = &db->aDb[iDb].pSchema->idxHash;
   423         -  len = sqlite3Strlen30(zIdxName);
   424         -  pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
          419  +  pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
   425    420     if( ALWAYS(pIndex) ){
   426    421       if( pIndex->pTable->pIndex==pIndex ){
   427    422         pIndex->pTable->pIndex = pIndex->pNext;
   428    423       }else{
   429    424         Index *p;
   430    425         /* Justification of ALWAYS();  The index must be on the list of
   431    426         ** indices. */
................................................................................
   577    572     /* Delete all indices associated with this table. */
   578    573     for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
   579    574       pNext = pIndex->pNext;
   580    575       assert( pIndex->pSchema==pTable->pSchema );
   581    576       if( !db || db->pnBytesFreed==0 ){
   582    577         char *zName = pIndex->zName; 
   583    578         TESTONLY ( Index *pOld = ) sqlite3HashInsert(
   584         -         &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
          579  +         &pIndex->pSchema->idxHash, zName, 0
   585    580         );
   586    581         assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
   587    582         assert( pOld==pIndex || pOld==0 );
   588    583       }
   589    584       freeIndex(db, pIndex);
   590    585     }
   591    586   
................................................................................
   620    615   
   621    616     assert( db!=0 );
   622    617     assert( iDb>=0 && iDb<db->nDb );
   623    618     assert( zTabName );
   624    619     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   625    620     testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
   626    621     pDb = &db->aDb[iDb];
   627         -  p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
   628         -                        sqlite3Strlen30(zTabName),0);
          622  +  p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
   629    623     sqlite3DeleteTable(db, p);
   630    624     db->flags |= SQLITE_InternChanges;
   631    625   }
   632    626   
   633    627   /*
   634    628   ** Given a token, return a string that consists of the text of that
   635    629   ** token.  Space to hold the returned string
................................................................................
  1943   1937   
  1944   1938     /* Add the table to the in-memory representation of the database.
  1945   1939     */
  1946   1940     if( db->init.busy ){
  1947   1941       Table *pOld;
  1948   1942       Schema *pSchema = p->pSchema;
  1949   1943       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1950         -    pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
  1951         -                             sqlite3Strlen30(p->zName),p);
         1944  +    pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
  1952   1945       if( pOld ){
  1953   1946         assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
  1954   1947         db->mallocFailed = 1;
  1955   1948         return;
  1956   1949       }
  1957   1950       pParse->pNewTable = 0;
  1958   1951       db->flags |= SQLITE_InternChanges;
................................................................................
  2126   2119         assert( pTable->aCol==0 );
  2127   2120         pTable->nCol = pSelTab->nCol;
  2128   2121         pTable->aCol = pSelTab->aCol;
  2129   2122         pSelTab->nCol = 0;
  2130   2123         pSelTab->aCol = 0;
  2131   2124         sqlite3DeleteTable(db, pSelTab);
  2132   2125         assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
  2133         -      pTable->pSchema->flags |= DB_UnresetViews;
         2126  +      pTable->pSchema->schemaFlags |= DB_UnresetViews;
  2134   2127       }else{
  2135   2128         pTable->nCol = 0;
  2136   2129         nErr++;
  2137   2130       }
  2138   2131       sqlite3SelectDelete(db, pSel);
  2139   2132     } else {
  2140   2133       nErr++;
................................................................................
  2594   2587     }
  2595   2588     pFKey->isDeferred = 0;
  2596   2589     pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
  2597   2590     pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
  2598   2591   
  2599   2592     assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
  2600   2593     pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
  2601         -      pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
         2594  +      pFKey->zTo, (void *)pFKey
  2602   2595     );
  2603   2596     if( pNextTo==pFKey ){
  2604   2597       db->mallocFailed = 1;
  2605   2598       goto fk_end;
  2606   2599     }
  2607   2600     if( pNextTo ){
  2608   2601       assert( pNextTo->pPrevTo==0 );
................................................................................
  2703   2696     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
  2704   2697     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2705   2698                       (char *)pKey, P4_KEYINFO);
  2706   2699     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
  2707   2700   
  2708   2701     addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
  2709   2702     assert( pKey!=0 || db->mallocFailed || pParse->nErr );
  2710         -  if( pIndex->onError!=OE_None && pKey!=0 ){
         2703  +  if( IsUniqueIndex(pIndex) && pKey!=0 ){
  2711   2704       int j2 = sqlite3VdbeCurrentAddr(v) + 3;
  2712   2705       sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
  2713   2706       addr2 = sqlite3VdbeCurrentAddr(v);
  2714   2707       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
  2715         -                         pKey->nField - pIndex->nKeyCol); VdbeCoverage(v);
         2708  +                         pIndex->nKeyCol); VdbeCoverage(v);
  2716   2709       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
  2717   2710     }else{
  2718   2711       addr2 = sqlite3VdbeCurrentAddr(v);
  2719   2712     }
  2720   2713     sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
  2721   2714     sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
  2722   2715     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
................................................................................
  3100   3093       ** If there are different collating sequences or if the columns of
  3101   3094       ** the constraint occur in different orders, then the constraints are
  3102   3095       ** considered distinct and both result in separate indices.
  3103   3096       */
  3104   3097       Index *pIdx;
  3105   3098       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  3106   3099         int k;
  3107         -      assert( pIdx->onError!=OE_None );
         3100  +      assert( IsUniqueIndex(pIdx) );
  3108   3101         assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
  3109         -      assert( pIndex->onError!=OE_None );
         3102  +      assert( IsUniqueIndex(pIndex) );
  3110   3103   
  3111   3104         if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
  3112   3105         for(k=0; k<pIdx->nKeyCol; k++){
  3113   3106           const char *z1;
  3114   3107           const char *z2;
  3115   3108           if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
  3116   3109           z1 = pIdx->azColl[k];
................................................................................
  3142   3135     /* Link the new Index structure to its table and to the other
  3143   3136     ** in-memory database structures. 
  3144   3137     */
  3145   3138     if( db->init.busy ){
  3146   3139       Index *p;
  3147   3140       assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
  3148   3141       p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
  3149         -                          pIndex->zName, sqlite3Strlen30(pIndex->zName),
  3150         -                          pIndex);
         3142  +                          pIndex->zName, pIndex);
  3151   3143       if( p ){
  3152   3144         assert( p==pIndex );  /* Malloc must have failed */
  3153   3145         db->mallocFailed = 1;
  3154   3146         goto exit_create_index;
  3155   3147       }
  3156   3148       db->flags |= SQLITE_InternChanges;
  3157   3149       if( pTblName!=0 ){
................................................................................
  3293   3285     ** 6 and each subsequent value (if any) is 5.  */
  3294   3286     memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
  3295   3287     for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
  3296   3288       a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
  3297   3289     }
  3298   3290   
  3299   3291     assert( 0==sqlite3LogEst(1) );
  3300         -  if( pIdx->onError!=OE_None ) a[pIdx->nKeyCol] = 0;
         3292  +  if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
  3301   3293   }
  3302   3294   
  3303   3295   /*
  3304   3296   ** This routine will drop an existing named index.  This routine
  3305   3297   ** implements the DROP INDEX statement.
  3306   3298   */
  3307   3299   void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){

Changes to src/callback.c.

   150    150   */
   151    151   static CollSeq *findCollSeqEntry(
   152    152     sqlite3 *db,          /* Database connection */
   153    153     const char *zName,    /* Name of the collating sequence */
   154    154     int create            /* Create a new entry if true */
   155    155   ){
   156    156     CollSeq *pColl;
   157         -  int nName = sqlite3Strlen30(zName);
   158         -  pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
          157  +  pColl = sqlite3HashFind(&db->aCollSeq, zName);
   159    158   
   160    159     if( 0==pColl && create ){
   161         -    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
          160  +    int nName = sqlite3Strlen30(zName);
          161  +    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1);
   162    162       if( pColl ){
   163    163         CollSeq *pDel = 0;
   164    164         pColl[0].zName = (char*)&pColl[3];
   165    165         pColl[0].enc = SQLITE_UTF8;
   166    166         pColl[1].zName = (char*)&pColl[3];
   167    167         pColl[1].enc = SQLITE_UTF16LE;
   168    168         pColl[2].zName = (char*)&pColl[3];
   169    169         pColl[2].enc = SQLITE_UTF16BE;
   170    170         memcpy(pColl[0].zName, zName, nName);
   171    171         pColl[0].zName[nName] = 0;
   172         -      pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
          172  +      pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
   173    173   
   174    174         /* If a malloc() failure occurred in sqlite3HashInsert(), it will 
   175    175         ** return the pColl pointer to be deleted (because it wasn't added
   176    176         ** to the hash table).
   177    177         */
   178    178         assert( pDel==0 || pDel==pColl );
   179    179         if( pDel!=0 ){
................................................................................
   443    443     for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
   444    444       Table *pTab = sqliteHashData(pElem);
   445    445       sqlite3DeleteTable(0, pTab);
   446    446     }
   447    447     sqlite3HashClear(&temp1);
   448    448     sqlite3HashClear(&pSchema->fkeyHash);
   449    449     pSchema->pSeqTab = 0;
   450         -  if( pSchema->flags & DB_SchemaLoaded ){
          450  +  if( pSchema->schemaFlags & DB_SchemaLoaded ){
   451    451       pSchema->iGeneration++;
   452         -    pSchema->flags &= ~DB_SchemaLoaded;
          452  +    pSchema->schemaFlags &= ~DB_SchemaLoaded;
   453    453     }
   454    454   }
   455    455   
   456    456   /*
   457    457   ** Find and return the schema associated with a BTree.  Create
   458    458   ** a new one if necessary.
   459    459   */

Changes to src/delete.c.

   462    462     
   463    463       /* Unless this is a view, open cursors for the table we are 
   464    464       ** deleting from and all its indices. If this is a view, then the
   465    465       ** only effect this statement has is to fire the INSTEAD OF 
   466    466       ** triggers.
   467    467       */
   468    468       if( !isView ){
          469  +      testcase( IsVirtual(pTab) );
   469    470         sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
   470    471                                    &iDataCur, &iIdxCur);
   471         -      assert( pPk || iDataCur==iTabCur );
   472         -      assert( pPk || iIdxCur==iDataCur+1 );
          472  +      assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
          473  +      assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
   473    474       }
   474    475     
   475    476       /* Set up a loop over the rowids/primary-keys that were found in the
   476    477       ** where-clause loop above.
   477    478       */
   478    479       if( okOnePass ){
   479    480         /* Just one row.  Hence the top-of-loop is a no-op */
   480         -      assert( nKey==nPk ); /* OP_Found will use an unpacked key */
          481  +      assert( nKey==nPk );  /* OP_Found will use an unpacked key */
          482  +      assert( !IsVirtual(pTab) );
   481    483         if( aToOpen[iDataCur-iTabCur] ){
   482    484           assert( pPk!=0 );
   483    485           sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
   484    486           VdbeCoverage(v);
   485    487         }
   486    488       }else if( pPk ){
   487    489         addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);

Changes to src/expr.c.

  1364   1364     if( op==TK_REGISTER ) op = p->op2;
  1365   1365     switch( op ){
  1366   1366       case TK_INTEGER:
  1367   1367       case TK_STRING:
  1368   1368       case TK_FLOAT:
  1369   1369       case TK_BLOB:
  1370   1370         return 0;
         1371  +    case TK_COLUMN:
         1372  +      assert( p->pTab!=0 );
         1373  +      return p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0;
  1371   1374       default:
  1372   1375         return 1;
  1373   1376     }
  1374   1377   }
  1375   1378   
  1376   1379   /*
  1377   1380   ** Return TRUE if the given expression is a constant which would be
................................................................................
  1471   1474   ** address of the new instruction.
  1472   1475   */
  1473   1476   int sqlite3CodeOnce(Parse *pParse){
  1474   1477     Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
  1475   1478     return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
  1476   1479   }
  1477   1480   
         1481  +/*
         1482  +** Generate code that checks the left-most column of index table iCur to see if
         1483  +** it contains any NULL entries.  Cause the register at regHasNull to be set
         1484  +** to a non-NULL value if iCur contains no NULLs.  Cause register regHasNull
         1485  +** to be set to NULL if iCur contains one or more NULL values.
         1486  +*/
         1487  +static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
         1488  +  int j1;
         1489  +  sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
         1490  +  j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
         1491  +  sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
         1492  +  sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
         1493  +  VdbeComment((v, "first_entry_in(%d)", iCur));
         1494  +  sqlite3VdbeJumpHere(v, j1);
         1495  +}
         1496  +
         1497  +
         1498  +#ifndef SQLITE_OMIT_SUBQUERY
         1499  +/*
         1500  +** The argument is an IN operator with a list (not a subquery) on the 
         1501  +** right-hand side.  Return TRUE if that list is constant.
         1502  +*/
         1503  +static int sqlite3InRhsIsConstant(Expr *pIn){
         1504  +  Expr *pLHS;
         1505  +  int res;
         1506  +  assert( !ExprHasProperty(pIn, EP_xIsSelect) );
         1507  +  pLHS = pIn->pLeft;
         1508  +  pIn->pLeft = 0;
         1509  +  res = sqlite3ExprIsConstant(pIn);
         1510  +  pIn->pLeft = pLHS;
         1511  +  return res;
         1512  +}
         1513  +#endif
         1514  +
  1478   1515   /*
  1479   1516   ** This function is used by the implementation of the IN (...) operator.
  1480   1517   ** The pX parameter is the expression on the RHS of the IN operator, which
  1481   1518   ** might be either a list of expressions or a subquery.
  1482   1519   **
  1483   1520   ** The job of this routine is to find or create a b-tree object that can
  1484   1521   ** be used either to test for membership in the RHS set or to iterate through
  1485   1522   ** all members of the RHS set, skipping duplicates.
  1486   1523   **
  1487         -** A cursor is opened on the b-tree object that the RHS of the IN operator
         1524  +** A cursor is opened on the b-tree object that is the RHS of the IN operator
  1488   1525   ** and pX->iTable is set to the index of that cursor.
  1489   1526   **
  1490   1527   ** The returned value of this function indicates the b-tree type, as follows:
  1491   1528   **
  1492   1529   **   IN_INDEX_ROWID      - The cursor was opened on a database table.
  1493   1530   **   IN_INDEX_INDEX_ASC  - The cursor was opened on an ascending index.
  1494   1531   **   IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
  1495   1532   **   IN_INDEX_EPH        - The cursor was opened on a specially created and
  1496   1533   **                         populated epheremal table.
         1534  +**   IN_INDEX_NOOP       - No cursor was allocated.  The IN operator must be
         1535  +**                         implemented as a sequence of comparisons.
  1497   1536   **
  1498   1537   ** An existing b-tree might be used if the RHS expression pX is a simple
  1499   1538   ** subquery such as:
  1500   1539   **
  1501   1540   **     SELECT <column> FROM <table>
  1502   1541   **
  1503   1542   ** If the RHS of the IN operator is a list or a more complex subquery, then
  1504   1543   ** an ephemeral table might need to be generated from the RHS and then
  1505   1544   ** pX->iTable made to point to the ephermeral table instead of an
  1506         -** existing table.  
         1545  +** existing table.
  1507   1546   **
  1508         -** If the prNotFound parameter is 0, then the b-tree will be used to iterate
  1509         -** through the set members, skipping any duplicates. In this case an
  1510         -** epheremal table must be used unless the selected <column> is guaranteed
         1547  +** The inFlags parameter must contain exactly one of the bits
         1548  +** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP.  If inFlags contains
         1549  +** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
         1550  +** fast membership test.  When the IN_INDEX_LOOP bit is set, the
         1551  +** IN index will be used to loop over all values of the RHS of the
         1552  +** IN operator.
         1553  +**
         1554  +** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
         1555  +** through the set members) then the b-tree must not contain duplicates.
         1556  +** An epheremal table must be used unless the selected <column> is guaranteed
  1511   1557   ** to be unique - either because it is an INTEGER PRIMARY KEY or it
  1512   1558   ** has a UNIQUE constraint or UNIQUE index.
  1513   1559   **
  1514         -** If the prNotFound parameter is not 0, then the b-tree will be used 
  1515         -** for fast set membership tests. In this case an epheremal table must 
         1560  +** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 
         1561  +** for fast set membership tests) then an epheremal table must 
  1516   1562   ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
  1517   1563   ** be found with <column> as its left-most column.
         1564  +**
         1565  +** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
         1566  +** if the RHS of the IN operator is a list (not a subquery) then this
         1567  +** routine might decide that creating an ephemeral b-tree for membership
         1568  +** testing is too expensive and return IN_INDEX_NOOP.  In that case, the
         1569  +** calling routine should implement the IN operator using a sequence
         1570  +** of Eq or Ne comparison operations.
  1518   1571   **
  1519   1572   ** When the b-tree is being used for membership tests, the calling function
  1520         -** needs to know whether or not the structure contains an SQL NULL 
  1521         -** value in order to correctly evaluate expressions like "X IN (Y, Z)".
  1522         -** If there is any chance that the (...) might contain a NULL value at
         1573  +** might need to know whether or not the RHS side of the IN operator
         1574  +** contains a NULL.  If prRhsHasNull is not a NULL pointer and 
         1575  +** if there is any chance that the (...) might contain a NULL value at
  1523   1576   ** runtime, then a register is allocated and the register number written
  1524         -** to *prNotFound. If there is no chance that the (...) contains a
  1525         -** NULL value, then *prNotFound is left unchanged.
         1577  +** to *prRhsHasNull. If there is no chance that the (...) contains a
         1578  +** NULL value, then *prRhsHasNull is left unchanged.
  1526   1579   **
  1527         -** If a register is allocated and its location stored in *prNotFound, then
  1528         -** its initial value is NULL.  If the (...) does not remain constant
  1529         -** for the duration of the query (i.e. the SELECT within the (...)
  1530         -** is a correlated subquery) then the value of the allocated register is
  1531         -** reset to NULL each time the subquery is rerun. This allows the
  1532         -** caller to use vdbe code equivalent to the following:
  1533         -**
  1534         -**   if( register==NULL ){
  1535         -**     has_null = <test if data structure contains null>
  1536         -**     register = 1
  1537         -**   }
  1538         -**
  1539         -** in order to avoid running the <test if data structure contains null>
  1540         -** test more often than is necessary.
         1580  +** If a register is allocated and its location stored in *prRhsHasNull, then
         1581  +** the value in that register will be NULL if the b-tree contains one or more
         1582  +** NULL values, and it will be some non-NULL value if the b-tree contains no
         1583  +** NULL values.
  1541   1584   */
  1542   1585   #ifndef SQLITE_OMIT_SUBQUERY
  1543         -int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
         1586  +int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
  1544   1587     Select *p;                            /* SELECT to the right of IN operator */
  1545   1588     int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
  1546   1589     int iTab = pParse->nTab++;            /* Cursor of the RHS table */
  1547         -  int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
         1590  +  int mustBeUnique;                     /* True if RHS must be unique */
  1548   1591     Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
  1549   1592   
  1550   1593     assert( pX->op==TK_IN );
         1594  +  mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
  1551   1595   
  1552   1596     /* Check to see if an existing table or index can be used to
  1553   1597     ** satisfy the query.  This is preferable to generating a new 
  1554   1598     ** ephemeral table.
  1555   1599     */
  1556   1600     p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
  1557   1601     if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
................................................................................
  1600   1644         ** it is not, it is not possible to use any index.
  1601   1645         */
  1602   1646         int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
  1603   1647   
  1604   1648         for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
  1605   1649           if( (pIdx->aiColumn[0]==iCol)
  1606   1650            && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
  1607         -         && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
         1651  +         && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
  1608   1652           ){
  1609   1653             int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
  1610   1654             sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
  1611   1655             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1612   1656             VdbeComment((v, "%s", pIdx->zName));
  1613   1657             assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
  1614   1658             eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
  1615   1659   
  1616         -          if( prNotFound && !pTab->aCol[iCol].notNull ){
  1617         -            *prNotFound = ++pParse->nMem;
  1618         -            sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
         1660  +          if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
         1661  +            *prRhsHasNull = ++pParse->nMem;
         1662  +            sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
  1619   1663             }
  1620   1664             sqlite3VdbeJumpHere(v, iAddr);
  1621   1665           }
  1622   1666         }
  1623   1667       }
  1624   1668     }
         1669  +
         1670  +  /* If no preexisting index is available for the IN clause
         1671  +  ** and IN_INDEX_NOOP is an allowed reply
         1672  +  ** and the RHS of the IN operator is a list, not a subquery
         1673  +  ** and the RHS is not contant or has two or fewer terms,
         1674  +  ** then it is not worth creating an ephermeral table to evaluate
         1675  +  ** the IN operator so return IN_INDEX_NOOP.
         1676  +  */
         1677  +  if( eType==0
         1678  +   && (inFlags & IN_INDEX_NOOP_OK)
         1679  +   && !ExprHasProperty(pX, EP_xIsSelect)
         1680  +   && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
         1681  +  ){
         1682  +    eType = IN_INDEX_NOOP;
         1683  +  }
         1684  +     
  1625   1685   
  1626   1686     if( eType==0 ){
  1627         -    /* Could not found an existing table or index to use as the RHS b-tree.
         1687  +    /* Could not find an existing table or index to use as the RHS b-tree.
  1628   1688       ** We will have to generate an ephemeral table to do the job.
  1629   1689       */
  1630   1690       u32 savedNQueryLoop = pParse->nQueryLoop;
  1631   1691       int rMayHaveNull = 0;
  1632   1692       eType = IN_INDEX_EPH;
  1633         -    if( prNotFound ){
  1634         -      *prNotFound = rMayHaveNull = ++pParse->nMem;
  1635         -      sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
  1636         -    }else{
         1693  +    if( inFlags & IN_INDEX_LOOP ){
  1637   1694         pParse->nQueryLoop = 0;
  1638   1695         if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
  1639   1696           eType = IN_INDEX_ROWID;
  1640   1697         }
         1698  +    }else if( prRhsHasNull ){
         1699  +      *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
  1641   1700       }
  1642   1701       sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
  1643   1702       pParse->nQueryLoop = savedNQueryLoop;
  1644   1703     }else{
  1645   1704       pX->iTable = iTab;
  1646   1705     }
  1647   1706     return eType;
................................................................................
  1664   1723   ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
  1665   1724   ** to some integer key column of a table B-Tree. In this case, use an
  1666   1725   ** intkey B-Tree to store the set of IN(...) values instead of the usual
  1667   1726   ** (slower) variable length keys B-Tree.
  1668   1727   **
  1669   1728   ** If rMayHaveNull is non-zero, that means that the operation is an IN
  1670   1729   ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
  1671         -** Furthermore, the IN is in a WHERE clause and that we really want
  1672         -** to iterate over the RHS of the IN operator in order to quickly locate
  1673         -** all corresponding LHS elements.  All this routine does is initialize
  1674         -** the register given by rMayHaveNull to NULL.  Calling routines will take
  1675         -** care of changing this register value to non-NULL if the RHS is NULL-free.
  1676         -**
  1677         -** If rMayHaveNull is zero, that means that the subquery is being used
  1678         -** for membership testing only.  There is no need to initialize any
  1679         -** registers to indicate the presence or absence of NULLs on the RHS.
         1730  +** All this routine does is initialize the register given by rMayHaveNull
         1731  +** to NULL.  Calling routines will take care of changing this register
         1732  +** value to non-NULL if the RHS is NULL-free.
  1680   1733   **
  1681   1734   ** For a SELECT or EXISTS operator, return the register that holds the
  1682   1735   ** result.  For IN operators or if an error occurs, the return value is 0.
  1683   1736   */
  1684   1737   #ifndef SQLITE_OMIT_SUBQUERY
  1685   1738   int sqlite3CodeSubselect(
  1686   1739     Parse *pParse,          /* Parsing context */
  1687   1740     Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
  1688         -  int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
         1741  +  int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
  1689   1742     int isRowid             /* If true, LHS of IN operator is a rowid */
  1690   1743   ){
  1691         -  int testAddr = -1;                      /* One-time test address */
         1744  +  int jmpIfDynamic = -1;                      /* One-time test address */
  1692   1745     int rReg = 0;                           /* Register storing resulting */
  1693   1746     Vdbe *v = sqlite3GetVdbe(pParse);
  1694   1747     if( NEVER(v==0) ) return 0;
  1695   1748     sqlite3ExprCachePush(pParse);
  1696   1749   
  1697   1750     /* This code must be run in its entirety every time it is encountered
  1698   1751     ** if any of the following is true:
................................................................................
  1701   1754     **    *  The right-hand side is an expression list containing variables
  1702   1755     **    *  We are inside a trigger
  1703   1756     **
  1704   1757     ** If all of the above are false, then we can run this code just once
  1705   1758     ** save the results, and reuse the same result on subsequent invocations.
  1706   1759     */
  1707   1760     if( !ExprHasProperty(pExpr, EP_VarSelect) ){
  1708         -    testAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
         1761  +    jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
  1709   1762     }
  1710   1763   
  1711   1764   #ifndef SQLITE_OMIT_EXPLAIN
  1712   1765     if( pParse->explain==2 ){
  1713   1766       char *zMsg = sqlite3MPrintf(
  1714         -        pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
         1767  +        pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ",
  1715   1768           pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
  1716   1769       );
  1717   1770       sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
  1718   1771     }
  1719   1772   #endif
  1720   1773   
  1721   1774     switch( pExpr->op ){
  1722   1775       case TK_IN: {
  1723   1776         char affinity;              /* Affinity of the LHS of the IN */
  1724   1777         int addr;                   /* Address of OP_OpenEphemeral instruction */
  1725   1778         Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
  1726   1779         KeyInfo *pKeyInfo = 0;      /* Key information */
  1727   1780   
  1728         -      if( rMayHaveNull ){
  1729         -        sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
  1730         -      }
  1731         -
  1732   1781         affinity = sqlite3ExprAffinity(pLeft);
  1733   1782   
  1734   1783         /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
  1735   1784         ** expression it is handled the same way.  An ephemeral table is 
  1736   1785         ** filled with single-field index keys representing the results
  1737   1786         ** from the SELECT or the <exprlist>.
  1738   1787         **
................................................................................
  1750   1799   
  1751   1800         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1752   1801           /* Case 1:     expr IN (SELECT ...)
  1753   1802           **
  1754   1803           ** Generate code to write the results of the select into the temporary
  1755   1804           ** table allocated and opened above.
  1756   1805           */
         1806  +        Select *pSelect = pExpr->x.pSelect;
  1757   1807           SelectDest dest;
  1758   1808           ExprList *pEList;
  1759   1809   
  1760   1810           assert( !isRowid );
  1761   1811           sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
  1762   1812           dest.affSdst = (u8)affinity;
  1763   1813           assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
  1764         -        pExpr->x.pSelect->iLimit = 0;
         1814  +        pSelect->iLimit = 0;
         1815  +        testcase( pSelect->selFlags & SF_Distinct );
         1816  +        pSelect->selFlags &= ~SF_Distinct;
  1765   1817           testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
  1766         -        if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
         1818  +        if( sqlite3Select(pParse, pSelect, &dest) ){
  1767   1819             sqlite3KeyInfoUnref(pKeyInfo);
  1768   1820             return 0;
  1769   1821           }
  1770         -        pEList = pExpr->x.pSelect->pEList;
         1822  +        pEList = pSelect->pEList;
  1771   1823           assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
  1772   1824           assert( pEList!=0 );
  1773   1825           assert( pEList->nExpr>0 );
  1774   1826           assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  1775   1827           pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
  1776   1828                                                            pEList->a[0].pExpr);
  1777   1829         }else if( ALWAYS(pExpr->x.pList!=0) ){
................................................................................
  1794   1846             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  1795   1847             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  1796   1848           }
  1797   1849   
  1798   1850           /* Loop through each expression in <exprlist>. */
  1799   1851           r1 = sqlite3GetTempReg(pParse);
  1800   1852           r2 = sqlite3GetTempReg(pParse);
  1801         -        sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
         1853  +        if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
  1802   1854           for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
  1803   1855             Expr *pE2 = pItem->pExpr;
  1804   1856             int iValToIns;
  1805   1857   
  1806   1858             /* If the expression is not constant then we will need to
  1807   1859             ** disable the test that was generated above that makes sure
  1808   1860             ** this code only executes once.  Because for a non-constant
  1809   1861             ** expression we need to rerun this code each time.
  1810   1862             */
  1811         -          if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
  1812         -            sqlite3VdbeChangeToNoop(v, testAddr);
  1813         -            testAddr = -1;
         1863  +          if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
         1864  +            sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
         1865  +            jmpIfDynamic = -1;
  1814   1866             }
  1815   1867   
  1816   1868             /* Evaluate the expression and insert it into the temp table */
  1817   1869             if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
  1818   1870               sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
  1819   1871             }else{
  1820   1872               r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
................................................................................
  1856   1908         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
  1857   1909   
  1858   1910         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
  1859   1911         pSel = pExpr->x.pSelect;
  1860   1912         sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
  1861   1913         if( pExpr->op==TK_SELECT ){
  1862   1914           dest.eDest = SRT_Mem;
         1915  +        dest.iSdst = dest.iSDParm;
  1863   1916           sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
  1864   1917           VdbeComment((v, "Init subquery result"));
  1865   1918         }else{
  1866   1919           dest.eDest = SRT_Exists;
  1867   1920           sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
  1868   1921           VdbeComment((v, "Init EXISTS result"));
  1869   1922         }
................................................................................
  1876   1929         }
  1877   1930         rReg = dest.iSDParm;
  1878   1931         ExprSetVVAProperty(pExpr, EP_NoReduce);
  1879   1932         break;
  1880   1933       }
  1881   1934     }
  1882   1935   
  1883         -  if( testAddr>=0 ){
  1884         -    sqlite3VdbeJumpHere(v, testAddr);
         1936  +  if( rHasNullFlag ){
         1937  +    sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
         1938  +  }
         1939  +
         1940  +  if( jmpIfDynamic>=0 ){
         1941  +    sqlite3VdbeJumpHere(v, jmpIfDynamic);
  1885   1942     }
  1886   1943     sqlite3ExprCachePop(pParse);
  1887   1944   
  1888   1945     return rReg;
  1889   1946   }
  1890   1947   #endif /* SQLITE_OMIT_SUBQUERY */
  1891   1948   
................................................................................
  1898   1955   **
  1899   1956   ** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
  1900   1957   ** is an array of zero or more values.  The expression is true if the LHS is
  1901   1958   ** contained within the RHS.  The value of the expression is unknown (NULL)
  1902   1959   ** if the LHS is NULL or if the LHS is not contained within the RHS and the
  1903   1960   ** RHS contains one or more NULL values.
  1904   1961   **
  1905         -** This routine generates code will jump to destIfFalse if the LHS is not 
         1962  +** This routine generates code that jumps to destIfFalse if the LHS is not 
  1906   1963   ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
  1907   1964   ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
  1908   1965   ** within the RHS then fall through.
  1909   1966   */
  1910   1967   static void sqlite3ExprCodeIN(
  1911   1968     Parse *pParse,        /* Parsing and code generating context */
  1912   1969     Expr *pExpr,          /* The IN expression */
................................................................................
  1921   1978   
  1922   1979     /* Compute the RHS.   After this step, the table with cursor
  1923   1980     ** pExpr->iTable will contains the values that make up the RHS.
  1924   1981     */
  1925   1982     v = pParse->pVdbe;
  1926   1983     assert( v!=0 );       /* OOM detected prior to this routine */
  1927   1984     VdbeNoopComment((v, "begin IN expr"));
  1928         -  eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
         1985  +  eType = sqlite3FindInIndex(pParse, pExpr,
         1986  +                             IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
         1987  +                             destIfFalse==destIfNull ? 0 : &rRhsHasNull);
  1929   1988   
  1930   1989     /* Figure out the affinity to use to create a key from the results
  1931   1990     ** of the expression. affinityStr stores a static string suitable for
  1932   1991     ** P4 of OP_MakeRecord.
  1933   1992     */
  1934   1993     affinity = comparisonAffinity(pExpr);
  1935   1994   
  1936   1995     /* Code the LHS, the <expr> from "<expr> IN (...)".
  1937   1996     */
  1938   1997     sqlite3ExprCachePush(pParse);
  1939   1998     r1 = sqlite3GetTempReg(pParse);
  1940   1999     sqlite3ExprCode(pParse, pExpr->pLeft, r1);
  1941   2000   
  1942         -  /* If the LHS is NULL, then the result is either false or NULL depending
  1943         -  ** on whether the RHS is empty or not, respectively.
         2001  +  /* If sqlite3FindInIndex() did not find or create an index that is
         2002  +  ** suitable for evaluating the IN operator, then evaluate using a
         2003  +  ** sequence of comparisons.
  1944   2004     */
  1945         -  if( destIfNull==destIfFalse ){
  1946         -    /* Shortcut for the common case where the false and NULL outcomes are
  1947         -    ** the same. */
  1948         -    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
  1949         -  }else{
  1950         -    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
  1951         -    sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
  1952         -    VdbeCoverage(v);
  1953         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
  1954         -    sqlite3VdbeJumpHere(v, addr1);
  1955         -  }
  1956         -
  1957         -  if( eType==IN_INDEX_ROWID ){
  1958         -    /* In this case, the RHS is the ROWID of table b-tree
  1959         -    */
  1960         -    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
  1961         -    sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
  1962         -    VdbeCoverage(v);
         2005  +  if( eType==IN_INDEX_NOOP ){
         2006  +    ExprList *pList = pExpr->x.pList;
         2007  +    CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
         2008  +    int labelOk = sqlite3VdbeMakeLabel(v);
         2009  +    int r2, regToFree;
         2010  +    int regCkNull = 0;
         2011  +    int ii;
         2012  +    assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
         2013  +    if( destIfNull!=destIfFalse ){
         2014  +      regCkNull = sqlite3GetTempReg(pParse);
         2015  +      sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
         2016  +    }
         2017  +    for(ii=0; ii<pList->nExpr; ii++){
         2018  +      r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
         2019  +      if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
         2020  +        sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
         2021  +      }
         2022  +      if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
         2023  +        sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
         2024  +                          (void*)pColl, P4_COLLSEQ);
         2025  +        VdbeCoverageIf(v, ii<pList->nExpr-1);
         2026  +        VdbeCoverageIf(v, ii==pList->nExpr-1);
         2027  +        sqlite3VdbeChangeP5(v, affinity);
         2028  +      }else{
         2029  +        assert( destIfNull==destIfFalse );
         2030  +        sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
         2031  +                          (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
         2032  +        sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
         2033  +      }
         2034  +      sqlite3ReleaseTempReg(pParse, regToFree);
         2035  +    }
         2036  +    if( regCkNull ){
         2037  +      sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
         2038  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
         2039  +    }
         2040  +    sqlite3VdbeResolveLabel(v, labelOk);
         2041  +    sqlite3ReleaseTempReg(pParse, regCkNull);
  1963   2042     }else{
  1964         -    /* In this case, the RHS is an index b-tree.
  1965         -    */
  1966         -    sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
  1967         -
  1968         -    /* If the set membership test fails, then the result of the 
  1969         -    ** "x IN (...)" expression must be either 0 or NULL. If the set
  1970         -    ** contains no NULL values, then the result is 0. If the set 
  1971         -    ** contains one or more NULL values, then the result of the
  1972         -    ** expression is also NULL.
         2043  +  
         2044  +    /* If the LHS is NULL, then the result is either false or NULL depending
         2045  +    ** on whether the RHS is empty or not, respectively.
  1973   2046       */
  1974         -    if( rRhsHasNull==0 || destIfFalse==destIfNull ){
  1975         -      /* This branch runs if it is known at compile time that the RHS
  1976         -      ** cannot contain NULL values. This happens as the result
  1977         -      ** of a "NOT NULL" constraint in the database schema.
  1978         -      **
  1979         -      ** Also run this branch if NULL is equivalent to FALSE
  1980         -      ** for this particular IN operator.
         2047  +    if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
         2048  +      if( destIfNull==destIfFalse ){
         2049  +        /* Shortcut for the common case where the false and NULL outcomes are
         2050  +        ** the same. */
         2051  +        sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
         2052  +      }else{
         2053  +        int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
         2054  +        sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
         2055  +        VdbeCoverage(v);
         2056  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
         2057  +        sqlite3VdbeJumpHere(v, addr1);
         2058  +      }
         2059  +    }
         2060  +  
         2061  +    if( eType==IN_INDEX_ROWID ){
         2062  +      /* In this case, the RHS is the ROWID of table b-tree
  1981   2063         */
  1982         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
         2064  +      sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
         2065  +      sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
  1983   2066         VdbeCoverage(v);
  1984   2067       }else{
  1985         -      /* In this branch, the RHS of the IN might contain a NULL and
  1986         -      ** the presence of a NULL on the RHS makes a difference in the
  1987         -      ** outcome.
         2068  +      /* In this case, the RHS is an index b-tree.
  1988   2069         */
  1989         -      int j1, j2;
  1990         -
  1991         -      /* First check to see if the LHS is contained in the RHS.  If so,
  1992         -      ** then the presence of NULLs in the RHS does not matter, so jump
  1993         -      ** over all of the code that follows.
         2070  +      sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
         2071  +  
         2072  +      /* If the set membership test fails, then the result of the 
         2073  +      ** "x IN (...)" expression must be either 0 or NULL. If the set
         2074  +      ** contains no NULL values, then the result is 0. If the set 
         2075  +      ** contains one or more NULL values, then the result of the
         2076  +      ** expression is also NULL.
  1994   2077         */
  1995         -      j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
  1996         -      VdbeCoverage(v);
  1997         -
  1998         -      /* Here we begin generating code that runs if the LHS is not
  1999         -      ** contained within the RHS.  Generate additional code that
  2000         -      ** tests the RHS for NULLs.  If the RHS contains a NULL then
  2001         -      ** jump to destIfNull.  If there are no NULLs in the RHS then
  2002         -      ** jump to destIfFalse.
  2003         -      */
  2004         -      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); VdbeCoverage(v);
  2005         -      sqlite3VdbeAddOp2(v, OP_IfNot, rRhsHasNull, destIfFalse); VdbeCoverage(v);
  2006         -      j2 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
  2007         -      VdbeCoverage(v);
  2008         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, rRhsHasNull);
  2009         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
  2010         -      sqlite3VdbeJumpHere(v, j2);
  2011         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, rRhsHasNull);
  2012         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
  2013         -
  2014         -      /* The OP_Found at the top of this branch jumps here when true, 
  2015         -      ** causing the overall IN expression evaluation to fall through.
  2016         -      */
  2017         -      sqlite3VdbeJumpHere(v, j1);
         2078  +      assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
         2079  +      if( rRhsHasNull==0 ){
         2080  +        /* This branch runs if it is known at compile time that the RHS
         2081  +        ** cannot contain NULL values. This happens as the result
         2082  +        ** of a "NOT NULL" constraint in the database schema.
         2083  +        **
         2084  +        ** Also run this branch if NULL is equivalent to FALSE
         2085  +        ** for this particular IN operator.
         2086  +        */
         2087  +        sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
         2088  +        VdbeCoverage(v);
         2089  +      }else{
         2090  +        /* In this branch, the RHS of the IN might contain a NULL and
         2091  +        ** the presence of a NULL on the RHS makes a difference in the
         2092  +        ** outcome.
         2093  +        */
         2094  +        int j1;
         2095  +  
         2096  +        /* First check to see if the LHS is contained in the RHS.  If so,
         2097  +        ** then the answer is TRUE the presence of NULLs in the RHS does
         2098  +        ** not matter.  If the LHS is not contained in the RHS, then the
         2099  +        ** answer is NULL if the RHS contains NULLs and the answer is
         2100  +        ** FALSE if the RHS is NULL-free.
         2101  +        */
         2102  +        j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
         2103  +        VdbeCoverage(v);
         2104  +        sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
         2105  +        VdbeCoverage(v);
         2106  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
         2107  +        sqlite3VdbeJumpHere(v, j1);
         2108  +      }
  2018   2109       }
  2019   2110     }
  2020   2111     sqlite3ReleaseTempReg(pParse, r1);
  2021   2112     sqlite3ExprCachePop(pParse);
  2022   2113     VdbeComment((v, "end IN expr"));
  2023   2114   }
  2024   2115   #endif /* SQLITE_OMIT_SUBQUERY */
................................................................................
  2501   2592       case TK_AS: {
  2502   2593         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  2503   2594         break;
  2504   2595       }
  2505   2596   #ifndef SQLITE_OMIT_CAST
  2506   2597       case TK_CAST: {
  2507   2598         /* Expressions of the form:   CAST(pLeft AS token) */
  2508         -      int aff, to_op;
  2509   2599         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  2510         -      assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2511         -      aff = sqlite3AffinityType(pExpr->u.zToken, 0);
  2512         -      to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
  2513         -      assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
  2514         -      assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
  2515         -      assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
  2516         -      assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
  2517         -      assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
  2518         -      testcase( to_op==OP_ToText );
  2519         -      testcase( to_op==OP_ToBlob );
  2520         -      testcase( to_op==OP_ToNumeric );
  2521         -      testcase( to_op==OP_ToInt );
  2522         -      testcase( to_op==OP_ToReal );
  2523   2600         if( inReg!=target ){
  2524   2601           sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
  2525   2602           inReg = target;
  2526   2603         }
  2527         -      sqlite3VdbeAddOp1(v, to_op, inReg);
         2604  +      sqlite3VdbeAddOp2(v, OP_Cast, target,
         2605  +                        sqlite3AffinityType(pExpr->u.zToken, 0));
  2528   2606         testcase( usedAsColumnCache(pParse, inReg, inReg) );
  2529   2607         sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
  2530   2608         break;
  2531   2609       }
  2532   2610   #endif /* SQLITE_OMIT_CAST */
  2533   2611       case TK_LT:
  2534   2612       case TK_LE:
................................................................................
  2634   2712         assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
  2635   2713         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
  2636   2714         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  2637   2715         testcase( regFree1==0 );
  2638   2716         addr = sqlite3VdbeAddOp1(v, op, r1);
  2639   2717         VdbeCoverageIf(v, op==TK_ISNULL);
  2640   2718         VdbeCoverageIf(v, op==TK_NOTNULL);
  2641         -      sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
         2719  +      sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
  2642   2720         sqlite3VdbeJumpHere(v, addr);
  2643   2721         break;
  2644   2722       }
  2645   2723       case TK_AGG_FUNCTION: {
  2646   2724         AggInfo *pInfo = pExpr->pAggInfo;
  2647   2725         if( pInfo==0 ){
  2648   2726           assert( !ExprHasProperty(pExpr, EP_IntValue) );
................................................................................
  2670   2748           pFarg = pExpr->x.pList;
  2671   2749         }
  2672   2750         nFarg = pFarg ? pFarg->nExpr : 0;
  2673   2751         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2674   2752         zId = pExpr->u.zToken;
  2675   2753         nId = sqlite3Strlen30(zId);
  2676   2754         pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
  2677         -      if( pDef==0 ){
         2755  +      if( pDef==0 || pDef->xFunc==0 ){
  2678   2756           sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
  2679   2757           break;
  2680   2758         }
  2681   2759   
  2682   2760         /* Attempt a direct implementation of the built-in COALESCE() and
  2683   2761         ** IFNULL() functions.  This avoids unnecessary evalation of
  2684   2762         ** arguments past the first non-NULL argument.

Changes to src/fkey.c.

   221    221       assert( nCol>1 );
   222    222       aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
   223    223       if( !aiCol ) return 1;
   224    224       *paiCol = aiCol;
   225    225     }
   226    226   
   227    227     for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
   228         -    if( pIdx->nKeyCol==nCol && pIdx->onError!=OE_None ){ 
          228  +    if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){ 
   229    229         /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
   230    230         ** of columns. If each indexed column corresponds to a foreign key
   231    231         ** column of pFKey, then this index is a winner.  */
   232    232   
   233    233         if( zKey==0 ){
   234    234           /* If zKey is NULL, then this foreign key is implicitly mapped to 
   235    235           ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
................................................................................
   655    655   ** Calling this function with table "t1" as an argument returns a pointer
   656    656   ** to the FKey structure representing the foreign key constraint on table
   657    657   ** "t2". Calling this function with "t2" as the argument would return a
   658    658   ** NULL pointer (as there are no FK constraints for which t2 is the parent
   659    659   ** table).
   660    660   */
   661    661   FKey *sqlite3FkReferences(Table *pTab){
   662         -  int nName = sqlite3Strlen30(pTab->zName);
   663         -  return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
          662  +  return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
   664    663   }
   665    664   
   666    665   /*
   667    666   ** The second argument is a Trigger structure allocated by the 
   668    667   ** fkActionTrigger() routine. This function deletes the Trigger structure
   669    668   ** and all of its sub-components.
   670    669   **
................................................................................
  1334   1333       /* Remove the FK from the fkeyHash hash table. */
  1335   1334       if( !db || db->pnBytesFreed==0 ){
  1336   1335         if( pFKey->pPrevTo ){
  1337   1336           pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
  1338   1337         }else{
  1339   1338           void *p = (void *)pFKey->pNextTo;
  1340   1339           const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
  1341         -        sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
         1340  +        sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
  1342   1341         }
  1343   1342         if( pFKey->pNextTo ){
  1344   1343           pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
  1345   1344         }
  1346   1345       }
  1347   1346   
  1348   1347       /* EV: R-30323-21917 Each foreign key constraint in SQLite is

Changes to src/func.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** This file contains the C functions that implement various SQL
    13         -** functions of SQLite.  
    14         -**
    15         -** There is only one exported symbol in this file - the function
    16         -** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17         -** All other code has file scope.
           12  +** This file contains the C-language implementions for many of the SQL
           13  +** functions of SQLite.  (Some function, and in particular the date and
           14  +** time functions, are implemented separately.)
    18     15   */
    19     16   #include "sqliteInt.h"
    20     17   #include <stdlib.h>
    21     18   #include <assert.h>
    22     19   #include "vdbeInt.h"
    23     20   
    24     21   /*

Changes to src/hash.c.

    48     48     }
    49     49     pH->count = 0;
    50     50   }
    51     51   
    52     52   /*
    53     53   ** The hashing function.
    54     54   */
    55         -static unsigned int strHash(const char *z, int nKey){
           55  +static unsigned int strHash(const char *z){
    56     56     unsigned int h = 0;
    57         -  assert( nKey>=0 );
    58         -  while( nKey > 0  ){
    59         -    h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
    60         -    nKey--;
           57  +  unsigned char c;
           58  +  while( (c = (unsigned char)*z++)!=0 ){
           59  +    h = (h<<3) ^ h ^ sqlite3UpperToLower[c];
    61     60     }
    62     61     return h;
    63     62   }
    64     63   
    65     64   
    66     65   /* Link pNew element into the hash table pH.  If pEntry!=0 then also
    67     66   ** insert pNew into the pEntry hash bucket.
................................................................................
   125    124   
   126    125     if( new_ht==0 ) return 0;
   127    126     sqlite3_free(pH->ht);
   128    127     pH->ht = new_ht;
   129    128     pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
   130    129     memset(new_ht, 0, new_size*sizeof(struct _ht));
   131    130     for(elem=pH->first, pH->first=0; elem; elem = next_elem){
   132         -    unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
          131  +    unsigned int h = strHash(elem->pKey) % new_size;
   133    132       next_elem = elem->next;
   134    133       insertElement(pH, &new_ht[h], elem);
   135    134     }
   136    135     return 1;
   137    136   }
   138    137   
   139    138   /* This function (for internal use only) locates an element in an
   140         -** hash table that matches the given key.  The hash for this key has
   141         -** already been computed and is passed as the 4th parameter.
          139  +** hash table that matches the given key.  The hash for this key is
          140  +** also computed and returned in the *pH parameter.
   142    141   */
   143         -static HashElem *findElementGivenHash(
          142  +static HashElem *findElementWithHash(
   144    143     const Hash *pH,     /* The pH to be searched */
   145    144     const char *pKey,   /* The key we are searching for */
   146         -  int nKey,           /* Bytes in key (not counting zero terminator) */
   147         -  unsigned int h      /* The hash for this key. */
          145  +  unsigned int *pHash /* Write the hash value here */
   148    146   ){
   149    147     HashElem *elem;                /* Used to loop thru the element list */
   150    148     int count;                     /* Number of elements left to test */
          149  +  unsigned int h;                /* The computed hash */
   151    150   
   152    151     if( pH->ht ){
   153         -    struct _ht *pEntry = &pH->ht[h];
          152  +    struct _ht *pEntry;
          153  +    h = strHash(pKey) % pH->htsize;
          154  +    pEntry = &pH->ht[h];
   154    155       elem = pEntry->chain;
   155    156       count = pEntry->count;
   156    157     }else{
          158  +    h = 0;
   157    159       elem = pH->first;
   158    160       count = pH->count;
   159    161     }
   160         -  while( count-- && ALWAYS(elem) ){
   161         -    if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
          162  +  *pHash = h;
          163  +  while( count-- ){
          164  +    assert( elem!=0 );
          165  +    if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ 
   162    166         return elem;
   163    167       }
   164    168       elem = elem->next;
   165    169     }
   166    170     return 0;
   167    171   }
   168    172   
................................................................................
   197    201       assert( pH->first==0 );
   198    202       assert( pH->count==0 );
   199    203       sqlite3HashClear(pH);
   200    204     }
   201    205   }
   202    206   
   203    207   /* Attempt to locate an element of the hash table pH with a key
   204         -** that matches pKey,nKey.  Return the data for this element if it is
          208  +** that matches pKey.  Return the data for this element if it is
   205    209   ** found, or NULL if there is no match.
   206    210   */
   207         -void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
          211  +void *sqlite3HashFind(const Hash *pH, const char *pKey){
   208    212     HashElem *elem;    /* The element that matches key */
   209    213     unsigned int h;    /* A hash on key */
   210    214   
   211    215     assert( pH!=0 );
   212    216     assert( pKey!=0 );
   213         -  assert( nKey>=0 );
   214         -  if( pH->ht ){
   215         -    h = strHash(pKey, nKey) % pH->htsize;
   216         -  }else{
   217         -    h = 0;
   218         -  }
   219         -  elem = findElementGivenHash(pH, pKey, nKey, h);
          217  +  elem = findElementWithHash(pH, pKey, &h);
   220    218     return elem ? elem->data : 0;
   221    219   }
   222    220   
   223         -/* Insert an element into the hash table pH.  The key is pKey,nKey
          221  +/* Insert an element into the hash table pH.  The key is pKey
   224    222   ** and the data is "data".
   225    223   **
   226    224   ** If no element exists with a matching key, then a new
   227    225   ** element is created and NULL is returned.
   228    226   **
   229    227   ** If another element already exists with the same key, then the
   230    228   ** new data replaces the old data and the old data is returned.
   231    229   ** The key is not copied in this instance.  If a malloc fails, then
   232    230   ** the new data is returned and the hash table is unchanged.
   233    231   **
   234    232   ** If the "data" parameter to this function is NULL, then the
   235    233   ** element corresponding to "key" is removed from the hash table.
   236    234   */
   237         -void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
          235  +void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
   238    236     unsigned int h;       /* the hash of the key modulo hash table size */
   239    237     HashElem *elem;       /* Used to loop thru the element list */
   240    238     HashElem *new_elem;   /* New element added to the pH */
   241    239   
   242    240     assert( pH!=0 );
   243    241     assert( pKey!=0 );
   244         -  assert( nKey>=0 );
   245         -  if( pH->htsize ){
   246         -    h = strHash(pKey, nKey) % pH->htsize;
   247         -  }else{
   248         -    h = 0;
   249         -  }
   250         -  elem = findElementGivenHash(pH,pKey,nKey,h);
          242  +  elem = findElementWithHash(pH,pKey,&h);
   251    243     if( elem ){
   252    244       void *old_data = elem->data;
   253    245       if( data==0 ){
   254    246         removeElementGivenHash(pH,elem,h);
   255    247       }else{
   256    248         elem->data = data;
   257    249         elem->pKey = pKey;
   258         -      assert(nKey==elem->nKey);
   259    250       }
   260    251       return old_data;
   261    252     }
   262    253     if( data==0 ) return 0;
   263    254     new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
   264    255     if( new_elem==0 ) return data;
   265    256     new_elem->pKey = pKey;
   266         -  new_elem->nKey = nKey;
   267    257     new_elem->data = data;
   268    258     pH->count++;
   269    259     if( pH->count>=10 && pH->count > 2*pH->htsize ){
   270    260       if( rehash(pH, pH->count*2) ){
   271    261         assert( pH->htsize>0 );
   272         -      h = strHash(pKey, nKey) % pH->htsize;
          262  +      h = strHash(pKey) % pH->htsize;
   273    263       }
   274    264     }
   275         -  if( pH->ht ){
   276         -    insertElement(pH, &pH->ht[h], new_elem);
   277         -  }else{
   278         -    insertElement(pH, 0, new_elem);
   279         -  }
          265  +  insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
   280    266     return 0;
   281    267   }

Changes to src/hash.h.

    55     55   **
    56     56   ** Again, this structure is intended to be opaque, but it can't really
    57     57   ** be opaque because it is used by macros.
    58     58   */
    59     59   struct HashElem {
    60     60     HashElem *next, *prev;       /* Next and previous elements in the table */
    61     61     void *data;                  /* Data associated with this element */
    62         -  const char *pKey; int nKey;  /* Key associated with this element */
           62  +  const char *pKey;            /* Key associated with this element */
    63     63   };
    64     64   
    65     65   /*
    66     66   ** Access routines.  To delete, insert a NULL pointer.
    67     67   */
    68     68   void sqlite3HashInit(Hash*);
    69         -void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
    70         -void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
           69  +void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
           70  +void *sqlite3HashFind(const Hash*, const char *pKey);
    71     71   void sqlite3HashClear(Hash*);
    72     72   
    73     73   /*
    74     74   ** Macros for looping over all elements of a hash table.  The idiom is
    75     75   ** like this:
    76     76   **
    77     77   **   Hash h;

Changes to src/insert.c.

  1608   1608   ** or the first index for WITHOUT ROWID tables) if it is non-negative.
  1609   1609   ** If iBase is negative, then allocate the next available cursor.
  1610   1610   **
  1611   1611   ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
  1612   1612   ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
  1613   1613   ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
  1614   1614   ** pTab->pIndex list.
         1615  +**
         1616  +** If pTab is a virtual table, then this routine is a no-op and the
         1617  +** *piDataCur and *piIdxCur values are left uninitialized.
  1615   1618   */
  1616   1619   int sqlite3OpenTableAndIndices(
  1617   1620     Parse *pParse,   /* Parsing context */
  1618   1621     Table *pTab,     /* Table to be opened */
  1619   1622     int op,          /* OP_OpenRead or OP_OpenWrite */
  1620   1623     int iBase,       /* Use this for the table cursor, if there is one */
  1621   1624     u8 *aToOpen,     /* If not NULL: boolean for each table and index */
................................................................................
  1626   1629     int iDb;
  1627   1630     int iDataCur;
  1628   1631     Index *pIdx;
  1629   1632     Vdbe *v;
  1630   1633   
  1631   1634     assert( op==OP_OpenRead || op==OP_OpenWrite );
  1632   1635     if( IsVirtual(pTab) ){
  1633         -    assert( aToOpen==0 );
  1634         -    *piDataCur = 0;
  1635         -    *piIdxCur = 1;
         1636  +    /* This routine is a no-op for virtual tables. Leave the output
         1637  +    ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
         1638  +    ** can detect if they are used by mistake in the caller. */
  1636   1639       return 0;
  1637   1640     }
  1638   1641     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  1639   1642     v = sqlite3GetVdbe(pParse);
  1640   1643     assert( v!=0 );
  1641   1644     if( iBase<0 ) iBase = pParse->nTab;
  1642   1645     iDataCur = iBase++;
................................................................................
  1882   1885        && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0) 
  1883   1886            || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0))
  1884   1887       ){
  1885   1888         return 0;    /* Default values must be the same for all columns */
  1886   1889       }
  1887   1890     }
  1888   1891     for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
  1889         -    if( pDestIdx->onError!=OE_None ){
         1892  +    if( IsUniqueIndex(pDestIdx) ){
  1890   1893         destHasUniqueIdx = 1;
  1891   1894       }
  1892   1895       for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
  1893   1896         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
  1894   1897       }
  1895   1898       if( pSrcIdx==0 ){
  1896   1899         return 0;    /* pDestIdx has no corresponding index in pSrc */

Changes to src/legacy.c.

    40     40     char **azCols = 0;          /* Names of result columns */
    41     41     int callbackIsInit;         /* True if callback data is initialized */
    42     42   
    43     43     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
    44     44     if( zSql==0 ) zSql = "";
    45     45   
    46     46     sqlite3_mutex_enter(db->mutex);
    47         -  sqlite3Error(db, SQLITE_OK, 0);
           47  +  sqlite3Error(db, SQLITE_OK);
    48     48     while( rc==SQLITE_OK && zSql[0] ){
    49     49       int nCol;
    50     50       char **azVals = 0;
    51     51   
    52     52       pStmt = 0;
    53     53       rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
    54     54       assert( rc==SQLITE_OK || pStmt==0 );
................................................................................
    92     92               if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
    93     93                 db->mallocFailed = 1;
    94     94                 goto exec_out;
    95     95               }
    96     96             }
    97     97           }
    98     98           if( xCallback(pArg, nCol, azVals, azCols) ){
           99  +          /* EVIDENCE-OF: R-38229-40159 If the callback function to
          100  +          ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
          101  +          ** return SQLITE_ABORT. */
    99    102             rc = SQLITE_ABORT;
   100    103             sqlite3VdbeFinalize((Vdbe *)pStmt);
   101    104             pStmt = 0;
   102         -          sqlite3Error(db, SQLITE_ABORT, 0);
          105  +          sqlite3Error(db, SQLITE_ABORT);
   103    106             goto exec_out;
   104    107           }
   105    108         }
   106    109   
   107    110         if( rc!=SQLITE_ROW ){
   108    111           rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
   109    112           pStmt = 0;
................................................................................
   125    128     if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
   126    129       int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
   127    130       *pzErrMsg = sqlite3Malloc(nErrMsg);
   128    131       if( *pzErrMsg ){
   129    132         memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
   130    133       }else{
   131    134         rc = SQLITE_NOMEM;
   132         -      sqlite3Error(db, SQLITE_NOMEM, 0);
          135  +      sqlite3Error(db, SQLITE_NOMEM);
   133    136       }
   134    137     }else if( pzErrMsg ){
   135    138       *pzErrMsg = 0;
   136    139     }
   137    140   
   138    141     assert( (rc&db->errMask)==rc );
   139    142     sqlite3_mutex_leave(db->mutex);
   140    143     return rc;
   141    144   }

Changes to src/loadext.c.

   745    745       }else{
   746    746         xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   747    747                 wsdAutoext.aExt[i];
   748    748       }
   749    749       sqlite3_mutex_leave(mutex);
   750    750       zErrmsg = 0;
   751    751       if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
   752         -      sqlite3Error(db, rc,
          752  +      sqlite3ErrorWithMsg(db, rc,
   753    753               "automatic extension loading failed: %s", zErrmsg);
   754    754         go = 0;
   755    755       }
   756    756       sqlite3_free(zErrmsg);
   757    757     }
   758    758   }

Changes to src/main.c.

   823    823   }
   824    824   
   825    825   /*
   826    826   ** Close an existing SQLite database
   827    827   */
   828    828   static int sqlite3Close(sqlite3 *db, int forceZombie){
   829    829     if( !db ){
          830  +    /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
          831  +    ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
   830    832       return SQLITE_OK;
   831    833     }
   832    834     if( !sqlite3SafetyCheckSickOrOk(db) ){
   833    835       return SQLITE_MISUSE_BKPT;
   834    836     }
   835    837     sqlite3_mutex_enter(db->mutex);
   836    838   
................................................................................
   846    848     */
   847    849     sqlite3VtabRollback(db);
   848    850   
   849    851     /* Legacy behavior (sqlite3_close() behavior) is to return
   850    852     ** SQLITE_BUSY if the connection can not be closed immediately.
   851    853     */
   852    854     if( !forceZombie && connectionIsBusy(db) ){
   853         -    sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
          855  +    sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
   854    856          "statements or unfinished backups");
   855    857       sqlite3_mutex_leave(db->mutex);
   856    858       return SQLITE_BUSY;
   857    859     }
   858    860   
   859    861   #ifdef SQLITE_ENABLE_SQLLOG
   860    862     if( sqlite3GlobalConfig.xSqllog ){
................................................................................
   976    978         pMod->xDestroy(pMod->pAux);
   977    979       }
   978    980       sqlite3DbFree(db, pMod);
   979    981     }
   980    982     sqlite3HashClear(&db->aModule);
   981    983   #endif
   982    984   
   983         -  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
          985  +  sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
   984    986     sqlite3ValueFree(db->pErr);
   985    987     sqlite3CloseExtensions(db);
   986    988   
   987    989     db->magic = SQLITE_MAGIC_ERROR;
   988    990   
   989    991     /* The temp-database schema is allocated differently from the other schema
   990    992     ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
................................................................................
  1052   1054     }
  1053   1055   }
  1054   1056   
  1055   1057   /*
  1056   1058   ** Return a static string containing the name corresponding to the error code
  1057   1059   ** specified in the argument.
  1058   1060   */
  1059         -#if defined(SQLITE_TEST)
         1061  +#if (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) || defined(SQLITE_TEST)
  1060   1062   const char *sqlite3ErrName(int rc){
  1061   1063     const char *zName = 0;
  1062   1064     int i, origRc = rc;
  1063   1065     for(i=0; i<2 && zName==0; i++, rc &= 0xff){
  1064   1066       switch( rc ){
  1065   1067         case SQLITE_OK:                 zName = "SQLITE_OK";                break;
  1066   1068         case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
................................................................................
  1087   1089         case SQLITE_IOERR_FSYNC:        zName = "SQLITE_IOERR_FSYNC";       break;
  1088   1090         case SQLITE_IOERR_DIR_FSYNC:    zName = "SQLITE_IOERR_DIR_FSYNC";   break;
  1089   1091         case SQLITE_IOERR_TRUNCATE:     zName = "SQLITE_IOERR_TRUNCATE";    break;
  1090   1092         case SQLITE_IOERR_FSTAT:        zName = "SQLITE_IOERR_FSTAT";       break;
  1091   1093         case SQLITE_IOERR_UNLOCK:       zName = "SQLITE_IOERR_UNLOCK";      break;
  1092   1094         case SQLITE_IOERR_RDLOCK:       zName = "SQLITE_IOERR_RDLOCK";      break;
  1093   1095         case SQLITE_IOERR_DELETE:       zName = "SQLITE_IOERR_DELETE";      break;
  1094         -      case SQLITE_IOERR_BLOCKED:      zName = "SQLITE_IOERR_BLOCKED";     break;
  1095   1096         case SQLITE_IOERR_NOMEM:        zName = "SQLITE_IOERR_NOMEM";       break;
  1096   1097         case SQLITE_IOERR_ACCESS:       zName = "SQLITE_IOERR_ACCESS";      break;
  1097   1098         case SQLITE_IOERR_CHECKRESERVEDLOCK:
  1098   1099                                   zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
  1099   1100         case SQLITE_IOERR_LOCK:         zName = "SQLITE_IOERR_LOCK";        break;
  1100   1101         case SQLITE_IOERR_CLOSE:        zName = "SQLITE_IOERR_CLOSE";       break;
  1101   1102         case SQLITE_IOERR_DIR_CLOSE:    zName = "SQLITE_IOERR_DIR_CLOSE";   break;
................................................................................
  1410   1411     ** and there are active VMs, then return SQLITE_BUSY. If a function
  1411   1412     ** is being overridden/deleted but there are no active VMs, allow the
  1412   1413     ** operation to continue but invalidate all precompiled statements.
  1413   1414     */
  1414   1415     p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
  1415   1416     if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
  1416   1417       if( db->nVdbeActive ){
  1417         -      sqlite3Error(db, SQLITE_BUSY, 
         1418  +      sqlite3ErrorWithMsg(db, SQLITE_BUSY, 
  1418   1419           "unable to delete/modify user-function due to active statements");
  1419   1420         assert( !db->mallocFailed );
  1420   1421         return SQLITE_BUSY;
  1421   1422       }else{
  1422   1423         sqlite3ExpirePreparedStatements(db);
  1423   1424       }
  1424   1425     }
................................................................................
  1748   1749   
  1749   1750     sqlite3_mutex_enter(db->mutex);
  1750   1751     if( zDb && zDb[0] ){
  1751   1752       iDb = sqlite3FindDbName(db, zDb);
  1752   1753     }
  1753   1754     if( iDb<0 ){
  1754   1755       rc = SQLITE_ERROR;
  1755         -    sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
         1756  +    sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
  1756   1757     }else{
  1757   1758       rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
  1758         -    sqlite3Error(db, rc, 0);
         1759  +    sqlite3Error(db, rc);
  1759   1760     }
  1760   1761     rc = sqlite3ApiExit(db, rc);
  1761   1762     sqlite3_mutex_leave(db->mutex);
  1762   1763     return rc;
  1763   1764   #endif
  1764   1765   }
  1765   1766   
................................................................................
  1906   1907     }
  1907   1908     sqlite3_mutex_enter(db->mutex);
  1908   1909     if( db->mallocFailed ){
  1909   1910       z = (void *)outOfMem;
  1910   1911     }else{
  1911   1912       z = sqlite3_value_text16(db->pErr);
  1912   1913       if( z==0 ){
  1913         -      sqlite3Error(db, db->errCode, sqlite3ErrStr(db->errCode));
         1914  +      sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
  1914   1915         z = sqlite3_value_text16(db->pErr);
  1915   1916       }
  1916   1917       /* A malloc() may have failed within the call to sqlite3_value_text16()
  1917   1918       ** above. If this is the case, then the db->mallocFailed flag needs to
  1918   1919       ** be cleared before returning. Do this directly, instead of via
  1919   1920       ** sqlite3ApiExit(), to avoid setting the database handle error message.
  1920   1921       */
................................................................................
  1993   1994     u8 enc,
  1994   1995     void* pCtx,
  1995   1996     int(*xCompare)(void*,int,const void*,int,const void*),
  1996   1997     void(*xDel)(void*)
  1997   1998   ){
  1998   1999     CollSeq *pColl;
  1999   2000     int enc2;
  2000         -  int nName = sqlite3Strlen30(zName);
  2001   2001     
  2002   2002     assert( sqlite3_mutex_held(db->mutex) );
  2003   2003   
  2004   2004     /* If SQLITE_UTF16 is specified as the encoding type, transform this
  2005   2005     ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
  2006   2006     ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
  2007   2007     */
................................................................................
  2018   2018     /* Check if this call is removing or replacing an existing collation 
  2019   2019     ** sequence. If so, and there are active VMs, return busy. If there
  2020   2020     ** are no active VMs, invalidate any pre-compiled statements.
  2021   2021     */
  2022   2022     pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
  2023   2023     if( pColl && pColl->xCmp ){
  2024   2024       if( db->nVdbeActive ){
  2025         -      sqlite3Error(db, SQLITE_BUSY, 
         2025  +      sqlite3ErrorWithMsg(db, SQLITE_BUSY, 
  2026   2026           "unable to delete/modify collation sequence due to active statements");
  2027   2027         return SQLITE_BUSY;
  2028   2028       }
  2029   2029       sqlite3ExpirePreparedStatements(db);
  2030   2030       invalidateCachedKeyInfo(db);
  2031   2031   
  2032   2032       /* If collation sequence pColl was created directly by a call to
  2033   2033       ** sqlite3_create_collation, and not generated by synthCollSeq(),
  2034   2034       ** then any copies made by synthCollSeq() need to be invalidated.
  2035   2035       ** Also, collation destructor - CollSeq.xDel() - function may need
  2036   2036       ** to be called.
  2037   2037       */ 
  2038   2038       if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
  2039         -      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
         2039  +      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
  2040   2040         int j;
  2041   2041         for(j=0; j<3; j++){
  2042   2042           CollSeq *p = &aColl[j];
  2043   2043           if( p->enc==pColl->enc ){
  2044   2044             if( p->xDel ){
  2045   2045               p->xDel(p->pUser);
  2046   2046             }
................................................................................
  2052   2052   
  2053   2053     pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
  2054   2054     if( pColl==0 ) return SQLITE_NOMEM;
  2055   2055     pColl->xCmp = xCompare;
  2056   2056     pColl->pUser = pCtx;
  2057   2057     pColl->xDel = xDel;
  2058   2058     pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
  2059         -  sqlite3Error(db, SQLITE_OK, 0);
         2059  +  sqlite3Error(db, SQLITE_OK);
  2060   2060     return SQLITE_OK;
  2061   2061   }
  2062   2062   
  2063   2063   
  2064   2064   /*
  2065   2065   ** This array defines hard upper bounds on limit values.  The
  2066   2066   ** initializer must be kept in sync with the SQLITE_LIMIT_*
................................................................................
  2072   2072     SQLITE_MAX_COLUMN,
  2073   2073     SQLITE_MAX_EXPR_DEPTH,
  2074   2074     SQLITE_MAX_COMPOUND_SELECT,
  2075   2075     SQLITE_MAX_VDBE_OP,
  2076   2076     SQLITE_MAX_FUNCTION_ARG,
  2077   2077     SQLITE_MAX_ATTACHED,
  2078   2078     SQLITE_MAX_LIKE_PATTERN_LENGTH,
  2079         -  SQLITE_MAX_VARIABLE_NUMBER,
         2079  +  SQLITE_MAX_VARIABLE_NUMBER,      /* IMP: R-38091-32352 */
  2080   2080     SQLITE_MAX_TRIGGER_DEPTH,
  2081   2081   };
  2082   2082   
  2083   2083   /*
  2084   2084   ** Make sure the hard limits are set to reasonable values
  2085   2085   */
  2086   2086   #if SQLITE_MAX_LENGTH<100
................................................................................
  2537   2537     createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
  2538   2538   
  2539   2539     /* Parse the filename/URI argument. */
  2540   2540     db->openFlags = flags;
  2541   2541     rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
  2542   2542     if( rc!=SQLITE_OK ){
  2543   2543       if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
  2544         -    sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
         2544  +    sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
  2545   2545       sqlite3_free(zErrMsg);
  2546   2546       goto opendb_out;
  2547   2547     }
  2548   2548   
  2549   2549     /* Open the backend database driver */
  2550   2550     rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
  2551   2551                           flags | SQLITE_OPEN_MAIN_DB);
  2552   2552     if( rc!=SQLITE_OK ){
  2553   2553       if( rc==SQLITE_IOERR_NOMEM ){
  2554   2554         rc = SQLITE_NOMEM;
  2555   2555       }
  2556         -    sqlite3Error(db, rc, 0);
         2556  +    sqlite3Error(db, rc);
  2557   2557       goto opendb_out;
  2558   2558     }
  2559   2559     db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
  2560   2560     db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
  2561   2561   
  2562   2562   
  2563   2563     /* The default safety_level for the main database is 'full'; for the temp
................................................................................
  2573   2573       goto opendb_out;
  2574   2574     }
  2575   2575   
  2576   2576     /* Register all built-in functions, but do not attempt to read the
  2577   2577     ** database schema yet. This is delayed until the first time the database
  2578   2578     ** is accessed.
  2579   2579     */
  2580         -  sqlite3Error(db, SQLITE_OK, 0);
         2580  +  sqlite3Error(db, SQLITE_OK);
  2581   2581     sqlite3RegisterBuiltinFunctions(db);
  2582   2582   
  2583   2583     /* Load automatic extensions - extensions that have been registered
  2584   2584     ** using the sqlite3_automatic_extension() API.
  2585   2585     */
  2586   2586     rc = sqlite3_errcode(db);
  2587   2587     if( rc==SQLITE_OK ){
................................................................................
  2630   2630     */
  2631   2631   #ifdef SQLITE_DEFAULT_LOCKING_MODE
  2632   2632     db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
  2633   2633     sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
  2634   2634                             SQLITE_DEFAULT_LOCKING_MODE);
  2635   2635   #endif
  2636   2636   
  2637         -  if( rc ) sqlite3Error(db, rc, 0);
         2637  +  if( rc ) sqlite3Error(db, rc);
  2638   2638   
  2639   2639     /* Enable the lookaside-malloc subsystem */
  2640   2640     setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
  2641   2641                           sqlite3GlobalConfig.nLookaside);
  2642   2642   
  2643   2643     sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
  2644   2644   
................................................................................
  2992   2992   
  2993   2993     if( SQLITE_OK==rc && !pTab ){
  2994   2994       sqlite3DbFree(db, zErrMsg);
  2995   2995       zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
  2996   2996           zColumnName);
  2997   2997       rc = SQLITE_ERROR;
  2998   2998     }
  2999         -  sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
         2999  +  sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
  3000   3000     sqlite3DbFree(db, zErrMsg);
  3001   3001     rc = sqlite3ApiExit(db, rc);
  3002   3002     sqlite3_mutex_leave(db->mutex);
  3003   3003     return rc;
  3004   3004   }
  3005   3005   #endif
  3006   3006   
................................................................................
  3356   3356   #ifdef SQLITE_VDBE_COVERAGE
  3357   3357         typedef void (*branch_callback)(void*,int,u8,u8);
  3358   3358         sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
  3359   3359         sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
  3360   3360   #endif
  3361   3361         break;
  3362   3362       }
         3363  +
         3364  +    /*   sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
         3365  +    **
         3366  +    ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
         3367  +    ** not.
         3368  +    */
         3369  +    case SQLITE_TESTCTRL_ISINIT: {
         3370  +      if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
         3371  +      break;
         3372  +    }
  3363   3373   
  3364   3374     }
  3365   3375     va_end(ap);
  3366   3376   #endif /* SQLITE_OMIT_BUILTIN_TEST */
  3367   3377     return rc;
  3368   3378   }
  3369   3379   

Changes to src/malloc.c.

   348    348   ** embedded processor.
   349    349   */
   350    350   void *sqlite3ScratchMalloc(int n){
   351    351     void *p;
   352    352     assert( n>0 );
   353    353   
   354    354     sqlite3_mutex_enter(mem0.mutex);
          355  +  sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   355    356     if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
   356    357       p = mem0.pScratchFree;
   357    358       mem0.pScratchFree = mem0.pScratchFree->pNext;
   358    359       mem0.nScratchFree--;
   359    360       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
   360         -    sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   361    361       sqlite3_mutex_leave(mem0.mutex);
   362    362     }else{
   363         -    if( sqlite3GlobalConfig.bMemstat ){
   364         -      sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   365         -      n = mallocWithAlarm(n, &p);
   366         -      if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
          363  +    sqlite3_mutex_leave(mem0.mutex);
          364  +    p = sqlite3Malloc(n);
          365  +    if( sqlite3GlobalConfig.bMemstat && p ){
          366  +      sqlite3_mutex_enter(mem0.mutex);
          367  +      sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
   367    368         sqlite3_mutex_leave(mem0.mutex);
   368         -    }else{
   369         -      sqlite3_mutex_leave(mem0.mutex);
   370         -      p = sqlite3GlobalConfig.m.xMalloc(n);
   371    369       }
   372    370       sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
   373    371     }
   374    372     assert( sqlite3_mutex_notheld(mem0.mutex) );
   375    373   
   376    374   
   377    375   #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
................................................................................
   474    472       sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
   475    473       sqlite3GlobalConfig.m.xFree(p);
   476    474       sqlite3_mutex_leave(mem0.mutex);
   477    475     }else{
   478    476       sqlite3GlobalConfig.m.xFree(p);
   479    477     }
   480    478   }
          479  +
          480  +/*
          481  +** Add the size of memory allocation "p" to the count in
          482  +** *db->pnBytesFreed.
          483  +*/
          484  +static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
          485  +  *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
          486  +}
   481    487   
   482    488   /*
   483    489   ** Free memory that might be associated with a particular database
   484    490   ** connection.
   485    491   */
   486    492   void sqlite3DbFree(sqlite3 *db, void *p){
   487    493     assert( db==0 || sqlite3_mutex_held(db->mutex) );
   488    494     if( p==0 ) return;
   489    495     if( db ){
   490    496       if( db->pnBytesFreed ){
   491         -      *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
          497  +      measureAllocationSize(db, p);
   492    498         return;
   493    499       }
   494    500       if( isLookaside(db, p) ){
   495    501         LookasideSlot *pBuf = (LookasideSlot*)p;
   496    502   #if SQLITE_DEBUG
   497    503         /* Trash all content in the buffer being freed */
   498    504         memset(p, 0xaa, db->lookaside.sz);
................................................................................
   751    757     va_start(ap, zFormat);
   752    758     z = sqlite3VMPrintf(db, zFormat, ap);
   753    759     va_end(ap);
   754    760     sqlite3DbFree(db, *pz);
   755    761     *pz = z;
   756    762   }
   757    763   
          764  +/*
          765  +** Take actions at the end of an API call to indicate an OOM error
          766  +*/
          767  +static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
          768  +  db->mallocFailed = 0;
          769  +  sqlite3Error(db, SQLITE_NOMEM);
          770  +  return SQLITE_NOMEM;
          771  +}
   758    772   
   759    773   /*
   760    774   ** This function must be called before exiting any API function (i.e. 
   761    775   ** returning control to the user) that has called sqlite3_malloc or
   762    776   ** sqlite3_realloc.
   763    777   **
   764    778   ** The returned value is normally a copy of the second argument to this
................................................................................
   771    785   */
   772    786   int sqlite3ApiExit(sqlite3* db, int rc){
   773    787     /* If the db handle is not NULL, then we must hold the connection handle
   774    788     ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
   775    789     ** is unsafe, as is the call to sqlite3Error().
   776    790     */
   777    791     assert( !db || sqlite3_mutex_held(db->mutex) );
   778         -  if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
   779         -    sqlite3Error(db, SQLITE_NOMEM, 0);
   780         -    db->mallocFailed = 0;
   781         -    rc = SQLITE_NOMEM;
          792  +  if( db==0 ) return rc & 0xff;
          793  +  if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
          794  +    return apiOomError(db);
   782    795     }
   783         -  return rc & (db ? db->errMask : 0xff);
          796  +  return rc & db->errMask;
   784    797   }

Changes to src/mutex.c.

    77     77   }
    78     78   
    79     79   /*
    80     80   ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
    81     81   */
    82     82   sqlite3_mutex *sqlite3_mutex_alloc(int id){
    83     83   #ifndef SQLITE_OMIT_AUTOINIT
    84         -  if( sqlite3_initialize() ) return 0;
           84  +  if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
    85     85   #endif
    86     86     return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
    87     87   }
    88     88   
    89     89   sqlite3_mutex *sqlite3MutexAlloc(int id){
    90     90     if( !sqlite3GlobalConfig.bCoreMutex ){
    91     91       return 0;

Changes to src/mutex_noop.c.

   103    103   
   104    104   /*
   105    105   ** The sqlite3_mutex_alloc() routine allocates a new
   106    106   ** mutex and returns a pointer to it.  If it returns NULL
   107    107   ** that means that a mutex could not be allocated. 
   108    108   */
   109    109   static sqlite3_mutex *debugMutexAlloc(int id){
   110         -  static sqlite3_debug_mutex aStatic[6];
          110  +  static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_APP3 - 1];
   111    111     sqlite3_debug_mutex *pNew = 0;
   112    112     switch( id ){
   113    113       case SQLITE_MUTEX_FAST:
   114    114       case SQLITE_MUTEX_RECURSIVE: {
   115    115         pNew = sqlite3Malloc(sizeof(*pNew));
   116    116         if( pNew ){
   117    117           pNew->id = id;

Changes to src/mutex_unix.c.

    92     92   ** to sqlite3_mutex_alloc() is one of these integer constants:
    93     93   **
    94     94   ** <ul>
    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         -** <li>  SQLITE_MUTEX_STATIC_MEM2
           99  +** <li>  SQLITE_MUTEX_STATIC_OPEN
   100    100   ** <li>  SQLITE_MUTEX_STATIC_PRNG
   101    101   ** <li>  SQLITE_MUTEX_STATIC_LRU
   102    102   ** <li>  SQLITE_MUTEX_STATIC_PMEM
          103  +** <li>  SQLITE_MUTEX_STATIC_APP1
          104  +** <li>  SQLITE_MUTEX_STATIC_APP2
          105  +** <li>  SQLITE_MUTEX_STATIC_APP3
   103    106   ** </ul>
   104    107   **
   105    108   ** The first two constants cause sqlite3_mutex_alloc() to create
   106    109   ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   107    110   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   108    111   ** The mutex implementation does not need to make a distinction
   109    112   ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
................................................................................
   124    127   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   125    128   ** returns a different mutex on every call.  But for the static 
   126    129   ** mutex types, the same mutex is returned on every call that has
   127    130   ** the same type number.
   128    131   */
   129    132   static sqlite3_mutex *pthreadMutexAlloc(int iType){
   130    133     static sqlite3_mutex staticMutexes[] = {
          134  +    SQLITE3_MUTEX_INITIALIZER,
          135  +    SQLITE3_MUTEX_INITIALIZER,
          136  +    SQLITE3_MUTEX_INITIALIZER,
   131    137       SQLITE3_MUTEX_INITIALIZER,
   132    138       SQLITE3_MUTEX_INITIALIZER,
   133    139       SQLITE3_MUTEX_INITIALIZER,
   134    140       SQLITE3_MUTEX_INITIALIZER,
   135    141       SQLITE3_MUTEX_INITIALIZER,
   136    142       SQLITE3_MUTEX_INITIALIZER
   137    143     };

Changes to src/mutex_w32.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** This file contains the C functions that implement mutexes for win32
           12  +** This file contains the C functions that implement mutexes for Win32.
    13     13   */
    14     14   #include "sqliteInt.h"
    15     15   
    16     16   #if SQLITE_OS_WIN
           17  +/*
           18  +** Include code that is common to all os_*.c files
           19  +*/
           20  +#include "os_common.h"
           21  +
    17     22   /*
    18     23   ** Include the header file for the Windows VFS.
    19     24   */
    20     25   #include "os_win.h"
    21     26   #endif
    22     27   
    23     28   /*
    24     29   ** The code in this file is only used if we are compiling multithreaded
    25         -** on a win32 system.
           30  +** on a Win32 system.
    26     31   */
    27     32   #ifdef SQLITE_MUTEX_W32
    28     33   
    29     34   /*
    30     35   ** Each recursive mutex is an instance of the following structure.
    31     36   */
    32     37   struct sqlite3_mutex {
    33     38     CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
    34     39     int id;                    /* Mutex type */
    35     40   #ifdef SQLITE_DEBUG
    36     41     volatile int nRef;         /* Number of enterances */
    37     42     volatile DWORD owner;      /* Thread holding this mutex */
    38         -  int trace;                 /* True to trace changes */
           43  +  volatile int trace;        /* True to trace changes */
    39     44   #endif
    40     45   };
    41         -#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
    42         -#ifdef SQLITE_DEBUG
    43         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
    44         -#else
    45         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
    46         -#endif
    47     46   
    48     47   /*
    49         -** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
    50         -** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
    51         -**
    52         -** Here is an interesting observation:  Win95, Win98, and WinME lack
    53         -** the LockFileEx() API.  But we can still statically link against that
    54         -** API as long as we don't call it win running Win95/98/ME.  A call to
    55         -** this routine is used to determine if the host is Win95/98/ME or
    56         -** WinNT/2K/XP so that we will know whether or not we can safely call
    57         -** the LockFileEx() API.
    58         -**
    59         -** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
    60         -** which is only available if your application was compiled with 
    61         -** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
    62         -** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
    63         -** this out as well.
           48  +** These are the initializer values used when declaring a "static" mutex
           49  +** on Win32.  It should be noted that all mutexes require initialization
           50  +** on the Win32 platform.
    64     51   */
    65         -#if 0
    66         -#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
    67         -# define mutexIsNT()  (1)
           52  +#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
           53  +
           54  +#ifdef SQLITE_DEBUG
           55  +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
           56  +                                    0L, (DWORD)0, 0 }
    68     57   #else
    69         -  static int mutexIsNT(void){
    70         -    static int osType = 0;
    71         -    if( osType==0 ){
    72         -      OSVERSIONINFO sInfo;
    73         -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
    74         -      GetVersionEx(&sInfo);
    75         -      osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
    76         -    }
    77         -    return osType==2;
    78         -  }
    79         -#endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
           58  +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
    80     59   #endif
    81     60   
    82     61   #ifdef SQLITE_DEBUG
    83     62   /*
    84     63   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
    85     64   ** intended for use only inside assert() statements.
    86     65   */
    87     66   static int winMutexHeld(sqlite3_mutex *p){
    88     67     return p->nRef!=0 && p->owner==GetCurrentThreadId();
    89     68   }
           69  +
    90     70   static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
    91     71     return p->nRef==0 || p->owner!=tid;
    92     72   }
           73  +
    93     74   static int winMutexNotheld(sqlite3_mutex *p){
    94         -  DWORD tid = GetCurrentThreadId(); 
           75  +  DWORD tid = GetCurrentThreadId();
    95     76     return winMutexNotheld2(p, tid);
    96     77   }
    97     78   #endif
    98     79   
    99         -
   100     80   /*
   101     81   ** Initialize and deinitialize the mutex subsystem.
   102     82   */
   103         -static sqlite3_mutex winMutex_staticMutexes[6] = {
           83  +static sqlite3_mutex winMutex_staticMutexes[] = {
           84  +  SQLITE3_MUTEX_INITIALIZER,
           85  +  SQLITE3_MUTEX_INITIALIZER,
           86  +  SQLITE3_MUTEX_INITIALIZER,
   104     87     SQLITE3_MUTEX_INITIALIZER,
   105     88     SQLITE3_MUTEX_INITIALIZER,
   106     89     SQLITE3_MUTEX_INITIALIZER,
   107     90     SQLITE3_MUTEX_INITIALIZER,
   108     91     SQLITE3_MUTEX_INITIALIZER,
   109     92     SQLITE3_MUTEX_INITIALIZER
   110     93   };
           94  +
   111     95   static int winMutex_isInit = 0;
   112         -/* As winMutexInit() and winMutexEnd() are called as part
   113         -** of the sqlite3_initialize and sqlite3_shutdown()
   114         -** processing, the "interlocked" magic is probably not
   115         -** strictly necessary.
           96  +static int winMutex_isNt = -1; /* <0 means "need to query" */
           97  +
           98  +/* As the winMutexInit() and winMutexEnd() functions are called as part
           99  +** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
          100  +** "interlocked" magic used here is probably not strictly necessary.
   116    101   */
   117         -static LONG winMutex_lock = 0;
          102  +static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
   118    103   
          104  +int sqlite3_win32_is_nt(void); /* os_win.c */
   119    105   void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
   120    106   
   121         -static int winMutexInit(void){ 
          107  +static int winMutexInit(void){
   122    108     /* The first to increment to 1 does actual initialization */
   123    109     if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
   124    110       int i;
   125    111       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
   126    112   #if SQLITE_OS_WINRT
   127    113         InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
   128    114   #else
   129    115         InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
   130    116   #endif
   131    117       }
   132    118       winMutex_isInit = 1;
   133    119     }else{
   134         -    /* Someone else is in the process of initing the static mutexes */
          120  +    /* Another thread is (in the process of) initializing the static
          121  +    ** mutexes */
   135    122       while( !winMutex_isInit ){
   136    123         sqlite3_win32_sleep(1);
   137    124       }
   138    125     }
   139         -  return SQLITE_OK; 
          126  +  return SQLITE_OK;
   140    127   }
   141    128   
   142         -static int winMutexEnd(void){ 
   143         -  /* The first to decrement to 0 does actual shutdown 
          129  +static int winMutexEnd(void){
          130  +  /* The first to decrement to 0 does actual shutdown
   144    131     ** (which should be the last to shutdown.) */
   145    132     if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
   146    133       if( winMutex_isInit==1 ){
   147    134         int i;
   148    135         for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
   149    136           DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
   150    137         }
   151    138         winMutex_isInit = 0;
   152    139       }
   153    140     }
   154         -  return SQLITE_OK; 
          141  +  return SQLITE_OK;
   155    142   }
   156    143   
   157    144   /*
   158    145   ** The sqlite3_mutex_alloc() routine allocates a new
   159    146   ** mutex and returns a pointer to it.  If it returns NULL
   160    147   ** that means that a mutex could not be allocated.  SQLite
   161    148   ** will unwind its stack and return an error.  The argument
................................................................................
   162    149   ** to sqlite3_mutex_alloc() is one of these integer constants:
   163    150   **
   164    151   ** <ul>
   165    152   ** <li>  SQLITE_MUTEX_FAST
   166    153   ** <li>  SQLITE_MUTEX_RECURSIVE
   167    154   ** <li>  SQLITE_MUTEX_STATIC_MASTER
   168    155   ** <li>  SQLITE_MUTEX_STATIC_MEM
   169         -** <li>  SQLITE_MUTEX_STATIC_MEM2
          156  +** <li>  SQLITE_MUTEX_STATIC_OPEN
   170    157   ** <li>  SQLITE_MUTEX_STATIC_PRNG
   171    158   ** <li>  SQLITE_MUTEX_STATIC_LRU
   172    159   ** <li>  SQLITE_MUTEX_STATIC_PMEM
          160  +** <li>  SQLITE_MUTEX_STATIC_APP1
          161  +** <li>  SQLITE_MUTEX_STATIC_APP2
          162  +** <li>  SQLITE_MUTEX_STATIC_APP3
   173    163   ** </ul>
   174    164   **
   175    165   ** The first two constants cause sqlite3_mutex_alloc() to create
   176    166   ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   177    167   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   178    168   ** The mutex implementation does not need to make a distinction
   179    169   ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
................................................................................
   188    178   ** may add additional static mutexes.  Static mutexes are for internal
   189    179   ** use by SQLite only.  Applications that use SQLite mutexes should
   190    180   ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
   191    181   ** SQLITE_MUTEX_RECURSIVE.
   192    182   **
   193    183   ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
   194    184   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   195         -** returns a different mutex on every call.  But for the static 
          185  +** returns a different mutex on every call.  But for the static
   196    186   ** mutex types, the same mutex is returned on every call that has
   197    187   ** the same type number.
   198    188   */
   199    189   static sqlite3_mutex *winMutexAlloc(int iType){
   200    190     sqlite3_mutex *p;
   201    191   
   202    192     switch( iType ){
   203    193       case SQLITE_MUTEX_FAST:
   204    194       case SQLITE_MUTEX_RECURSIVE: {
   205    195         p = sqlite3MallocZero( sizeof(*p) );
   206         -      if( p ){  
          196  +      if( p ){
   207    197   #ifdef SQLITE_DEBUG
   208    198           p->id = iType;
          199  +#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
          200  +        p->trace = 1;
          201  +#endif
   209    202   #endif
   210    203   #if SQLITE_OS_WINRT
   211    204           InitializeCriticalSectionEx(&p->mutex, 0, 0);
   212    205   #else
   213    206           InitializeCriticalSection(&p->mutex);
   214    207   #endif
   215    208         }
   216    209         break;
   217    210       }
   218    211       default: {
   219         -      assert( winMutex_isInit==1 );
   220    212         assert( iType-2 >= 0 );
   221    213         assert( iType-2 < ArraySize(winMutex_staticMutexes) );
          214  +      assert( winMutex_isInit==1 );
   222    215         p = &winMutex_staticMutexes[iType-2];
   223    216   #ifdef SQLITE_DEBUG
   224    217         p->id = iType;
          218  +#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
          219  +      p->trace = 1;
          220  +#endif
   225    221   #endif
   226    222         break;
   227    223       }
   228    224     }
   229    225     return p;
   230    226   }
   231    227   
................................................................................
   233    229   /*
   234    230   ** This routine deallocates a previously
   235    231   ** allocated mutex.  SQLite is careful to deallocate every
   236    232   ** mutex that it allocates.
   237    233   */
   238    234   static void winMutexFree(sqlite3_mutex *p){
   239    235     assert( p );
          236  +#ifdef SQLITE_DEBUG
   240    237     assert( p->nRef==0 && p->owner==0 );
   241    238     assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
          239  +#endif
          240  +  assert( winMutex_isInit==1 );
   242    241     DeleteCriticalSection(&p->mutex);
   243    242     sqlite3_free(p);
   244    243   }
   245    244   
   246    245   /*
   247    246   ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   248    247   ** to enter a mutex.  If another thread is already within the mutex,
................................................................................
   251    250   ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   252    251   ** be entered multiple times by the same thread.  In such cases the,
   253    252   ** mutex must be exited an equal number of times before another thread
   254    253   ** can enter.  If the same thread tries to enter any other kind of mutex
   255    254   ** more than once, the behavior is undefined.
   256    255   */
   257    256   static void winMutexEnter(sqlite3_mutex *p){
          257  +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
          258  +  DWORD tid = GetCurrentThreadId();
          259  +#endif
   258    260   #ifdef SQLITE_DEBUG
   259         -  DWORD tid = GetCurrentThreadId(); 
          261  +  assert( p );
   260    262     assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
          263  +#else
          264  +  assert( p );
   261    265   #endif
          266  +  assert( winMutex_isInit==1 );
   262    267     EnterCriticalSection(&p->mutex);
   263    268   #ifdef SQLITE_DEBUG
   264    269     assert( p->nRef>0 || p->owner==0 );
   265         -  p->owner = tid; 
          270  +  p->owner = tid;
   266    271     p->nRef++;
   267    272     if( p->trace ){
   268         -    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
          273  +    OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
          274  +             tid, p, p->trace, p->nRef));
   269    275     }
   270    276   #endif
   271    277   }
          278  +
   272    279   static int winMutexTry(sqlite3_mutex *p){
   273         -#ifndef NDEBUG
   274         -  DWORD tid = GetCurrentThreadId(); 
          280  +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
          281  +  DWORD tid = GetCurrentThreadId();
   275    282   #endif
   276    283     int rc = SQLITE_BUSY;
          284  +  assert( p );
   277    285     assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
   278    286     /*
   279    287     ** The sqlite3_mutex_try() routine is very rarely used, and when it
   280    288     ** is used it is merely an optimization.  So it is OK for it to always
   281         -  ** fail.  
          289  +  ** fail.
   282    290     **
   283    291     ** The TryEnterCriticalSection() interface is only available on WinNT.
   284    292     ** And some windows compilers complain if you try to use it without
   285    293     ** first doing some #defines that prevent SQLite from building on Win98.
   286    294     ** For that reason, we will omit this optimization for now.  See
   287    295     ** ticket #2685.
   288    296     */
   289         -#if 0
   290         -  if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
          297  +#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
          298  +  assert( winMutex_isInit==1 );
          299  +  assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
          300  +  if( winMutex_isNt<0 ){
          301  +    winMutex_isNt = sqlite3_win32_is_nt();
          302  +  }
          303  +  assert( winMutex_isNt==0 || winMutex_isNt==1 );
          304  +  if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
          305  +#ifdef SQLITE_DEBUG
   291    306       p->owner = tid;
   292    307       p->nRef++;
          308  +#endif
   293    309       rc = SQLITE_OK;
   294    310     }
   295    311   #else
   296    312     UNUSED_PARAMETER(p);
   297    313   #endif
   298    314   #ifdef SQLITE_DEBUG
   299         -  if( rc==SQLITE_OK && p->trace ){
   300         -    printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
          315  +  if( p->trace ){
          316  +    OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
          317  +             tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
   301    318     }
   302    319   #endif
   303    320     return rc;
   304    321   }
   305    322   
   306    323   /*
   307    324   ** The sqlite3_mutex_leave() routine exits a mutex that was
   308    325   ** previously entered by the same thread.  The behavior
   309    326   ** is undefined if the mutex is not currently entered or
   310    327   ** is not currently allocated.  SQLite will never do either.
   311    328   */
   312    329   static void winMutexLeave(sqlite3_mutex *p){
   313         -#ifndef NDEBUG
          330  +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
   314    331     DWORD tid = GetCurrentThreadId();
          332  +#endif
          333  +  assert( p );
          334  +#ifdef SQLITE_DEBUG
   315    335     assert( p->nRef>0 );
   316    336     assert( p->owner==tid );
   317    337     p->nRef--;
   318    338     if( p->nRef==0 ) p->owner = 0;
   319    339     assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
   320    340   #endif
          341  +  assert( winMutex_isInit==1 );
   321    342     LeaveCriticalSection(&p->mutex);
   322    343   #ifdef SQLITE_DEBUG
   323    344     if( p->trace ){
   324         -    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
          345  +    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
          346  +             tid, p, p->trace, p->nRef));
   325    347     }
   326    348   #endif
   327    349   }
   328    350   
   329    351   sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   330    352     static const sqlite3_mutex_methods sMutex = {
   331    353       winMutexInit,
................................................................................
   339    361       winMutexHeld,
   340    362       winMutexNotheld
   341    363   #else
   342    364       0,
   343    365       0
   344    366   #endif
   345    367     };
   346         -
   347    368     return &sMutex;
   348    369   }
          370  +
   349    371   #endif /* SQLITE_MUTEX_W32 */

Changes to src/notify.c.

   180    180         removeFromBlockedList(db);
   181    181         addToBlockedList(db);
   182    182       }
   183    183     }
   184    184   
   185    185     leaveMutex();
   186    186     assert( !db->mallocFailed );
   187         -  sqlite3Error(db, rc, (rc?"database is deadlocked":0));
          187  +  sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
   188    188     sqlite3_mutex_leave(db->mutex);
   189    189     return rc;
   190    190   }
   191    191   
   192    192   /*
   193    193   ** This function is called while stepping or preparing a statement 
   194    194   ** associated with connection db. The operation will return SQLITE_LOCKED

Changes to src/os_unix.c.

    90     90   #include <sys/stat.h>
    91     91   #include <fcntl.h>
    92     92   #include <unistd.h>
    93     93   #include <time.h>
    94     94   #include <sys/time.h>
    95     95   #include <errno.h>
    96     96   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
    97         -#include <sys/mman.h>
           97  +# include <sys/mman.h>
    98     98   #endif
    99     99   
   100         -
   101         -#if SQLITE_ENABLE_LOCKING_STYLE
          100  +#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
   102    101   # include <sys/ioctl.h>
   103    102   # if OS_VXWORKS
   104    103   #  include <semaphore.h>
   105    104   #  include <limits.h>
   106    105   # else
   107    106   #  include <sys/file.h>
   108    107   #  include <sys/param.h>
................................................................................
   314    313   
   315    314   /*
   316    315   ** On some systems, calls to fchown() will trigger a message in a security
   317    316   ** log if they come from non-root processes.  So avoid calling fchown() if
   318    317   ** we are not running as root.
   319    318   */
   320    319   static int posixFchown(int fd, uid_t uid, gid_t gid){
          320  +#if OS_VXWORKS
          321  +  return 0;
          322  +#else
   321    323     return geteuid() ? 0 : fchown(fd,uid,gid);
          324  +#endif
   322    325   }
   323    326   
   324    327   /* Forward reference */
   325    328   static int openDirectory(const char*, int*);
   326    329   static int unixGetpagesize(void);
   327    330   
   328    331   /*
................................................................................
   370    373   
   371    374     { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
   372    375   #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
   373    376   
   374    377     { "read",         (sqlite3_syscall_ptr)read,       0  },
   375    378   #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
   376    379   
   377         -#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
          380  +#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
   378    381     { "pread",        (sqlite3_syscall_ptr)pread,      0  },
   379    382   #else
   380    383     { "pread",        (sqlite3_syscall_ptr)0,          0  },
   381    384   #endif
   382    385   #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
   383    386   
   384    387   #if defined(USE_PREAD64)
................................................................................
   387    390     { "pread64",      (sqlite3_syscall_ptr)0,          0  },
   388    391   #endif
   389    392   #define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
   390    393   
   391    394     { "write",        (sqlite3_syscall_ptr)write,      0  },
   392    395   #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
   393    396   
   394         -#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
          397  +#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
   395    398     { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
   396    399   #else
   397    400     { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
   398    401   #endif
   399    402   #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
   400    403                       aSyscall[12].pCurrent)
   401    404   
................................................................................
   757    760           (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
   758    761         return SQLITE_BUSY;
   759    762       }
   760    763       /* else fall through */
   761    764     case EPERM: 
   762    765       return SQLITE_PERM;
   763    766       
   764         -  /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
   765         -  ** this module never makes such a call. And the code in SQLite itself 
   766         -  ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
   767         -  ** this case is also commented out. If the system does set errno to EDEADLK,
   768         -  ** the default SQLITE_IOERR_XXX code will be returned. */
   769         -#if 0
   770         -  case EDEADLK:
   771         -    return SQLITE_IOERR_BLOCKED;
   772         -#endif
   773         -    
   774    767   #if EOPNOTSUPP!=ENOTSUP
   775    768     case EOPNOTSUPP: 
   776    769       /* something went terribly awry, unless during file system support 
   777    770        * introspection, in which it actually means what it says */
   778    771   #endif
   779    772   #ifdef ENOTSUP
   780    773     case ENOTSUP: 
................................................................................
  1299   1292     return SQLITE_OK;
  1300   1293   }
  1301   1294   
  1302   1295   /*
  1303   1296   ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
  1304   1297   */
  1305   1298   static int fileHasMoved(unixFile *pFile){
         1299  +#if OS_VXWORKS
         1300  +  return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
         1301  +#else
  1306   1302     struct stat buf;
  1307   1303     return pFile->pInode!=0 &&
  1308         -         (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
         1304  +      (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
         1305  +#endif
  1309   1306   }
  1310   1307   
  1311   1308   
  1312   1309   /*
  1313   1310   ** Check a unixFile that is a database.  Verify the following:
  1314   1311   **
  1315   1312   ** (1) There is exactly one hard link on the file
................................................................................
  2444   2441     if( pFile->eFileLock>SHARED_LOCK ){
  2445   2442       reserved = 1;
  2446   2443     }
  2447   2444     
  2448   2445     /* Otherwise see if some other process holds it. */
  2449   2446     if( !reserved ){
  2450   2447       sem_t *pSem = pFile->pInode->pSem;
  2451         -    struct stat statBuf;
  2452   2448   
  2453   2449       if( sem_trywait(pSem)==-1 ){
  2454   2450         int tErrno = errno;
  2455   2451         if( EAGAIN != tErrno ){
  2456   2452           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
  2457   2453           pFile->lastErrno = tErrno;
  2458   2454         } else {
................................................................................
  2497   2493   ** access the file.
  2498   2494   **
  2499   2495   ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
  2500   2496   ** routine to lower a locking level.
  2501   2497   */
  2502   2498   static int semLock(sqlite3_file *id, int eFileLock) {
  2503   2499     unixFile *pFile = (unixFile*)id;
  2504         -  int fd;
  2505   2500     sem_t *pSem = pFile->pInode->pSem;
  2506   2501     int rc = SQLITE_OK;
  2507   2502   
  2508   2503     /* if we already have a lock, it is exclusive.  
  2509   2504     ** Just adjust level and punt on outta here. */
  2510   2505     if (pFile->eFileLock > NO_LOCK) {
  2511   2506       pFile->eFileLock = eFileLock;
................................................................................
  5884   5879     const char *zPath,        /* Name of file to be deleted */
  5885   5880     int dirSync               /* If true, fsync() directory after deleting file */
  5886   5881   ){
  5887   5882     int rc = SQLITE_OK;
  5888   5883     UNUSED_PARAMETER(NotUsed);
  5889   5884     SimulateIOError(return SQLITE_IOERR_DELETE);
  5890   5885     if( osUnlink(zPath)==(-1) ){
  5891         -    if( errno==ENOENT ){
         5886  +    if( errno==ENOENT
         5887  +#if OS_VXWORKS
         5888  +        || errno==0x380003
         5889  +#endif
         5890  +    ){
  5892   5891         rc = SQLITE_IOERR_DELETE_NOENT;
  5893   5892       }else{
  5894   5893         rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
  5895   5894       }
  5896   5895       return rc;
  5897   5896     }
  5898   5897   #ifndef SQLITE_DISABLE_DIRSYNC

Changes to src/os_win.c.

    68     68   #endif
    69     69   
    70     70   #ifndef NTDDI_WINBLUE
    71     71   #  define NTDDI_WINBLUE                     0x06030000
    72     72   #endif
    73     73   
    74     74   /*
    75         -** Check if the GetVersionEx[AW] functions should be considered deprecated
    76         -** and avoid using them in that case.  It should be noted here that if the
    77         -** value of the SQLITE_WIN32_GETVERSIONEX pre-processor macro is zero
    78         -** (whether via this block or via being manually specified), that implies
    79         -** the underlying operating system will always be based on the Windows NT
    80         -** Kernel.
           75  +** Check to see if the GetVersionEx[AW] functions are deprecated on the
           76  +** target system.  GetVersionEx was first deprecated in Win8.1.
    81     77   */
    82     78   #ifndef SQLITE_WIN32_GETVERSIONEX
    83     79   #  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
    84         -#    define SQLITE_WIN32_GETVERSIONEX   0
           80  +#    define SQLITE_WIN32_GETVERSIONEX   0   /* GetVersionEx() is deprecated */
    85     81   #  else
    86         -#    define SQLITE_WIN32_GETVERSIONEX   1
           82  +#    define SQLITE_WIN32_GETVERSIONEX   1   /* GetVersionEx() is current */
    87     83   #  endif
    88     84   #endif
    89     85   
    90     86   /*
    91     87   ** This constant should already be defined (in the "WinDef.h" SDK file).
    92     88   */
    93     89   #ifndef MAX_PATH
................................................................................
   151    147   #endif
   152    148   
   153    149   /*
   154    150   ** This macro is used when a local variable is set to a value that is
   155    151   ** [sometimes] not used by the code (e.g. via conditional compilation).
   156    152   */
   157    153   #ifndef UNUSED_VARIABLE_VALUE
   158         -#  define UNUSED_VARIABLE_VALUE(x) (void)(x)
          154  +#  define UNUSED_VARIABLE_VALUE(x)      (void)(x)
   159    155   #endif
   160    156   
   161    157   /*
   162    158   ** Returns the character that should be used as the directory separator.
   163    159   */
   164    160   #ifndef winGetDirSep
   165    161   #  define winGetDirSep()                '\\'
................................................................................
   200    196   WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
   201    197   #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
   202    198   
   203    199   /*
   204    200   ** Some Microsoft compilers lack this definition.
   205    201   */
   206    202   #ifndef INVALID_FILE_ATTRIBUTES
   207         -# define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
          203  +# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
   208    204   #endif
   209    205   
   210    206   #ifndef FILE_FLAG_MASK
   211    207   # define FILE_FLAG_MASK          (0xFF3C0000)
   212    208   #endif
   213    209   
   214    210   #ifndef FILE_ATTRIBUTE_MASK
................................................................................
   250    246   #ifndef SQLITE_OMIT_WAL
   251    247     winShm *pShm;           /* Instance of shared memory on this file */
   252    248   #endif
   253    249     const char *zPath;      /* Full pathname of this file */
   254    250     int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
   255    251   #if SQLITE_OS_WINCE
   256    252     LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
   257         -  HANDLE hMutex;          /* Mutex used to control access to shared lock */  
          253  +  HANDLE hMutex;          /* Mutex used to control access to shared lock */
   258    254     HANDLE hShared;         /* Shared memory segment used for locking */
   259    255     winceLock local;        /* Locks obtained by this instance of winFile */
   260    256     winceLock *shared;      /* Global shared lock memory for the file  */
   261    257   #endif
   262    258   #if SQLITE_MAX_MMAP_SIZE>0
   263    259     int nFetchOut;                /* Number of outstanding xFetch references */
   264    260     HANDLE hMap;                  /* Handle for accessing memory mapping */
................................................................................
   410    406   ** 1:   Operating system is Win9x.
   411    407   ** 2:   Operating system is WinNT.
   412    408   **
   413    409   ** In order to facilitate testing on a WinNT system, the test fixture
   414    410   ** can manually set this value to 1 to emulate Win98 behavior.
   415    411   */
   416    412   #ifdef SQLITE_TEST
   417         -int sqlite3_os_type = 0;
   418         -#elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
   419         -      defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
   420         -static int sqlite3_os_type = 0;
          413  +LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
          414  +#else
          415  +static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
   421    416   #endif
   422    417   
   423    418   #ifndef SYSCALL
   424    419   #  define SYSCALL sqlite3_syscall_ptr
   425    420   #endif
   426    421   
   427    422   /*
................................................................................
  1044   1039   #else
  1045   1040     { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
  1046   1041   #endif
  1047   1042   
  1048   1043   #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
  1049   1044           LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
  1050   1045   
         1046  +/*
         1047  +** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
         1048  +**       is really just a macro that uses a compiler intrinsic (e.g. x64).
         1049  +**       So do not try to make this is into a redefinable interface.
         1050  +*/
         1051  +#if defined(InterlockedCompareExchange)
         1052  +  { "InterlockedCompareExchange", (SYSCALL)0,                    0 },
         1053  +
         1054  +#define osInterlockedCompareExchange InterlockedCompareExchange
         1055  +#else
         1056  +  { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
         1057  +
         1058  +#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
         1059  +        SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
         1060  +#endif /* defined(InterlockedCompareExchange) */
         1061  +
  1051   1062   }; /* End of the overrideable system calls */
  1052   1063   
  1053   1064   /*
  1054   1065   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
  1055   1066   ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
  1056   1067   ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
  1057   1068   ** system call named zName.
................................................................................
  1294   1305   #if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
  1295   1306   # define osIsNT()  (1)
  1296   1307   #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
  1297   1308   # define osIsNT()  (1)
  1298   1309   #elif !defined(SQLITE_WIN32_HAS_WIDE)
  1299   1310   # define osIsNT()  (0)
  1300   1311   #else
  1301         -  static int osIsNT(void){
  1302         -    if( sqlite3_os_type==0 ){
  1303         -#if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
  1304         -      OSVERSIONINFOW sInfo;
  1305         -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1306         -      osGetVersionExW(&sInfo);
  1307         -#else
  1308         -      OSVERSIONINFOA sInfo;
  1309         -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1310         -      osGetVersionExA(&sInfo);
  1311         -#endif
  1312         -      sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
  1313         -    }
  1314         -    return sqlite3_os_type==2;
  1315         -  }
  1316         -#endif
         1312  +# define osIsNT()  ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
         1313  +#endif
         1314  +
         1315  +/*
         1316  +** This function determines if the machine is running a version of Windows
         1317  +** based on the NT kernel.
         1318  +*/
         1319  +int sqlite3_win32_is_nt(void){
         1320  +#if defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
         1321  +  if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
         1322  +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
         1323  +        defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
         1324  +    OSVERSIONINFOW sInfo;
         1325  +    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
         1326  +    osGetVersionExW(&sInfo);
         1327  +    osInterlockedCompareExchange(&sqlite3_os_type,
         1328  +        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
         1329  +#elif defined(SQLITE_WIN32_HAS_ANSI)
         1330  +    OSVERSIONINFOA sInfo;
         1331  +    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
         1332  +    osGetVersionExA(&sInfo);
         1333  +    osInterlockedCompareExchange(&sqlite3_os_type,
         1334  +        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
         1335  +#endif
         1336  +  }
         1337  +  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
         1338  +#elif SQLITE_TEST
         1339  +  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
         1340  +#else
         1341  +  return 1;
         1342  +#endif
         1343  +}
  1317   1344   
  1318   1345   #ifdef SQLITE_WIN32_MALLOC
  1319   1346   /*
  1320   1347   ** Allocate nBytes of memory.
  1321   1348   */
  1322   1349   static void *winMemMalloc(int nBytes){
  1323   1350     HANDLE hHeap;
................................................................................
  1517   1544   
  1518   1545   void sqlite3MemSetDefault(void){
  1519   1546     sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
  1520   1547   }
  1521   1548   #endif /* SQLITE_WIN32_MALLOC */
  1522   1549   
  1523   1550   /*
  1524         -** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). 
         1551  +** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
  1525   1552   **
  1526   1553   ** Space to hold the returned string is obtained from malloc.
  1527   1554   */
  1528   1555   static LPWSTR winUtf8ToUnicode(const char *zFilename){
  1529   1556     int nChar;
  1530   1557     LPWSTR zWideFilename;
  1531   1558   
................................................................................
  1570   1597     }
  1571   1598     return zFilename;
  1572   1599   }
  1573   1600   
  1574   1601   /*
  1575   1602   ** Convert an ANSI string to Microsoft Unicode, based on the
  1576   1603   ** current codepage settings for file apis.
  1577         -** 
         1604  +**
  1578   1605   ** Space to hold the returned string is obtained
  1579   1606   ** from sqlite3_malloc.
  1580   1607   */
  1581   1608   static LPWSTR winMbcsToUnicode(const char *zFilename){
  1582   1609     int nByte;
  1583   1610     LPWSTR zMbcsFilename;
  1584   1611     int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
................................................................................
  1644   1671     }
  1645   1672     zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
  1646   1673     sqlite3_free(zTmpWide);
  1647   1674     return zFilenameUtf8;
  1648   1675   }
  1649   1676   
  1650   1677   /*
  1651         -** Convert UTF-8 to multibyte character string.  Space to hold the 
         1678  +** Convert UTF-8 to multibyte character string.  Space to hold the
  1652   1679   ** returned string is obtained from sqlite3_malloc().
  1653   1680   */
  1654   1681   char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
  1655   1682     char *zFilenameMbcs;
  1656   1683     LPWSTR zTmpWide;
  1657   1684   
  1658   1685     zTmpWide = winUtf8ToUnicode(zFilename);
................................................................................
  1784   1811   /*
  1785   1812   **
  1786   1813   ** This function - winLogErrorAtLine() - is only ever called via the macro
  1787   1814   ** winLogError().
  1788   1815   **
  1789   1816   ** This routine is invoked after an error occurs in an OS function.
  1790   1817   ** It logs a message using sqlite3_log() containing the current value of
  1791         -** error code and, if possible, the human-readable equivalent from 
         1818  +** error code and, if possible, the human-readable equivalent from
  1792   1819   ** FormatMessage.
  1793   1820   **
  1794   1821   ** The first argument passed to the macro should be the error code that
  1795         -** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
         1822  +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
  1796   1823   ** The two subsequent arguments should be the name of the OS function that
  1797   1824   ** failed and the associated file-system path, if any.
  1798   1825   */
  1799   1826   #define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
  1800   1827   static int winLogErrorAtLine(
  1801   1828     int errcode,                    /* SQLite error code */
  1802   1829     DWORD lastErrno,                /* Win32 last error */
................................................................................
  1819   1846     );
  1820   1847   
  1821   1848     return errcode;
  1822   1849   }
  1823   1850   
  1824   1851   /*
  1825   1852   ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
  1826         -** will be retried following a locking error - probably caused by 
         1853  +** will be retried following a locking error - probably caused by
  1827   1854   ** antivirus software.  Also the initial delay before the first retry.
  1828   1855   ** The delay increases linearly with each retry.
  1829   1856   */
  1830   1857   #ifndef SQLITE_WIN32_IOERR_RETRY
  1831   1858   # define SQLITE_WIN32_IOERR_RETRY 10
  1832   1859   #endif
  1833   1860   #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
................................................................................
  1894   1921   }
  1895   1922   
  1896   1923   /*
  1897   1924   ** Log a I/O error retry episode.
  1898   1925   */
  1899   1926   static void winLogIoerr(int nRetry){
  1900   1927     if( nRetry ){
  1901         -    sqlite3_log(SQLITE_IOERR, 
         1928  +    sqlite3_log(SQLITE_IOERR,
  1902   1929         "delayed %dms for lock/sharing conflict",
  1903   1930         winIoerrRetryDelay*nRetry*(nRetry+1)/2
  1904   1931       );
  1905   1932     }
  1906   1933   }
  1907   1934   
  1908   1935   #if SQLITE_OS_WINCE
................................................................................
  1988   2015       sqlite3_free(zName);
  1989   2016       return winLogError(SQLITE_IOERR, pFile->lastErrno,
  1990   2017                          "winceCreateLock1", zFilename);
  1991   2018     }
  1992   2019   
  1993   2020     /* Acquire the mutex before continuing */
  1994   2021     winceMutexAcquire(pFile->hMutex);
  1995         -  
  1996         -  /* Since the names of named mutexes, semaphores, file mappings etc are 
         2022  +
         2023  +  /* Since the names of named mutexes, semaphores, file mappings etc are
  1997   2024     ** case-sensitive, take advantage of that by uppercasing the mutex name
  1998   2025     ** and using that as the shared filemapping name.
  1999   2026     */
  2000   2027     osCharUpperW(zName);
  2001   2028     pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  2002   2029                                           PAGE_READWRITE, 0, sizeof(winceLock),
  2003         -                                        zName);  
         2030  +                                        zName);
  2004   2031   
  2005         -  /* Set a flag that indicates we're the first to create the memory so it 
         2032  +  /* Set a flag that indicates we're the first to create the memory so it
  2006   2033     ** must be zero-initialized */
  2007   2034     lastErrno = osGetLastError();
  2008   2035     if (lastErrno == ERROR_ALREADY_EXISTS){
  2009   2036       bInit = FALSE;
  2010   2037     }
  2011   2038   
  2012   2039     sqlite3_free(zName);
  2013   2040   
  2014   2041     /* If we succeeded in making the shared memory handle, map it. */
  2015   2042     if( pFile->hShared ){
  2016         -    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
         2043  +    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
  2017   2044                FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
  2018   2045       /* If mapping failed, close the shared memory handle and erase it */
  2019   2046       if( !pFile->shared ){
  2020   2047         pFile->lastErrno = osGetLastError();
  2021   2048         winLogError(SQLITE_IOERR, pFile->lastErrno,
  2022   2049                     "winceCreateLock2", zFilename);
  2023   2050         bLogged = TRUE;
................................................................................
  2035   2062         bLogged = TRUE;
  2036   2063       }
  2037   2064       winceMutexRelease(pFile->hMutex);
  2038   2065       osCloseHandle(pFile->hMutex);
  2039   2066       pFile->hMutex = NULL;
  2040   2067       return SQLITE_IOERR;
  2041   2068     }
  2042         -  
         2069  +
  2043   2070     /* Initialize the shared memory if we're supposed to */
  2044   2071     if( bInit ){
  2045   2072       memset(pFile->shared, 0, sizeof(winceLock));
  2046   2073     }
  2047   2074   
  2048   2075     winceMutexRelease(pFile->hMutex);
  2049   2076     return SQLITE_OK;
................................................................................
  2073   2100       }
  2074   2101   
  2075   2102       /* De-reference and close our copy of the shared memory handle */
  2076   2103       osUnmapViewOfFile(pFile->shared);
  2077   2104       osCloseHandle(pFile->hShared);
  2078   2105   
  2079   2106       /* Done with the mutex */
  2080         -    winceMutexRelease(pFile->hMutex);    
         2107  +    winceMutexRelease(pFile->hMutex);
  2081   2108       osCloseHandle(pFile->hMutex);
  2082   2109       pFile->hMutex = NULL;
  2083   2110     }
  2084   2111   }
  2085   2112   
  2086         -/* 
         2113  +/*
  2087   2114   ** An implementation of the LockFile() API of Windows for CE
  2088   2115   */
  2089   2116   static BOOL winceLockFile(
  2090   2117     LPHANDLE phFile,
  2091   2118     DWORD dwFileOffsetLow,
  2092   2119     DWORD dwFileOffsetHigh,
  2093   2120     DWORD nNumberOfBytesToLockLow,
................................................................................
  2290   2317   ** Some Microsoft compilers lack this definition.
  2291   2318   */
  2292   2319   #ifndef INVALID_SET_FILE_POINTER
  2293   2320   # define INVALID_SET_FILE_POINTER ((DWORD)-1)
  2294   2321   #endif
  2295   2322   
  2296   2323   /*
  2297         -** Move the current position of the file handle passed as the first 
  2298         -** argument to offset iOffset within the file. If successful, return 0. 
         2324  +** Move the current position of the file handle passed as the first
         2325  +** argument to offset iOffset within the file. If successful, return 0.
  2299   2326   ** Otherwise, set pFile->lastErrno and return non-zero.
  2300   2327   */
  2301   2328   static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
  2302   2329   #if !SQLITE_OS_WINRT
  2303   2330     LONG upperBits;                 /* Most sig. 32 bits of new offset */
  2304   2331     LONG lowerBits;                 /* Least sig. 32 bits of new offset */
  2305   2332     DWORD dwRet;                    /* Value returned by SetFilePointer() */
................................................................................
  2306   2333     DWORD lastErrno;                /* Value returned by GetLastError() */
  2307   2334   
  2308   2335     OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
  2309   2336   
  2310   2337     upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
  2311   2338     lowerBits = (LONG)(iOffset & 0xffffffff);
  2312   2339   
  2313         -  /* API oddity: If successful, SetFilePointer() returns a dword 
         2340  +  /* API oddity: If successful, SetFilePointer() returns a dword
  2314   2341     ** containing the lower 32-bits of the new file-offset. Or, if it fails,
  2315         -  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, 
  2316         -  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine 
  2317         -  ** whether an error has actually occurred, it is also necessary to call 
         2342  +  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
         2343  +  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
         2344  +  ** whether an error has actually occurred, it is also necessary to call
  2318   2345     ** GetLastError().
  2319   2346     */
  2320   2347     dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
  2321   2348   
  2322   2349     if( (dwRet==INVALID_SET_FILE_POINTER
  2323   2350         && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
  2324   2351       pFile->lastErrno = lastErrno;
................................................................................
  2393   2420   #if SQLITE_OS_WINCE
  2394   2421   #define WINCE_DELETION_ATTEMPTS 3
  2395   2422     winceDestroyLock(pFile);
  2396   2423     if( pFile->zDeleteOnClose ){
  2397   2424       int cnt = 0;
  2398   2425       while(
  2399   2426              osDeleteFileW(pFile->zDeleteOnClose)==0
  2400         -        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
         2427  +        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
  2401   2428           && cnt++ < WINCE_DELETION_ATTEMPTS
  2402   2429       ){
  2403   2430          sqlite3_win32_sleep(100);  /* Wait a little before trying again */
  2404   2431       }
  2405   2432       sqlite3_free(pFile->zDeleteOnClose);
  2406   2433     }
  2407   2434   #endif
................................................................................
  3241   3268   */
  3242   3269   static int winDeviceCharacteristics(sqlite3_file *id){
  3243   3270     winFile *p = (winFile*)id;
  3244   3271     return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
  3245   3272            ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
  3246   3273   }
  3247   3274   
  3248         -/* 
         3275  +/*
  3249   3276   ** Windows will only let you create file view mappings
  3250   3277   ** on allocation size granularity boundaries.
  3251   3278   ** During sqlite3_os_init() we do a GetSystemInfo()
  3252   3279   ** to get the granularity size.
  3253   3280   */
  3254   3281   static SYSTEM_INFO winSysInfo;
  3255   3282   
  3256   3283   #ifndef SQLITE_OMIT_WAL
  3257   3284   
  3258   3285   /*
  3259   3286   ** Helper functions to obtain and relinquish the global mutex. The
  3260         -** global mutex is used to protect the winLockInfo objects used by 
         3287  +** global mutex is used to protect the winLockInfo objects used by
  3261   3288   ** this file, all of which may be shared by multiple threads.
  3262   3289   **
  3263         -** Function winShmMutexHeld() is used to assert() that the global mutex 
  3264         -** is held when required. This function is only used as part of assert() 
         3290  +** Function winShmMutexHeld() is used to assert() that the global mutex
         3291  +** is held when required. This function is only used as part of assert()
  3265   3292   ** statements. e.g.
  3266   3293   **
  3267   3294   **   winShmEnterMutex()
  3268   3295   **     assert( winShmMutexHeld() );
  3269   3296   **   winShmLeaveMutex()
  3270   3297   */
  3271   3298   static void winShmEnterMutex(void){
................................................................................
  3287   3314   ** point to a single instance of this object.  In other words, each
  3288   3315   ** log-summary is opened only once per process.
  3289   3316   **
  3290   3317   ** winShmMutexHeld() must be true when creating or destroying
  3291   3318   ** this object or while reading or writing the following fields:
  3292   3319   **
  3293   3320   **      nRef
  3294         -**      pNext 
         3321  +**      pNext
  3295   3322   **
  3296   3323   ** The following fields are read-only after the object is created:
  3297         -** 
         3324  +**
  3298   3325   **      fid
  3299   3326   **      zFilename
  3300   3327   **
  3301   3328   ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
  3302   3329   ** winShmMutexHeld() is true when reading or writing any other field
  3303   3330   ** in this structure.
  3304   3331   **
................................................................................
  3386   3413       rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
  3387   3414     }else{
  3388   3415       /* Initialize the locking parameters */
  3389   3416       DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
  3390   3417       if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
  3391   3418       rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
  3392   3419     }
  3393         -  
         3420  +
  3394   3421     if( rc!= 0 ){
  3395   3422       rc = SQLITE_OK;
  3396   3423     }else{
  3397   3424       pFile->lastErrno =  osGetLastError();
  3398   3425       rc = SQLITE_BUSY;
  3399   3426     }
  3400   3427   
................................................................................
  3482   3509     pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
  3483   3510     if( pNew==0 ){
  3484   3511       sqlite3_free(p);
  3485   3512       return SQLITE_IOERR_NOMEM;
  3486   3513     }
  3487   3514     pNew->zFilename = (char*)&pNew[1];
  3488   3515     sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  3489         -  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
         3516  +  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
  3490   3517   
  3491   3518     /* Look to see if there is an existing winShmNode that can be used.
  3492   3519     ** If no matching winShmNode currently exists, create a new one.
  3493   3520     */
  3494   3521     winShmEnterMutex();
  3495   3522     for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
  3496   3523       /* TBD need to come up with better match here.  Perhaps
................................................................................
  3519   3546                    SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
  3520   3547                    0);
  3521   3548       if( SQLITE_OK!=rc ){
  3522   3549         goto shm_open_err;
  3523   3550       }
  3524   3551   
  3525   3552       /* Check to see if another process is holding the dead-man switch.
  3526         -    ** If not, truncate the file to zero length. 
         3553  +    ** If not, truncate the file to zero length.
  3527   3554       */
  3528   3555       if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
  3529   3556         rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
  3530   3557         if( rc!=SQLITE_OK ){
  3531   3558           rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
  3532   3559                            "winOpenShm", pDbFd->zPath);
  3533   3560         }
................................................................................
  3548   3575     pDbFd->pShm = p;
  3549   3576     winShmLeaveMutex();
  3550   3577   
  3551   3578     /* The reference count on pShmNode has already been incremented under
  3552   3579     ** the cover of the winShmEnterMutex() mutex and the pointer from the
  3553   3580     ** new (struct winShm) object to the pShmNode has been set. All that is
  3554   3581     ** left to do is to link the new object into the linked list starting
  3555         -  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
         3582  +  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
  3556   3583     ** mutex.
  3557   3584     */
  3558   3585     sqlite3_mutex_enter(pShmNode->mutex);
  3559   3586     p->pNext = pShmNode->pFirst;
  3560   3587     pShmNode->pFirst = p;
  3561   3588     sqlite3_mutex_leave(pShmNode->mutex);
  3562   3589     return SQLITE_OK;
................................................................................
  3568   3595     sqlite3_free(p);
  3569   3596     sqlite3_free(pNew);
  3570   3597     winShmLeaveMutex();
  3571   3598     return rc;
  3572   3599   }
  3573   3600   
  3574   3601   /*
  3575         -** Close a connection to shared-memory.  Delete the underlying 
         3602  +** Close a connection to shared-memory.  Delete the underlying
  3576   3603   ** storage if deleteFlag is true.
  3577   3604   */
  3578   3605   static int winShmUnmap(
  3579   3606     sqlite3_file *fd,          /* Database holding shared memory */
  3580   3607     int deleteFlag             /* Delete after closing if true */
  3581   3608   ){
  3582   3609     winFile *pDbFd;       /* Database holding shared-memory */
................................................................................
  3657   3684         rc = SQLITE_OK;
  3658   3685       }
  3659   3686   
  3660   3687       /* Undo the local locks */
  3661   3688       if( rc==SQLITE_OK ){
  3662   3689         p->exclMask &= ~mask;
  3663   3690         p->sharedMask &= ~mask;
  3664         -    } 
         3691  +    }
  3665   3692     }else if( flags & SQLITE_SHM_SHARED ){
  3666   3693       u16 allShared = 0;  /* Union of locks held by connections other than "p" */
  3667   3694   
  3668   3695       /* Find out which shared locks are already held by sibling connections.
  3669   3696       ** If any sibling already holds an exclusive lock, go ahead and return
  3670   3697       ** SQLITE_BUSY.
  3671   3698       */
................................................................................
  3696   3723       */
  3697   3724       for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
  3698   3725         if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
  3699   3726           rc = SQLITE_BUSY;
  3700   3727           break;
  3701   3728         }
  3702   3729       }
  3703         -  
         3730  +
  3704   3731       /* Get the exclusive locks at the system level.  Then if successful
  3705   3732       ** also mark the local connection as being locked.
  3706   3733       */
  3707   3734       if( rc==SQLITE_OK ){
  3708   3735         rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
  3709   3736         if( rc==SQLITE_OK ){
  3710   3737           assert( (p->sharedMask & mask)==0 );
................................................................................
  3716   3743     OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
  3717   3744              osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
  3718   3745              sqlite3ErrName(rc)));
  3719   3746     return rc;
  3720   3747   }
  3721   3748   
  3722   3749   /*
  3723         -** Implement a memory barrier or memory fence on shared memory.  
         3750  +** Implement a memory barrier or memory fence on shared memory.
  3724   3751   **
  3725   3752   ** All loads and stores begun before the barrier must complete before
  3726   3753   ** any load or store begun after the barrier.
  3727   3754   */
  3728   3755   static void winShmBarrier(
  3729   3756     sqlite3_file *fd          /* Database holding the shared memory */
  3730   3757   ){
................................................................................
  3731   3758     UNUSED_PARAMETER(fd);
  3732   3759     /* MemoryBarrier(); // does not work -- do not know why not */
  3733   3760     winShmEnterMutex();
  3734   3761     winShmLeaveMutex();
  3735   3762   }
  3736   3763   
  3737   3764   /*
  3738         -** This function is called to obtain a pointer to region iRegion of the 
  3739         -** shared-memory associated with the database file fd. Shared-memory regions 
  3740         -** are numbered starting from zero. Each shared-memory region is szRegion 
         3765  +** This function is called to obtain a pointer to region iRegion of the
         3766  +** shared-memory associated with the database file fd. Shared-memory regions
         3767  +** are numbered starting from zero. Each shared-memory region is szRegion
  3741   3768   ** bytes in size.
  3742   3769   **
  3743   3770   ** If an error occurs, an error code is returned and *pp is set to NULL.
  3744   3771   **
  3745   3772   ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
  3746   3773   ** region has not been allocated (by any client, including one running in a
  3747         -** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
  3748         -** isWrite is non-zero and the requested shared-memory region has not yet 
         3774  +** separate process), then *pp is set to NULL and SQLITE_OK returned. If
         3775  +** isWrite is non-zero and the requested shared-memory region has not yet
  3749   3776   ** been allocated, it is allocated by this function.
  3750   3777   **
  3751   3778   ** If the shared-memory region has already been allocated or is allocated by
  3752         -** this call as described above, then it is mapped into this processes 
  3753         -** address space (if it is not already), *pp is set to point to the mapped 
         3779  +** this call as described above, then it is mapped into this processes
         3780  +** address space (if it is not already), *pp is set to point to the mapped
  3754   3781   ** memory and SQLITE_OK returned.
  3755   3782   */
  3756   3783   static int winShmMap(
  3757   3784     sqlite3_file *fd,               /* Handle open on database file */
  3758   3785     int iRegion,                    /* Region to retrieve */
  3759   3786     int szRegion,                   /* Size of regions */
  3760   3787     int isWrite,                    /* True to extend file if necessary */
................................................................................
  3818   3845         goto shmpage_out;
  3819   3846       }
  3820   3847       pShmNode->aRegion = apNew;
  3821   3848   
  3822   3849       while( pShmNode->nRegion<=iRegion ){
  3823   3850         HANDLE hMap = NULL;         /* file-mapping handle */
  3824   3851         void *pMap = 0;             /* Mapped memory region */
  3825         -     
         3852  +
  3826   3853   #if SQLITE_OS_WINRT
  3827   3854         hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
  3828   3855             NULL, PAGE_READWRITE, nByte, NULL
  3829   3856         );
  3830   3857   #elif defined(SQLITE_WIN32_HAS_WIDE)
  3831         -      hMap = osCreateFileMappingW(pShmNode->hFile.h, 
         3858  +      hMap = osCreateFileMappingW(pShmNode->hFile.h,
  3832   3859             NULL, PAGE_READWRITE, 0, nByte, NULL
  3833   3860         );
  3834   3861   #elif defined(SQLITE_WIN32_HAS_ANSI)
  3835         -      hMap = osCreateFileMappingA(pShmNode->hFile.h, 
         3862  +      hMap = osCreateFileMappingA(pShmNode->hFile.h,
  3836   3863             NULL, PAGE_READWRITE, 0, nByte, NULL
  3837   3864         );
  3838   3865   #endif
  3839   3866         OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
  3840   3867                  osGetCurrentProcessId(), pShmNode->nRegion, nByte,
  3841   3868                  hMap ? "ok" : "failed"));
  3842   3869         if( hMap ){
................................................................................
  3925   3952     OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
  3926   3953              osGetCurrentProcessId(), pFile));
  3927   3954     return SQLITE_OK;
  3928   3955   }
  3929   3956   
  3930   3957   /*
  3931   3958   ** Memory map or remap the file opened by file-descriptor pFd (if the file
  3932         -** is already mapped, the existing mapping is replaced by the new). Or, if 
  3933         -** there already exists a mapping for this file, and there are still 
         3959  +** is already mapped, the existing mapping is replaced by the new). Or, if
         3960  +** there already exists a mapping for this file, and there are still
  3934   3961   ** outstanding xFetch() references to it, this function is a no-op.
  3935   3962   **
  3936         -** If parameter nByte is non-negative, then it is the requested size of 
  3937         -** the mapping to create. Otherwise, if nByte is less than zero, then the 
         3963  +** If parameter nByte is non-negative, then it is the requested size of
         3964  +** the mapping to create. Otherwise, if nByte is less than zero, then the
  3938   3965   ** requested size is the size of the file on disk. The actual size of the
  3939         -** created mapping is either the requested size or the value configured 
         3966  +** created mapping is either the requested size or the value configured
  3940   3967   ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
  3941   3968   **
  3942   3969   ** SQLITE_OK is returned if no error occurs (even if the mapping is not
  3943   3970   ** recreated as a result of outstanding references) or an SQLite error
  3944   3971   ** code otherwise.
  3945   3972   */
  3946   3973   static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
................................................................................
  3961   3988         return SQLITE_IOERR_FSTAT;
  3962   3989       }
  3963   3990     }
  3964   3991     if( nMap>pFd->mmapSizeMax ){
  3965   3992       nMap = pFd->mmapSizeMax;
  3966   3993     }
  3967   3994     nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
  3968         - 
         3995  +
  3969   3996     if( nMap==0 && pFd->mmapSize>0 ){
  3970   3997       winUnmapfile(pFd);
  3971   3998     }
  3972   3999     if( nMap!=pFd->mmapSize ){
  3973   4000       void *pNew = 0;
  3974   4001       DWORD protect = PAGE_READONLY;
  3975   4002       DWORD flags = FILE_MAP_READ;
................................................................................
  4033   4060   ** iOff. The mapping must be valid for at least nAmt bytes.
  4034   4061   **
  4035   4062   ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
  4036   4063   ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
  4037   4064   ** Finally, if an error does occur, return an SQLite error code. The final
  4038   4065   ** value of *pp is undefined in this case.
  4039   4066   **
  4040         -** If this function does return a pointer, the caller must eventually 
         4067  +** If this function does return a pointer, the caller must eventually
  4041   4068   ** release the reference by calling winUnfetch().
  4042   4069   */
  4043   4070   static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
  4044   4071   #if SQLITE_MAX_MMAP_SIZE>0
  4045   4072     winFile *pFd = (winFile*)fd;   /* The underlying database file */
  4046   4073   #endif
  4047   4074     *pp = 0;
................................................................................
  4068   4095   
  4069   4096     OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
  4070   4097              osGetCurrentProcessId(), fd, pp, *pp));
  4071   4098     return SQLITE_OK;
  4072   4099   }
  4073   4100   
  4074   4101   /*
  4075         -** If the third argument is non-NULL, then this function releases a 
         4102  +** If the third argument is non-NULL, then this function releases a
  4076   4103   ** reference obtained by an earlier call to winFetch(). The second
  4077   4104   ** argument passed to this function must be the same as the corresponding
  4078         -** argument that was passed to the winFetch() invocation. 
         4105  +** argument that was passed to the winFetch() invocation.
  4079   4106   **
  4080         -** Or, if the third argument is NULL, then this function is being called 
  4081         -** to inform the VFS layer that, according to POSIX, any existing mapping 
         4107  +** Or, if the third argument is NULL, then this function is being called
         4108  +** to inform the VFS layer that, according to POSIX, any existing mapping
  4082   4109   ** may now be invalid and should be unmapped.
  4083   4110   */
  4084   4111   static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
  4085   4112   #if SQLITE_MAX_MMAP_SIZE>0
  4086   4113     winFile *pFd = (winFile*)fd;   /* The underlying database file */
  4087   4114   
  4088         -  /* If p==0 (unmap the entire file) then there must be no outstanding 
         4115  +  /* If p==0 (unmap the entire file) then there must be no outstanding
  4089   4116     ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
  4090   4117     ** then there must be at least one outstanding.  */
  4091   4118     assert( (p==0)==(pFd->nFetchOut==0) );
  4092   4119   
  4093   4120     /* If p!=0, it must match the iOff value. */
  4094   4121     assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
  4095   4122   
................................................................................
  4227   4254     size_t i, j;
  4228   4255     int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
  4229   4256     int nMax, nBuf, nDir, nLen;
  4230   4257     char *zBuf;
  4231   4258   
  4232   4259     /* It's odd to simulate an io-error here, but really this is just
  4233   4260     ** using the io-error infrastructure to test that SQLite handles this
  4234         -  ** function failing. 
         4261  +  ** function failing.
  4235   4262     */
  4236   4263     SimulateIOError( return SQLITE_IOERR );
  4237   4264   
  4238   4265     /* Allocate a temporary buffer to store the fully qualified file
  4239   4266     ** name for the temporary file.  If this fails, we cannot continue.
  4240   4267     */
  4241   4268     nMax = pVfs->mxPathname; nBuf = nMax + 2;
................................................................................
  4409   4436     if( !winMakeEndInDirSep(nDir+1, zBuf) ){
  4410   4437       sqlite3_free(zBuf);
  4411   4438       OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
  4412   4439       return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
  4413   4440     }
  4414   4441   
  4415   4442     /*
  4416         -  ** Check that the output buffer is large enough for the temporary file 
         4443  +  ** Check that the output buffer is large enough for the temporary file
  4417   4444     ** name in the following format:
  4418   4445     **
  4419   4446     **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
  4420   4447     **
  4421   4448     ** If not, return SQLITE_ERROR.  The number 17 is used here in order to
  4422   4449     ** account for the space used by the 15 character random suffix and the
  4423   4450     ** two trailing NUL characters.  The final directory separator character
................................................................................
  4512   4539     int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  4513   4540     int isCreate     = (flags & SQLITE_OPEN_CREATE);
  4514   4541     int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  4515   4542     int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
  4516   4543   
  4517   4544   #ifndef NDEBUG
  4518   4545     int isOpenJournal = (isCreate && (
  4519         -        eType==SQLITE_OPEN_MASTER_JOURNAL 
  4520         -     || eType==SQLITE_OPEN_MAIN_JOURNAL 
         4546  +        eType==SQLITE_OPEN_MASTER_JOURNAL
         4547  +     || eType==SQLITE_OPEN_MAIN_JOURNAL
  4521   4548        || eType==SQLITE_OPEN_WAL
  4522   4549     ));
  4523   4550   #endif
  4524   4551   
  4525   4552     OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
  4526   4553              zUtf8Name, id, flags, pOutFlags));
  4527   4554   
  4528         -  /* Check the following statements are true: 
         4555  +  /* Check the following statements are true:
  4529   4556     **
  4530         -  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
         4557  +  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
  4531   4558     **   (b) if CREATE is set, then READWRITE must also be set, and
  4532   4559     **   (c) if EXCLUSIVE is set, then CREATE must also be set.
  4533   4560     **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  4534   4561     */
  4535   4562     assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
  4536   4563     assert(isCreate==0 || isReadWrite);
  4537   4564     assert(isExclusive==0 || isCreate);
  4538   4565     assert(isDelete==0 || isCreate);
  4539   4566   
  4540         -  /* The main DB, main journal, WAL file and master journal are never 
         4567  +  /* The main DB, main journal, WAL file and master journal are never
  4541   4568     ** automatically deleted. Nor are they ever temporary files.  */
  4542   4569     assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
  4543   4570     assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
  4544   4571     assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
  4545   4572     assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
  4546   4573   
  4547   4574     /* Assert that the upper layer has set one of the "file-type" flags. */
  4548         -  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
  4549         -       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
  4550         -       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
         4575  +  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
         4576  +       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
         4577  +       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
  4551   4578          || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  4552   4579     );
  4553   4580   
  4554   4581     assert( pFile!=0 );
  4555   4582     memset(pFile, 0, sizeof(winFile));
  4556   4583     pFile->h = INVALID_HANDLE_VALUE;
  4557   4584   
................................................................................
  4558   4585   #if SQLITE_OS_WINRT
  4559   4586     if( !zUtf8Name && !sqlite3_temp_directory ){
  4560   4587       sqlite3_log(SQLITE_ERROR,
  4561   4588           "sqlite3_temp_directory variable should be set for WinRT");
  4562   4589     }
  4563   4590   #endif
  4564   4591   
  4565         -  /* If the second argument to this function is NULL, generate a 
  4566         -  ** temporary file name to use 
         4592  +  /* If the second argument to this function is NULL, generate a
         4593  +  ** temporary file name to use
  4567   4594     */
  4568   4595     if( !zUtf8Name ){
  4569   4596       assert( isDelete && !isOpenJournal );
  4570   4597       rc = winGetTempname(pVfs, &zTmpname);
  4571   4598       if( rc!=SQLITE_OK ){
  4572   4599         OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
  4573   4600         return rc;
................................................................................
  4599   4626   
  4600   4627     if( isReadWrite ){
  4601   4628       dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
  4602   4629     }else{
  4603   4630       dwDesiredAccess = GENERIC_READ;
  4604   4631     }
  4605   4632   
  4606         -  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
  4607         -  ** created. SQLite doesn't use it to indicate "exclusive access" 
         4633  +  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
         4634  +  ** created. SQLite doesn't use it to indicate "exclusive access"
  4608   4635     ** as it is usually understood.
  4609   4636     */
  4610   4637     if( isExclusive ){
  4611   4638       /* Creates a new file, only if it does not already exist. */
  4612   4639       /* If the file exists, it fails. */
  4613   4640       dwCreationDisposition = CREATE_NEW;
  4614   4641     }else if( isCreate ){
................................................................................
  4689   4716   
  4690   4717     if( h==INVALID_HANDLE_VALUE ){
  4691   4718       pFile->lastErrno = lastErrno;
  4692   4719       winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
  4693   4720       sqlite3_free(zConverted);
  4694   4721       sqlite3_free(zTmpname);
  4695   4722       if( isReadWrite && !isExclusive ){
  4696         -      return winOpen(pVfs, zName, id, 
         4723  +      return winOpen(pVfs, zName, id,
  4697   4724            ((flags|SQLITE_OPEN_READONLY) &
  4698   4725                        ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
  4699   4726            pOutFlags);
  4700   4727       }else{
  4701   4728         return SQLITE_CANTOPEN_BKPT;
  4702   4729       }
  4703   4730     }
................................................................................
  4898   4925       return SQLITE_IOERR_NOMEM;
  4899   4926     }
  4900   4927     if( osIsNT() ){
  4901   4928       int cnt = 0;
  4902   4929       WIN32_FILE_ATTRIBUTE_DATA sAttrData;
  4903   4930       memset(&sAttrData, 0, sizeof(sAttrData));
  4904   4931       while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
  4905         -                             GetFileExInfoStandard, 
         4932  +                             GetFileExInfoStandard,
  4906   4933                                &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
  4907   4934       if( rc ){
  4908   4935         /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
  4909   4936         ** as if it does not exist.
  4910   4937         */
  4911   4938         if(    flags==SQLITE_ACCESS_EXISTS
  4912         -          && sAttrData.nFileSizeHigh==0 
         4939  +          && sAttrData.nFileSizeHigh==0
  4913   4940             && sAttrData.nFileSizeLow==0 ){
  4914   4941           attr = INVALID_FILE_ATTRIBUTES;
  4915   4942         }else{
  4916   4943           attr = sAttrData.dwFileAttributes;
  4917   4944         }
  4918   4945       }else{
  4919   4946         winLogIoerr(cnt);
................................................................................
  5004   5031   */
  5005   5032   static int winFullPathname(
  5006   5033     sqlite3_vfs *pVfs,            /* Pointer to vfs object */
  5007   5034     const char *zRelative,        /* Possibly relative input path */
  5008   5035     int nFull,                    /* Size of output buffer in bytes */
  5009   5036     char *zFull                   /* Output buffer */
  5010   5037   ){
  5011         -  
         5038  +
  5012   5039   #if defined(__CYGWIN__)
  5013   5040     SimulateIOError( return SQLITE_ERROR );
  5014   5041     UNUSED_PARAMETER(nFull);
  5015   5042     assert( nFull>=pVfs->mxPathname );
  5016   5043     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
  5017   5044       /*
  5018   5045       ** NOTE: We are dealing with a relative path name and the data
................................................................................
  5317   5344   /*
  5318   5345   ** Find the current time (in Universal Coordinated Time).  Write into *piNow
  5319   5346   ** the current time and date as a Julian Day number times 86_400_000.  In
  5320   5347   ** other words, write into *piNow the number of milliseconds since the Julian
  5321   5348   ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
  5322   5349   ** proleptic Gregorian calendar.
  5323   5350   **
  5324         -** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
         5351  +** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
  5325   5352   ** cannot be found.
  5326   5353   */
  5327   5354   static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
  5328         -  /* FILETIME structure is a 64-bit value representing the number of 
  5329         -     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
         5355  +  /* FILETIME structure is a 64-bit value representing the number of
         5356  +     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
  5330   5357     */
  5331   5358     FILETIME ft;
  5332   5359     static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
  5333   5360   #ifdef SQLITE_TEST
  5334   5361     static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
  5335   5362   #endif
  5336   5363     /* 2^32 - to avoid use of LL and warnings in gcc */
  5337         -  static const sqlite3_int64 max32BitValue = 
         5364  +  static const sqlite3_int64 max32BitValue =
  5338   5365         (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
  5339   5366         (sqlite3_int64)294967296;
  5340   5367   
  5341   5368   #if SQLITE_OS_WINCE
  5342   5369     SYSTEMTIME time;
  5343   5370     osGetSystemTime(&time);
  5344   5371     /* if SystemTimeToFileTime() fails, it returns zero. */
................................................................................
  5346   5373       return SQLITE_ERROR;
  5347   5374     }
  5348   5375   #else
  5349   5376     osGetSystemTimeAsFileTime( &ft );
  5350   5377   #endif
  5351   5378   
  5352   5379     *piNow = winFiletimeEpoch +
  5353         -            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
         5380  +            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
  5354   5381                  (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
  5355   5382   
  5356   5383   #ifdef SQLITE_TEST
  5357   5384     if( sqlite3_current_time ){
  5358   5385       *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
  5359   5386     }
  5360   5387   #endif
................................................................................
  5465   5492       winGetSystemCall,    /* xGetSystemCall */
  5466   5493       winNextSystemCall,   /* xNextSystemCall */
  5467   5494     };
  5468   5495   #endif
  5469   5496   
  5470   5497     /* Double-check that the aSyscall[] array has been constructed
  5471   5498     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  5472         -  assert( ArraySize(aSyscall)==76 );
         5499  +  assert( ArraySize(aSyscall)==77 );
  5473   5500   
  5474   5501     /* get memory map allocation granularity */
  5475   5502     memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
  5476   5503   #if SQLITE_OS_WINRT
  5477   5504     osGetNativeSystemInfo(&winSysInfo);
  5478   5505   #else
  5479   5506     osGetSystemInfo(&winSysInfo);
................................................................................
  5483   5510   
  5484   5511     sqlite3_vfs_register(&winVfs, 1);
  5485   5512   
  5486   5513   #if defined(SQLITE_WIN32_HAS_WIDE)
  5487   5514     sqlite3_vfs_register(&winLongPathVfs, 0);
  5488   5515   #endif
  5489   5516   
  5490         -  return SQLITE_OK; 
         5517  +  return SQLITE_OK;
  5491   5518   }
  5492   5519   
  5493         -int sqlite3_os_end(void){ 
         5520  +int sqlite3_os_end(void){
  5494   5521   #if SQLITE_OS_WINRT
  5495   5522     if( sleepObj!=NULL ){
  5496   5523       osCloseHandle(sleepObj);
  5497   5524       sleepObj = NULL;
  5498   5525     }
  5499   5526   #endif
  5500   5527     return SQLITE_OK;
  5501   5528   }
  5502   5529   
  5503   5530   #endif /* SQLITE_OS_WIN */

Changes to src/os_win.h.

    60     60   ** Determine if we are dealing with WinRT, which provides only a subset of
    61     61   ** the full Win32 API.
    62     62   */
    63     63   #if !defined(SQLITE_OS_WINRT)
    64     64   # define SQLITE_OS_WINRT 0
    65     65   #endif
    66     66   
           67  +/*
           68  +** For WinCE, some API function parameters do not appear to be declared as
           69  +** volatile.
           70  +*/
           71  +#if SQLITE_OS_WINCE
           72  +# define SQLITE_WIN32_VOLATILE
           73  +#else
           74  +# define SQLITE_WIN32_VOLATILE volatile
           75  +#endif
           76  +
    67     77   #endif /* _OS_WIN_H_ */

Changes to src/pager.c.

  1673   1673      && jrnlSize>pPager->journalOff
  1674   1674     ){
  1675   1675       rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
  1676   1676     }
  1677   1677     return rc;
  1678   1678   }
  1679   1679   
  1680         -/*
  1681         -** Find a page in the hash table given its page number. Return
  1682         -** a pointer to the page or NULL if the requested page is not 
  1683         -** already in memory.
  1684         -*/
  1685         -static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
  1686         -  PgHdr *p = 0;                     /* Return value */
  1687         -
  1688         -  /* It is not possible for a call to PcacheFetch() with createFlag==0 to
  1689         -  ** fail, since no attempt to allocate dynamic memory will be made.
  1690         -  */
  1691         -  (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
  1692         -  return p;
  1693         -}
  1694         -
  1695   1680   /*
  1696   1681   ** Discard the entire contents of the in-memory page-cache.
  1697   1682   */
  1698   1683   static void pager_reset(Pager *pPager){
  1699   1684     sqlite3BackupRestart(pPager->pBackup);
  1700   1685     sqlite3PcacheClear(pPager->pPCache);
  1701   1686   }
................................................................................
  1980   1965         }
  1981   1966       }
  1982   1967     }
  1983   1968   
  1984   1969   #ifdef SQLITE_CHECK_PAGES
  1985   1970     sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
  1986   1971     if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
  1987         -    PgHdr *p = pager_lookup(pPager, 1);
         1972  +    PgHdr *p = sqlite3PagerLookup(pPager, 1);
  1988   1973       if( p ){
  1989   1974         p->pageHash = 0;
  1990   1975         sqlite3PagerUnrefNotNull(p);
  1991   1976       }
  1992   1977     }
  1993   1978   #endif
  1994   1979   
................................................................................
  2259   2244     ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
  2260   2245     ** is possible to fail a statement on a database that does not yet exist.
  2261   2246     ** Do not attempt to write if database file has never been opened.
  2262   2247     */
  2263   2248     if( pagerUseWal(pPager) ){
  2264   2249       pPg = 0;
  2265   2250     }else{
  2266         -    pPg = pager_lookup(pPager, pgno);
         2251  +    pPg = sqlite3PagerLookup(pPager, pgno);
  2267   2252     }
  2268   2253     assert( pPg || !MEMDB );
  2269   2254     assert( pPager->eState!=PAGER_OPEN || pPg==0 );
  2270   2255     PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
  2271   2256              PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
  2272   2257              (isMainJrnl?"main-journal":"sub-journal")
  2273   2258     ));
................................................................................
  3633   3618   
  3634   3619       if( rc==SQLITE_OK ){
  3635   3620         pager_reset(pPager);
  3636   3621         pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
  3637   3622         pPager->pageSize = pageSize;
  3638   3623         sqlite3PageFree(pPager->pTmpSpace);
  3639   3624         pPager->pTmpSpace = pNew;
  3640         -      sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
         3625  +      rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
  3641   3626       }
  3642   3627     }
  3643   3628   
  3644   3629     *pPageSize = pPager->pageSize;
  3645   3630     if( rc==SQLITE_OK ){
  3646   3631       if( nReserve<0 ) nReserve = pPager->nReserve;
  3647   3632       assert( nReserve>=0 && nReserve<1000 );
................................................................................
  4396   4381     **
  4397   4382     ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
  4398   4383     ** regardless of whether or not a sync is required.  This is set during
  4399   4384     ** a rollback or by user request, respectively.
  4400   4385     **
  4401   4386     ** Spilling is also prohibited when in an error state since that could
  4402   4387     ** lead to database corruption.   In the current implementaton it 
  4403         -  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
         4388  +  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
  4404   4389     ** while in the error state, hence it is impossible for this routine to
  4405   4390     ** be called in the error state.  Nevertheless, we include a NEVER()
  4406   4391     ** test for the error state as a safeguard against future changes.
  4407   4392     */
  4408   4393     if( NEVER(pPager->errCode) ) return SQLITE_OK;
  4409   4394     testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
  4410   4395     testcase( pPager->doNotSpill & SPILLFLAG_OFF );
................................................................................
  4732   4717     */
  4733   4718     if( rc==SQLITE_OK ){
  4734   4719       assert( pPager->memDb==0 );
  4735   4720       rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
  4736   4721       testcase( rc!=SQLITE_OK );
  4737   4722     }
  4738   4723   
  4739         -  /* If an error occurred in either of the blocks above, free the 
  4740         -  ** Pager structure and close the file.
         4724  +  /* Initialize the PCache object. */
         4725  +  if( rc==SQLITE_OK ){
         4726  +    assert( nExtra<1000 );
         4727  +    nExtra = ROUND8(nExtra);
         4728  +    rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
         4729  +                           !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
         4730  +  }
         4731  +
         4732  +  /* If an error occurred above, free the  Pager structure and close the file.
  4741   4733     */
  4742   4734     if( rc!=SQLITE_OK ){
  4743         -    assert( !pPager->pTmpSpace );
  4744   4735       sqlite3OsClose(pPager->fd);
         4736  +    sqlite3PageFree(pPager->pTmpSpace);
  4745   4737       sqlite3_free(pPager);
  4746   4738       return rc;
  4747   4739     }
  4748   4740   
  4749         -  /* Initialize the PCache object. */
  4750         -  assert( nExtra<1000 );
  4751         -  nExtra = ROUND8(nExtra);
  4752         -  sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
  4753         -                    !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
  4754         -
  4755   4741     PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
  4756   4742     IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
  4757   4743   
  4758   4744     pPager->useJournal = (u8)useJournal;
  4759   4745     /* pPager->stmtOpen = 0; */
  4760   4746     /* pPager->stmtInUse = 0; */
  4761   4747     /* pPager->nRef = 0; */
................................................................................
  5296   5282     }
  5297   5283   
  5298   5284     /* If the pager is in the error state, return an error immediately. 
  5299   5285     ** Otherwise, request the page from the PCache layer. */
  5300   5286     if( pPager->errCode!=SQLITE_OK ){
  5301   5287       rc = pPager->errCode;
  5302   5288     }else{
  5303         -
  5304   5289       if( bMmapOk && pagerUseWal(pPager) ){
  5305   5290         rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
  5306   5291         if( rc!=SQLITE_OK ) goto pager_acquire_err;
  5307   5292       }
  5308   5293   
  5309   5294       if( bMmapOk && iFrame==0 ){
  5310   5295         void *pData = 0;
................................................................................
  5311   5296   
  5312   5297         rc = sqlite3OsFetch(pPager->fd, 
  5313   5298             (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
  5314   5299         );
  5315   5300   
  5316   5301         if( rc==SQLITE_OK && pData ){
  5317   5302           if( pPager->eState>PAGER_READER ){
  5318         -          (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
         5303  +          pPg = sqlite3PagerLookup(pPager, pgno);
  5319   5304           }
  5320   5305           if( pPg==0 ){
  5321   5306             rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
  5322   5307           }else{
  5323   5308             sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
  5324   5309           }
  5325   5310           if( pPg ){
................................................................................
  5329   5314           }
  5330   5315         }
  5331   5316         if( rc!=SQLITE_OK ){
  5332   5317           goto pager_acquire_err;
  5333   5318         }
  5334   5319       }
  5335   5320   
  5336         -    rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
         5321  +    {
         5322  +      sqlite3_pcache_page *pBase;
         5323  +      pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
         5324  +      if( pBase==0 ){
         5325  +        rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
         5326  +        if( rc!=SQLITE_OK ) goto pager_acquire_err;
         5327  +      }
         5328  +      pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
         5329  +      if( pPg==0 ) rc = SQLITE_NOMEM;
         5330  +    }
  5337   5331     }
  5338   5332   
  5339   5333     if( rc!=SQLITE_OK ){
  5340   5334       /* Either the call to sqlite3PcacheFetch() returned an error or the
  5341   5335       ** pager was already in the error-state when this function was called.
  5342   5336       ** Set pPg to 0 and jump to the exception handler.  */
  5343   5337       pPg = 0;
................................................................................
  5426   5420   ** See also sqlite3PagerGet().  The difference between this routine
  5427   5421   ** and sqlite3PagerGet() is that _get() will go to the disk and read
  5428   5422   ** in the page if the page is not already in cache.  This routine
  5429   5423   ** returns NULL if the page is not in cache or if a disk I/O error 
  5430   5424   ** has ever happened.
  5431   5425   */
  5432   5426   DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
  5433         -  PgHdr *pPg = 0;
         5427  +  sqlite3_pcache_page *pPage;
  5434   5428     assert( pPager!=0 );
  5435   5429     assert( pgno!=0 );
  5436   5430     assert( pPager->pPCache!=0 );
  5437         -  assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
  5438         -  sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
  5439         -  return pPg;
         5431  +  pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
         5432  +  return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
  5440   5433   }
  5441   5434   
  5442   5435   /*
  5443   5436   ** Release a page reference.
  5444   5437   **
  5445   5438   ** If the number of references to the page drop to zero, then the
  5446   5439   ** page is added to the LRU list.  When all references to all pages
................................................................................
  5767   5760     /* Update the database size and return.
  5768   5761     */
  5769   5762     if( pPager->dbSize<pPg->pgno ){
  5770   5763       pPager->dbSize = pPg->pgno;
  5771   5764     }
  5772   5765     return rc;
  5773   5766   }
         5767  +
         5768  +/*
         5769  +** This is a variant of sqlite3PagerWrite() that runs when the sector size
         5770  +** is larger than the page size.  SQLite makes the (reasonable) assumption that
         5771  +** all bytes of a sector are written together by hardware.  Hence, all bytes of
         5772  +** a sector need to be journalled in case of a power loss in the middle of
         5773  +** a write.
         5774  +**
         5775  +** Usually, the sector size is less than or equal to the page size, in which
         5776  +** case pages can be individually written.  This routine only runs in the exceptional
         5777  +** case where the page size is smaller than the sector size.
         5778  +*/
         5779  +static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
         5780  +  int rc = SQLITE_OK;            /* Return code */
         5781  +  Pgno nPageCount;               /* Total number of pages in database file */
         5782  +  Pgno pg1;                      /* First page of the sector pPg is located on. */
         5783  +  int nPage = 0;                 /* Number of pages starting at pg1 to journal */
         5784  +  int ii;                        /* Loop counter */
         5785  +  int needSync = 0;              /* True if any page has PGHDR_NEED_SYNC */
         5786  +  Pager *pPager = pPg->pPager;   /* The pager that owns pPg */
         5787  +  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
         5788  +
         5789  +  /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
         5790  +  ** a journal header to be written between the pages journaled by
         5791  +  ** this function.
         5792  +  */
         5793  +  assert( !MEMDB );
         5794  +  assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
         5795  +  pPager->doNotSpill |= SPILLFLAG_NOSYNC;
         5796  +
         5797  +  /* This trick assumes that both the page-size and sector-size are
         5798  +  ** an integer power of 2. It sets variable pg1 to the identifier
         5799  +  ** of the first page of the sector pPg is located on.
         5800  +  */
         5801  +  pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
         5802  +
         5803  +  nPageCount = pPager->dbSize;
         5804  +  if( pPg->pgno>nPageCount ){
         5805  +    nPage = (pPg->pgno - pg1)+1;
         5806  +  }else if( (pg1+nPagePerSector-1)>nPageCount ){
         5807  +    nPage = nPageCount+1-pg1;
         5808  +  }else{
         5809  +    nPage = nPagePerSector;
         5810  +  }
         5811  +  assert(nPage>0);
         5812  +  assert(pg1<=pPg->pgno);
         5813  +  assert((pg1+nPage)>pPg->pgno);
         5814  +
         5815  +  for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
         5816  +    Pgno pg = pg1+ii;
         5817  +    PgHdr *pPage;
         5818  +    if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
         5819  +      if( pg!=PAGER_MJ_PGNO(pPager) ){
         5820  +        rc = sqlite3PagerGet(pPager, pg, &pPage);
         5821  +        if( rc==SQLITE_OK ){
         5822  +          rc = pager_write(pPage);
         5823  +          if( pPage->flags&PGHDR_NEED_SYNC ){
         5824  +            needSync = 1;
         5825  +          }
         5826  +          sqlite3PagerUnrefNotNull(pPage);
         5827  +        }
         5828  +      }
         5829  +    }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
         5830  +      if( pPage->flags&PGHDR_NEED_SYNC ){
         5831  +        needSync = 1;
         5832  +      }
         5833  +      sqlite3PagerUnrefNotNull(pPage);
         5834  +    }
         5835  +  }
         5836  +
         5837  +  /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
         5838  +  ** starting at pg1, then it needs to be set for all of them. Because
         5839  +  ** writing to any of these nPage pages may damage the others, the
         5840  +  ** journal file must contain sync()ed copies of all of them
         5841  +  ** before any of them can be written out to the database file.
         5842  +  */
         5843  +  if( rc==SQLITE_OK && needSync ){
         5844  +    assert( !MEMDB );
         5845  +    for(ii=0; ii<nPage; ii++){
         5846  +      PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
         5847  +      if( pPage ){
         5848  +        pPage->flags |= PGHDR_NEED_SYNC;
         5849  +        sqlite3PagerUnrefNotNull(pPage);
         5850  +      }
         5851  +    }
         5852  +  }
         5853  +
         5854  +  assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
         5855  +  pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
         5856  +  return rc;
         5857  +}
  5774   5858   
  5775   5859   /*
  5776   5860   ** Mark a data page as writeable. This routine must be called before 
  5777   5861   ** making changes to a page. The caller must check the return value 
  5778   5862   ** of this function and be careful not to change any page data unless 
  5779   5863   ** this routine returns SQLITE_OK.
  5780   5864   **
................................................................................
  5782   5866   ** function also deals with the special case where 2 or more pages
  5783   5867   ** fit on a single disk sector. In this case all co-resident pages
  5784   5868   ** must have been written to the journal file before returning.
  5785   5869   **
  5786   5870   ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
  5787   5871   ** as appropriate. Otherwise, SQLITE_OK.
  5788   5872   */
  5789         -int sqlite3PagerWrite(DbPage *pDbPage){
  5790         -  int rc = SQLITE_OK;
  5791         -
  5792         -  PgHdr *pPg = pDbPage;
  5793         -  Pager *pPager = pPg->pPager;
  5794         -
         5873  +int sqlite3PagerWrite(PgHdr *pPg){
  5795   5874     assert( (pPg->flags & PGHDR_MMAP)==0 );
  5796         -  assert( pPager->eState>=PAGER_WRITER_LOCKED );
  5797         -  assert( pPager->eState!=PAGER_ERROR );
  5798         -  assert( assert_pager_state(pPager) );
  5799         -
  5800         -  if( pPager->sectorSize > (u32)pPager->pageSize ){
  5801         -    Pgno nPageCount;          /* Total number of pages in database file */
  5802         -    Pgno pg1;                 /* First page of the sector pPg is located on. */
  5803         -    int nPage = 0;            /* Number of pages starting at pg1 to journal */
  5804         -    int ii;                   /* Loop counter */
  5805         -    int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
  5806         -    Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  5807         -
  5808         -    /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
  5809         -    ** a journal header to be written between the pages journaled by
  5810         -    ** this function.
  5811         -    */
  5812         -    assert( !MEMDB );
  5813         -    assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
  5814         -    pPager->doNotSpill |= SPILLFLAG_NOSYNC;
  5815         -
  5816         -    /* This trick assumes that both the page-size and sector-size are
  5817         -    ** an integer power of 2. It sets variable pg1 to the identifier
  5818         -    ** of the first page of the sector pPg is located on.
  5819         -    */
  5820         -    pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
  5821         -
  5822         -    nPageCount = pPager->dbSize;
  5823         -    if( pPg->pgno>nPageCount ){
  5824         -      nPage = (pPg->pgno - pg1)+1;
  5825         -    }else if( (pg1+nPagePerSector-1)>nPageCount ){
  5826         -      nPage = nPageCount+1-pg1;
  5827         -    }else{
  5828         -      nPage = nPagePerSector;
  5829         -    }
  5830         -    assert(nPage>0);
  5831         -    assert(pg1<=pPg->pgno);
  5832         -    assert((pg1+nPage)>pPg->pgno);
  5833         -
  5834         -    for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
  5835         -      Pgno pg = pg1+ii;
  5836         -      PgHdr *pPage;
  5837         -      if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
  5838         -        if( pg!=PAGER_MJ_PGNO(pPager) ){
  5839         -          rc = sqlite3PagerGet(pPager, pg, &pPage);
  5840         -          if( rc==SQLITE_OK ){
  5841         -            rc = pager_write(pPage);
  5842         -            if( pPage->flags&PGHDR_NEED_SYNC ){
  5843         -              needSync = 1;
  5844         -            }
  5845         -            sqlite3PagerUnrefNotNull(pPage);
  5846         -          }
  5847         -        }
  5848         -      }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
  5849         -        if( pPage->flags&PGHDR_NEED_SYNC ){
  5850         -          needSync = 1;
  5851         -        }
  5852         -        sqlite3PagerUnrefNotNull(pPage);
  5853         -      }
  5854         -    }
  5855         -
  5856         -    /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
  5857         -    ** starting at pg1, then it needs to be set for all of them. Because
  5858         -    ** writing to any of these nPage pages may damage the others, the
  5859         -    ** journal file must contain sync()ed copies of all of them
  5860         -    ** before any of them can be written out to the database file.
  5861         -    */
  5862         -    if( rc==SQLITE_OK && needSync ){
  5863         -      assert( !MEMDB );
  5864         -      for(ii=0; ii<nPage; ii++){
  5865         -        PgHdr *pPage = pager_lookup(pPager, pg1+ii);
  5866         -        if( pPage ){
  5867         -          pPage->flags |= PGHDR_NEED_SYNC;
  5868         -          sqlite3PagerUnrefNotNull(pPage);
  5869         -        }
  5870         -      }
  5871         -    }
  5872         -
  5873         -    assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
  5874         -    pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
         5875  +  assert( pPg->pPager->eState>=PAGER_WRITER_LOCKED );
         5876  +  assert( pPg->pPager->eState!=PAGER_ERROR );
         5877  +  assert( assert_pager_state(pPg->pPager) );
         5878  +  if( pPg->pPager->sectorSize > (u32)pPg->pPager->pageSize ){
         5879  +    return pagerWriteLargeSector(pPg);
  5875   5880     }else{
  5876         -    rc = pager_write(pDbPage);
         5881  +    return pager_write(pPg);
  5877   5882     }
  5878         -  return rc;
  5879   5883   }
  5880   5884   
  5881   5885   /*
  5882   5886   ** Return TRUE if the page given in the argument was previously passed
  5883   5887   ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
  5884   5888   ** to change the content of the page.
  5885   5889   */
................................................................................
  6767   6771   
  6768   6772     /* If the cache contains a page with page-number pgno, remove it
  6769   6773     ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
  6770   6774     ** page pgno before the 'move' operation, it needs to be retained 
  6771   6775     ** for the page moved there.
  6772   6776     */
  6773   6777     pPg->flags &= ~PGHDR_NEED_SYNC;
  6774         -  pPgOld = pager_lookup(pPager, pgno);
         6778  +  pPgOld = sqlite3PagerLookup(pPager, pgno);
  6775   6779     assert( !pPgOld || pPgOld->nRef==1 );
  6776   6780     if( pPgOld ){
  6777   6781       pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
  6778   6782       if( MEMDB ){
  6779   6783         /* Do not discard pages from an in-memory database since we might
  6780   6784         ** need to rollback later.  Just move the page out of the way. */
  6781   6785         sqlite3PcacheMove(pPgOld, pPager->dbSize+1);

Changes to src/pcache.c.

    58     58     for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
    59     59       assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
    60     60     }
    61     61     return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
    62     62   }
    63     63   #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
    64     64   
           65  +/* Allowed values for second argument to pcacheManageDirtyList() */
           66  +#define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */
           67  +#define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */
           68  +#define PCACHE_DIRTYLIST_FRONT    3    /* Move pPage to the front of the list */
           69  +
    65     70   /*
    66         -** Remove page pPage from the list of dirty pages.
           71  +** Manage pPage's participation on the dirty list.  Bits of the addRemove
           72  +** argument determines what operation to do.  The 0x01 bit means first
           73  +** remove pPage from the dirty list.  The 0x02 means add pPage back to
           74  +** the dirty list.  Doing both moves pPage to the front of the dirty list.
    67     75   */
    68         -static void pcacheRemoveFromDirtyList(PgHdr *pPage){
           76  +static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
    69     77     PCache *p = pPage->pCache;
    70     78   
    71         -  assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
    72         -  assert( pPage->pDirtyPrev || pPage==p->pDirty );
    73         -
    74         -  /* Update the PCache1.pSynced variable if necessary. */
    75         -  if( p->pSynced==pPage ){
    76         -    PgHdr *pSynced = pPage->pDirtyPrev;
    77         -    while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
    78         -      pSynced = pSynced->pDirtyPrev;
           79  +  if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
           80  +    assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
           81  +    assert( pPage->pDirtyPrev || pPage==p->pDirty );
           82  +  
           83  +    /* Update the PCache1.pSynced variable if necessary. */
           84  +    if( p->pSynced==pPage ){
           85  +      PgHdr *pSynced = pPage->pDirtyPrev;
           86  +      while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
           87  +        pSynced = pSynced->pDirtyPrev;
           88  +      }
           89  +      p->pSynced = pSynced;
           90  +    }
           91  +  
           92  +    if( pPage->pDirtyNext ){
           93  +      pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
           94  +    }else{
           95  +      assert( pPage==p->pDirtyTail );
           96  +      p->pDirtyTail = pPage->pDirtyPrev;
           97  +    }
           98  +    if( pPage->pDirtyPrev ){
           99  +      pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
          100  +    }else{
          101  +      assert( pPage==p->pDirty );
          102  +      p->pDirty = pPage->pDirtyNext;
          103  +      if( p->pDirty==0 && p->bPurgeable ){
          104  +        assert( p->eCreate==1 );
          105  +        p->eCreate = 2;
          106  +      }
          107  +    }
          108  +    pPage->pDirtyNext = 0;
          109  +    pPage->pDirtyPrev = 0;
          110  +    expensive_assert( pcacheCheckSynced(p) );
          111  +  }
          112  +  if( addRemove & PCACHE_DIRTYLIST_ADD ){
          113  +    assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
          114  +  
          115  +    pPage->pDirtyNext = p->pDirty;
          116  +    if( pPage->pDirtyNext ){
          117  +      assert( pPage->pDirtyNext->pDirtyPrev==0 );
          118  +      pPage->pDirtyNext->pDirtyPrev = pPage;
          119  +    }else if( p->bPurgeable ){
          120  +      assert( p->eCreate==2 );
          121  +      p->eCreate = 1;
          122  +    }
          123  +    p->pDirty = pPage;
          124  +    if( !p->pDirtyTail ){
          125  +      p->pDirtyTail = pPage;
    79    126       }
    80         -    p->pSynced = pSynced;
    81         -  }
    82         -
    83         -  if( pPage->pDirtyNext ){
    84         -    pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
    85         -  }else{
    86         -    assert( pPage==p->pDirtyTail );
    87         -    p->pDirtyTail = pPage->pDirtyPrev;
    88         -  }
    89         -  if( pPage->pDirtyPrev ){
    90         -    pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
    91         -  }else{
    92         -    assert( pPage==p->pDirty );
    93         -    p->pDirty = pPage->pDirtyNext;
    94         -    if( p->pDirty==0 && p->bPurgeable ){
    95         -      assert( p->eCreate==1 );
    96         -      p->eCreate = 2;
          127  +    if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
          128  +      p->pSynced = pPage;
    97    129       }
          130  +    expensive_assert( pcacheCheckSynced(p) );
    98    131     }
    99         -  pPage->pDirtyNext = 0;
   100         -  pPage->pDirtyPrev = 0;
   101         -
   102         -  expensive_assert( pcacheCheckSynced(p) );
   103         -}
   104         -
   105         -/*
   106         -** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
   107         -** pPage).
   108         -*/
   109         -static void pcacheAddToDirtyList(PgHdr *pPage){
   110         -  PCache *p = pPage->pCache;
   111         -
   112         -  assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
   113         -
   114         -  pPage->pDirtyNext = p->pDirty;
   115         -  if( pPage->pDirtyNext ){
   116         -    assert( pPage->pDirtyNext->pDirtyPrev==0 );
   117         -    pPage->pDirtyNext->pDirtyPrev = pPage;
   118         -  }else if( p->bPurgeable ){
   119         -    assert( p->eCreate==2 );
   120         -    p->eCreate = 1;
   121         -  }
   122         -  p->pDirty = pPage;
   123         -  if( !p->pDirtyTail ){
   124         -    p->pDirtyTail = pPage;
   125         -  }
   126         -  if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
   127         -    p->pSynced = pPage;
   128         -  }
   129         -  expensive_assert( pcacheCheckSynced(p) );
   130    132   }
   131    133   
   132    134   /*
   133    135   ** Wrapper around the pluggable caches xUnpin method. If the cache is
   134    136   ** being used for an in-memory database, this function is a no-op.
   135    137   */
   136    138   static void pcacheUnpin(PgHdr *p){
   137         -  PCache *pCache = p->pCache;
   138         -  if( pCache->bPurgeable ){
          139  +  if( p->pCache->bPurgeable ){
   139    140       if( p->pgno==1 ){
   140         -      pCache->pPage1 = 0;
          141  +      p->pCache->pPage1 = 0;
   141    142       }
   142         -    sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
          143  +    sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
          144  +  }
          145  +}
          146  +
          147  +/*
          148  +** Compute the number of pages of cache requested.
          149  +*/
          150  +static int numberOfCachePages(PCache *p){
          151  +  if( p->szCache>=0 ){
          152  +    return p->szCache;
          153  +  }else{
          154  +    return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
   143    155     }
   144    156   }
   145    157   
   146    158   /*************************************************** General Interfaces ******
   147    159   **
   148    160   ** Initialize and shutdown the page cache subsystem. Neither of these 
   149    161   ** functions are threadsafe.
................................................................................
   171    183   
   172    184   /*
   173    185   ** Create a new PCache object. Storage space to hold the object
   174    186   ** has already been allocated and is passed in as the p pointer. 
   175    187   ** The caller discovers how much space needs to be allocated by 
   176    188   ** calling sqlite3PcacheSize().
   177    189   */
   178         -void sqlite3PcacheOpen(
          190  +int sqlite3PcacheOpen(
   179    191     int szPage,                  /* Size of every page */
   180    192     int szExtra,                 /* Extra space associated with each page */
   181    193     int bPurgeable,              /* True if pages are on backing store */
   182    194     int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
   183    195     void *pStress,               /* Argument to xStress */
   184    196     PCache *p                    /* Preallocated space for the PCache */
   185    197   ){
   186    198     memset(p, 0, sizeof(PCache));
   187         -  p->szPage = szPage;
          199  +  p->szPage = 1;
   188    200     p->szExtra = szExtra;
   189    201     p->bPurgeable = bPurgeable;
   190    202     p->eCreate = 2;
   191    203     p->xStress = xStress;
   192    204     p->pStress = pStress;
   193    205     p->szCache = 100;
          206  +  return sqlite3PcacheSetPageSize(p, szPage);
   194    207   }
   195    208   
   196    209   /*
   197    210   ** Change the page size for PCache object. The caller must ensure that there
   198    211   ** are no outstanding page references when this function is called.
   199    212   */
   200         -void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
          213  +int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
   201    214     assert( pCache->nRef==0 && pCache->pDirty==0 );
   202         -  if( pCache->pCache ){
   203         -    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   204         -    pCache->pCache = 0;
          215  +  if( pCache->szPage ){
          216  +    sqlite3_pcache *pNew;
          217  +    pNew = sqlite3GlobalConfig.pcache2.xCreate(
          218  +                szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
          219  +    );
          220  +    if( pNew==0 ) return SQLITE_NOMEM;
          221  +    sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
          222  +    if( pCache->pCache ){
          223  +      sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
          224  +    }
          225  +    pCache->pCache = pNew;
   205    226       pCache->pPage1 = 0;
          227  +    pCache->szPage = szPage;
   206    228     }
   207         -  pCache->szPage = szPage;
   208         -}
   209         -
   210         -/*
   211         -** Compute the number of pages of cache requested.
   212         -*/
   213         -static int numberOfCachePages(PCache *p){
   214         -  if( p->szCache>=0 ){
   215         -    return p->szCache;
   216         -  }else{
   217         -    return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
   218         -  }
          229  +  return SQLITE_OK;
   219    230   }
   220    231   
   221    232   /*
   222    233   ** Try to obtain a page from the cache.
          234  +**
          235  +** This routine returns a pointer to an sqlite3_pcache_page object if
          236  +** such an object is already in cache, or if a new one is created.
          237  +** This routine returns a NULL pointer if the object was not in cache
          238  +** and could not be created.
          239  +**
          240  +** The createFlags should be 0 to check for existing pages and should
          241  +** be 3 (not 1, but 3) to try to create a new page.
          242  +**
          243  +** If the createFlag is 0, then NULL is always returned if the page
          244  +** is not already in the cache.  If createFlag is 1, then a new page
          245  +** is created only if that can be done without spilling dirty pages
          246  +** and without exceeding the cache size limit.
          247  +**
          248  +** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
          249  +** initialize the sqlite3_pcache_page object and convert it into a
          250  +** PgHdr object.  The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
          251  +** routines are split this way for performance reasons. When separated
          252  +** they can both (usually) operate without having to push values to
          253  +** the stack on entry and pop them back off on exit, which saves a
          254  +** lot of pushing and popping.
   223    255   */
   224         -int sqlite3PcacheFetch(
          256  +sqlite3_pcache_page *sqlite3PcacheFetch(
   225    257     PCache *pCache,       /* Obtain the page from this cache */
   226    258     Pgno pgno,            /* Page number to obtain */
   227         -  int createFlag,       /* If true, create page if it does not exist already */
   228         -  PgHdr **ppPage        /* Write the page here */
          259  +  int createFlag        /* If true, create page if it does not exist already */
   229    260   ){
   230         -  sqlite3_pcache_page *pPage;
   231         -  PgHdr *pPgHdr = 0;
   232    261     int eCreate;
   233    262   
   234    263     assert( pCache!=0 );
   235         -  assert( createFlag==1 || createFlag==0 );
          264  +  assert( pCache->pCache!=0 );
          265  +  assert( createFlag==3 || createFlag==0 );
   236    266     assert( pgno>0 );
   237    267   
   238         -  /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
   239         -  ** allocate it now.
   240         -  */
   241         -  if( !pCache->pCache ){
   242         -    sqlite3_pcache *p;
   243         -    if( !createFlag ){
   244         -      *ppPage = 0;
   245         -      return SQLITE_OK;
   246         -    }
   247         -    p = sqlite3GlobalConfig.pcache2.xCreate(
   248         -        pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
   249         -    );
   250         -    if( !p ){
   251         -      return SQLITE_NOMEM;
   252         -    }
   253         -    sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
   254         -    pCache->pCache = p;
   255         -  }
   256         -
   257    268     /* eCreate defines what to do if the page does not exist.
   258    269     **    0     Do not allocate a new page.  (createFlag==0)
   259    270     **    1     Allocate a new page if doing so is inexpensive.
   260    271     **          (createFlag==1 AND bPurgeable AND pDirty)
   261    272     **    2     Allocate a new page even it doing so is difficult.
   262    273     **          (createFlag==1 AND !(bPurgeable AND pDirty)
   263    274     */
   264         -  eCreate = createFlag==0 ? 0 : pCache->eCreate;
   265         -  assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
   266         -  pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
   267         -  if( !pPage && eCreate==1 ){
   268         -    PgHdr *pPg;
   269         -
   270         -    /* Find a dirty page to write-out and recycle. First try to find a 
   271         -    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
   272         -    ** cleared), but if that is not possible settle for any other 
   273         -    ** unreferenced dirty page.
   274         -    */
   275         -    expensive_assert( pcacheCheckSynced(pCache) );
   276         -    for(pPg=pCache->pSynced; 
   277         -        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
   278         -        pPg=pPg->pDirtyPrev
   279         -    );
   280         -    pCache->pSynced = pPg;
   281         -    if( !pPg ){
   282         -      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
   283         -    }
   284         -    if( pPg ){
   285         -      int rc;
          275  +  eCreate = createFlag & pCache->eCreate;
          276  +  assert( eCreate==0 || eCreate==1 || eCreate==2 );
          277  +  assert( createFlag==0 || pCache->eCreate==eCreate );
          278  +  assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
          279  +  return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
          280  +}
          281  +
          282  +/*
          283  +** If the sqlite3PcacheFetch() routine is unable to allocate a new
          284  +** page because new clean pages are available for reuse and the cache
          285  +** size limit has been reached, then this routine can be invoked to 
          286  +** try harder to allocate a page.  This routine might invoke the stress
          287  +** callback to spill dirty pages to the journal.  It will then try to
          288  +** allocate the new page and will only fail to allocate a new page on
          289  +** an OOM error.
          290  +**
          291  +** This routine should be invoked only after sqlite3PcacheFetch() fails.
          292  +*/
          293  +int sqlite3PcacheFetchStress(
          294  +  PCache *pCache,                 /* Obtain the page from this cache */
          295  +  Pgno pgno,                      /* Page number to obtain */
          296  +  sqlite3_pcache_page **ppPage    /* Write result here */
          297  +){
          298  +  PgHdr *pPg;
          299  +  if( pCache->eCreate==2 ) return 0;
          300  +
          301  +
          302  +  /* Find a dirty page to write-out and recycle. First try to find a 
          303  +  ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
          304  +  ** cleared), but if that is not possible settle for any other 
          305  +  ** unreferenced dirty page.
          306  +  */
          307  +  expensive_assert( pcacheCheckSynced(pCache) );
          308  +  for(pPg=pCache->pSynced; 
          309  +      pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
          310  +      pPg=pPg->pDirtyPrev
          311  +  );
          312  +  pCache->pSynced = pPg;
          313  +  if( !pPg ){
          314  +    for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
          315  +  }
          316  +  if( pPg ){
          317  +    int rc;
   286    318   #ifdef SQLITE_LOG_CACHE_SPILL
   287         -      sqlite3_log(SQLITE_FULL, 
   288         -                  "spill page %d making room for %d - cache used: %d/%d",
   289         -                  pPg->pgno, pgno,
   290         -                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
   291         -                  numberOfCachePages(pCache));
          319  +    sqlite3_log(SQLITE_FULL, 
          320  +                "spill page %d making room for %d - cache used: %d/%d",
          321  +                pPg->pgno, pgno,
          322  +                sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
          323  +                numberOfCachePages(pCache));
   292    324   #endif
   293         -      rc = pCache->xStress(pCache->pStress, pPg);
   294         -      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
   295         -        return rc;
   296         -      }
   297         -    }
   298         -
   299         -    pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
   300         -  }
   301         -
   302         -  if( pPage ){
   303         -    pPgHdr = (PgHdr *)pPage->pExtra;
   304         -
   305         -    if( !pPgHdr->pPage ){
   306         -      memset(pPgHdr, 0, sizeof(PgHdr));
   307         -      pPgHdr->pPage = pPage;
   308         -      pPgHdr->pData = pPage->pBuf;
   309         -      pPgHdr->pExtra = (void *)&pPgHdr[1];
   310         -      memset(pPgHdr->pExtra, 0, pCache->szExtra);
   311         -      pPgHdr->pCache = pCache;
   312         -      pPgHdr->pgno = pgno;
   313         -    }
   314         -    assert( pPgHdr->pCache==pCache );
   315         -    assert( pPgHdr->pgno==pgno );
   316         -    assert( pPgHdr->pData==pPage->pBuf );
   317         -    assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
   318         -
   319         -    if( 0==pPgHdr->nRef ){
   320         -      pCache->nRef++;
   321         -    }
   322         -    pPgHdr->nRef++;
   323         -    if( pgno==1 ){
   324         -      pCache->pPage1 = pPgHdr;
          325  +    rc = pCache->xStress(pCache->pStress, pPg);
          326  +    if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
          327  +      return rc;
   325    328       }
   326    329     }
   327         -  *ppPage = pPgHdr;
   328         -  return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
          330  +  *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
          331  +  return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK;
          332  +}
          333  +
          334  +/*
          335  +** This is a helper routine for sqlite3PcacheFetchFinish()
          336  +**
          337  +** In the uncommon case where the page being fetched has not been
          338  +** initialized, this routine is invoked to do the initialization.
          339  +** This routine is broken out into a separate function since it
          340  +** requires extra stack manipulation that can be avoided in the common
          341  +** case.
          342  +*/
          343  +static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
          344  +  PCache *pCache,             /* Obtain the page from this cache */
          345  +  Pgno pgno,                  /* Page number obtained */
          346  +  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
          347  +){
          348  +  PgHdr *pPgHdr;
          349  +  assert( pPage!=0 );
          350  +  pPgHdr = (PgHdr*)pPage->pExtra;
          351  +  assert( pPgHdr->pPage==0 );
          352  + memset(pPgHdr, 0, sizeof(PgHdr));
          353  +  pPgHdr->pPage = pPage;
          354  +  pPgHdr->pData = pPage->pBuf;
          355  +  pPgHdr->pExtra = (void *)&pPgHdr[1];
          356  +  memset(pPgHdr->pExtra, 0, pCache->szExtra);
          357  +  pPgHdr->pCache = pCache;
          358  +  pPgHdr->pgno = pgno;
          359  +  return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
          360  +}
          361  +
          362  +/*
          363  +** This routine converts the sqlite3_pcache_page object returned by
          364  +** sqlite3PcacheFetch() into an initialized PgHdr object.  This routine
          365  +** must be called after sqlite3PcacheFetch() in order to get a usable
          366  +** result.
          367  +*/
          368  +PgHdr *sqlite3PcacheFetchFinish(
          369  +  PCache *pCache,             /* Obtain the page from this cache */
          370  +  Pgno pgno,                  /* Page number obtained */
          371  +  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
          372  +){
          373  +  PgHdr *pPgHdr;
          374  +
          375  +  if( pPage==0 ) return 0;
          376  +  pPgHdr = (PgHdr *)pPage->pExtra;
          377  +
          378  +  if( !pPgHdr->pPage ){
          379  +    return pcacheFetchFinishWithInit(pCache, pgno, pPage);
          380  +  }
          381  +  if( 0==pPgHdr->nRef ){
          382  +    pCache->nRef++;
          383  +  }
          384  +  pPgHdr->nRef++;
          385  +  if( pgno==1 ){
          386  +    pCache->pPage1 = pPgHdr;
          387  +  }
          388  +  return pPgHdr;
   329    389   }
   330    390   
   331    391   /*
   332    392   ** Decrement the reference count on a page. If the page is clean and the
   333    393   ** reference count drops to 0, then it is made elible for recycling.
   334    394   */
   335         -void sqlite3PcacheRelease(PgHdr *p){
          395  +void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
   336    396     assert( p->nRef>0 );
   337    397     p->nRef--;
   338    398     if( p->nRef==0 ){
   339         -    PCache *pCache = p->pCache;
   340         -    pCache->nRef--;
          399  +    p->pCache->nRef--;
   341    400       if( (p->flags&PGHDR_DIRTY)==0 ){
   342    401         pcacheUnpin(p);
   343    402       }else{
   344    403         /* Move the page to the head of the dirty list. */
   345         -      pcacheRemoveFromDirtyList(p);
   346         -      pcacheAddToDirtyList(p);
          404  +      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
   347    405       }
   348    406     }
   349    407   }
   350    408   
   351    409   /*
   352    410   ** Increase the reference count of a supplied page by 1.
   353    411   */
................................................................................
   358    416   
   359    417   /*
   360    418   ** Drop a page from the cache. There must be exactly one reference to the
   361    419   ** page. This function deletes that reference, so after it returns the
   362    420   ** page pointed to by p is invalid.
   363    421   */
   364    422   void sqlite3PcacheDrop(PgHdr *p){
   365         -  PCache *pCache;
   366    423     assert( p->nRef==1 );
   367    424     if( p->flags&PGHDR_DIRTY ){
   368         -    pcacheRemoveFromDirtyList(p);
          425  +    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   369    426     }
   370         -  pCache = p->pCache;
   371         -  pCache->nRef--;
          427  +  p->pCache->nRef--;
   372    428     if( p->pgno==1 ){
   373         -    pCache->pPage1 = 0;
          429  +    p->pCache->pPage1 = 0;
   374    430     }
   375         -  sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
          431  +  sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
   376    432   }
   377    433   
   378    434   /*
   379    435   ** Make sure the page is marked as dirty. If it isn't dirty already,
   380    436   ** make it so.
   381    437   */
   382    438   void sqlite3PcacheMakeDirty(PgHdr *p){
   383    439     p->flags &= ~PGHDR_DONT_WRITE;
   384    440     assert( p->nRef>0 );
   385    441     if( 0==(p->flags & PGHDR_DIRTY) ){
   386    442       p->flags |= PGHDR_DIRTY;
   387         -    pcacheAddToDirtyList( p);
          443  +    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
   388    444     }
   389    445   }
   390    446   
   391    447   /*
   392    448   ** Make sure the page is marked as clean. If it isn't clean already,
   393    449   ** make it so.
   394    450   */
   395    451   void sqlite3PcacheMakeClean(PgHdr *p){
   396    452     if( (p->flags & PGHDR_DIRTY) ){
   397         -    pcacheRemoveFromDirtyList(p);
          453  +    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   398    454       p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
   399    455       if( p->nRef==0 ){
   400    456         pcacheUnpin(p);
   401    457       }
   402    458     }
   403    459   }
   404    460   
................................................................................
   429    485   void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
   430    486     PCache *pCache = p->pCache;
   431    487     assert( p->nRef>0 );
   432    488     assert( newPgno>0 );
   433    489     sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
   434    490     p->pgno = newPgno;
   435    491     if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
   436         -    pcacheRemoveFromDirtyList(p);
   437         -    pcacheAddToDirtyList(p);
          492  +    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
   438    493     }
   439    494   }
   440    495   
   441    496   /*
   442    497   ** Drop every cache entry whose page number is greater than "pgno". The
   443    498   ** caller must ensure that there are no outstanding references to any pages
   444    499   ** other than page 1 with a page number greater than pgno.
................................................................................
   471    526     }
   472    527   }
   473    528   
   474    529   /*
   475    530   ** Close a cache.
   476    531   */
   477    532   void sqlite3PcacheClose(PCache *pCache){
   478         -  if( pCache->pCache ){
   479         -    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   480         -  }
          533  +  assert( pCache->pCache!=0 );
          534  +  sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   481    535   }
   482    536   
   483    537   /* 
   484    538   ** Discard the contents of the cache.
   485    539   */
   486    540   void sqlite3PcacheClear(PCache *pCache){
   487    541     sqlite3PcacheTruncate(pCache, 0);
................................................................................
   582    636     return p->nRef;
   583    637   }
   584    638   
   585    639   /* 
   586    640   ** Return the total number of pages in the cache.
   587    641   */
   588    642   int sqlite3PcachePagecount(PCache *pCache){
   589         -  int nPage = 0;
   590         -  if( pCache->pCache ){
   591         -    nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
   592         -  }
   593         -  return nPage;
          643  +  assert( pCache->pCache!=0 );
          644  +  return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
   594    645   }
   595    646   
   596    647   #ifdef SQLITE_TEST
   597    648   /*
   598    649   ** Get the suggested cache-size value.
   599    650   */
   600    651   int sqlite3PcacheGetCachesize(PCache *pCache){
................................................................................
   602    653   }
   603    654   #endif
   604    655   
   605    656   /*
   606    657   ** Set the suggested cache-size value.
   607    658   */
   608    659   void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
          660  +  assert( pCache->pCache!=0 );
   609    661     pCache->szCache = mxPage;
   610         -  if( pCache->pCache ){
   611         -    sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
   612         -                                           numberOfCachePages(pCache));
   613         -  }
          662  +  sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
          663  +                                         numberOfCachePages(pCache));
   614    664   }
   615    665   
   616    666   /*
   617    667   ** Free up as much memory as possible from the page cache.
   618    668   */
   619    669   void sqlite3PcacheShrink(PCache *pCache){
   620         -  if( pCache->pCache ){
   621         -    sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
   622         -  }
          670  +  assert( pCache->pCache!=0 );
          671  +  sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
   623    672   }
   624    673   
   625    674   #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
   626    675   /*
   627    676   ** For all dirty pages currently in the cache, invoke the specified
   628    677   ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
   629    678   ** defined.

Changes to src/pcache.h.

    64     64   */
    65     65   void sqlite3PCacheBufferSetup(void *, int sz, int n);
    66     66   
    67     67   /* Create a new pager cache.
    68     68   ** Under memory stress, invoke xStress to try to make pages clean.
    69     69   ** Only clean and unpinned pages can be reclaimed.
    70     70   */
    71         -void sqlite3PcacheOpen(
           71  +int sqlite3PcacheOpen(
    72     72     int szPage,                    /* Size of every page */
    73     73     int szExtra,                   /* Extra space associated with each page */
    74     74     int bPurgeable,                /* True if pages are on backing store */
    75     75     int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
    76     76     void *pStress,                 /* Argument to xStress */
    77     77     PCache *pToInit                /* Preallocated space for the PCache */
    78     78   );
    79     79   
    80     80   /* Modify the page-size after the cache has been created. */
    81         -void sqlite3PcacheSetPageSize(PCache *, int);
           81  +int sqlite3PcacheSetPageSize(PCache *, int);
    82     82   
    83     83   /* Return the size in bytes of a PCache object.  Used to preallocate
    84     84   ** storage space.
    85     85   */
    86     86   int sqlite3PcacheSize(void);
    87     87   
    88     88   /* One release per successful fetch.  Page is pinned until released.
    89     89   ** Reference counted. 
    90     90   */
    91         -int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
           91  +sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
           92  +int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
           93  +PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
    92     94   void sqlite3PcacheRelease(PgHdr*);
    93     95   
    94     96   void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
    95     97   void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
    96     98   void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
    97     99   void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
    98    100   

Changes to src/pcache1.c.

   379    379   
   380    380   /*
   381    381   ** This function is used to resize the hash table used by the cache passed
   382    382   ** as the first argument.
   383    383   **
   384    384   ** The PCache mutex must be held when this function is called.
   385    385   */
   386         -static int pcache1ResizeHash(PCache1 *p){
          386  +static void pcache1ResizeHash(PCache1 *p){
   387    387     PgHdr1 **apNew;
   388    388     unsigned int nNew;
   389    389     unsigned int i;
   390    390   
   391    391     assert( sqlite3_mutex_held(p->pGroup->mutex) );
   392    392   
   393    393     nNew = p->nHash*2;
................................................................................
   411    411           apNew[h] = pPage;
   412    412         }
   413    413       }
   414    414       sqlite3_free(p->apHash);
   415    415       p->apHash = apNew;
   416    416       p->nHash = nNew;
   417    417     }
   418         -
   419         -  return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
   420    418   }
   421    419   
   422    420   /*
   423    421   ** This function is used internally to remove the page pPage from the 
   424    422   ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
   425    423   ** LRU list, then this function is a no-op.
   426    424   **
................................................................................
   547    545   */
   548    546   static void pcache1Shutdown(void *NotUsed){
   549    547     UNUSED_PARAMETER(NotUsed);
   550    548     assert( pcache1.isInit!=0 );
   551    549     memset(&pcache1, 0, sizeof(pcache1));
   552    550   }
   553    551   
          552  +/* forward declaration */
          553  +static void pcache1Destroy(sqlite3_pcache *p);
          554  +
   554    555   /*
   555    556   ** Implementation of the sqlite3_pcache.xCreate method.
   556    557   **
   557    558   ** Allocate a new cache.
   558    559   */
   559    560   static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
   560    561     PCache1 *pCache;      /* The newly created page cache */
................................................................................
   591    592       }else{
   592    593         pGroup = &pcache1.grp;
   593    594       }
   594    595       pCache->pGroup = pGroup;
   595    596       pCache->szPage = szPage;
   596    597       pCache->szExtra = szExtra;
   597    598       pCache->bPurgeable = (bPurgeable ? 1 : 0);
          599  +    pcache1EnterMutex(pGroup);
          600  +    pcache1ResizeHash(pCache);
   598    601       if( bPurgeable ){
   599    602         pCache->nMin = 10;
   600         -      pcache1EnterMutex(pGroup);
   601    603         pGroup->nMinPage += pCache->nMin;
   602    604         pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
   603         -      pcache1LeaveMutex(pGroup);
          605  +    }
          606  +    pcache1LeaveMutex(pGroup);
          607  +    if( pCache->nHash==0 ){
          608  +      pcache1Destroy((sqlite3_pcache*)pCache);
          609  +      pCache = 0;
   604    610       }
   605    611     }
   606    612     return (sqlite3_pcache *)pCache;
   607    613   }
   608    614   
   609    615   /*
   610    616   ** Implementation of the sqlite3_pcache.xCachesize method. 
................................................................................
   652    658     PCache1 *pCache = (PCache1*)p;
   653    659     pcache1EnterMutex(pCache->pGroup);
   654    660     n = pCache->nPage;
   655    661     pcache1LeaveMutex(pCache->pGroup);
   656    662     return n;
   657    663   }
   658    664   
          665  +
          666  +/*
          667  +** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
          668  +** in the header of the pcache1Fetch() procedure.
          669  +**
          670  +** This steps are broken out into a separate procedure because they are
          671  +** usually not needed, and by avoiding the stack initialization required
          672  +** for these steps, the main pcache1Fetch() procedure can run faster.
          673  +*/
          674  +static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
          675  +  PCache1 *pCache, 
          676  +  unsigned int iKey, 
          677  +  int createFlag
          678  +){
          679  +  unsigned int nPinned;
          680  +  PGroup *pGroup = pCache->pGroup;
          681  +  PgHdr1 *pPage = 0;
          682  +
          683  +  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
          684  +  assert( pCache->nPage >= pCache->nRecyclable );
          685  +  nPinned = pCache->nPage - pCache->nRecyclable;
          686  +  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
          687  +  assert( pCache->n90pct == pCache->nMax*9/10 );
          688  +  if( createFlag==1 && (
          689  +        nPinned>=pGroup->mxPinned
          690  +     || nPinned>=pCache->n90pct
          691  +     || pcache1UnderMemoryPressure(pCache)
          692  +  )){
          693  +    return 0;
          694  +  }
          695  +
          696  +  if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
          697  +  assert( pCache->nHash>0 && pCache->apHash );
          698  +
          699  +  /* Step 4. Try to recycle a page. */
          700  +  if( pCache->bPurgeable && pGroup->pLruTail && (
          701  +         (pCache->nPage+1>=pCache->nMax)
          702  +      || pGroup->nCurrentPage>=pGroup->nMaxPage
          703  +      || pcache1UnderMemoryPressure(pCache)
          704  +  )){
          705  +    PCache1 *pOther;
          706  +    pPage = pGroup->pLruTail;
          707  +    assert( pPage->isPinned==0 );
          708  +    pcache1RemoveFromHash(pPage);
          709  +    pcache1PinPage(pPage);
          710  +    pOther = pPage->pCache;
          711  +
          712  +    /* We want to verify that szPage and szExtra are the same for pOther
          713  +    ** and pCache.  Assert that we can verify this by comparing sums. */
          714  +    assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
          715  +    assert( pCache->szExtra<512 );
          716  +    assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
          717  +    assert( pOther->szExtra<512 );
          718  +
          719  +    if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
          720  +      pcache1FreePage(pPage);
          721  +      pPage = 0;
          722  +    }else{
          723  +      pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
          724  +    }
          725  +  }
          726  +
          727  +  /* Step 5. If a usable page buffer has still not been found, 
          728  +  ** attempt to allocate a new one. 
          729  +  */
          730  +  if( !pPage ){
          731  +    if( createFlag==1 ) sqlite3BeginBenignMalloc();
          732  +    pPage = pcache1AllocPage(pCache);
          733  +    if( createFlag==1 ) sqlite3EndBenignMalloc();
          734  +  }
          735  +
          736  +  if( pPage ){
          737  +    unsigned int h = iKey % pCache->nHash;
          738  +    pCache->nPage++;
          739  +    pPage->iKey = iKey;
          740  +    pPage->pNext = pCache->apHash[h];
          741  +    pPage->pCache = pCache;
          742  +    pPage->pLruPrev = 0;
          743  +    pPage->pLruNext = 0;
          744  +    pPage->isPinned = 1;
          745  +    *(void **)pPage->page.pExtra = 0;
          746  +    pCache->apHash[h] = pPage;
          747  +    if( iKey>pCache->iMaxKey ){
          748  +      pCache->iMaxKey = iKey;
          749  +    }
          750  +  }
          751  +  return pPage;
          752  +}
          753  +
   659    754   /*
   660    755   ** Implementation of the sqlite3_pcache.xFetch method. 
   661    756   **
   662    757   ** Fetch a page by key value.
   663    758   **
   664    759   ** Whether or not a new page may be allocated by this function depends on
   665    760   ** the value of the createFlag argument.  0 means do not allocate a new
................................................................................
   711    806   **   5. Otherwise, allocate and return a new page buffer.
   712    807   */
   713    808   static sqlite3_pcache_page *pcache1Fetch(
   714    809     sqlite3_pcache *p, 
   715    810     unsigned int iKey, 
   716    811     int createFlag
   717    812   ){
   718         -  unsigned int nPinned;
   719    813     PCache1 *pCache = (PCache1 *)p;
   720         -  PGroup *pGroup;
   721    814     PgHdr1 *pPage = 0;
   722    815   
   723    816     assert( offsetof(PgHdr1,page)==0 );
   724    817     assert( pCache->bPurgeable || createFlag!=1 );
   725    818     assert( pCache->bPurgeable || pCache->nMin==0 );
   726    819     assert( pCache->bPurgeable==0 || pCache->nMin==10 );
   727    820     assert( pCache->nMin==0 || pCache->bPurgeable );
   728         -  pcache1EnterMutex(pGroup = pCache->pGroup);
          821  +  assert( pCache->nHash>0 );
          822  +  pcache1EnterMutex(pCache->pGroup);
   729    823   
   730    824     /* Step 1: Search the hash table for an existing entry. */
   731         -  if( pCache->nHash>0 ){
   732         -    unsigned int h = iKey % pCache->nHash;
   733         -    for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
   734         -  }
          825  +  pPage = pCache->apHash[iKey % pCache->nHash];
          826  +  while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
   735    827   
   736    828     /* Step 2: Abort if no existing page is found and createFlag is 0 */
   737    829     if( pPage ){
   738    830       if( !pPage->isPinned ) pcache1PinPage(pPage);
   739         -    goto fetch_out;
   740         -  }
   741         -  if( createFlag==0 ){
   742         -    goto fetch_out;
          831  +  }else if( createFlag ){
          832  +    /* Steps 3, 4, and 5 implemented by this subroutine */
          833  +    pPage = pcache1FetchStage2(pCache, iKey, createFlag);
   743    834     }
   744         -
   745         -  /* The pGroup local variable will normally be initialized by the
   746         -  ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
   747         -  ** then pcache1EnterMutex() is a no-op, so we have to initialize the
   748         -  ** local variable here.  Delaying the initialization of pGroup is an
   749         -  ** optimization:  The common case is to exit the module before reaching
   750         -  ** this point.
   751         -  */
   752         -#ifdef SQLITE_MUTEX_OMIT
   753         -  pGroup = pCache->pGroup;
   754         -#endif
   755         -
   756         -  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
   757         -  assert( pCache->nPage >= pCache->nRecyclable );
   758         -  nPinned = pCache->nPage - pCache->nRecyclable;
   759         -  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
   760         -  assert( pCache->n90pct == pCache->nMax*9/10 );
   761         -  if( createFlag==1 && (
   762         -        nPinned>=pGroup->mxPinned
   763         -     || nPinned>=pCache->n90pct
   764         -     || pcache1UnderMemoryPressure(pCache)
   765         -  )){
   766         -    goto fetch_out;
   767         -  }
   768         -
   769         -  if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
   770         -    goto fetch_out;
   771         -  }
   772         -  assert( pCache->nHash>0 && pCache->apHash );
   773         -
   774         -  /* Step 4. Try to recycle a page. */
   775         -  if( pCache->bPurgeable && pGroup->pLruTail && (
   776         -         (pCache->nPage+1>=pCache->nMax)
   777         -      || pGroup->nCurrentPage>=pGroup->nMaxPage
   778         -      || pcache1UnderMemoryPressure(pCache)
   779         -  )){
   780         -    PCache1 *pOther;
   781         -    pPage = pGroup->pLruTail;
   782         -    assert( pPage->isPinned==0 );
   783         -    pcache1RemoveFromHash(pPage);
   784         -    pcache1PinPage(pPage);
   785         -    pOther = pPage->pCache;
   786         -
   787         -    /* We want to verify that szPage and szExtra are the same for pOther
   788         -    ** and pCache.  Assert that we can verify this by comparing sums. */
   789         -    assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
   790         -    assert( pCache->szExtra<512 );
   791         -    assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
   792         -    assert( pOther->szExtra<512 );
   793         -
   794         -    if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
   795         -      pcache1FreePage(pPage);
   796         -      pPage = 0;
   797         -    }else{
   798         -      pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
   799         -    }
   800         -  }
   801         -
   802         -  /* Step 5. If a usable page buffer has still not been found, 
   803         -  ** attempt to allocate a new one. 
   804         -  */
   805         -  if( !pPage ){
   806         -    if( createFlag==1 ) sqlite3BeginBenignMalloc();
   807         -    pPage = pcache1AllocPage(pCache);
   808         -    if( createFlag==1 ) sqlite3EndBenignMalloc();
   809         -  }
   810         -
   811         -  if( pPage ){
   812         -    unsigned int h = iKey % pCache->nHash;
   813         -    pCache->nPage++;
   814         -    pPage->iKey = iKey;
   815         -    pPage->pNext = pCache->apHash[h];
   816         -    pPage->pCache = pCache;
   817         -    pPage->pLruPrev = 0;
   818         -    pPage->pLruNext = 0;
   819         -    pPage->isPinned = 1;
   820         -    *(void **)pPage->page.pExtra = 0;
   821         -    pCache->apHash[h] = pPage;
   822         -  }
   823         -
   824         -fetch_out:
   825         -  if( pPage && iKey>pCache->iMaxKey ){
   826         -    pCache->iMaxKey = iKey;
   827         -  }
   828         -  pcache1LeaveMutex(pGroup);
          835  +  assert( pPage==0 || pCache->iMaxKey>=iKey );
          836  +  pcache1LeaveMutex(pCache->pGroup);
   829    837     return (sqlite3_pcache_page*)pPage;
   830    838   }
   831    839   
   832    840   
   833    841   /*
   834    842   ** Implementation of the sqlite3_pcache.xUnpin method.
   835    843   **

Changes to src/pragma.c.

   476    476   ** if the omitFull parameter it 1.
   477    477   **
   478    478   ** Note that the values returned are one less that the values that
   479    479   ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
   480    480   ** to support legacy SQL code.  The safety level used to be boolean
   481    481   ** and older scripts may have used numbers 0 for OFF and 1 for ON.
   482    482   */
   483         -static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
          483  +static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
   484    484                                /* 123456789 123456789 */
   485    485     static const char zText[] = "onoffalseyestruefull";
   486    486     static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
   487    487     static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
   488    488     static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
   489    489     int i, n;
   490    490     if( sqlite3Isdigit(*z) ){
................................................................................
   498    498     }
   499    499     return dflt;
   500    500   }
   501    501   
   502    502   /*
   503    503   ** Interpret the given string as a boolean value.
   504    504   */
   505         -u8 sqlite3GetBoolean(const char *z, int dflt){
          505  +u8 sqlite3GetBoolean(const char *z, u8 dflt){
   506    506     return getSafetyLevel(z,1,dflt)!=0;
   507    507   }
   508    508   
   509    509   /* The sqlite3GetBoolean() function is used by other modules but the
   510    510   ** remainder of this file is specific to PRAGMA processing.  So omit
   511    511   ** the rest of the file if PRAGMAs are omitted from the build.
   512    512   */
................................................................................
  1540   1540         sqlite3CodeVerifySchema(pParse, iDb);
  1541   1541         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
  1542   1542         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
  1543   1543         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
  1544   1544         for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
  1545   1545           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
  1546   1546           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
  1547         -        sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
         1547  +        sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
  1548   1548           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
  1549   1549         }
  1550   1550       }
  1551   1551     }
  1552   1552     break;
  1553   1553   
  1554   1554     case PragTyp_DATABASE_LIST: {
................................................................................
  1790   1790   
  1791   1791       /* Code that appears at the end of the integrity check.  If no error
  1792   1792       ** messages have been generated, output OK.  Otherwise output the
  1793   1793       ** error message
  1794   1794       */
  1795   1795       static const int iLn = VDBE_OFFSET_LINENO(2);
  1796   1796       static const VdbeOpList endCode[] = {
  1797         -      { OP_AddImm,      1, 0,        0},    /* 0 */
  1798         -      { OP_IfNeg,       1, 0,        0},    /* 1 */
  1799         -      { OP_String8,     0, 3,        0},    /* 2 */
         1797  +      { OP_IfNeg,       1, 0,        0},    /* 0 */
         1798  +      { OP_String8,     0, 3,        0},    /* 1 */
  1800   1799         { OP_ResultRow,   3, 1,        0},
  1801   1800       };
  1802   1801   
  1803   1802       int isQuick = (sqlite3Tolower(zLeft[0])=='q');
  1804   1803   
  1805   1804       /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
  1806   1805       ** then iDb is set to the index of the database identified by <db>.
................................................................................
  1904   1903           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
  1905   1904           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1906   1905             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
  1907   1906           }
  1908   1907           pParse->nMem = MAX(pParse->nMem, 8+j);
  1909   1908           sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
  1910   1909           loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
         1910  +        /* Verify that all NOT NULL columns really are NOT NULL */
         1911  +        for(j=0; j<pTab->nCol; j++){
         1912  +          char *zErr;
         1913  +          int jmp2, jmp3;
         1914  +          if( j==pTab->iPKey ) continue;
         1915  +          if( pTab->aCol[j].notNull==0 ) continue;
         1916  +          sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
         1917  +          sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
         1918  +          jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
         1919  +          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
         1920  +          zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
         1921  +                              pTab->aCol[j].zName);
         1922  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
         1923  +          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
         1924  +          jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
         1925  +          sqlite3VdbeAddOp0(v, OP_Halt);
         1926  +          sqlite3VdbeJumpHere(v, jmp2);
         1927  +          sqlite3VdbeJumpHere(v, jmp3);
         1928  +        }
         1929  +        /* Validate index entries for the current row */
  1911   1930           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1912         -          int jmp2, jmp3, jmp4;
         1931  +          int jmp2, jmp3, jmp4, jmp5;
         1932  +          int ckUniq = sqlite3VdbeMakeLabel(v);
  1913   1933             if( pPk==pIdx ) continue;
  1914   1934             r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
  1915   1935                                          pPrior, r1);
  1916   1936             pPrior = pIdx;
  1917   1937             sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
  1918         -          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
         1938  +          /* Verify that an index entry exists for the current table row */
         1939  +          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
  1919   1940                                         pIdx->nColumn); VdbeCoverage(v);
  1920   1941             sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
  1921   1942             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
  1922   1943             sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
  1923         -          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
  1924         -                            P4_STATIC);
         1944  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, 
         1945  +                            " missing from index ", P4_STATIC);
  1925   1946             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1926         -          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
         1947  +          jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
         1948  +                                   pIdx->zName, P4_TRANSIENT);
  1927   1949             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1928   1950             sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
  1929   1951             jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
  1930   1952             sqlite3VdbeAddOp0(v, OP_Halt);
  1931         -          sqlite3VdbeJumpHere(v, jmp4);
  1932   1953             sqlite3VdbeJumpHere(v, jmp2);
         1954  +          /* For UNIQUE indexes, verify that only one entry exists with the
         1955  +          ** current key.  The entry is unique if (1) any column is NULL
         1956  +          ** or (2) the next entry has a different key */
         1957  +          if( IsUniqueIndex(pIdx) ){
         1958  +            int uniqOk = sqlite3VdbeMakeLabel(v);
         1959  +            int jmp6;
         1960  +            int kk;
         1961  +            for(kk=0; kk<pIdx->nKeyCol; kk++){
         1962  +              int iCol = pIdx->aiColumn[kk];
         1963  +              assert( iCol>=0 && iCol<pTab->nCol );
         1964  +              if( pTab->aCol[iCol].notNull ) continue;
         1965  +              sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
         1966  +              VdbeCoverage(v);
         1967  +            }
         1968  +            jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
         1969  +            sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
         1970  +            sqlite3VdbeJumpHere(v, jmp6);
         1971  +            sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
         1972  +                                 pIdx->nKeyCol); VdbeCoverage(v);
         1973  +            sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
         1974  +            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
         1975  +                              "non-unique entry in index ", P4_STATIC);
         1976  +            sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
         1977  +            sqlite3VdbeResolveLabel(v, uniqOk);
         1978  +          }
         1979  +          sqlite3VdbeJumpHere(v, jmp4);
  1933   1980             sqlite3ResolvePartIdxLabel(pParse, jmp3);
  1934   1981           }
  1935   1982           sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
  1936   1983           sqlite3VdbeJumpHere(v, loopTop-1);
  1937   1984   #ifndef SQLITE_OMIT_BTREECOUNT
  1938   1985           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
  1939   1986                        "wrong # of entries in index ", P4_STATIC);
................................................................................
  1950   1997             sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
  1951   1998             sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
  1952   1999           }
  1953   2000   #endif /* SQLITE_OMIT_BTREECOUNT */
  1954   2001         } 
  1955   2002       }
  1956   2003       addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
  1957         -    sqlite3VdbeChangeP2(v, addr, -mxErr);
  1958         -    sqlite3VdbeJumpHere(v, addr+1);
  1959         -    sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
         2004  +    sqlite3VdbeChangeP3(v, addr, -mxErr);
         2005  +    sqlite3VdbeJumpHere(v, addr);
         2006  +    sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
  1960   2007     }
  1961   2008     break;
  1962   2009   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  1963   2010   
  1964   2011   #ifndef SQLITE_OMIT_UTF16
  1965   2012     /*
  1966   2013     **   PRAGMA encoding

Changes to src/prepare.c.

   589    589     for(i=0; i<db->nDb; i++) {
   590    590       Btree *pBt = db->aDb[i].pBt;
   591    591       if( pBt ){
   592    592         assert( sqlite3BtreeHoldsMutex(pBt) );
   593    593         rc = sqlite3BtreeSchemaLocked(pBt);
   594    594         if( rc ){
   595    595           const char *zDb = db->aDb[i].zName;
   596         -        sqlite3Error(db, rc, "database schema is locked: %s", zDb);
          596  +        sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
   597    597           testcase( db->flags & SQLITE_ReadUncommitted );
   598    598           goto end_prepare;
   599    599         }
   600    600       }
   601    601     }
   602    602   
   603    603     sqlite3VtabUnlockList(db);
................................................................................
   606    606     pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
   607    607     if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
   608    608       char *zSqlCopy;
   609    609       int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
   610    610       testcase( nBytes==mxLen );
   611    611       testcase( nBytes==mxLen+1 );
   612    612       if( nBytes>mxLen ){
   613         -      sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
          613  +      sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
   614    614         rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
   615    615         goto end_prepare;
   616    616       }
   617    617       zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
   618    618       if( zSqlCopy ){
   619    619         sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
   620    620         sqlite3DbFree(db, zSqlCopy);
................................................................................
   673    673       sqlite3VdbeFinalize(pParse->pVdbe);
   674    674       assert(!(*ppStmt));
   675    675     }else{
   676    676       *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
   677    677     }
   678    678   
   679    679     if( zErrMsg ){
   680         -    sqlite3Error(db, rc, "%s", zErrMsg);
          680  +    sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
   681    681       sqlite3DbFree(db, zErrMsg);
   682    682     }else{
   683         -    sqlite3Error(db, rc, 0);
          683  +    sqlite3Error(db, rc);
   684    684     }
   685    685   
   686    686     /* Delete any TriggerPrg structures allocated while parsing this statement. */
   687    687     while( pParse->pTriggerPrg ){
   688    688       TriggerPrg *pT = pParse->pTriggerPrg;
   689    689       pParse->pTriggerPrg = pT->pNext;
   690    690       sqlite3DbFree(db, pT);

Changes to src/printf.c.

   780    780   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
   781    781   ** So enlarge if first, then do the append.
   782    782   **
   783    783   ** This is a helper routine to sqlite3StrAccumAppend() that does special-case
   784    784   ** work (enlarging the buffer) using tail recursion, so that the
   785    785   ** sqlite3StrAccumAppend() routine can use fast calling semantics.
   786    786   */
   787         -static void enlargeAndAppend(StrAccum *p, const char *z, int N){
          787  +static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
   788    788     N = sqlite3StrAccumEnlarge(p, N);
   789    789     if( N>0 ){
   790    790       memcpy(&p->zText[p->nChar], z, N);
   791    791       p->nChar += N;
   792    792     }
   793    793   }
   794    794   
................................................................................
   799    799   void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
   800    800     assert( z!=0 );
   801    801     assert( p->zText!=0 || p->nChar==0 || p->accError );
   802    802     assert( N>=0 );
   803    803     assert( p->accError==0 || p->nAlloc==0 );
   804    804     if( p->nChar+N >= p->nAlloc ){
   805    805       enlargeAndAppend(p,z,N);
   806         -    return;
          806  +  }else{
          807  +    assert( p->zText );
          808  +    p->nChar += N;
          809  +    memcpy(&p->zText[p->nChar-N], z, N);
   807    810     }
   808         -  assert( p->zText );
   809         -  memcpy(&p->zText[p->nChar], z, N);
   810         -  p->nChar += N;
   811    811   }
   812    812   
   813    813   /*
   814    814   ** Append the complete text of zero-terminated string z[] to the p string.
   815    815   */
   816    816   void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
   817    817     sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));

Changes to src/resolve.c.

   350    350               if( iCol==pTab->iPKey ){
   351    351                 iCol = -1;
   352    352               }
   353    353               break;
   354    354             }
   355    355           }
   356    356           if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
   357         -          /* IMP: R-24309-18625 */
          357  +          /* IMP: R-51414-32910 */
   358    358             /* IMP: R-44911-55124 */
   359    359             iCol = -1;
   360    360           }
   361    361           if( iCol<pTab->nCol ){
   362    362             cnt++;
   363    363             if( iCol<0 ){
   364    364               pExpr->affinity = SQLITE_AFF_INTEGER;
................................................................................
   706    706                                         "constant between 0.0 and 1.0");
   707    707                 pNC->nErr++;
   708    708               }
   709    709             }else{
   710    710               /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to
   711    711               ** likelihood(X, 0.0625).
   712    712               ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for
   713         -            ** likelihood(X,0.0625). */
          713  +            ** likelihood(X,0.0625).
          714  +            ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for
          715  +            ** likelihood(X,0.9375).
          716  +            ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to
          717  +            ** likelihood(X,0.9375). */
   714    718               /* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
   715    719               pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938;
   716    720             }             
   717    721           }
   718    722         }
   719    723   #ifndef SQLITE_OMIT_AUTHORIZATION
   720    724         if( pDef ){

Changes to src/select.c.

   537    537   static void codeOffset(
   538    538     Vdbe *v,          /* Generate code into this VM */
   539    539     int iOffset,      /* Register holding the offset counter */
   540    540     int iContinue     /* Jump here to skip the current record */
   541    541   ){
   542    542     if( iOffset>0 ){
   543    543       int addr;
   544         -    sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
   545         -    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
          544  +    addr = sqlite3VdbeAddOp3(v, OP_IfNeg, iOffset, 0, -1); VdbeCoverage(v);
   546    545       sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
   547    546       VdbeComment((v, "skip OFFSET records"));
   548    547       sqlite3VdbeJumpHere(v, addr);
   549    548     }
   550    549   }
   551    550   
   552    551   /*
................................................................................
   703    702             }else{
   704    703               sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
   705    704               VdbeCoverage(v);
   706    705              }
   707    706             sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
   708    707             sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
   709    708           }
   710         -        assert( sqlite3VdbeCurrentAddr(v)==iJump );
          709  +        assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
   711    710           sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
   712    711           break;
   713    712         }
   714    713   
   715    714         case WHERE_DISTINCT_UNIQUE: {
   716    715           sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
   717    716           break;
................................................................................
   826    825       ** of the scan loop.
   827    826       */
   828    827       case SRT_Mem: {
   829    828         assert( nResultCol==1 );
   830    829         if( pSort ){
   831    830           pushOntoSorter(pParse, pSort, p, regResult);
   832    831         }else{
   833         -        sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
          832  +        assert( regResult==iParm );
   834    833           /* The LIMIT clause will jump out of the loop for us */
   835    834         }
   836    835         break;
   837    836       }
   838    837   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
   839    838   
   840    839       case SRT_Coroutine:       /* Send data to a co-routine */

Changes to src/shell.c.

   428    428       fflush(stdout);
   429    429       zResult = local_getline(zPrior, stdin);
   430    430   #endif
   431    431     }
   432    432     return zResult;
   433    433   }
   434    434   
   435         -struct previous_mode_data {
   436         -  int valid;        /* Is there legit data in here? */
   437         -  int mode;
   438         -  int showHeader;
   439         -  int colWidth[100];
          435  +/*
          436  +** Shell output mode information from before ".explain on", 
          437  +** saved so that it can be restored by ".explain off"
          438  +*/
          439  +typedef struct SavedModeInfo SavedModeInfo;
          440  +struct SavedModeInfo {
          441  +  int valid;          /* Is there legit data in here? */
          442  +  int mode;           /* Mode prior to ".explain on" */
          443  +  int showHeader;     /* The ".header" setting prior to ".explain on" */
          444  +  int colWidth[100];  /* Column widths prior to ".explain on" */
   440    445   };
   441    446   
   442    447   /*
   443         -** An pointer to an instance of this structure is passed from
   444         -** the main program to the callback.  This is used to communicate
   445         -** state and mode information.
          448  +** State information about the database connection is contained in an
          449  +** instance of the following structure.
   446    450   */
   447         -struct callback_data {
          451  +typedef struct ShellState ShellState;
          452  +struct ShellState {
   448    453     sqlite3 *db;           /* The database */
   449    454     int echoOn;            /* True to echo input commands */
   450    455     int autoEQP;           /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
   451    456     int statsOn;           /* True to display memory stats before each finalize */
   452    457     int outCount;          /* Revert to stdout when reaching zero */
   453    458     int cnt;               /* Number of records displayed so far */
   454    459     FILE *out;             /* Write results here */
   455    460     FILE *traceOut;        /* Output for sqlite3_trace() */
   456    461     int nErr;              /* Number of errors seen */
   457    462     int mode;              /* An output mode setting */
   458    463     int writableSchema;    /* True if PRAGMA writable_schema=ON */
   459    464     int showHeader;        /* True to show column names in List or Column mode */
          465  +  unsigned shellFlgs;    /* Various flags */
   460    466     char *zDestTable;      /* Name of destination table when MODE_Insert */
   461    467     char colSeparator[20]; /* Column separator character for several modes */
   462    468     char rowSeparator[20]; /* Row separator character for MODE_Ascii */
   463    469     char newline[20];      /* Record separator in MODE_Csv */
   464    470     int colWidth[100];     /* Requested width of each column when in column mode*/
   465    471     int actualWidth[100];  /* Actual width of each column */
   466    472     char nullvalue[20];    /* The text to print when a NULL comes back from
   467    473                            ** the database */
   468         -  struct previous_mode_data explainPrev;
   469         -                         /* Holds the mode information just before
   470         -                         ** .explain ON */
          474  +  SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
   471    475     char outfile[FILENAME_MAX]; /* Filename for *out */
   472    476     const char *zDbFilename;    /* name of the database file */
   473    477     char *zFreeOnClose;         /* Filename to free when closing */
   474    478     const char *zVfs;           /* Name of VFS to use */
   475    479     sqlite3_stmt *pStmt;   /* Current statement if any. */
   476    480     FILE *pLog;            /* Write log output here */
   477    481     int *aiIndent;         /* Array of indents used in MODE_Explain */
   478    482     int nIndent;           /* Size of array aiIndent[] */
   479    483     int iIndent;           /* Index of current op in aiIndent[] */
   480    484   };
   481    485   
          486  +/*
          487  +** These are the allowed shellFlgs values
          488  +*/
          489  +#define SHFLG_Scratch     0x00001     /* The --scratch option is used */
          490  +#define SHFLG_Pagecache   0x00002     /* The --pagecache option is used */
          491  +#define SHFLG_Lookaside   0x00004     /* Lookaside memory is used */
          492  +
   482    493   /*
   483    494   ** These are the allowed modes.
   484    495   */
   485    496   #define MODE_Line     0  /* One column per line.  Blank line between records */
   486    497   #define MODE_Column   1  /* One record per line in neat columns */
   487    498   #define MODE_List     2  /* One record per line with a separator */
   488    499   #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
................................................................................
   534    545     return 0x3fffffff & (int)(z2 - z);
   535    546   }
   536    547   
   537    548   /*
   538    549   ** A callback for the sqlite3_log() interface.
   539    550   */
   540    551   static void shellLog(void *pArg, int iErrCode, const char *zMsg){
   541         -  struct callback_data *p = (struct callback_data*)pArg;
          552  +  ShellState *p = (ShellState*)pArg;
   542    553     if( p->pLog==0 ) return;
   543    554     fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
   544    555     fflush(p->pLog);
   545    556   }
   546    557   
   547    558   /*
   548    559   ** Output the given string as a hex-encoded blob (eg. X'1234' )
................................................................................
   676    687   
   677    688   /*
   678    689   ** Output a single term of CSV.  Actually, p->separator is used for
   679    690   ** the separator, which may or may not be a comma.  p->nullvalue is
   680    691   ** the null value.  Strings are quoted if necessary.  The separator
   681    692   ** is only issued if bSep is true.
   682    693   */
   683         -static void output_csv(struct callback_data *p, const char *z, int bSep){
          694  +static void output_csv(ShellState *p, const char *z, int bSep){
   684    695     FILE *out = p->out;
   685    696     if( z==0 ){
   686    697       fprintf(out,"%s",p->nullvalue);
   687    698     }else{
   688    699       int i;
   689    700       int nSep = strlen30(p->colSeparator);
   690    701       for(i=0; z[i]; i++){
................................................................................
   725    736   
   726    737   /*
   727    738   ** This is the callback routine that the shell
   728    739   ** invokes for each row of a query result.
   729    740   */
   730    741   static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
   731    742     int i;
   732         -  struct callback_data *p = (struct callback_data*)pArg;
          743  +  ShellState *p = (ShellState*)pArg;
   733    744   
   734    745     switch( p->mode ){
   735    746       case MODE_Line: {
   736    747         int w = 5;
   737    748         if( azArg==0 ) break;
   738    749         for(i=0; i<nArg; i++){
   739    750           int len = strlen30(azCol[i] ? azCol[i] : "");
................................................................................
   956    967   */
   957    968   static int callback(void *pArg, int nArg, char **azArg, char **azCol){
   958    969     /* since we don't have type info, call the shell_callback with a NULL value */
   959    970     return shell_callback(pArg, nArg, azArg, azCol, NULL);
   960    971   }
   961    972   
   962    973   /*
   963         -** Set the destination table field of the callback_data structure to
          974  +** Set the destination table field of the ShellState structure to
   964    975   ** the name of the table given.  Escape any quote characters in the
   965    976   ** table name.
   966    977   */
   967         -static void set_table_name(struct callback_data *p, const char *zName){
          978  +static void set_table_name(ShellState *p, const char *zName){
   968    979     int i, n;
   969    980     int needQuote;
   970    981     char *z;
   971    982   
   972    983     if( p->zDestTable ){
   973    984       free(p->zDestTable);
   974    985       p->zDestTable = 0;
................................................................................
  1050   1061   **
  1051   1062   ** If the number of columns is 1 and that column contains text "--"
  1052   1063   ** then write the semicolon on a separate line.  That way, if a 
  1053   1064   ** "--" comment occurs at the end of the statement, the comment
  1054   1065   ** won't consume the semicolon terminator.
  1055   1066   */
  1056   1067   static int run_table_dump_query(
  1057         -  struct callback_data *p, /* Query context */
         1068  +  ShellState *p,           /* Query context */
  1058   1069     const char *zSelect,     /* SELECT statement to extract content */
  1059   1070     const char *zFirstRow    /* Print before first row, if not NULL */
  1060   1071   ){
  1061   1072     sqlite3_stmt *pSelect;
  1062   1073     int rc;
  1063   1074     int nResult;
  1064   1075     int i;
................................................................................
  1113   1124   }
  1114   1125   
  1115   1126   /*
  1116   1127   ** Display memory stats.
  1117   1128   */
  1118   1129   static int display_stats(
  1119   1130     sqlite3 *db,                /* Database to query */
  1120         -  struct callback_data *pArg, /* Pointer to struct callback_data */
         1131  +  ShellState *pArg,           /* Pointer to ShellState */
  1121   1132     int bReset                  /* True to reset the stats */
  1122   1133   ){
  1123   1134     int iCur;
  1124   1135     int iHiwtr;
  1125   1136   
  1126   1137     if( pArg && pArg->out ){
  1127   1138       
  1128   1139       iHiwtr = iCur = -1;
  1129   1140       sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
  1130   1141       fprintf(pArg->out, "Memory Used:                         %d (max %d) bytes\n", iCur, iHiwtr);
  1131   1142       iHiwtr = iCur = -1;
  1132   1143       sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
  1133   1144       fprintf(pArg->out, "Number of Outstanding Allocations:   %d (max %d)\n", iCur, iHiwtr);
  1134         -/*
  1135         -** Not currently used by the CLI.
  1136         -**    iHiwtr = iCur = -1;
  1137         -**    sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
  1138         -**    fprintf(pArg->out, "Number of Pcache Pages Used:         %d (max %d) pages\n", iCur, iHiwtr);
  1139         -*/
         1145  +    if( pArg->shellFlgs & SHFLG_Pagecache ){
         1146  +      iHiwtr = iCur = -1;
         1147  +      sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
         1148  +      fprintf(pArg->out, "Number of Pcache Pages Used:         %d (max %d) pages\n", iCur, iHiwtr);
         1149  +    }
  1140   1150       iHiwtr = iCur = -1;
  1141   1151       sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
  1142   1152       fprintf(pArg->out, "Number of Pcache Overflow Bytes:     %d (max %d) bytes\n", iCur, iHiwtr);
  1143         -/*
  1144         -** Not currently used by the CLI.
  1145         -**    iHiwtr = iCur = -1;
  1146         -**    sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
  1147         -**    fprintf(pArg->out, "Number of Scratch Allocations Used:  %d (max %d)\n", iCur, iHiwtr);
  1148         -*/
         1153  +    if( pArg->shellFlgs & SHFLG_Scratch ){
         1154  +      iHiwtr = iCur = -1;
         1155  +      sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
         1156  +      fprintf(pArg->out, "Number of Scratch Allocations Used:  %d (max %d)\n", iCur, iHiwtr);
         1157  +    }
  1149   1158       iHiwtr = iCur = -1;
  1150   1159       sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
  1151   1160       fprintf(pArg->out, "Number of Scratch Overflow Bytes:    %d (max %d) bytes\n", iCur, iHiwtr);
  1152   1161       iHiwtr = iCur = -1;
  1153   1162       sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
  1154   1163       fprintf(pArg->out, "Largest Allocation:                  %d bytes\n", iHiwtr);
  1155   1164       iHiwtr = iCur = -1;
................................................................................
  1162   1171       iHiwtr = iCur = -1;
  1163   1172       sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
  1164   1173       fprintf(pArg->out, "Deepest Parser Stack:                %d (max %d)\n", iCur, iHiwtr);
  1165   1174   #endif
  1166   1175     }
  1167   1176   
  1168   1177     if( pArg && pArg->out && db ){
  1169         -    iHiwtr = iCur = -1;
  1170         -    sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
  1171         -    fprintf(pArg->out, "Lookaside Slots Used:                %d (max %d)\n", iCur, iHiwtr);
  1172         -    sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
  1173         -    fprintf(pArg->out, "Successful lookaside attempts:       %d\n", iHiwtr);
  1174         -    sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
  1175         -    fprintf(pArg->out, "Lookaside failures due to size:      %d\n", iHiwtr);
  1176         -    sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
  1177         -    fprintf(pArg->out, "Lookaside failures due to OOM:       %d\n", iHiwtr);
         1178  +    if( pArg->shellFlgs & SHFLG_Lookaside ){
         1179  +      iHiwtr = iCur = -1;
         1180  +      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
         1181  +      fprintf(pArg->out, "Lookaside Slots Used:                %d (max %d)\n", iCur, iHiwtr);
         1182  +      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
         1183  +      fprintf(pArg->out, "Successful lookaside attempts:       %d\n", iHiwtr);
         1184  +      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
         1185  +      fprintf(pArg->out, "Lookaside failures due to size:      %d\n", iHiwtr);
         1186  +      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
         1187  +      fprintf(pArg->out, "Lookaside failures due to OOM:       %d\n", iHiwtr);
         1188  +    }
  1178   1189       iHiwtr = iCur = -1;
  1179   1190       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
  1180   1191       fprintf(pArg->out, "Pager Heap Usage:                    %d bytes\n", iCur);    iHiwtr = iCur = -1;
  1181   1192       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
  1182   1193       fprintf(pArg->out, "Page cache hits:                     %d\n", iCur);
  1183   1194       iHiwtr = iCur = -1;
  1184   1195       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
................................................................................
  1220   1231       if( 0==strcmp(zStr, azArray[i]) ) return 1;
  1221   1232     }
  1222   1233     return 0;
  1223   1234   }
  1224   1235   
  1225   1236   /*
  1226   1237   ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
  1227         -** and populate the callback_data.aiIndent[] array with the number of
         1238  +** and populate the ShellState.aiIndent[] array with the number of
  1228   1239   ** spaces each opcode should be indented before it is output. 
  1229   1240   **
  1230   1241   ** The indenting rules are:
  1231   1242   **
  1232   1243   **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
  1233   1244   **       all opcodes that occur between the p2 jump destination and the opcode
  1234   1245   **       itself by 2 spaces.
................................................................................
  1236   1247   **     * For each "Goto", if the jump destination is earlier in the program
  1237   1248   **       and ends on one of:
  1238   1249   **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
  1239   1250   **       or if the P1 parameter is one instead of zero,
  1240   1251   **       then indent all opcodes between the earlier instruction
  1241   1252   **       and "Goto" by 2 spaces.
  1242   1253   */
  1243         -static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
         1254  +static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
  1244   1255     const char *zSql;               /* The text of the SQL statement */
  1245   1256     const char *z;                  /* Used to check if this is an EXPLAIN */
  1246   1257     int *abYield = 0;               /* True if op is an OP_Yield */
  1247   1258     int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
  1248   1259     int iOp;                        /* Index of operation in p->aiIndent[] */
  1249   1260   
  1250   1261     const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
................................................................................
  1296   1307     sqlite3_free(abYield);
  1297   1308     sqlite3_reset(pSql);
  1298   1309   }
  1299   1310   
  1300   1311   /*
  1301   1312   ** Free the array allocated by explain_data_prepare().
  1302   1313   */
  1303         -static void explain_data_delete(struct callback_data *p){
         1314  +static void explain_data_delete(ShellState *p){
  1304   1315     sqlite3_free(p->aiIndent);
  1305   1316     p->aiIndent = 0;
  1306   1317     p->nIndent = 0;
  1307   1318     p->iIndent = 0;
  1308   1319   }
  1309   1320   
  1310   1321   /*
................................................................................
  1313   1324   ** set via the supplied callback.
  1314   1325   **
  1315   1326   ** This is very similar to SQLite's built-in sqlite3_exec() 
  1316   1327   ** function except it takes a slightly different callback 
  1317   1328   ** and callback data argument.
  1318   1329   */
  1319   1330   static int shell_exec(
  1320         -  sqlite3 *db,                                /* An open database */
  1321         -  const char *zSql,                           /* SQL to be evaluated */
         1331  +  sqlite3 *db,                              /* An open database */
         1332  +  const char *zSql,                         /* SQL to be evaluated */
  1322   1333     int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
  1323         -                                              /* (not the same as sqlite3_exec) */
  1324         -  struct callback_data *pArg,                 /* Pointer to struct callback_data */
  1325         -  char **pzErrMsg                             /* Error msg written here */
         1334  +                                            /* (not the same as sqlite3_exec) */
         1335  +  ShellState *pArg,                         /* Pointer to ShellState */
         1336  +  char **pzErrMsg                           /* Error msg written here */
  1326   1337   ){
  1327   1338     sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
  1328   1339     int rc = SQLITE_OK;             /* Return Code */
  1329   1340     int rc2;
  1330   1341     const char *zLeftover;          /* Tail of unprocessed SQL */
  1331   1342   
  1332   1343     if( pzErrMsg ){
................................................................................
  1486   1497   */
  1487   1498   static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
  1488   1499     int rc;
  1489   1500     const char *zTable;
  1490   1501     const char *zType;
  1491   1502     const char *zSql;
  1492   1503     const char *zPrepStmt = 0;
  1493         -  struct callback_data *p = (struct callback_data *)pArg;
         1504  +  ShellState *p = (ShellState *)pArg;
  1494   1505   
  1495   1506     UNUSED_PARAMETER(azCol);
  1496   1507     if( nArg!=3 ) return 1;
  1497   1508     zTable = azArg[0];
  1498   1509     zType = azArg[1];
  1499   1510     zSql = azArg[2];
  1500   1511     
................................................................................
  1582   1593   ** Run zQuery.  Use dump_callback() as the callback routine so that
  1583   1594   ** the contents of the query are output as SQL statements.
  1584   1595   **
  1585   1596   ** If we get a SQLITE_CORRUPT error, rerun the query after appending
  1586   1597   ** "ORDER BY rowid DESC" to the end.
  1587   1598   */
  1588   1599   static int run_schema_dump_query(
  1589         -  struct callback_data *p, 
         1600  +  ShellState *p, 
  1590   1601     const char *zQuery
  1591   1602   ){
  1592   1603     int rc;
  1593   1604     char *zErr = 0;
  1594   1605     rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
  1595   1606     if( rc==SQLITE_CORRUPT ){
  1596   1607       char *zQ2;
................................................................................
  1686   1697     ".trace FILE|off        Output each SQL statement as it is run\n"
  1687   1698     ".vfsname ?AUX?         Print the name of the VFS stack\n"
  1688   1699     ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
  1689   1700     "                         Negative values right-justify\n"
  1690   1701   ;
  1691   1702   
  1692   1703   /* Forward reference */
  1693         -static int process_input(struct callback_data *p, FILE *in);
         1704  +static int process_input(ShellState *p, FILE *in);
  1694   1705   /*
  1695   1706   ** Implementation of the "readfile(X)" SQL function.  The entire content
  1696   1707   ** of the file named X is read and returned as a BLOB.  NULL is returned
  1697   1708   ** if the file does not exist or is unreadable.
  1698   1709   */
  1699   1710   static void readfileFunc(
  1700   1711     sqlite3_context *context,
................................................................................
  1731   1742   static void writefileFunc(
  1732   1743     sqlite3_context *context,
  1733   1744     int argc,
  1734   1745     sqlite3_value **argv
  1735   1746   ){
  1736   1747     FILE *out;
  1737   1748     const char *z;
  1738         -  int n;
  1739   1749     sqlite3_int64 rc;
  1740   1750     const char *zFile;
  1741   1751   
  1742   1752     zFile = (const char*)sqlite3_value_text(argv[0]);
  1743   1753     if( zFile==0 ) return;
  1744   1754     out = fopen(zFile, "wb");
  1745   1755     if( out==0 ) return;
  1746   1756     z = (const char*)sqlite3_value_blob(argv[1]);
  1747   1757     if( z==0 ){
  1748         -    n = 0;
  1749   1758       rc = 0;
  1750   1759     }else{
  1751         -    n = sqlite3_value_bytes(argv[1]);
  1752         -    rc = fwrite(z, 1, n, out);
         1760  +    rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
  1753   1761     }
  1754   1762     fclose(out);
  1755   1763     sqlite3_result_int64(context, rc);
  1756   1764   }
  1757   1765   
  1758   1766   /*
  1759   1767   ** Make sure the database is open.  If it is not, then open it.  If
  1760   1768   ** the database fails to open, print an error message and exit.
  1761   1769   */
  1762         -static void open_db(struct callback_data *p, int keepAlive){
         1770  +static void open_db(ShellState *p, int keepAlive){
  1763   1771     if( p->db==0 ){
  1764   1772       sqlite3_initialize();
  1765   1773       sqlite3_open(p->zDbFilename, &p->db);
  1766   1774       db = p->db;
  1767   1775       if( db && sqlite3_errcode(db)==SQLITE_OK ){
  1768   1776         sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
  1769   1777             shellstaticFunc, 0, 0);
................................................................................
  1936   1944   }
  1937   1945   
  1938   1946   /*
  1939   1947   ** A routine for handling output from sqlite3_trace().
  1940   1948   */
  1941   1949   static void sql_trace_callback(void *pArg, const char *z){
  1942   1950     FILE *f = (FILE*)pArg;
  1943         -  if( f ) fprintf(f, "%s\n", z);
         1951  +  if( f ){
         1952  +    int i = (int)strlen(z);
         1953  +    while( i>0 && z[i-1]==';' ){ i--; }
         1954  +    fprintf(f, "%.*s;\n", i, z);
         1955  +  }
  1944   1956   }
  1945   1957   
  1946   1958   /*
  1947   1959   ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
  1948   1960   ** a useful spot to set a debugger breakpoint.
  1949   1961   */
  1950   1962   static void test_breakpoint(void){
................................................................................
  2092   2104   
  2093   2105   /*
  2094   2106   ** Try to transfer data for table zTable.  If an error is seen while
  2095   2107   ** moving forward, try to go backwards.  The backwards movement won't
  2096   2108   ** work for WITHOUT ROWID tables.
  2097   2109   */
  2098   2110   static void tryToCloneData(
  2099         -  struct callback_data *p,
         2111  +  ShellState *p,
  2100   2112     sqlite3 *newDb,
  2101   2113     const char *zTable
  2102   2114   ){
  2103   2115     sqlite3_stmt *pQuery = 0; 
  2104   2116     sqlite3_stmt *pInsert = 0;
  2105   2117     char *zQuery = 0;
  2106   2118     char *zInsert = 0;
................................................................................
  2205   2217   /*
  2206   2218   ** Try to transfer all rows of the schema that match zWhere.  For
  2207   2219   ** each row, invoke xForEach() on the object defined by that row.
  2208   2220   ** If an error is encountered while moving forward through the
  2209   2221   ** sqlite_master table, try again moving backwards.
  2210   2222   */
  2211   2223   static void tryToCloneSchema(
  2212         -  struct callback_data *p,
         2224  +  ShellState *p,
  2213   2225     sqlite3 *newDb,
  2214   2226     const char *zWhere,
  2215         -  void (*xForEach)(struct callback_data*,sqlite3*,const char*)
         2227  +  void (*xForEach)(ShellState*,sqlite3*,const char*)
  2216   2228   ){
  2217   2229     sqlite3_stmt *pQuery = 0;
  2218   2230     char *zQuery = 0;
  2219   2231     int rc;
  2220   2232     const unsigned char *zName;
  2221   2233     const unsigned char *zSql;
  2222   2234     char *zErrMsg = 0;
................................................................................
  2279   2291   }
  2280   2292   
  2281   2293   /*
  2282   2294   ** Open a new database file named "zNewDb".  Try to recover as much information
  2283   2295   ** as possible out of the main database (which might be corrupt) and write it
  2284   2296   ** into zNewDb.
  2285   2297   */
  2286         -static void tryToClone(struct callback_data *p, const char *zNewDb){
         2298  +static void tryToClone(ShellState *p, const char *zNewDb){
  2287   2299     int rc;
  2288   2300     sqlite3 *newDb = 0;
  2289   2301     if( access(zNewDb,0)==0 ){
  2290   2302       fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
  2291   2303       return;
  2292   2304     }
  2293   2305     rc = sqlite3_open(zNewDb, &newDb);
................................................................................
  2304   2316     }
  2305   2317     sqlite3_close(newDb);
  2306   2318   }
  2307   2319   
  2308   2320   /*
  2309   2321   ** Change the output file back to stdout
  2310   2322   */
  2311         -static void output_reset(struct callback_data *p){
         2323  +static void output_reset(ShellState *p){
  2312   2324     if( p->outfile[0]=='|' ){
  2313   2325       pclose(p->out);
  2314   2326     }else{
  2315   2327       output_file_close(p->out);
  2316   2328     }
  2317   2329     p->outfile[0] = 0;
  2318   2330     p->out = stdout;
................................................................................
  2320   2332   
  2321   2333   /*
  2322   2334   ** If an input line begins with "." then invoke this routine to
  2323   2335   ** process that line.
  2324   2336   **
  2325   2337   ** Return 1 on error, 2 to exit, and 0 otherwise.
  2326   2338   */
  2327         -static int do_meta_command(char *zLine, struct callback_data *p){
         2339  +static int do_meta_command(char *zLine, ShellState *p){
  2328   2340     int i = 1;
  2329   2341     int nArg = 0;
  2330   2342     int n, c;
  2331   2343     int rc = 0;
  2332   2344     char *azArg[50];
  2333   2345   
  2334   2346     /* Parse the input line into tokens.
................................................................................
  2438   2450       }else{
  2439   2451         fprintf(stderr, "Usage: .clone FILENAME\n");
  2440   2452         rc = 1;
  2441   2453       }
  2442   2454     }else
  2443   2455   
  2444   2456     if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
  2445         -    struct callback_data data;
         2457  +    ShellState data;
  2446   2458       char *zErrMsg = 0;
  2447   2459       open_db(p, 0);
  2448   2460       memcpy(&data, p, sizeof(data));
  2449   2461       data.showHeader = 1;
  2450   2462       data.mode = MODE_Column;
  2451   2463       data.colWidth[0] = 3;
  2452   2464       data.colWidth[1] = 15;
................................................................................
  2536   2548       if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
  2537   2549       rc = 2;
  2538   2550     }else
  2539   2551   
  2540   2552     if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
  2541   2553       int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
  2542   2554       if(val == 1) {
  2543         -      if(!p->explainPrev.valid) {
  2544         -        p->explainPrev.valid = 1;
  2545         -        p->explainPrev.mode = p->mode;
  2546         -        p->explainPrev.showHeader = p->showHeader;
  2547         -        memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
         2555  +      if(!p->normalMode.valid) {
         2556  +        p->normalMode.valid = 1;
         2557  +        p->normalMode.mode = p->mode;
         2558  +        p->normalMode.showHeader = p->showHeader;
         2559  +        memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
  2548   2560         }
  2549   2561         /* We could put this code under the !p->explainValid
  2550   2562         ** condition so that it does not execute if we are already in
  2551   2563         ** explain mode. However, always executing it allows us an easy
  2552   2564         ** was to reset to explain mode in case the user previously
  2553   2565         ** did an .explain followed by a .width, .mode or .header
  2554   2566         ** command.
................................................................................
  2560   2572         p->colWidth[1] = 13;                 /* opcode */
  2561   2573         p->colWidth[2] = 4;                  /* P1 */
  2562   2574         p->colWidth[3] = 4;                  /* P2 */
  2563   2575         p->colWidth[4] = 4;                  /* P3 */
  2564   2576         p->colWidth[5] = 13;                 /* P4 */
  2565   2577         p->colWidth[6] = 2;                  /* P5 */
  2566   2578         p->colWidth[7] = 13;                  /* Comment */
  2567         -    }else if (p->explainPrev.valid) {
  2568         -      p->explainPrev.valid = 0;
  2569         -      p->mode = p->explainPrev.mode;
  2570         -      p->showHeader = p->explainPrev.showHeader;
  2571         -      memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
         2579  +    }else if (p->normalMode.valid) {
         2580  +      p->normalMode.valid = 0;
         2581  +      p->mode = p->normalMode.mode;
         2582  +      p->showHeader = p->normalMode.showHeader;
         2583  +      memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
  2572   2584       }
  2573   2585     }else
  2574   2586   
  2575   2587     if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
  2576         -    struct callback_data data;
         2588  +    ShellState data;
  2577   2589       char *zErrMsg = 0;
  2578   2590       int doStats = 0;
  2579   2591       if( nArg!=1 ){
  2580   2592         fprintf(stderr, "Usage: .fullschema\n");
  2581   2593         rc = 1;
  2582   2594         goto meta_command_exit;
  2583   2595       }
................................................................................
  2586   2598       data.showHeader = 0;
  2587   2599       data.mode = MODE_Semi;
  2588   2600       rc = sqlite3_exec(p->db,
  2589   2601          "SELECT sql FROM"
  2590   2602          "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
  2591   2603          "     FROM sqlite_master UNION ALL"
  2592   2604          "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
  2593         -       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
         2605  +       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
  2594   2606          "ORDER BY rowid",
  2595   2607          callback, &data, &zErrMsg
  2596   2608       );
  2597   2609       if( rc==SQLITE_OK ){
  2598   2610         sqlite3_stmt *pStmt;
  2599   2611         rc = sqlite3_prepare_v2(p->db,
  2600   2612                  "SELECT rowid FROM sqlite_master"
................................................................................
  2817   2829       xCloser(sCtx.in);
  2818   2830       sqlite3_free(sCtx.z);
  2819   2831       sqlite3_finalize(pStmt);
  2820   2832       if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
  2821   2833     }else
  2822   2834   
  2823   2835     if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
  2824         -    struct callback_data data;
         2836  +    ShellState data;
  2825   2837       char *zErrMsg = 0;
  2826   2838       open_db(p, 0);
  2827   2839       memcpy(&data, p, sizeof(data));
  2828   2840       data.showHeader = 0;
  2829   2841       data.mode = MODE_List;
  2830   2842       if( nArg==1 ){
  2831   2843         rc = sqlite3_exec(p->db,
................................................................................
  3118   3130         fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
  3119   3131         rc = 1;
  3120   3132       }
  3121   3133       sqlite3_close(pSrc);
  3122   3134     }else
  3123   3135   
  3124   3136     if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
  3125         -    struct callback_data data;
         3137  +    ShellState data;
  3126   3138       char *zErrMsg = 0;
  3127   3139       open_db(p, 0);
  3128   3140       memcpy(&data, p, sizeof(data));
  3129   3141       data.showHeader = 0;
  3130   3142       data.mode = MODE_Semi;
  3131   3143       if( nArg==2 ){
  3132   3144         int i;
................................................................................
  3174   3186         }
  3175   3187       }else if( nArg==1 ){
  3176   3188         rc = sqlite3_exec(p->db,
  3177   3189            "SELECT sql FROM "
  3178   3190            "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
  3179   3191            "     FROM sqlite_master UNION ALL"
  3180   3192            "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
  3181         -         "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
         3193  +         "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
  3182   3194            "ORDER BY rowid",
  3183   3195            callback, &data, &zErrMsg
  3184   3196         );
  3185   3197       }else{
  3186   3198         fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
  3187   3199         rc = 1;
  3188   3200         goto meta_command_exit;
................................................................................
  3257   3269       }
  3258   3270     }else
  3259   3271   
  3260   3272     if( c=='s'
  3261   3273      && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
  3262   3274     ){
  3263   3275       char *zCmd;
  3264         -    int i;
         3276  +    int i, x;
  3265   3277       if( nArg<2 ){
  3266   3278         fprintf(stderr, "Usage: .system COMMAND\n");
  3267   3279         rc = 1;
  3268   3280         goto meta_command_exit;
  3269   3281       }
  3270   3282       zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
  3271   3283       for(i=2; i<nArg; i++){
  3272   3284         zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
  3273   3285                                zCmd, azArg[i]);
  3274   3286       }
  3275         -    (void)system(zCmd);
         3287  +    x = system(zCmd);
  3276   3288       sqlite3_free(zCmd);
         3289  +    if( x ) fprintf(stderr, "System command returns %d\n", x);
  3277   3290     }else
  3278   3291   
  3279   3292     if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
  3280   3293       int i;
  3281   3294       if( nArg!=1 ){
  3282   3295         fprintf(stderr, "Usage: .show\n");
  3283   3296         rc = 1;
  3284   3297         goto meta_command_exit;
  3285   3298       }
  3286   3299       fprintf(p->out,"%12.12s: %s\n","echo", p->echoOn ? "on" : "off");
  3287   3300       fprintf(p->out,"%12.12s: %s\n","eqp", p->autoEQP ? "on" : "off");
  3288         -    fprintf(p->out,"%12.12s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
         3301  +    fprintf(p->out,"%9.9s: %s\n","explain", p->normalMode.valid ? "on" :"off");
  3289   3302       fprintf(p->out,"%12.12s: %s\n","headers", p->showHeader ? "on" : "off");
  3290   3303       fprintf(p->out,"%12.12s: %s\n","mode", modeDescr[p->mode]);
  3291   3304       fprintf(p->out,"%12.12s: ", "nullvalue");
  3292   3305         output_c_string(p->out, p->nullvalue);
  3293   3306         fprintf(p->out, "\n");
  3294   3307       fprintf(p->out,"%12.12s: %s\n","output",
  3295   3308               strlen30(p->outfile) ? p->outfile : "stdout");
................................................................................
  3677   3690   ** is interactive - the user is typing it it.  Otherwise, input
  3678   3691   ** is coming from a file or device.  A prompt is issued and history
  3679   3692   ** is saved only if input is interactive.  An interrupt signal will
  3680   3693   ** cause this routine to exit immediately, unless input is interactive.
  3681   3694   **
  3682   3695   ** Return the number of errors.
  3683   3696   */
  3684         -static int process_input(struct callback_data *p, FILE *in){
         3697  +static int process_input(ShellState *p, FILE *in){
  3685   3698     char *zLine = 0;          /* A single input line */
  3686   3699     char *zSql = 0;           /* Accumulated SQL text */
  3687   3700     int nLine;                /* Length of current line */
  3688   3701     int nSql = 0;             /* Bytes of zSql[] used */
  3689   3702     int nAlloc = 0;           /* Allocated zSql[] space */
  3690   3703     int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
  3691   3704     char *zErrMsg;            /* Error message returned */
................................................................................
  3856   3869   /*
  3857   3870   ** Read input from the file given by sqliterc_override.  Or if that
  3858   3871   ** parameter is NULL, take input from ~/.sqliterc
  3859   3872   **
  3860   3873   ** Returns the number of errors.
  3861   3874   */
  3862   3875   static int process_sqliterc(
  3863         -  struct callback_data *p,        /* Configuration data */
         3876  +  ShellState *p,                  /* Configuration data */
  3864   3877     const char *sqliterc_override   /* Name of config file. NULL to use default */
  3865   3878   ){
  3866   3879     char *home_dir = NULL;
  3867   3880     const char *sqliterc = sqliterc_override;
  3868   3881     char *zBuf = 0;
  3869   3882     FILE *in = NULL;
  3870   3883     int rc = 0;
................................................................................
  3911   3924     "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
  3912   3925   #endif
  3913   3926     "   -help                show this message\n"
  3914   3927     "   -html                set output mode to HTML\n"
  3915   3928     "   -interactive         force interactive I/O\n"
  3916   3929     "   -line                set output mode to 'line'\n"
  3917   3930     "   -list                set output mode to 'list'\n"
         3931  +  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
  3918   3932     "   -mmap N              default mmap size set to N\n"
  3919   3933   #ifdef SQLITE_ENABLE_MULTIPLEX
  3920   3934     "   -multiplex           enable the multiplexor VFS\n"
  3921   3935   #endif
  3922   3936     "   -newline SEP         set newline character(s) for CSV\n"
  3923   3937     "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
         3938  +  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
  3924   3939     "   -rowseparator SEP    set output line separator. Default: '\\n'\n"
         3940  +  "   -scratch SIZE N      use N slots of SZ bytes each for scratch memory\n"
  3925   3941     "   -separator SEP       set output field separator. Default: '|'\n"
  3926   3942     "   -stats               print memory stats before each finalize\n"
  3927   3943     "   -version             show SQLite version\n"
  3928   3944     "   -vfs NAME            use NAME as the default VFS\n"
  3929   3945   #ifdef SQLITE_ENABLE_VFSTRACE
  3930   3946     "   -vfstrace            enable tracing of all VFS calls\n"
  3931   3947   #endif
................................................................................
  3942   3958     }
  3943   3959     exit(1);
  3944   3960   }
  3945   3961   
  3946   3962   /*
  3947   3963   ** Initialize the state information in data
  3948   3964   */
  3949         -static void main_init(struct callback_data *data) {
         3965  +static void main_init(ShellState *data) {
  3950   3966     memset(data, 0, sizeof(*data));
  3951   3967     data->mode = MODE_List;
  3952   3968     memcpy(data->colSeparator,SEP_Column, 2);
  3953   3969     memcpy(data->rowSeparator,SEP_Row, 2);
  3954   3970     memcpy(data->newline,SEP_CrLf, 3);
  3955   3971     data->showHeader = 0;
         3972  +  data->shellFlgs = SHFLG_Lookaside;
  3956   3973     sqlite3_config(SQLITE_CONFIG_URI, 1);
  3957   3974     sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
         3975  +  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
  3958   3976     sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
  3959   3977     sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
  3960         -  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
  3961   3978   }
  3962   3979   
  3963   3980   /*
  3964   3981   ** Output text to the console in a font that attracts extra attention.
  3965   3982   */
  3966   3983   #ifdef _WIN32
  3967   3984   static void printBold(const char *zText){
................................................................................
  3991   4008       exit(1);
  3992   4009     }
  3993   4010     return argv[i];
  3994   4011   }
  3995   4012   
  3996   4013   int main(int argc, char **argv){
  3997   4014     char *zErrMsg = 0;
  3998         -  struct callback_data data;
         4015  +  ShellState data;
  3999   4016     const char *zInitFile = 0;
  4000   4017     char *zFirstCmd = 0;
  4001   4018     int i;
  4002   4019     int rc = 0;
  4003   4020     int warnInmemoryDb = 0;
  4004   4021   
  4005   4022   #if USE_SYSTEM_SQLITE+0!=1
................................................................................
  4062   4079         sqlite3_int64 szHeap;
  4063   4080   
  4064   4081         zSize = cmdline_option_value(argc, argv, ++i);
  4065   4082         szHeap = integerValue(zSize);
  4066   4083         if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
  4067   4084         sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
  4068   4085   #endif
         4086  +    }else if( strcmp(z,"-scratch")==0 ){
         4087  +      int n, sz;
         4088  +      sz = integerValue(cmdline_option_value(argc,argv,++i));
         4089  +      if( sz>400000 ) sz = 400000;
         4090  +      if( sz<2500 ) sz = 2500;
         4091  +      n = integerValue(cmdline_option_value(argc,argv,++i));
         4092  +      if( n>10 ) n = 10;
         4093  +      if( n<1 ) n = 1;
         4094  +      sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n);
         4095  +      data.shellFlgs |= SHFLG_Scratch;
         4096  +    }else if( strcmp(z,"-pagecache")==0 ){
         4097  +      int n, sz;
         4098  +      sz = integerValue(cmdline_option_value(argc,argv,++i));
         4099  +      if( sz>70000 ) sz = 70000;
         4100  +      if( sz<800 ) sz = 800;
         4101  +      n = integerValue(cmdline_option_value(argc,argv,++i));
         4102  +      if( n<10 ) n = 10;
         4103  +      sqlite3_config(SQLITE_CONFIG_PAGECACHE, malloc(n*sz+1), sz, n);
         4104  +      data.shellFlgs |= SHFLG_Pagecache;
         4105  +    }else if( strcmp(z,"-lookaside")==0 ){
         4106  +      int n, sz;
         4107  +      sz = integerValue(cmdline_option_value(argc,argv,++i));
         4108  +      if( sz<0 ) sz = 0;
         4109  +      n = integerValue(cmdline_option_value(argc,argv,++i));
         4110  +      if( n<0 ) n = 0;
         4111  +      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
         4112  +      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
  4069   4113   #ifdef SQLITE_ENABLE_VFSTRACE
  4070   4114       }else if( strcmp(z,"-vfstrace")==0 ){
  4071   4115         extern int vfstrace_register(
  4072   4116            const char *zTraceName,
  4073   4117            const char *zOldVfsName,
  4074   4118            int (*xOut)(const char*,void*),
  4075   4119            void *pOutArg,
................................................................................
  4186   4230         return 0;
  4187   4231       }else if( strcmp(z,"-interactive")==0 ){
  4188   4232         stdin_is_interactive = 1;
  4189   4233       }else if( strcmp(z,"-batch")==0 ){
  4190   4234         stdin_is_interactive = 0;
  4191   4235       }else if( strcmp(z,"-heap")==0 ){
  4192   4236         i++;
         4237  +    }else if( strcmp(z,"-scratch")==0 ){
         4238  +      i+=2;
         4239  +    }else if( strcmp(z,"-pagecache")==0 ){
         4240  +      i+=2;
         4241  +    }else if( strcmp(z,"-lookaside")==0 ){
         4242  +      i+=2;
  4193   4243       }else if( strcmp(z,"-mmap")==0 ){
  4194   4244         i++;
  4195   4245       }else if( strcmp(z,"-vfs")==0 ){
  4196   4246         i++;
  4197   4247   #ifdef SQLITE_ENABLE_VFSTRACE
  4198   4248       }else if( strcmp(z,"-vfstrace")==0 ){
  4199   4249         i++;

Changes to src/sqlite.h.in.

   260    260   #endif
   261    261   
   262    262   /*
   263    263   ** CAPI3REF: Closing A Database Connection
   264    264   **
   265    265   ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   266    266   ** for the [sqlite3] object.
   267         -** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
          267  +** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
   268    268   ** the [sqlite3] object is successfully destroyed and all associated
   269    269   ** resources are deallocated.
   270    270   **
   271    271   ** ^If the database connection is associated with unfinalized prepared
   272    272   ** statements or unfinished sqlite3_backup objects then sqlite3_close()
   273    273   ** will leave the database connection open and return [SQLITE_BUSY].
   274    274   ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
   275         -** and unfinished sqlite3_backups, then the database connection becomes
          275  +** and/or unfinished sqlite3_backups, then the database connection becomes
   276    276   ** an unusable "zombie" which will automatically be deallocated when the
   277    277   ** last prepared statement is finalized or the last sqlite3_backup is
   278    278   ** finished.  The sqlite3_close_v2() interface is intended for use with
   279    279   ** host languages that are garbage collected, and where the order in which
   280    280   ** destructors are called is arbitrary.
   281    281   **
   282    282   ** Applications should [sqlite3_finalize | finalize] all [prepared statements],
   283    283   ** [sqlite3_blob_close | close] all [BLOB handles], and 
   284    284   ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
   285    285   ** with the [sqlite3] object prior to attempting to close the object.  ^If
   286    286   ** sqlite3_close_v2() is called on a [database connection] that still has
   287    287   ** outstanding [prepared statements], [BLOB handles], and/or
   288         -** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
          288  +** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
   289    289   ** of resources is deferred until all [prepared statements], [BLOB handles],
   290    290   ** and [sqlite3_backup] objects are also destroyed.
   291    291   **
   292    292   ** ^If an [sqlite3] object is destroyed while a transaction is open,
   293    293   ** the transaction is automatically rolled back.
   294    294   **
   295    295   ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
................................................................................
   377    377     int (*callback)(void*,int,char**,char**),  /* Callback function */
   378    378     void *,                                    /* 1st argument to callback */
   379    379     char **errmsg                              /* Error msg written here */
   380    380   );
   381    381   
   382    382   /*
   383    383   ** CAPI3REF: Result Codes
   384         -** KEYWORDS: SQLITE_OK {error code} {error codes}
   385         -** KEYWORDS: {result code} {result codes}
          384  +** KEYWORDS: {result code definitions}
   386    385   **
   387    386   ** Many SQLite functions return an integer result code from the set shown
   388    387   ** here in order to indicate success or failure.
   389    388   **
   390    389   ** New error codes may be added in future versions of SQLite.
   391    390   **
   392         -** See also: [SQLITE_IOERR_READ | extended result codes],
   393         -** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
          391  +** See also: [extended result code definitions]
   394    392   */
   395    393   #define SQLITE_OK           0   /* Successful result */
   396    394   /* beginning-of-error-codes */
   397    395   #define SQLITE_ERROR        1   /* SQL error or missing database */
   398    396   #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
   399    397   #define SQLITE_PERM         3   /* Access permission denied */
   400    398   #define SQLITE_ABORT        4   /* Callback routine requested an abort */
................................................................................
   424    422   #define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
   425    423   #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
   426    424   #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
   427    425   /* end-of-error-codes */
   428    426   
   429    427   /*
   430    428   ** CAPI3REF: Extended Result Codes
   431         -** KEYWORDS: {extended error code} {extended error codes}
   432         -** KEYWORDS: {extended result code} {extended result codes}
          429  +** KEYWORDS: {extended result code definitions}
   433    430   **
   434         -** In its default configuration, SQLite API routines return one of 26 integer
   435         -** [SQLITE_OK | result codes].  However, experience has shown that many of
          431  +** In its default configuration, SQLite API routines return one of 30 integer
          432  +** [result codes].  However, experience has shown that many of
   436    433   ** these result codes are too coarse-grained.  They do not provide as
   437    434   ** much information about problems as programmers might like.  In an effort to
   438    435   ** address this, newer versions of SQLite (version 3.3.8 and later) include
   439    436   ** support for additional result codes that provide more detailed information
   440         -** about errors. The extended result codes are enabled or disabled
          437  +** about errors. These [extended result codes] are enabled or disabled
   441    438   ** on a per database connection basis using the
   442         -** [sqlite3_extended_result_codes()] API.
   443         -**
   444         -** Some of the available extended result codes are listed here.
   445         -** One may expect the number of extended result codes will increase
   446         -** over time.  Software that uses extended result codes should expect
   447         -** to see new result codes in future releases of SQLite.
   448         -**
   449         -** The SQLITE_OK result code will never be extended.  It will always
   450         -** be exactly zero.
          439  +** [sqlite3_extended_result_codes()] API.  Or, the extended code for
          440  +** the most recent error can be obtained using
          441  +** [sqlite3_extended_errcode()].
   451    442   */
   452    443   #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
   453    444   #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
   454    445   #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
   455    446   #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
   456    447   #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
   457    448   #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
................................................................................
   676    667   ** integer opcode.  The third argument is a generic pointer intended to
   677    668   ** point to a structure that may contain arguments or space in which to
   678    669   ** write return values.  Potential uses for xFileControl() might be
   679    670   ** functions to enable blocking locks with timeouts, to change the
   680    671   ** locking strategy (for example to use dot-file locks), to inquire
   681    672   ** about the status of a lock, or to break stale locks.  The SQLite
   682    673   ** core reserves all opcodes less than 100 for its own use.
   683         -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
          674  +** A [file control opcodes | list of opcodes] less than 100 is available.
   684    675   ** Applications that define a custom xFileControl method should use opcodes
   685    676   ** greater than 100 to avoid conflicts.  VFS implementations should
   686    677   ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
   687    678   ** recognize.
   688    679   **
   689    680   ** The xSectorSize() method returns the sector size of the
   690    681   ** device that underlies the file.  The sector size is the
................................................................................
   749    740     int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
   750    741     /* Methods above are valid for version 3 */
   751    742     /* Additional methods may be added in future releases */
   752    743   };
   753    744   
   754    745   /*
   755    746   ** CAPI3REF: Standard File Control Opcodes
          747  +** KEYWORDS: {file control opcodes} {file control opcode}
   756    748   **
   757    749   ** These integer constants are opcodes for the xFileControl method
   758    750   ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
   759    751   ** interface.
   760    752   **
   761    753   ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
   762    754   ** opcode causes the xFileControl method to write the current state of
................................................................................
  2036   2028   ** that might be invoked with argument P whenever
  2037   2029   ** an attempt is made to access a database table associated with
  2038   2030   ** [database connection] D when another thread
  2039   2031   ** or process has the table locked.
  2040   2032   ** The sqlite3_busy_handler() interface is used to implement
  2041   2033   ** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
  2042   2034   **
  2043         -** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
         2035  +** ^If the busy callback is NULL, then [SQLITE_BUSY]
  2044   2036   ** is returned immediately upon encountering the lock.  ^If the busy callback
  2045   2037   ** is not NULL, then the callback might be invoked with two arguments.
  2046   2038   **
  2047   2039   ** ^The first argument to the busy handler is a copy of the void* pointer which
  2048   2040   ** is the third argument to sqlite3_busy_handler().  ^The second argument to
  2049   2041   ** the busy handler callback is the number of times that the busy handler has
  2050   2042   ** been invoked for the same locking event.  ^If the
  2051   2043   ** busy callback returns 0, then no additional attempts are made to
  2052         -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned
         2044  +** access the database and [SQLITE_BUSY] is returned
  2053   2045   ** to the application.
  2054   2046   ** ^If the callback returns non-zero, then another attempt
  2055   2047   ** is made to access the database and the cycle repeats.
  2056   2048   **
  2057   2049   ** The presence of a busy handler does not guarantee that it will be invoked
  2058   2050   ** when there is lock contention. ^If SQLite determines that invoking the busy
  2059   2051   ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
  2060         -** or [SQLITE_IOERR_BLOCKED] to the application instead of invoking the 
         2052  +** to the application instead of invoking the 
  2061   2053   ** busy handler.
  2062   2054   ** Consider a scenario where one process is holding a read lock that
  2063   2055   ** it is trying to promote to a reserved lock and
  2064   2056   ** a second process is holding a reserved lock that it is trying
  2065   2057   ** to promote to an exclusive lock.  The first process cannot proceed
  2066   2058   ** because it is blocked by the second and the second process cannot
  2067   2059   ** proceed because it is blocked by the first.  If both processes
................................................................................
  2068   2060   ** invoke the busy handlers, neither will make any progress.  Therefore,
  2069   2061   ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
  2070   2062   ** will induce the first process to release its read lock and allow
  2071   2063   ** the second process to proceed.
  2072   2064   **
  2073   2065   ** ^The default busy callback is NULL.
  2074   2066   **
  2075         -** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
  2076         -** when SQLite is in the middle of a large transaction where all the
  2077         -** changes will not fit into the in-memory cache.  SQLite will
  2078         -** already hold a RESERVED lock on the database file, but it needs
  2079         -** to promote this lock to EXCLUSIVE so that it can spill cache
  2080         -** pages into the database file without harm to concurrent
  2081         -** readers.  ^If it is unable to promote the lock, then the in-memory
  2082         -** cache will be left in an inconsistent state and so the error
  2083         -** code is promoted from the relatively benign [SQLITE_BUSY] to
  2084         -** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
  2085         -** forces an automatic rollback of the changes.  See the
  2086         -** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
  2087         -** CorruptionFollowingBusyError</a> wiki page for a discussion of why
  2088         -** this is important.
  2089         -**
  2090   2067   ** ^(There can only be a single busy handler defined for each
  2091   2068   ** [database connection].  Setting a new busy handler clears any
  2092   2069   ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
  2093   2070   ** or evaluating [PRAGMA busy_timeout=N] will change the
  2094   2071   ** busy handler and thus clear any previously set busy handler.
  2095   2072   **
  2096   2073   ** The busy callback should not take any actions which modify the
................................................................................
  2107   2084   ** CAPI3REF: Set A Busy Timeout
  2108   2085   **
  2109   2086   ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  2110   2087   ** for a specified amount of time when a table is locked.  ^The handler
  2111   2088   ** will sleep multiple times until at least "ms" milliseconds of sleeping
  2112   2089   ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
  2113   2090   ** the handler returns 0 which causes [sqlite3_step()] to return
  2114         -** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
         2091  +** [SQLITE_BUSY].
  2115   2092   **
  2116   2093   ** ^Calling this routine with an argument less than or equal to zero
  2117   2094   ** turns off all busy handlers.
  2118   2095   **
  2119   2096   ** ^(There can only be a single busy handler for a particular
  2120   2097   ** [database connection] any any given moment.  If another busy handler
  2121   2098   ** was defined  (using [sqlite3_busy_handler()]) prior to calling
................................................................................
  2519   2496   **
  2520   2497   ** The [sqlite3_set_authorizer | authorizer callback function] must
  2521   2498   ** return either [SQLITE_OK] or one of these two constants in order
  2522   2499   ** to signal SQLite whether or not the action is permitted.  See the
  2523   2500   ** [sqlite3_set_authorizer | authorizer documentation] for additional
  2524   2501   ** information.
  2525   2502   **
  2526         -** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
  2527         -** from the [sqlite3_vtab_on_conflict()] interface.
         2503  +** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
         2504  +** returned from the [sqlite3_vtab_on_conflict()] interface.
  2528   2505   */
  2529   2506   #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
  2530   2507   #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
  2531   2508   
  2532   2509   /*
  2533   2510   ** CAPI3REF: Authorizer Action Codes
  2534   2511   **
................................................................................
  5871   5848   ** to sqlite3_mutex_alloc() is one of these integer constants:
  5872   5849   **
  5873   5850   ** <ul>
  5874   5851   ** <li>  SQLITE_MUTEX_FAST
  5875   5852   ** <li>  SQLITE_MUTEX_RECURSIVE
  5876   5853   ** <li>  SQLITE_MUTEX_STATIC_MASTER
  5877   5854   ** <li>  SQLITE_MUTEX_STATIC_MEM
  5878         -** <li>  SQLITE_MUTEX_STATIC_MEM2
         5855  +** <li>  SQLITE_MUTEX_STATIC_OPEN
  5879   5856   ** <li>  SQLITE_MUTEX_STATIC_PRNG
  5880   5857   ** <li>  SQLITE_MUTEX_STATIC_LRU
  5881         -** <li>  SQLITE_MUTEX_STATIC_LRU2
         5858  +** <li>  SQLITE_MUTEX_STATIC_PMEM
         5859  +** <li>  SQLITE_MUTEX_STATIC_APP1
         5860  +** <li>  SQLITE_MUTEX_STATIC_APP2
  5882   5861   ** </ul>)^
  5883   5862   **
  5884   5863   ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
  5885   5864   ** cause sqlite3_mutex_alloc() to create
  5886   5865   ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  5887   5866   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  5888   5867   ** The mutex implementation does not need to make a distinction
................................................................................
  6078   6057   #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
  6079   6058   #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
  6080   6059   #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
  6081   6060   #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
  6082   6061   #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
  6083   6062   #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
  6084   6063   #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
         6064  +#define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
         6065  +#define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
         6066  +#define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
  6085   6067   
  6086   6068   /*
  6087   6069   ** CAPI3REF: Retrieve the mutex for a database connection
  6088   6070   **
  6089   6071   ** ^This interface returns a pointer the [sqlite3_mutex] object that 
  6090   6072   ** serializes access to the [database connection] given in the argument
  6091   6073   ** when the [threading mode] is Serialized.
................................................................................
  6173   6155   #define SQLITE_TESTCTRL_ISKEYWORD               16
  6174   6156   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6175   6157   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6176   6158   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
  6177   6159   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  6178   6160   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  6179   6161   #define SQLITE_TESTCTRL_BYTEORDER               22
  6180         -#define SQLITE_TESTCTRL_LAST                    22
         6162  +#define SQLITE_TESTCTRL_ISINIT                  23
         6163  +#define SQLITE_TESTCTRL_LAST                    23
  6181   6164   
  6182   6165   /*
  6183   6166   ** CAPI3REF: SQLite Runtime Status
  6184   6167   **
  6185   6168   ** ^This interface is used to retrieve runtime status information
  6186   6169   ** about the performance of SQLite, and optionally to reset various
  6187   6170   ** highwater marks.  ^The first argument is an integer code for
................................................................................
  7354   7337   ** of the SQL statement that triggered the call to the [xUpdate] method of the
  7355   7338   ** [virtual table].
  7356   7339   */
  7357   7340   int sqlite3_vtab_on_conflict(sqlite3 *);
  7358   7341   
  7359   7342   /*
  7360   7343   ** CAPI3REF: Conflict resolution modes
         7344  +** KEYWORDS: {conflict resolution mode}
  7361   7345   **
  7362   7346   ** These constants are returned by [sqlite3_vtab_on_conflict()] to
  7363   7347   ** inform a [virtual table] implementation what the [ON CONFLICT] mode
  7364   7348   ** is for the SQL statement being evaluated.
  7365   7349   **
  7366   7350   ** Note that the [SQLITE_IGNORE] constant is also used as a potential
  7367   7351   ** return value from the [sqlite3_set_authorizer()] callback and that

Changes to src/sqlite3.rc.

    31     31    */
    32     32   
    33     33   #if defined(_WIN32)
    34     34   LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
    35     35   #pragma code_page(1252)
    36     36   #endif /* defined(_WIN32) */
    37     37   
           38  +/*
           39  + * Icon
           40  + */
           41  +
           42  +#define IDI_SQLITE 101
           43  +
           44  +IDI_SQLITE ICON "..\\art\\sqlite370.ico"
           45  +
    38     46   /*
    39     47    * Version
    40     48    */
    41     49   
    42     50   VS_VERSION_INFO VERSIONINFO
    43     51     FILEVERSION SQLITE_RESOURCE_VERSION
    44     52     PRODUCTVERSION SQLITE_RESOURCE_VERSION

Changes to src/sqliteInt.h.

   149    149   # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
   150    150   # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
   151    151   #else                          /* Generates a warning - but it always works */
   152    152   # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
   153    153   # define SQLITE_PTR_TO_INT(X)  ((int)(X))
   154    154   #endif
   155    155   
          156  +/*
          157  +** A macro to hint to the compiler that a function should not be
          158  +** inlined.
          159  +*/
          160  +#if defined(__GNUC__)
          161  +#  define SQLITE_NOINLINE  __attribute__((noinline))
          162  +#elif defined(_MSC_VER)
          163  +#  define SQLITE_NOINLINE  __declspec(noinline)
          164  +#else
          165  +#  define SQLITE_NOINLINE
          166  +#endif
          167  +
   156    168   /*
   157    169   ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
   158    170   ** 0 means mutexes are permanently disable and the library is never
   159    171   ** threadsafe.  1 means the library is serialized which is the highest
   160    172   ** level of threadsafety.  2 means the library is multithreaded - multiple
   161    173   ** threads can use SQLite as long as no two threads try to use the same
   162    174   ** database connection at the same time.
................................................................................
   872    884     Hash tblHash;        /* All tables indexed by name */
   873    885     Hash idxHash;        /* All (named) indices indexed by name */
   874    886     Hash trigHash;       /* All triggers indexed by name */
   875    887     Hash fkeyHash;       /* All foreign keys by referenced table name */
   876    888     Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
   877    889     u8 file_format;      /* Schema format version for this file */
   878    890     u8 enc;              /* Text encoding used by this database */
   879         -  u16 flags;           /* Flags associated with this schema */
          891  +  u16 schemaFlags;     /* Flags associated with this schema */
   880    892     int cache_size;      /* Number of pages to use in the cache */
   881    893   };
   882    894   
   883    895   /*
   884    896   ** These macros can be used to test, set, or clear bits in the 
   885    897   ** Db.pSchema->flags field.
   886    898   */
   887         -#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
   888         -#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
   889         -#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
   890         -#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
          899  +#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
          900  +#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
          901  +#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->schemaFlags|=(P)
          902  +#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->schemaFlags&=~(P)
   891    903   
   892    904   /*
   893    905   ** Allowed values for the DB.pSchema->flags field.
   894    906   **
   895    907   ** The DB_SchemaLoaded flag is set after the database schema has been
   896    908   ** read into internal hash tables.
   897    909   **
................................................................................
  1716   1728   #define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
  1717   1729   #define SQLITE_IDXTYPE_UNIQUE      1   /* Implements a UNIQUE constraint */
  1718   1730   #define SQLITE_IDXTYPE_PRIMARYKEY  2   /* Is the PRIMARY KEY for the table */
  1719   1731   
  1720   1732   /* Return true if index X is a PRIMARY KEY index */
  1721   1733   #define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
  1722   1734   
         1735  +/* Return true if index X is a UNIQUE index */
         1736  +#define IsUniqueIndex(X)      ((X)->onError!=OE_None)
         1737  +
  1723   1738   /*
  1724   1739   ** Each sample stored in the sqlite_stat3 table is represented in memory 
  1725   1740   ** using a structure of this type.  See documentation at the top of the
  1726   1741   ** analyze.c source file for additional information.
  1727   1742   */
  1728   1743   struct IndexSample {
  1729   1744     void *p;          /* Pointer to sampled record */
................................................................................
  3288   3303   LogEst sqlite3LogEstFromDouble(double);
  3289   3304   #endif
  3290   3305   u64 sqlite3LogEstToInt(LogEst);
  3291   3306   
  3292   3307   /*
  3293   3308   ** Routines to read and write variable-length integers.  These used to
  3294   3309   ** be defined locally, but now we use the varint routines in the util.c
  3295         -** file.  Code should use the MACRO forms below, as the Varint32 versions
  3296         -** are coded to assume the single byte case is already handled (which 
  3297         -** the MACRO form does).
         3310  +** file.
  3298   3311   */
  3299   3312   int sqlite3PutVarint(unsigned char*, u64);
  3300         -int sqlite3PutVarint32(unsigned char*, u32);
  3301   3313   u8 sqlite3GetVarint(const unsigned char *, u64 *);
  3302   3314   u8 sqlite3GetVarint32(const unsigned char *, u32 *);
  3303   3315   int sqlite3VarintLen(u64 v);
  3304   3316   
  3305   3317   /*
  3306         -** The header of a record consists of a sequence variable-length integers.
  3307         -** These integers are almost always small and are encoded as a single byte.
  3308         -** The following macros take advantage this fact to provide a fast encode
  3309         -** and decode of the integers in a record header.  It is faster for the common
  3310         -** case where the integer is a single byte.  It is a little slower when the
  3311         -** integer is two or more bytes.  But overall it is faster.
  3312         -**
  3313         -** The following expressions are equivalent:
  3314         -**
  3315         -**     x = sqlite3GetVarint32( A, &B );
  3316         -**     x = sqlite3PutVarint32( A, B );
  3317         -**
  3318         -**     x = getVarint32( A, B );
  3319         -**     x = putVarint32( A, B );
  3320         -**
         3318  +** The common case is for a varint to be a single byte.  They following
         3319  +** macros handle the common case without a procedure call, but then call
         3320  +** the procedure for larger varints.
  3321   3321   */
  3322   3322   #define getVarint32(A,B)  \
  3323   3323     (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
  3324   3324   #define putVarint32(A,B)  \
  3325   3325     (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
  3326         -  sqlite3PutVarint32((A),(B)))
         3326  +  sqlite3PutVarint((A),(B)))
  3327   3327   #define getVarint    sqlite3GetVarint
  3328   3328   #define putVarint    sqlite3PutVarint
  3329   3329   
  3330   3330   
  3331   3331   const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
  3332   3332   void sqlite3TableAffinity(Vdbe*, Table*, int);
  3333   3333   char sqlite3CompareAffinity(Expr *pExpr, char aff2);
  3334   3334   int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
  3335   3335   char sqlite3ExprAffinity(Expr *pExpr);
  3336   3336   int sqlite3Atoi64(const char*, i64*, int, u8);
  3337   3337   int sqlite3DecOrHexToI64(const char*, i64*);
  3338         -void sqlite3Error(sqlite3*, int, const char*,...);
         3338  +void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
         3339  +void sqlite3Error(sqlite3*,int);
  3339   3340   void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  3340   3341   u8 sqlite3HexToInt(int h);
  3341   3342   int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  3342   3343   
  3343   3344   #if defined(SQLITE_TEST) 
  3344   3345   const char *sqlite3ErrName(int);
  3345   3346   #endif
................................................................................
  3360   3361   int sqlite3MulInt64(i64*,i64);
  3361   3362   int sqlite3AbsInt32(int);
  3362   3363   #ifdef SQLITE_ENABLE_8_3_NAMES
  3363   3364   void sqlite3FileSuffix3(const char*, char*);
  3364   3365   #else
  3365   3366   # define sqlite3FileSuffix3(X,Y)
  3366   3367   #endif
  3367         -u8 sqlite3GetBoolean(const char *z,int);
         3368  +u8 sqlite3GetBoolean(const char *z,u8);
  3368   3369   
  3369   3370   const void *sqlite3ValueText(sqlite3_value*, u8);
  3370   3371   int sqlite3ValueBytes(sqlite3_value*, u8);
  3371   3372   void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  3372   3373                           void(*)(void*));
  3373   3374   void sqlite3ValueSetNull(sqlite3_value*);
  3374   3375   void sqlite3ValueFree(sqlite3_value*);
................................................................................
  3584   3585     void sqlite3BeginBenignMalloc(void);
  3585   3586     void sqlite3EndBenignMalloc(void);
  3586   3587   #else
  3587   3588     #define sqlite3BeginBenignMalloc()
  3588   3589     #define sqlite3EndBenignMalloc()
  3589   3590   #endif
  3590   3591   
  3591         -#define IN_INDEX_ROWID           1
  3592         -#define IN_INDEX_EPH             2
  3593         -#define IN_INDEX_INDEX_ASC       3
  3594         -#define IN_INDEX_INDEX_DESC      4
  3595         -int sqlite3FindInIndex(Parse *, Expr *, int*);
         3592  +/*
         3593  +** Allowed return values from sqlite3FindInIndex()
         3594  +*/
         3595  +#define IN_INDEX_ROWID        1   /* Search the rowid of the table */
         3596  +#define IN_INDEX_EPH          2   /* Search an ephemeral b-tree */
         3597  +#define IN_INDEX_INDEX_ASC    3   /* Existing index ASCENDING */
         3598  +#define IN_INDEX_INDEX_DESC   4   /* Existing index DESCENDING */
         3599  +#define IN_INDEX_NOOP         5   /* No table available. Use comparisons */
         3600  +/*
         3601  +** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
         3602  +*/
         3603  +#define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
         3604  +#define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
         3605  +#define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
         3606  +int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
  3596   3607   
  3597   3608   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  3598   3609     int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  3599   3610     int sqlite3JournalSize(sqlite3_vfs *);
  3600   3611     int sqlite3JournalCreate(sqlite3_file *);
  3601   3612     int sqlite3JournalExists(sqlite3_file *p);
  3602   3613   #else

Changes to src/tclsqlite.c.

  2377   2377       }
  2378   2378       zTable = Tcl_GetString(objv[objc-3]);
  2379   2379       zColumn = Tcl_GetString(objv[objc-2]);
  2380   2380       rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow);
  2381   2381   
  2382   2382       if( rc==TCL_OK ){
  2383   2383         rc = createIncrblobChannel(
  2384         -          interp, pDb, zDb, zTable, zColumn, iRow, isReadonly
         2384  +          interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly
  2385   2385         );
  2386   2386       }
  2387   2387   #endif
  2388   2388       break;
  2389   2389     }
  2390   2390   
  2391   2391     /*

Changes to src/test1.c.

  6577   6577     extern int sqlite3_opentemp_count;
  6578   6578     extern int sqlite3_like_count;
  6579   6579     extern int sqlite3_xferopt_count;
  6580   6580     extern int sqlite3_pager_readdb_count;
  6581   6581     extern int sqlite3_pager_writedb_count;
  6582   6582     extern int sqlite3_pager_writej_count;
  6583   6583   #if SQLITE_OS_WIN
  6584         -  extern int sqlite3_os_type;
         6584  +  extern LONG volatile sqlite3_os_type;
  6585   6585   #endif
  6586   6586   #ifdef SQLITE_DEBUG
  6587   6587     extern int sqlite3WhereTrace;
  6588   6588     extern int sqlite3OSTrace;
  6589   6589     extern int sqlite3WalTrace;
  6590   6590   #endif
  6591   6591   #ifdef SQLITE_TEST
................................................................................
  6635   6635   #endif
  6636   6636   #ifndef SQLITE_OMIT_UTF16
  6637   6637     Tcl_LinkVar(interp, "sqlite_last_needed_collation",
  6638   6638         (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  6639   6639   #endif
  6640   6640   #if SQLITE_OS_WIN
  6641   6641     Tcl_LinkVar(interp, "sqlite_os_type",
  6642         -      (char*)&sqlite3_os_type, TCL_LINK_INT);
         6642  +      (char*)&sqlite3_os_type, TCL_LINK_LONG);
  6643   6643   #endif
  6644   6644   #ifdef SQLITE_TEST
  6645   6645     {
  6646   6646       static const char *query_plan = "*** OBSOLETE VARIABLE ***";
  6647   6647       Tcl_LinkVar(interp, "sqlite_query_plan",
  6648   6648          (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  6649   6649     }

Changes to src/test_intarray.c.

   212    212   ** Each intarray object corresponds to a virtual table in the TEMP table
   213    213   ** with a name of zName.
   214    214   **
   215    215   ** Destroy the intarray object by dropping the virtual table.  If not done
   216    216   ** explicitly by the application, the virtual table will be dropped implicitly
   217    217   ** by the system when the database connection is closed.
   218    218   */
   219         -int sqlite3_intarray_create(
          219  +SQLITE_API int sqlite3_intarray_create(
   220    220     sqlite3 *db,
   221    221     const char *zName,
   222    222     sqlite3_intarray **ppReturn
   223    223   ){
   224    224     int rc = SQLITE_OK;
   225    225   #ifndef SQLITE_OMIT_VIRTUALTABLE
   226    226     sqlite3_intarray *p;
................................................................................
   246    246   /*
   247    247   ** Bind a new array array of integers to a specific intarray object.
   248    248   **
   249    249   ** The array of integers bound must be unchanged for the duration of
   250    250   ** any query against the corresponding virtual table.  If the integer
   251    251   ** array does change or is deallocated undefined behavior will result.
   252    252   */
   253         -int sqlite3_intarray_bind(
          253  +SQLITE_API int sqlite3_intarray_bind(
   254    254     sqlite3_intarray *pIntArray,   /* The intarray object to bind to */
   255    255     int nElements,                 /* Number of elements in the intarray */
   256    256     sqlite3_int64 *aElements,      /* Content of the intarray */
   257    257     void (*xFree)(void*)           /* How to dispose of the intarray when done */
   258    258   ){
   259    259     if( pIntArray->xFree ){
   260    260       pIntArray->xFree(pIntArray->a);

Changes to src/test_intarray.h.

    98     98   ** Each intarray object corresponds to a virtual table in the TEMP table
    99     99   ** with a name of zName.
   100    100   **
   101    101   ** Destroy the intarray object by dropping the virtual table.  If not done
   102    102   ** explicitly by the application, the virtual table will be dropped implicitly
   103    103   ** by the system when the database connection is closed.
   104    104   */
   105         -int sqlite3_intarray_create(
          105  +SQLITE_API int sqlite3_intarray_create(
   106    106     sqlite3 *db,
   107    107     const char *zName,
   108    108     sqlite3_intarray **ppReturn
   109    109   );
   110    110   
   111    111   /*
   112    112   ** Bind a new array array of integers to a specific intarray object.
   113    113   **
   114    114   ** The array of integers bound must be unchanged for the duration of
   115    115   ** any query against the corresponding virtual table.  If the integer
   116    116   ** array does change or is deallocated undefined behavior will result.
   117    117   */
   118         -int sqlite3_intarray_bind(
          118  +SQLITE_API int sqlite3_intarray_bind(
   119    119     sqlite3_intarray *pIntArray,   /* The intarray object to bind to */
   120    120     int nElements,                 /* Number of elements in the intarray */
   121    121     sqlite3_int64 *aElements,      /* Content of the intarray */
   122    122     void (*xFree)(void*)           /* How to dispose of the intarray when done */
   123    123   );
   124    124   
   125    125   #ifdef __cplusplus
   126    126   }  /* End of the 'extern "C"' block */
   127    127   #endif
   128    128   #endif /* _INTARRAY_H_ */

Changes to src/test_multiplex.c.

  1172   1172   **
  1173   1173   ** All SQLite database connections must be closed before calling this
  1174   1174   ** routine.
  1175   1175   **
  1176   1176   ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
  1177   1177   ** shutting down in order to free all remaining multiplex groups.
  1178   1178   */
  1179         -int sqlite3_multiplex_shutdown(void){
         1179  +int sqlite3_multiplex_shutdown(int eForce){
         1180  +  int rc = SQLITE_OK;
  1180   1181     if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE;
  1181         -  if( gMultiplex.pGroups ) return SQLITE_MISUSE;
         1182  +  if( gMultiplex.pGroups ){
         1183  +    sqlite3_log(SQLITE_MISUSE, "sqlite3_multiplex_shutdown() called "
         1184  +                "while database connections are still open");
         1185  +    if( !eForce ) return SQLITE_MISUSE;
         1186  +    rc = SQLITE_MISUSE;
         1187  +  }
  1182   1188     gMultiplex.isInitialized = 0;
  1183   1189     sqlite3_mutex_free(gMultiplex.pMutex);
  1184   1190     sqlite3_vfs_unregister(&gMultiplex.sThisVfs);
  1185   1191     memset(&gMultiplex, 0, sizeof(gMultiplex));
  1186         -  return SQLITE_OK;
         1192  +  return rc;
  1187   1193   }
  1188   1194   
  1189   1195   /***************************** Test Code ***********************************/
  1190   1196   #ifdef SQLITE_TEST
  1191   1197   #include <tcl.h>
  1192   1198   extern const char *sqlite3ErrName(int);
  1193   1199   
................................................................................
  1232   1238     int objc,
  1233   1239     Tcl_Obj *CONST objv[]
  1234   1240   ){
  1235   1241     int rc;                         /* Value returned by multiplex_shutdown() */
  1236   1242   
  1237   1243     UNUSED_PARAMETER(clientData);
  1238   1244   
  1239         -  if( objc!=1 ){
  1240         -    Tcl_WrongNumArgs(interp, 1, objv, "");
         1245  +  if( objc==2 && strcmp(Tcl_GetString(objv[1]),"-force")!=0 ){
         1246  +    objc = 3;
         1247  +  }
         1248  +  if( (objc!=1 && objc!=2) ){
         1249  +    Tcl_WrongNumArgs(interp, 1, objv, "?-force?");
  1241   1250       return TCL_ERROR;
  1242   1251     }
  1243   1252   
  1244   1253     /* Call sqlite3_multiplex_shutdown() */
  1245         -  rc = sqlite3_multiplex_shutdown();
         1254  +  rc = sqlite3_multiplex_shutdown(objc==2);
  1246   1255     Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
  1247   1256   
  1248   1257     return TCL_OK;
  1249   1258   }
  1250   1259   
  1251   1260   /*
  1252   1261   ** tclcmd:  sqlite3_multiplex_dump

Changes to src/test_multiplex.h.

    86     86   **
    87     87   ** All SQLite database connections must be closed before calling this
    88     88   ** routine.
    89     89   **
    90     90   ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
    91     91   ** shutting down in order to free all remaining multiplex groups.
    92     92   */
    93         -extern int sqlite3_multiplex_shutdown(void);
           93  +extern int sqlite3_multiplex_shutdown(int eForce);
    94     94   
    95     95   #ifdef __cplusplus
    96     96   }  /* End of the 'extern "C"' block */
    97     97   #endif
    98     98   
    99     99   #endif /* _TEST_MULTIPLEX_H */

Changes to src/trigger.c.

   176    176     /* Check that the trigger name is not reserved and that no trigger of the
   177    177     ** specified name exists */
   178    178     zName = sqlite3NameFromToken(db, pName);
   179    179     if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   180    180       goto trigger_cleanup;
   181    181     }
   182    182     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   183         -  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
   184         -                      zName, sqlite3Strlen30(zName)) ){
          183  +  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
   185    184       if( !noErr ){
   186    185         sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
   187    186       }else{
   188    187         assert( !db->init.busy );
   189    188         sqlite3CodeVerifySchema(pParse, iDb);
   190    189       }
   191    190       goto trigger_cleanup;
................................................................................
   320    319           sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
   321    320     }
   322    321   
   323    322     if( db->init.busy ){
   324    323       Trigger *pLink = pTrig;
   325    324       Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
   326    325       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   327         -    pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
          326  +    pTrig = sqlite3HashInsert(pHash, zName, pTrig);
   328    327       if( pTrig ){
   329    328         db->mallocFailed = 1;
   330    329       }else if( pLink->pSchema==pLink->pTabSchema ){
   331    330         Table *pTab;
   332         -      int n = sqlite3Strlen30(pLink->table);
   333         -      pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
          331  +      pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
   334    332         assert( pTab!=0 );
   335    333         pLink->pNext = pTab->pTrigger;
   336    334         pTab->pTrigger = pLink;
   337    335       }
   338    336     }
   339    337   
   340    338   triggerfinish_cleanup:
................................................................................
   485    483   ** instead of the trigger name.
   486    484   **/
   487    485   void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
   488    486     Trigger *pTrigger = 0;
   489    487     int i;
   490    488     const char *zDb;
   491    489     const char *zName;
   492         -  int nName;
   493    490     sqlite3 *db = pParse->db;
   494    491   
   495    492     if( db->mallocFailed ) goto drop_trigger_cleanup;
   496    493     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   497    494       goto drop_trigger_cleanup;
   498    495     }
   499    496   
   500    497     assert( pName->nSrc==1 );
   501    498     zDb = pName->a[0].zDatabase;
   502    499     zName = pName->a[0].zName;
   503         -  nName = sqlite3Strlen30(zName);
   504    500     assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
   505    501     for(i=OMIT_TEMPDB; i<db->nDb; i++){
   506    502       int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
   507    503       if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
   508    504       assert( sqlite3SchemaMutexHeld(db, j, 0) );
   509         -    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
          505  +    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
   510    506       if( pTrigger ) break;
   511    507     }
   512    508     if( !pTrigger ){
   513    509       if( !noErr ){
   514    510         sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
   515    511       }else{
   516    512         sqlite3CodeVerifyNamedSchema(pParse, zDb);
................................................................................
   525    521   }
   526    522   
   527    523   /*
   528    524   ** Return a pointer to the Table structure for the table that a trigger
   529    525   ** is set on.
   530    526   */
   531    527   static Table *tableOfTrigger(Trigger *pTrigger){
   532         -  int n = sqlite3Strlen30(pTrigger->table);
   533         -  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
          528  +  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
   534    529   }
   535    530   
   536    531   
   537    532   /*
   538    533   ** Drop a trigger given a pointer to that trigger. 
   539    534   */
   540    535   void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
................................................................................
   598    593   */
   599    594   void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
   600    595     Trigger *pTrigger;
   601    596     Hash *pHash;
   602    597   
   603    598     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   604    599     pHash = &(db->aDb[iDb].pSchema->trigHash);
   605         -  pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
          600  +  pTrigger = sqlite3HashInsert(pHash, zName, 0);
   606    601     if( ALWAYS(pTrigger) ){
   607    602       if( pTrigger->pSchema==pTrigger->pTabSchema ){
   608    603         Table *pTab = tableOfTrigger(pTrigger);
   609    604         Trigger **pp;
   610    605         for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
   611    606         *pp = (*pp)->pNext;
   612    607       }

Changes to src/update.c.

   434    434       if( aToOpen[iDataCur-iBaseCur] ){
   435    435         assert( pPk!=0 );
   436    436         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
   437    437         VdbeCoverageNeverTaken(v);
   438    438       }
   439    439       labelContinue = labelBreak;
   440    440       sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
   441         -    VdbeCoverage(v);
          441  +    VdbeCoverageIf(v, pPk==0);
          442  +    VdbeCoverageIf(v, pPk!=0);
   442    443     }else if( pPk ){
   443    444       labelContinue = sqlite3VdbeMakeLabel(v);
   444    445       sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
   445    446       addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
   446    447       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
   447    448       VdbeCoverage(v);
   448    449     }else{

Changes to src/utf.c.

   195    195   
   196    196   #ifndef SQLITE_OMIT_UTF16
   197    197   /*
   198    198   ** This routine transforms the internal text encoding used by pMem to
   199    199   ** desiredEnc. It is an error if the string is already of the desired
   200    200   ** encoding, or if *pMem does not contain a string value.
   201    201   */
   202         -int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
          202  +SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
   203    203     int len;                    /* Maximum length of output string in bytes */
   204    204     unsigned char *zOut;                  /* Output buffer */
   205    205     unsigned char *zIn;                   /* Input iterator */
   206    206     unsigned char *zTerm;                 /* End of input */
   207    207     unsigned char *z;                     /* Output iterator */
   208    208     unsigned int c;
   209    209   

Changes to src/util.c.

   106    106   */
   107    107   int sqlite3Strlen30(const char *z){
   108    108     const char *z2 = z;
   109    109     if( z==0 ) return 0;
   110    110     while( *z2 ){ z2++; }
   111    111     return 0x3fffffff & (int)(z2 - z);
   112    112   }
          113  +
          114  +/*
          115  +** Set the current error code to err_code and clear any prior error message.
          116  +*/
          117  +void sqlite3Error(sqlite3 *db, int err_code){
          118  +  assert( db!=0 );
          119  +  db->errCode = err_code;
          120  +  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
          121  +}
   113    122   
   114    123   /*
   115    124   ** Set the most recent error code and error string for the sqlite
   116    125   ** handle "db". The error code is set to "err_code".
   117    126   **
   118    127   ** If it is not NULL, string zFormat specifies the format of the
   119    128   ** error string in the style of the printf functions: The following
................................................................................
   128    137   ** zFormat and any string tokens that follow it are assumed to be
   129    138   ** encoded in UTF-8.
   130    139   **
   131    140   ** To clear the most recent error for sqlite handle "db", sqlite3Error
   132    141   ** should be called with err_code set to SQLITE_OK and zFormat set
   133    142   ** to NULL.
   134    143   */
   135         -void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
          144  +void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
   136    145     assert( db!=0 );
   137    146     db->errCode = err_code;
   138         -  if( zFormat && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
          147  +  if( zFormat==0 ){
          148  +    sqlite3Error(db, err_code);
          149  +  }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
   139    150       char *z;
   140    151       va_list ap;
   141    152       va_start(ap, zFormat);
   142    153       z = sqlite3VMPrintf(db, zFormat, ap);
   143    154       va_end(ap);
   144    155       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
   145         -  }else if( db->pErr ){
   146         -    sqlite3ValueSetNull(db->pErr);
   147    156     }
   148    157   }
   149    158   
   150    159   /*
   151    160   ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
   152    161   ** The following formatting characters are allowed:
   153    162   **
   154    163   **      %s      Insert a string
   155    164   **      %z      A string that should be freed after use
   156    165   **      %d      Insert an integer
   157    166   **      %T      Insert a token
   158    167   **      %S      Insert the first element of a SrcList
   159    168   **
   160         -** This function should be used to report any error that occurs whilst
          169  +** This function should be used to report any error that occurs while
   161    170   ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
   162    171   ** last thing the sqlite3_prepare() function does is copy the error
   163    172   ** stored by this function into the database handle using sqlite3Error().
   164         -** Function sqlite3Error() should be used during statement execution
   165         -** (sqlite3_step() etc.).
          173  +** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
          174  +** during statement execution (sqlite3_step() etc.).
   166    175   */
   167    176   void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
   168    177     char *zMsg;
   169    178     va_list ap;
   170    179     sqlite3 *db = pParse->db;
   171    180     va_start(ap, zFormat);
   172    181     zMsg = sqlite3VMPrintf(db, zFormat, ap);
................................................................................
   695    704   ** of bytes written is returned.
   696    705   **
   697    706   ** A variable-length integer consists of the lower 7 bits of each byte
   698    707   ** for all bytes that have the 8th bit set and one byte with the 8th
   699    708   ** bit clear.  Except, if we get to the 9th byte, it stores the full
   700    709   ** 8 bits and is the last byte.
   701    710   */
   702         -int sqlite3PutVarint(unsigned char *p, u64 v){
          711  +static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
   703    712     int i, j, n;
   704    713     u8 buf[10];
   705    714     if( v & (((u64)0xff000000)<<32) ){
   706    715       p[8] = (u8)v;
   707    716       v >>= 8;
   708    717       for(i=7; i>=0; i--){
   709    718         p[i] = (u8)((v & 0x7f) | 0x80);
................................................................................
   719    728     buf[0] &= 0x7f;
   720    729     assert( n<=9 );
   721    730     for(i=0, j=n-1; j>=0; j--, i++){
   722    731       p[i] = buf[j];
   723    732     }
   724    733     return n;
   725    734   }
   726         -
   727         -/*
   728         -** This routine is a faster version of sqlite3PutVarint() that only
   729         -** works for 32-bit positive integers and which is optimized for
   730         -** the common case of small integers.  A MACRO version, putVarint32,
   731         -** is provided which inlines the single-byte case.  All code should use
   732         -** the MACRO version as this function assumes the single-byte case has
   733         -** already been handled.
   734         -*/
   735         -int sqlite3PutVarint32(unsigned char *p, u32 v){
   736         -#ifndef putVarint32
   737         -  if( (v & ~0x7f)==0 ){
   738         -    p[0] = v;
          735  +int sqlite3PutVarint(unsigned char *p, u64 v){
          736  +  if( v<=0x7f ){
          737  +    p[0] = v&0x7f;
   739    738       return 1;
   740    739     }
   741         -#endif
   742         -  if( (v & ~0x3fff)==0 ){
   743         -    p[0] = (u8)((v>>7) | 0x80);
   744         -    p[1] = (u8)(v & 0x7f);
          740  +  if( v<=0x3fff ){
          741  +    p[0] = ((v>>7)&0x7f)|0x80;
          742  +    p[1] = v&0x7f;
   745    743       return 2;
   746    744     }
   747         -  return sqlite3PutVarint(p, v);
          745  +  return putVarint64(p,v);
   748    746   }
   749    747   
   750    748   /*
   751    749   ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
   752    750   ** are defined here rather than simply putting the constant expressions
   753    751   ** inline in order to work around bugs in the RVT compiler.
   754    752   **

Changes to src/vdbe.c.

   112    112   ** feature is used for test suite validation only and does not appear an
   113    113   ** production builds.
   114    114   **
   115    115   ** M is an integer, 2 or 3, that indices how many different ways the
   116    116   ** branch can go.  It is usually 2.  "I" is the direction the branch
   117    117   ** goes.  0 means falls through.  1 means branch is taken.  2 means the
   118    118   ** second alternative branch is taken.
          119  +**
          120  +** iSrcLine is the source code line (from the __LINE__ macro) that
          121  +** generated the VDBE instruction.  This instrumentation assumes that all
          122  +** source code is in a single file (the amalgamation).  Special values 1
          123  +** and 2 for the iSrcLine parameter mean that this particular branch is
          124  +** always taken or never taken, respectively.
   119    125   */
   120    126   #if !defined(SQLITE_VDBE_COVERAGE)
   121    127   # define VdbeBranchTaken(I,M)
   122    128   #else
   123    129   # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
   124    130     static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
   125    131       if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
................................................................................
   136    142   #endif
   137    143   
   138    144   /*
   139    145   ** Convert the given register into a string if it isn't one
   140    146   ** already. Return non-zero if a malloc() fails.
   141    147   */
   142    148   #define Stringify(P, enc) \
   143         -   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
          149  +   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
   144    150        { goto no_mem; }
   145    151   
   146    152   /*
   147    153   ** An ephemeral string value (signified by the MEM_Ephem flag) contains
   148    154   ** a pointer to a dynamically allocated string where some other entity
   149    155   ** is responsible for deallocating that string.  Because the register
   150    156   ** does not control the string, it might be deleted without the register
................................................................................
   218    224   }
   219    225   
   220    226   /*
   221    227   ** Try to convert a value into a numeric representation if we can
   222    228   ** do so without loss of information.  In other words, if the string
   223    229   ** looks like a number, convert it into a number.  If it does not
   224    230   ** look like a number, leave it alone.
          231  +**
          232  +** If the bTryForInt flag is true, then extra effort is made to give
          233  +** an integer representation.  Strings that look like floating point
          234  +** values but which have no fractional component (example: '48.00')
          235  +** will have a MEM_Int representation when bTryForInt is true.
          236  +**
          237  +** If bTryForInt is false, then if the input string contains a decimal
          238  +** point or exponential notation, the result is only MEM_Real, even
          239  +** if there is an exact integer representation of the quantity.
   225    240   */
   226         -static void applyNumericAffinity(Mem *pRec){
   227         -  if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
   228         -    double rValue;
   229         -    i64 iValue;
   230         -    u8 enc = pRec->enc;
   231         -    if( (pRec->flags&MEM_Str)==0 ) return;
   232         -    if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
   233         -    if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
   234         -      pRec->u.i = iValue;
   235         -      pRec->flags |= MEM_Int;
   236         -    }else{
   237         -      pRec->r = rValue;
   238         -      pRec->flags |= MEM_Real;
   239         -    }
          241  +static void applyNumericAffinity(Mem *pRec, int bTryForInt){
          242  +  double rValue;
          243  +  i64 iValue;
          244  +  u8 enc = pRec->enc;
          245  +  if( (pRec->flags&MEM_Str)==0 ) return;
          246  +  if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
          247  +  if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
          248  +    pRec->u.i = iValue;
          249  +    pRec->flags |= MEM_Int;
          250  +  }else{
          251  +    pRec->r = rValue;
          252  +    pRec->flags |= MEM_Real;
          253  +    if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
   240    254     }
   241    255   }
   242    256   
   243    257   /*
   244    258   ** Processing is determine by the affinity parameter:
   245    259   **
   246    260   ** SQLITE_AFF_INTEGER:
................................................................................
   265    279   ){
   266    280     if( affinity==SQLITE_AFF_TEXT ){
   267    281       /* Only attempt the conversion to TEXT if there is an integer or real
   268    282       ** representation (blob and NULL do not get converted) but no string
   269    283       ** representation.
   270    284       */
   271    285       if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
   272         -      sqlite3VdbeMemStringify(pRec, enc);
          286  +      sqlite3VdbeMemStringify(pRec, enc, 1);
   273    287       }
   274         -    pRec->flags &= ~(MEM_Real|MEM_Int);
   275    288     }else if( affinity!=SQLITE_AFF_NONE ){
   276    289       assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
   277    290                || affinity==SQLITE_AFF_NUMERIC );
   278         -    applyNumericAffinity(pRec);
   279         -    if( pRec->flags & MEM_Real ){
   280         -      sqlite3VdbeIntegerAffinity(pRec);
          291  +    if( (pRec->flags & MEM_Int)==0 ){
          292  +      if( (pRec->flags & MEM_Real)==0 ){
          293  +        applyNumericAffinity(pRec,1);
          294  +      }else{
          295  +        sqlite3VdbeIntegerAffinity(pRec);
          296  +      }
   281    297       }
   282    298     }
   283    299   }
   284    300   
   285    301   /*
   286    302   ** Try to convert the type of a function argument or a result column
   287    303   ** into a numeric representation.  Use either INTEGER or REAL whichever
................................................................................
   288    304   ** is appropriate.  But only do the conversion if it is possible without
   289    305   ** loss of information and return the revised type of the argument.
   290    306   */
   291    307   int sqlite3_value_numeric_type(sqlite3_value *pVal){
   292    308     int eType = sqlite3_value_type(pVal);
   293    309     if( eType==SQLITE_TEXT ){
   294    310       Mem *pMem = (Mem*)pVal;
   295         -    applyNumericAffinity(pMem);
          311  +    applyNumericAffinity(pMem, 0);
   296    312       eType = sqlite3_value_type(pVal);
   297    313     }
   298    314     return eType;
   299    315   }
   300    316   
   301    317   /*
   302    318   ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
................................................................................
   305    321   void sqlite3ValueApplyAffinity(
   306    322     sqlite3_value *pVal, 
   307    323     u8 affinity, 
   308    324     u8 enc
   309    325   ){
   310    326     applyAffinity((Mem *)pVal, affinity, enc);
   311    327   }
          328  +
          329  +/*
          330  +** pMem currently only holds a string type (or maybe a BLOB that we can
          331  +** interpret as a string if we want to).  Compute its corresponding
          332  +** numeric type, if has one.  Set the pMem->r and pMem->u.i fields
          333  +** accordingly.
          334  +*/
          335  +static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
          336  +  assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
          337  +  assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
          338  +  if( sqlite3AtoF(pMem->z, &pMem->r, pMem->n, pMem->enc)==0 ){
          339  +    return 0;
          340  +  }
          341  +  if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
          342  +    return MEM_Int;
          343  +  }
          344  +  return MEM_Real;
          345  +}
   312    346   
   313    347   /*
   314    348   ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
   315    349   ** none.  
   316    350   **
   317    351   ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
   318    352   ** But it does set pMem->r and pMem->u.i appropriately.
   319    353   */
   320    354   static u16 numericType(Mem *pMem){
   321    355     if( pMem->flags & (MEM_Int|MEM_Real) ){
   322    356       return pMem->flags & (MEM_Int|MEM_Real);
   323    357     }
   324    358     if( pMem->flags & (MEM_Str|MEM_Blob) ){
   325         -    if( sqlite3AtoF(pMem->z, &pMem->r, pMem->n, pMem->enc)==0 ){
   326         -      return 0;
   327         -    }
   328         -    if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
   329         -      return MEM_Int;
   330         -    }
   331         -    return MEM_Real;
          359  +    return computeNumericType(pMem);
   332    360     }
   333    361     return 0;
   334    362   }
   335    363   
   336    364   #ifdef SQLITE_DEBUG
   337    365   /*
   338    366   ** Write a nice string representation of the contents of cell pMem
................................................................................
   608    636       */
   609    637       assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   610    638       if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   611    639         assert( pOp->p2>0 );
   612    640         assert( pOp->p2<=(p->nMem-p->nCursor) );
   613    641         pOut = &aMem[pOp->p2];
   614    642         memAboutToChange(p, pOut);
   615         -      VdbeMemRelease(pOut);
          643  +      VdbeMemReleaseExtern(pOut);
   616    644         pOut->flags = MEM_Int;
   617    645       }
   618    646   
   619    647       /* Sanity checking on other operands */
   620    648   #ifdef SQLITE_DEBUG
   621    649       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   622    650         assert( pOp->p1>0 );
................................................................................
   763    791     pc = (int)pIn1->u.i;
   764    792     pIn1->flags = MEM_Undefined;
   765    793     break;
   766    794   }
   767    795   
   768    796   /* Opcode: InitCoroutine P1 P2 P3 * *
   769    797   **
   770         -** Set up register P1 so that it will OP_Yield to the co-routine
          798  +** Set up register P1 so that it will Yield to the coroutine
   771    799   ** located at address P3.
   772    800   **
   773         -** If P2!=0 then the co-routine implementation immediately follows
   774         -** this opcode.  So jump over the co-routine implementation to
          801  +** If P2!=0 then the coroutine implementation immediately follows
          802  +** this opcode.  So jump over the coroutine implementation to
   775    803   ** address P2.
          804  +**
          805  +** See also: EndCoroutine
   776    806   */
   777    807   case OP_InitCoroutine: {     /* jump */
   778    808     assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
   779    809     assert( pOp->p2>=0 && pOp->p2<p->nOp );
   780    810     assert( pOp->p3>=0 && pOp->p3<p->nOp );
   781    811     pOut = &aMem[pOp->p1];
   782    812     assert( !VdbeMemDynamic(pOut) );
................................................................................
   784    814     pOut->flags = MEM_Int;
   785    815     if( pOp->p2 ) pc = pOp->p2 - 1;
   786    816     break;
   787    817   }
   788    818   
   789    819   /* Opcode:  EndCoroutine P1 * * * *
   790    820   **
   791         -** The instruction at the address in register P1 is an OP_Yield.
   792         -** Jump to the P2 parameter of that OP_Yield.
          821  +** The instruction at the address in register P1 is a Yield.
          822  +** Jump to the P2 parameter of that Yield.
   793    823   ** After the jump, register P1 becomes undefined.
          824  +**
          825  +** See also: InitCoroutine
   794    826   */
   795    827   case OP_EndCoroutine: {           /* in1 */
   796    828     VdbeOp *pCaller;
   797    829     pIn1 = &aMem[pOp->p1];
   798    830     assert( pIn1->flags==MEM_Int );
   799    831     assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
   800    832     pCaller = &aOp[pIn1->u.i];
................................................................................
   803    835     pc = pCaller->p2 - 1;
   804    836     pIn1->flags = MEM_Undefined;
   805    837     break;
   806    838   }
   807    839   
   808    840   /* Opcode:  Yield P1 P2 * * *
   809    841   **
   810         -** Swap the program counter with the value in register P1.
          842  +** Swap the program counter with the value in register P1.  This
          843  +** has the effect of yielding to a coroutine.
   811    844   **
   812         -** If the co-routine ends with OP_Yield or OP_Return then continue
   813         -** to the next instruction.  But if the co-routine ends with
   814         -** OP_EndCoroutine, jump immediately to P2.
          845  +** If the coroutine that is launched by this instruction ends with
          846  +** Yield or Return then continue to the next instruction.  But if
          847  +** the coroutine launched by this instruction ends with
          848  +** EndCoroutine, then jump to P2 rather than continuing with the
          849  +** next instruction.
          850  +**
          851  +** See also: InitCoroutine
   815    852   */
   816    853   case OP_Yield: {            /* in1, jump */
   817    854     int pcDest;
   818    855     pIn1 = &aMem[pOp->p1];
   819    856     assert( VdbeMemDynamic(pIn1)==0 );
   820    857     pIn1->flags = MEM_Int;
   821    858     pcDest = (int)pIn1->u.i;
................................................................................
   970   1007   }
   971   1008   #endif
   972   1009   
   973   1010   /* Opcode: String8 * P2 * P4 *
   974   1011   ** Synopsis: r[P2]='P4'
   975   1012   **
   976   1013   ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
   977         -** into an OP_String before it is executed for the first time.  During
         1014  +** into a String before it is executed for the first time.  During
   978   1015   ** this transformation, the length of string P4 is computed and stored
   979   1016   ** as the P1 parameter.
   980   1017   */
   981   1018   case OP_String8: {         /* same as TK_STRING, out2-prerelease */
   982   1019     assert( pOp->p4.z!=0 );
   983   1020     pOp->opcode = OP_String;
   984   1021     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
................................................................................
  1038   1075     u16 nullFlag;
  1039   1076     cnt = pOp->p3-pOp->p2;
  1040   1077     assert( pOp->p3<=(p->nMem-p->nCursor) );
  1041   1078     pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
  1042   1079     while( cnt>0 ){
  1043   1080       pOut++;
  1044   1081       memAboutToChange(p, pOut);
  1045         -    VdbeMemRelease(pOut);
         1082  +    VdbeMemReleaseExtern(pOut);
  1046   1083       pOut->flags = nullFlag;
  1047   1084       cnt--;
  1048   1085     }
  1049   1086     break;
  1050   1087   }
  1051   1088   
  1052   1089   /* Opcode: SoftNull P1 * * * *
................................................................................
  1124   1161     pIn1 = &aMem[p1];
  1125   1162     pOut = &aMem[p2];
  1126   1163     do{
  1127   1164       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
  1128   1165       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
  1129   1166       assert( memIsValid(pIn1) );
  1130   1167       memAboutToChange(p, pOut);
  1131         -    VdbeMemRelease(pOut);
         1168  +    VdbeMemReleaseExtern(pOut);
  1132   1169       zMalloc = pOut->zMalloc;
  1133   1170       memcpy(pOut, pIn1, sizeof(Mem));
  1134   1171   #ifdef SQLITE_DEBUG
  1135   1172       if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
  1136   1173         pOut->pScopyFrom += p1 - pOp->p2;
  1137   1174       }
  1138   1175   #endif
................................................................................
  1504   1541     sqlite3_value **apVal;
  1505   1542     int n;
  1506   1543   
  1507   1544     n = pOp->p5;
  1508   1545     apVal = p->apArg;
  1509   1546     assert( apVal || n==0 );
  1510   1547     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  1511         -  pOut = &aMem[pOp->p3];
  1512         -  memAboutToChange(p, pOut);
         1548  +  ctx.pOut = &aMem[pOp->p3];
         1549  +  memAboutToChange(p, ctx.pOut);
  1513   1550   
  1514   1551     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
  1515   1552     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
  1516   1553     pArg = &aMem[pOp->p2];
  1517   1554     for(i=0; i<n; i++, pArg++){
  1518   1555       assert( memIsValid(pArg) );
  1519   1556       apVal[i] = pArg;
................................................................................
  1521   1558       REGISTER_TRACE(pOp->p2+i, pArg);
  1522   1559     }
  1523   1560   
  1524   1561     assert( pOp->p4type==P4_FUNCDEF );
  1525   1562     ctx.pFunc = pOp->p4.pFunc;
  1526   1563     ctx.iOp = pc;
  1527   1564     ctx.pVdbe = p;
  1528         -
  1529         -  /* The output cell may already have a buffer allocated. Move
  1530         -  ** the pointer to ctx.s so in case the user-function can use
  1531         -  ** the already allocated buffer instead of allocating a new one.
  1532         -  */
  1533         -  memcpy(&ctx.s, pOut, sizeof(Mem));
  1534         -  pOut->flags = MEM_Null;
  1535         -  pOut->xDel = 0;
  1536         -  pOut->zMalloc = 0;
  1537         -  MemSetTypeFlag(&ctx.s, MEM_Null);
         1565  +  MemSetTypeFlag(ctx.pOut, MEM_Null);
  1538   1566   
  1539   1567     ctx.fErrorOrAux = 0;
  1540   1568     if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  1541   1569       assert( pOp>aOp );
  1542   1570       assert( pOp[-1].p4type==P4_COLLSEQ );
  1543   1571       assert( pOp[-1].opcode==OP_CollSeq );
  1544   1572       ctx.pColl = pOp[-1].p4.pColl;
  1545   1573     }
  1546   1574     db->lastRowid = lastRowid;
  1547   1575     (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  1548   1576     lastRowid = db->lastRowid;
  1549   1577   
  1550         -  if( db->mallocFailed ){
  1551         -    /* Even though a malloc() has failed, the implementation of the
  1552         -    ** user function may have called an sqlite3_result_XXX() function
  1553         -    ** to return a value. The following call releases any resources
  1554         -    ** associated with such a value.
  1555         -    */
  1556         -    sqlite3VdbeMemRelease(&ctx.s);
  1557         -    goto no_mem;
  1558         -  }
  1559         -
  1560   1578     /* If the function returned an error, throw an exception */
  1561   1579     if( ctx.fErrorOrAux ){
  1562   1580       if( ctx.isError ){
  1563         -      sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
         1581  +      sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
  1564   1582         rc = ctx.isError;
  1565   1583       }
  1566   1584       sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
  1567   1585     }
  1568   1586   
  1569   1587     /* Copy the result of the function into register P3 */
  1570         -  sqlite3VdbeChangeEncoding(&ctx.s, encoding);
  1571         -  assert( pOut->flags==MEM_Null );
  1572         -  memcpy(pOut, &ctx.s, sizeof(Mem));
  1573         -  if( sqlite3VdbeMemTooBig(pOut) ){
         1588  +  sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
         1589  +  if( sqlite3VdbeMemTooBig(ctx.pOut) ){
  1574   1590       goto too_big;
  1575   1591     }
  1576   1592   
  1577         -#if 0
  1578         -  /* The app-defined function has done something that as caused this
  1579         -  ** statement to expire.  (Perhaps the function called sqlite3_exec()
  1580         -  ** with a CREATE TABLE statement.)
  1581         -  */
  1582         -  if( p->expired ) rc = SQLITE_ABORT;
  1583         -#endif
  1584         -
  1585         -  REGISTER_TRACE(pOp->p3, pOut);
  1586         -  UPDATE_MAX_BLOBSIZE(pOut);
         1593  +  REGISTER_TRACE(pOp->p3, ctx.pOut);
         1594  +  UPDATE_MAX_BLOBSIZE(ctx.pOut);
  1587   1595     break;
  1588   1596   }
  1589   1597   
  1590   1598   /* Opcode: BitAnd P1 P2 P3 * *
  1591   1599   ** Synopsis:  r[P3]=r[P1]&r[P2]
  1592   1600   **
  1593   1601   ** Take the bit-wise AND of the values in register P1 and P2 and
................................................................................
  1727   1735       sqlite3VdbeMemRealify(pIn1);
  1728   1736     }
  1729   1737     break;
  1730   1738   }
  1731   1739   #endif
  1732   1740   
  1733   1741   #ifndef SQLITE_OMIT_CAST
  1734         -/* Opcode: ToText P1 * * * *
         1742  +/* Opcode: Cast P1 P2 * * *
         1743  +** Synopsis: affinity(r[P1])
  1735   1744   **
  1736         -** Force the value in register P1 to be text.
  1737         -** If the value is numeric, convert it to a string using the
  1738         -** equivalent of sprintf().  Blob values are unchanged and
  1739         -** are afterwards simply interpreted as text.
         1745  +** Force the value in register P1 to be the type defined by P2.
         1746  +** 
         1747  +** <ul>
         1748  +** <li value="97"> TEXT
         1749  +** <li value="98"> BLOB
         1750  +** <li value="99"> NUMERIC
         1751  +** <li value="100"> INTEGER
         1752  +** <li value="101"> REAL
         1753  +** </ul>
  1740   1754   **
  1741   1755   ** A NULL value is not changed by this routine.  It remains NULL.
  1742   1756   */
  1743         -case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
         1757  +case OP_Cast: {                  /* in1 */
         1758  +  assert( pOp->p2>=SQLITE_AFF_TEXT && pOp->p2<=SQLITE_AFF_REAL );
         1759  +  testcase( pOp->p2==SQLITE_AFF_TEXT );
         1760  +  testcase( pOp->p2==SQLITE_AFF_NONE );
         1761  +  testcase( pOp->p2==SQLITE_AFF_NUMERIC );
         1762  +  testcase( pOp->p2==SQLITE_AFF_INTEGER );
         1763  +  testcase( pOp->p2==SQLITE_AFF_REAL );
  1744   1764     pIn1 = &aMem[pOp->p1];
  1745   1765     memAboutToChange(p, pIn1);
  1746         -  if( pIn1->flags & MEM_Null ) break;
  1747         -  assert( MEM_Str==(MEM_Blob>>3) );
  1748         -  pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
  1749         -  applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
  1750   1766     rc = ExpandBlob(pIn1);
  1751         -  assert( pIn1->flags & MEM_Str || db->mallocFailed );
  1752         -  pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
  1753         -  UPDATE_MAX_BLOBSIZE(pIn1);
  1754         -  break;
  1755         -}
  1756         -
  1757         -/* Opcode: ToBlob P1 * * * *
  1758         -**
  1759         -** Force the value in register P1 to be a BLOB.
  1760         -** If the value is numeric, convert it to a string first.
  1761         -** Strings are simply reinterpreted as blobs with no change
  1762         -** to the underlying data.
  1763         -**
  1764         -** A NULL value is not changed by this routine.  It remains NULL.
  1765         -*/
  1766         -case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
  1767         -  pIn1 = &aMem[pOp->p1];
  1768         -  if( pIn1->flags & MEM_Null ) break;
  1769         -  if( (pIn1->flags & MEM_Blob)==0 ){
  1770         -    applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
  1771         -    assert( pIn1->flags & MEM_Str || db->mallocFailed );
  1772         -    MemSetTypeFlag(pIn1, MEM_Blob);
  1773         -  }else{
  1774         -    pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
  1775         -  }
         1767  +  sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
  1776   1768     UPDATE_MAX_BLOBSIZE(pIn1);
  1777   1769     break;
  1778   1770   }
  1779         -
  1780         -/* Opcode: ToNumeric P1 * * * *
  1781         -**
  1782         -** Force the value in register P1 to be numeric (either an
  1783         -** integer or a floating-point number.)
  1784         -** If the value is text or blob, try to convert it to an using the
  1785         -** equivalent of atoi() or atof() and store 0 if no such conversion 
  1786         -** is possible.
  1787         -**
  1788         -** A NULL value is not changed by this routine.  It remains NULL.
  1789         -*/
  1790         -case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
  1791         -  pIn1 = &aMem[pOp->p1];
  1792         -  sqlite3VdbeMemNumerify(pIn1);
  1793         -  break;
  1794         -}
  1795   1771   #endif /* SQLITE_OMIT_CAST */
  1796   1772   
  1797         -/* Opcode: ToInt P1 * * * *
  1798         -**
  1799         -** Force the value in register P1 to be an integer.  If
  1800         -** The value is currently a real number, drop its fractional part.
  1801         -** If the value is text or blob, try to convert it to an integer using the
  1802         -** equivalent of atoi() and store 0 if no such conversion is possible.
  1803         -**
  1804         -** A NULL value is not changed by this routine.  It remains NULL.
  1805         -*/
  1806         -case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
  1807         -  pIn1 = &aMem[pOp->p1];
  1808         -  if( (pIn1->flags & MEM_Null)==0 ){
  1809         -    sqlite3VdbeMemIntegerify(pIn1);
  1810         -  }
  1811         -  break;
  1812         -}
  1813         -
  1814         -#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
  1815         -/* Opcode: ToReal P1 * * * *
  1816         -**
  1817         -** Force the value in register P1 to be a floating point number.
  1818         -** If The value is currently an integer, convert it.
  1819         -** If the value is text or blob, try to convert it to an integer using the
  1820         -** equivalent of atoi() and store 0.0 if no such conversion is possible.
  1821         -**
  1822         -** A NULL value is not changed by this routine.  It remains NULL.
  1823         -*/
  1824         -case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
  1825         -  pIn1 = &aMem[pOp->p1];
  1826         -  memAboutToChange(p, pIn1);
  1827         -  if( (pIn1->flags & MEM_Null)==0 ){
  1828         -    sqlite3VdbeMemRealify(pIn1);
  1829         -  }
  1830         -  break;
  1831         -}
  1832         -#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
  1833         -
  1834   1773   /* Opcode: Lt P1 P2 P3 P4 P5
  1835   1774   ** Synopsis: if r[P1]<r[P3] goto P2
  1836   1775   **
  1837   1776   ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
  1838   1777   ** jump to address P2.  
  1839   1778   **
  1840   1779   ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
................................................................................
  2192   2131       sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
  2193   2132     }
  2194   2133     break;
  2195   2134   }
  2196   2135   
  2197   2136   /* Opcode: Once P1 P2 * * *
  2198   2137   **
  2199         -** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
  2200         -** set the flag and fall through to the next instruction.  In other words,
  2201         -** this opcode causes all following opcodes up through P2 (but not including
  2202         -** P2) to run just once and to be skipped on subsequent times through the loop.
         2138  +** Check the "once" flag number P1. If it is set, jump to instruction P2. 
         2139  +** Otherwise, set the flag and fall through to the next instruction.
         2140  +** In other words, this opcode causes all following opcodes up through P2
         2141  +** (but not including P2) to run just once and to be skipped on subsequent
         2142  +** times through the loop.
         2143  +**
         2144  +** All "once" flags are initially cleared whenever a prepared statement
         2145  +** first begins to run.
  2203   2146   */
  2204   2147   case OP_Once: {             /* jump */
  2205   2148     assert( pOp->p1<p->nOnceFlag );
  2206   2149     VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
  2207   2150     if( p->aOnceFlag[pOp->p1] ){
  2208   2151       pc = pOp->p2-1;
  2209   2152     }else{
................................................................................
  2212   2155     break;
  2213   2156   }
  2214   2157   
  2215   2158   /* Opcode: If P1 P2 P3 * *
  2216   2159   **
  2217   2160   ** Jump to P2 if the value in register P1 is true.  The value
  2218   2161   ** is considered true if it is numeric and non-zero.  If the value
  2219         -** in P1 is NULL then take the jump if P3 is non-zero.
         2162  +** in P1 is NULL then take the jump if and only if P3 is non-zero.
  2220   2163   */
  2221   2164   /* Opcode: IfNot P1 P2 P3 * *
  2222   2165   **
  2223   2166   ** Jump to P2 if the value in register P1 is False.  The value
  2224   2167   ** is considered false if it has a numeric value of zero.  If the value
  2225         -** in P1 is NULL then take the jump if P3 is zero.
         2168  +** in P1 is NULL then take the jump if and only if P3 is non-zero.
  2226   2169   */
  2227   2170   case OP_If:                 /* jump, in1 */
  2228   2171   case OP_IfNot: {            /* jump, in1 */
  2229   2172     int c;
  2230   2173     pIn1 = &aMem[pOp->p1];
  2231   2174     if( pIn1->flags & MEM_Null ){
  2232   2175       c = pOp->p3;
................................................................................
  2493   2436     */
  2494   2437     assert( p2<pC->nHdrParsed );
  2495   2438     assert( rc==SQLITE_OK );
  2496   2439     assert( sqlite3VdbeCheckMemInvariants(pDest) );
  2497   2440     if( pC->szRow>=aOffset[p2+1] ){
  2498   2441       /* This is the common case where the desired content fits on the original
  2499   2442       ** page - where the content is not on an overflow page */
  2500         -    VdbeMemRelease(pDest);
         2443  +    VdbeMemReleaseExtern(pDest);
  2501   2444       sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
  2502   2445     }else{
  2503   2446       /* This branch happens only when content is on overflow pages */
  2504   2447       t = aType[p2];
  2505   2448       if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
  2506   2449             && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
  2507   2450        || (len = sqlite3VdbeSerialTypeLen(t))==0
................................................................................
  3243   3186   */
  3244   3187   case OP_ReopenIdx: {
  3245   3188     VdbeCursor *pCur;
  3246   3189   
  3247   3190     assert( pOp->p5==0 );
  3248   3191     assert( pOp->p4type==P4_KEYINFO );
  3249   3192     pCur = p->apCsr[pOp->p1];
  3250         -  if( pCur && pCur->pgnoRoot==pOp->p2 ){
         3193  +  if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
  3251   3194       assert( pCur->iDb==pOp->p3 );      /* Guaranteed by the code generator */
  3252   3195       break;
  3253   3196     }
  3254   3197     /* If the cursor is not currently open or is open on a different
  3255   3198     ** index, then fall through into OP_OpenRead to force a reopen */
  3256   3199   }
  3257   3200   case OP_OpenRead:
................................................................................
  3477   3420   case OP_Close: {
  3478   3421     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3479   3422     sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
  3480   3423     p->apCsr[pOp->p1] = 0;
  3481   3424     break;
  3482   3425   }
  3483   3426   
  3484         -/* Opcode: SeekGe P1 P2 P3 P4 *
         3427  +/* Opcode: SeekGE P1 P2 P3 P4 *
  3485   3428   ** Synopsis: key=r[P3@P4]
  3486   3429   **
  3487   3430   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
  3488   3431   ** use the value in register P3 as the key.  If cursor P1 refers 
  3489   3432   ** to an SQL index, then P3 is the first in an array of P4 registers 
  3490   3433   ** that are used as an unpacked index key. 
  3491   3434   **
  3492   3435   ** Reposition cursor P1 so that  it points to the smallest entry that 
  3493   3436   ** is greater than or equal to the key value. If there are no records 
  3494   3437   ** greater than or equal to the key and P2 is not zero, then jump to P2.
         3438  +**
         3439  +** This opcode leaves the cursor configured to move in forward order,
         3440  +** from the beginning toward the end.  In other words, the cursor is
         3441  +** configured to use Next, not Prev.
  3495   3442   **
  3496   3443   ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
  3497   3444   */
  3498         -/* Opcode: SeekGt P1 P2 P3 P4 *
         3445  +/* Opcode: SeekGT P1 P2 P3 P4 *
  3499   3446   ** Synopsis: key=r[P3@P4]
  3500   3447   **
  3501   3448   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
  3502   3449   ** use the value in register P3 as a key. If cursor P1 refers 
  3503   3450   ** to an SQL index, then P3 is the first in an array of P4 registers 
  3504   3451   ** that are used as an unpacked index key. 
  3505   3452   **
  3506   3453   ** Reposition cursor P1 so that  it points to the smallest entry that 
  3507   3454   ** is greater than the key value. If there are no records greater than 
  3508   3455   ** the key and P2 is not zero, then jump to P2.
         3456  +**
         3457  +** This opcode leaves the cursor configured to move in forward order,
         3458  +** from the beginning toward the end.  In other words, the cursor is
         3459  +** configured to use Next, not Prev.
  3509   3460   **
  3510   3461   ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
  3511   3462   */
  3512         -/* Opcode: SeekLt P1 P2 P3 P4 * 
         3463  +/* Opcode: SeekLT P1 P2 P3 P4 * 
  3513   3464   ** Synopsis: key=r[P3@P4]
  3514   3465   **
  3515   3466   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
  3516   3467   ** use the value in register P3 as a key. If cursor P1 refers 
  3517   3468   ** to an SQL index, then P3 is the first in an array of P4 registers 
  3518   3469   ** that are used as an unpacked index key. 
  3519   3470   **
  3520   3471   ** Reposition cursor P1 so that  it points to the largest entry that 
  3521   3472   ** is less than the key value. If there are no records less than 
  3522   3473   ** the key and P2 is not zero, then jump to P2.
         3474  +**
         3475  +** This opcode leaves the cursor configured to move in reverse order,
         3476  +** from the end toward the beginning.  In other words, the cursor is
         3477  +** configured to use Prev, not Next.
  3523   3478   **
  3524   3479   ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
  3525   3480   */
  3526         -/* Opcode: SeekLe P1 P2 P3 P4 *
         3481  +/* Opcode: SeekLE P1 P2 P3 P4 *
  3527   3482   ** Synopsis: key=r[P3@P4]
  3528   3483   **
  3529   3484   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
  3530   3485   ** use the value in register P3 as a key. If cursor P1 refers 
  3531   3486   ** to an SQL index, then P3 is the first in an array of P4 registers 
  3532   3487   ** that are used as an unpacked index key. 
  3533   3488   **
  3534   3489   ** Reposition cursor P1 so that it points to the largest entry that 
  3535   3490   ** is less than or equal to the key value. If there are no records 
  3536   3491   ** less than or equal to the key and P2 is not zero, then jump to P2.
         3492  +**
         3493  +** This opcode leaves the cursor configured to move in reverse order,
         3494  +** from the end toward the beginning.  In other words, the cursor is
         3495  +** configured to use Prev, not Next.
  3537   3496   **
  3538   3497   ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
  3539   3498   */
  3540   3499   case OP_SeekLT:         /* jump, in3 */
  3541   3500   case OP_SeekLE:         /* jump, in3 */
  3542   3501   case OP_SeekGE:         /* jump, in3 */
  3543   3502   case OP_SeekGT: {       /* jump, in3 */
................................................................................
  3556   3515     assert( OP_SeekLE == OP_SeekLT+1 );
  3557   3516     assert( OP_SeekGE == OP_SeekLT+2 );
  3558   3517     assert( OP_SeekGT == OP_SeekLT+3 );
  3559   3518     assert( pC->isOrdered );
  3560   3519     assert( pC->pCursor!=0 );
  3561   3520     oc = pOp->opcode;
  3562   3521     pC->nullRow = 0;
         3522  +#ifdef SQLITE_DEBUG
         3523  +  pC->seekOp = pOp->opcode;
         3524  +#endif
  3563   3525     if( pC->isTable ){
  3564   3526       /* The input value in P3 might be of any type: integer, real, string,
  3565   3527       ** blob, or NULL.  But it needs to be an integer before we can do
  3566   3528       ** the seek, so covert it. */
  3567   3529       pIn3 = &aMem[pOp->p3];
  3568         -    applyNumericAffinity(pIn3);
         3530  +    if( (pIn3->flags & (MEM_Int|MEM_Real))==0 ){
         3531  +      applyNumericAffinity(pIn3, 0);
         3532  +    }
  3569   3533       iKey = sqlite3VdbeIntValue(pIn3);
  3570   3534       pC->rowidIsValid = 0;
  3571   3535   
  3572   3536       /* If the P3 value could not be converted into an integer without
  3573   3537       ** loss of information, then special processing is required... */
  3574   3538       if( (pIn3->flags & MEM_Int)==0 ){
  3575   3539         if( (pIn3->flags & MEM_Real)==0 ){
................................................................................
  3710   3674   ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
  3711   3675   ** P4>0 then register P3 is the first of P4 registers that form an unpacked
  3712   3676   ** record.
  3713   3677   **
  3714   3678   ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
  3715   3679   ** is a prefix of any entry in P1 then a jump is made to P2 and
  3716   3680   ** P1 is left pointing at the matching entry.
         3681  +**
         3682  +** This operation leaves the cursor in a state where it can be
         3683  +** advanced in the forward direction.  The Next instruction will work,
         3684  +** but not the Prev instruction.
  3717   3685   **
  3718   3686   ** See also: NotFound, NoConflict, NotExists. SeekGe
  3719   3687   */
  3720   3688   /* Opcode: NotFound P1 P2 P3 P4 *
  3721   3689   ** Synopsis: key=r[P3@P4]
  3722   3690   **
  3723   3691   ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
................................................................................
  3725   3693   ** record.
  3726   3694   ** 
  3727   3695   ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
  3728   3696   ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1 
  3729   3697   ** does contain an entry whose prefix matches the P3/P4 record then control
  3730   3698   ** falls through to the next instruction and P1 is left pointing at the
  3731   3699   ** matching entry.
         3700  +**
         3701  +** This operation leaves the cursor in a state where it cannot be
         3702  +** advanced in either direction.  In other words, the Next and Prev
         3703  +** opcodes do not work after this operation.
  3732   3704   **
  3733   3705   ** See also: Found, NotExists, NoConflict
  3734   3706   */
  3735   3707   /* Opcode: NoConflict P1 P2 P3 P4 *
  3736   3708   ** Synopsis: key=r[P3@P4]
  3737   3709   **
  3738   3710   ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
................................................................................
  3744   3716   ** record are not-NULL then a check is done to determine if any row in the
  3745   3717   ** P1 index btree has a matching key prefix.  If there are no matches, jump
  3746   3718   ** immediately to P2.  If there is a match, fall through and leave the P1
  3747   3719   ** cursor pointing to the matching row.
  3748   3720   **
  3749   3721   ** This opcode is similar to OP_NotFound with the exceptions that the
  3750   3722   ** branch is always taken if any part of the search key input is NULL.
         3723  +**
         3724  +** This operation leaves the cursor in a state where it cannot be
         3725  +** advanced in either direction.  In other words, the Next and Prev
         3726  +** opcodes do not work after this operation.
  3751   3727   **
  3752   3728   ** See also: NotFound, Found, NotExists
  3753   3729   */
  3754   3730   case OP_NoConflict:     /* jump, in3 */
  3755   3731   case OP_NotFound:       /* jump, in3 */
  3756   3732   case OP_Found: {        /* jump, in3 */
  3757   3733     int alreadyExists;
................................................................................
  3767   3743     if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
  3768   3744   #endif
  3769   3745   
  3770   3746     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3771   3747     assert( pOp->p4type==P4_INT32 );
  3772   3748     pC = p->apCsr[pOp->p1];
  3773   3749     assert( pC!=0 );
         3750  +#ifdef SQLITE_DEBUG
         3751  +  pC->seekOp = pOp->opcode;
         3752  +#endif
  3774   3753     pIn3 = &aMem[pOp->p3];
  3775   3754     assert( pC->pCursor!=0 );
  3776   3755     assert( pC->isTable==0 );
  3777   3756     pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
  3778   3757     if( pOp->p4.i>0 ){
  3779   3758       r.pKeyInfo = pC->pKeyInfo;
  3780   3759       r.nField = (u16)pOp->p4.i;
................................................................................
  3837   3816   ** keys).  P3 is an integer rowid.  If P1 does not contain a record with
  3838   3817   ** rowid P3 then jump immediately to P2.  If P1 does contain a record
  3839   3818   ** with rowid P3 then leave the cursor pointing at that record and fall
  3840   3819   ** through to the next instruction.
  3841   3820   **
  3842   3821   ** The OP_NotFound opcode performs the same operation on index btrees
  3843   3822   ** (with arbitrary multi-value keys).
         3823  +**
         3824  +** This opcode leaves the cursor in a state where it cannot be advanced
         3825  +** in either direction.  In other words, the Next and Prev opcodes will
         3826  +** not work following this opcode.
  3844   3827   **
  3845   3828   ** See also: Found, NotFound, NoConflict
  3846   3829   */
  3847   3830   case OP_NotExists: {        /* jump, in3 */
  3848   3831     VdbeCursor *pC;
  3849   3832     BtCursor *pCrsr;
  3850   3833     int res;
................................................................................
  3851   3834     u64 iKey;
  3852   3835   
  3853   3836     pIn3 = &aMem[pOp->p3];
  3854   3837     assert( pIn3->flags & MEM_Int );
  3855   3838     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3856   3839     pC = p->apCsr[pOp->p1];
  3857   3840     assert( pC!=0 );
         3841  +#ifdef SQLITE_DEBUG
         3842  +  pC->seekOp = 0;
         3843  +#endif
  3858   3844     assert( pC->isTable );
  3859   3845     assert( pC->pseudoTableReg==0 );
  3860   3846     pCrsr = pC->pCursor;
  3861   3847     assert( pCrsr!=0 );
  3862   3848     res = 0;
  3863   3849     iKey = pIn3->u.i;
  3864   3850     rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
................................................................................
  4153   4139   /* Opcode: Delete P1 P2 * P4 *
  4154   4140   **
  4155   4141   ** Delete the record at which the P1 cursor is currently pointing.
  4156   4142   **
  4157   4143   ** The cursor will be left pointing at either the next or the previous
  4158   4144   ** record in the table. If it is left pointing at the next record, then
  4159   4145   ** the next Next instruction will be a no-op.  Hence it is OK to delete
  4160         -** a record from within an Next loop.
         4146  +** a record from within a Next loop.
  4161   4147   **
  4162   4148   ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
  4163   4149   ** incremented (otherwise not).
  4164   4150   **
  4165   4151   ** P1 must not be pseudo-table.  It has to be a real table with
  4166   4152   ** multiple rows.
  4167   4153   **
................................................................................
  4213   4199   case OP_ResetCount: {
  4214   4200     sqlite3VdbeSetChanges(db, p->nChange);
  4215   4201     p->nChange = 0;
  4216   4202     break;
  4217   4203   }
  4218   4204   
  4219   4205   /* Opcode: SorterCompare P1 P2 P3 P4
  4220         -** Synopsis:  if key(P1)!=rtrim(r[P3],P4) goto P2
         4206  +** Synopsis:  if key(P1)!=trim(r[P3],P4) goto P2
  4221   4207   **
  4222   4208   ** P1 is a sorter cursor. This instruction compares a prefix of the
  4223         -** the record blob in register P3 against a prefix of the entry that 
  4224         -** the sorter cursor currently points to.  The final P4 fields of both
  4225         -** the P3 and sorter record are ignored.
         4209  +** record blob in register P3 against a prefix of the entry that 
         4210  +** the sorter cursor currently points to.  Only the first P4 fields
         4211  +** of r[P3] and the sorter record are compared.
  4226   4212   **
  4227   4213   ** If either P3 or the sorter contains a NULL in one of their significant
  4228   4214   ** fields (not counting the P4 fields at the end which are ignored) then
  4229   4215   ** the comparison is assumed to be equal.
  4230   4216   **
  4231   4217   ** Fall through to next instruction if the two records compare equal to
  4232   4218   ** each other.  Jump to P2 if they are different.
  4233   4219   */
  4234   4220   case OP_SorterCompare: {
  4235   4221     VdbeCursor *pC;
  4236   4222     int res;
  4237         -  int nIgnore;
         4223  +  int nKeyCol;
  4238   4224   
  4239   4225     pC = p->apCsr[pOp->p1];
  4240   4226     assert( isSorter(pC) );
  4241   4227     assert( pOp->p4type==P4_INT32 );
  4242   4228     pIn3 = &aMem[pOp->p3];
  4243         -  nIgnore = pOp->p4.i;
  4244         -  rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
         4229  +  nKeyCol = pOp->p4.i;
         4230  +  rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
  4245   4231     VdbeBranchTaken(res!=0,2);
  4246   4232     if( res ){
  4247   4233       pc = pOp->p2-1;
  4248   4234     }
  4249   4235     break;
  4250   4236   };
  4251   4237   
................................................................................
  4417   4403       sqlite3BtreeClearCursor(pC->pCursor);
  4418   4404     }
  4419   4405     break;
  4420   4406   }
  4421   4407   
  4422   4408   /* Opcode: Last P1 P2 * * *
  4423   4409   **
  4424         -** The next use of the Rowid or Column or Next instruction for P1 
         4410  +** The next use of the Rowid or Column or Prev instruction for P1 
  4425   4411   ** will refer to the last entry in the database table or index.
  4426   4412   ** If the table or index is empty and P2>0, then jump immediately to P2.
  4427   4413   ** If P2 is 0 or if the table or index is not empty, fall through
  4428   4414   ** to the following instruction.
         4415  +**
         4416  +** This opcode leaves the cursor configured to move in reverse order,
         4417  +** from the end toward the beginning.  In other words, the cursor is
         4418  +** configured to use Prev, not Next.
  4429   4419   */
  4430   4420   case OP_Last: {        /* jump */
  4431   4421     VdbeCursor *pC;
  4432   4422     BtCursor *pCrsr;
  4433   4423     int res;
  4434   4424   
  4435   4425     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
................................................................................
  4439   4429     res = 0;
  4440   4430     assert( pCrsr!=0 );
  4441   4431     rc = sqlite3BtreeLast(pCrsr, &res);
  4442   4432     pC->nullRow = (u8)res;
  4443   4433     pC->deferredMoveto = 0;
  4444   4434     pC->rowidIsValid = 0;
  4445   4435     pC->cacheStatus = CACHE_STALE;
         4436  +#ifdef SQLITE_DEBUG
         4437  +  pC->seekOp = OP_Last;
         4438  +#endif
  4446   4439     if( pOp->p2>0 ){
  4447   4440       VdbeBranchTaken(res!=0,2);
  4448   4441       if( res ) pc = pOp->p2 - 1;
  4449   4442     }
  4450   4443     break;
  4451   4444   }
  4452   4445   
................................................................................
  4475   4468   /* Opcode: Rewind P1 P2 * * *
  4476   4469   **
  4477   4470   ** The next use of the Rowid or Column or Next instruction for P1 
  4478   4471   ** will refer to the first entry in the database table or index.
  4479   4472   ** If the table or index is empty and P2>0, then jump immediately to P2.
  4480   4473   ** If P2 is 0 or if the table or index is not empty, fall through
  4481   4474   ** to the following instruction.
         4475  +**
         4476  +** This opcode leaves the cursor configured to move in forward order,
         4477  +** from the beginning toward the end.  In other words, the cursor is
         4478  +** configured to use Next, not Prev.
  4482   4479   */
  4483   4480   case OP_Rewind: {        /* jump */
  4484   4481     VdbeCursor *pC;
  4485   4482     BtCursor *pCrsr;
  4486   4483     int res;
  4487   4484   
  4488   4485     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4489   4486     pC = p->apCsr[pOp->p1];
  4490   4487     assert( pC!=0 );
  4491   4488     assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
  4492   4489     res = 1;
         4490  +#ifdef SQLITE_DEBUG
         4491  +  pC->seekOp = OP_Rewind;
         4492  +#endif
  4493   4493     if( isSorter(pC) ){
  4494   4494       rc = sqlite3VdbeSorterRewind(db, pC, &res);
  4495   4495     }else{
  4496   4496       pCrsr = pC->pCursor;
  4497   4497       assert( pCrsr );
  4498   4498       rc = sqlite3BtreeFirst(pCrsr, &res);
  4499   4499       pC->deferredMoveto = 0;
................................................................................
  4511   4511   
  4512   4512   /* Opcode: Next P1 P2 P3 P4 P5
  4513   4513   **
  4514   4514   ** Advance cursor P1 so that it points to the next key/data pair in its
  4515   4515   ** table or index.  If there are no more key/value pairs then fall through
  4516   4516   ** to the following instruction.  But if the cursor advance was successful,
  4517   4517   ** jump immediately to P2.
         4518  +**
         4519  +** The Next opcode is only valid following an SeekGT, SeekGE, or
         4520  +** OP_Rewind opcode used to position the cursor.  Next is not allowed
         4521  +** to follow SeekLT, SeekLE, or OP_Last.
  4518   4522   **
  4519   4523   ** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
  4520   4524   ** been opened prior to this opcode or the program will segfault.
  4521   4525   **
  4522   4526   ** The P3 value is a hint to the btree implementation. If P3==1, that
  4523   4527   ** means P1 is an SQL index and that this instruction could have been
  4524   4528   ** omitted if that index had been unique.  P3 is usually 0.  P3 is
................................................................................
  4530   4534   ** If P5 is positive and the jump is taken, then event counter
  4531   4535   ** number P5-1 in the prepared statement is incremented.
  4532   4536   **
  4533   4537   ** See also: Prev, NextIfOpen
  4534   4538   */
  4535   4539   /* Opcode: NextIfOpen P1 P2 P3 P4 P5
  4536   4540   **
  4537         -** This opcode works just like OP_Next except that if cursor P1 is not
         4541  +** This opcode works just like Next except that if cursor P1 is not
  4538   4542   ** open it behaves a no-op.
  4539   4543   */
  4540   4544   /* Opcode: Prev P1 P2 P3 P4 P5
  4541   4545   **
  4542   4546   ** Back up cursor P1 so that it points to the previous key/data pair in its
  4543   4547   ** table or index.  If there is no previous key/value pairs then fall through
  4544   4548   ** to the following instruction.  But if the cursor backup was successful,
  4545   4549   ** jump immediately to P2.
  4546   4550   **
         4551  +**
         4552  +** The Prev opcode is only valid following an SeekLT, SeekLE, or
         4553  +** OP_Last opcode used to position the cursor.  Prev is not allowed
         4554  +** to follow SeekGT, SeekGE, or OP_Rewind.
         4555  +**
  4547   4556   ** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
  4548   4557   ** not open then the behavior is undefined.
  4549   4558   **
  4550   4559   ** The P3 value is a hint to the btree implementation. If P3==1, that
  4551   4560   ** means P1 is an SQL index and that this instruction could have been
  4552   4561   ** omitted if that index had been unique.  P3 is usually 0.  P3 is
  4553   4562   ** always either 0 or 1.
................................................................................
  4556   4565   ** sqlite3BtreePrevious().
  4557   4566   **
  4558   4567   ** If P5 is positive and the jump is taken, then event counter
  4559   4568   ** number P5-1 in the prepared statement is incremented.
  4560   4569   */
  4561   4570   /* Opcode: PrevIfOpen P1 P2 P3 P4 P5
  4562   4571   **
  4563         -** This opcode works just like OP_Prev except that if cursor P1 is not
         4572  +** This opcode works just like Prev except that if cursor P1 is not
  4564   4573   ** open it behaves a no-op.
  4565   4574   */
  4566   4575   case OP_SorterNext: {  /* jump */
  4567   4576     VdbeCursor *pC;
  4568   4577     int res;
  4569   4578   
  4570   4579     pC = p->apCsr[pOp->p1];
................................................................................
  4587   4596     assert( pC->pCursor );
  4588   4597     assert( res==0 || (res==1 && pC->isTable==0) );
  4589   4598     testcase( res==1 );
  4590   4599     assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
  4591   4600     assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
  4592   4601     assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
  4593   4602     assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
         4603  +
         4604  +  /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
         4605  +  ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
         4606  +  assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
         4607  +       || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
         4608  +       || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
         4609  +  assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
         4610  +       || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
         4611  +       || pC->seekOp==OP_Last );
         4612  +
  4594   4613     rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4595   4614   next_tail:
  4596   4615     pC->cacheStatus = CACHE_STALE;
  4597   4616     VdbeBranchTaken(res==0,2);
  4598   4617     if( res==0 ){
  4599   4618       pC->nullRow = 0;
  4600   4619       pc = pOp->p2 - 1;
................................................................................
  5067   5086   }
  5068   5087   #endif /* !defined(SQLITE_OMIT_ANALYZE) */
  5069   5088   
  5070   5089   /* Opcode: DropTable P1 * * P4 *
  5071   5090   **
  5072   5091   ** Remove the internal (in-memory) data structures that describe
  5073   5092   ** the table named P4 in database P1.  This is called after a table
  5074         -** is dropped in order to keep the internal representation of the
         5093  +** is dropped from disk (using the Destroy opcode) in order to keep 
         5094  +** the internal representation of the
  5075   5095   ** schema consistent with what is on disk.
  5076   5096   */
  5077   5097   case OP_DropTable: {
  5078   5098     sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
  5079   5099     break;
  5080   5100   }
  5081   5101   
  5082   5102   /* Opcode: DropIndex P1 * * P4 *
  5083   5103   **
  5084   5104   ** Remove the internal (in-memory) data structures that describe
  5085   5105   ** the index named P4 in database P1.  This is called after an index
  5086         -** is dropped in order to keep the internal representation of the
         5106  +** is dropped from disk (using the Destroy opcode)
         5107  +** in order to keep the internal representation of the
  5087   5108   ** schema consistent with what is on disk.
  5088   5109   */
  5089   5110   case OP_DropIndex: {
  5090   5111     sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
  5091   5112     break;
  5092   5113   }
  5093   5114   
  5094   5115   /* Opcode: DropTrigger P1 * * P4 *
  5095   5116   **
  5096   5117   ** Remove the internal (in-memory) data structures that describe
  5097   5118   ** the trigger named P4 in database P1.  This is called after a trigger
  5098         -** is dropped in order to keep the internal representation of the
         5119  +** is dropped from disk (using the Destroy opcode) in order to keep 
         5120  +** the internal representation of the
  5099   5121   ** schema consistent with what is on disk.
  5100   5122   */
  5101   5123   case OP_DropTrigger: {
  5102   5124     sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
  5103   5125     break;
  5104   5126   }
  5105   5127   
................................................................................
  5506   5528     VdbeBranchTaken( pIn1->u.i>0, 2);
  5507   5529     if( pIn1->u.i>0 ){
  5508   5530        pc = pOp->p2 - 1;
  5509   5531     }
  5510   5532     break;
  5511   5533   }
  5512   5534   
  5513         -/* Opcode: IfNeg P1 P2 * * *
  5514         -** Synopsis: if r[P1]<0 goto P2
         5535  +/* Opcode: IfNeg P1 P2 P3 * *
         5536  +** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
  5515   5537   **
  5516         -** If the value of register P1 is less than zero, jump to P2. 
  5517         -**
  5518         -** It is illegal to use this instruction on a register that does
  5519         -** not contain an integer.  An assertion fault will result if you try.
         5538  +** Register P1 must contain an integer.  Add literal P3 to the value in
         5539  +** register P1 then if the value of register P1 is less than zero, jump to P2. 
  5520   5540   */
  5521   5541   case OP_IfNeg: {        /* jump, in1 */
  5522   5542     pIn1 = &aMem[pOp->p1];
  5523   5543     assert( pIn1->flags&MEM_Int );
         5544  +  pIn1->u.i += pOp->p3;
  5524   5545     VdbeBranchTaken(pIn1->u.i<0, 2);
  5525   5546     if( pIn1->u.i<0 ){
  5526   5547        pc = pOp->p2 - 1;
  5527   5548     }
  5528   5549     break;
  5529   5550   }
  5530   5551   
  5531   5552   /* Opcode: IfZero P1 P2 P3 * *
  5532   5553   ** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
  5533   5554   **
  5534   5555   ** The register P1 must contain an integer.  Add literal P3 to the
  5535   5556   ** value in register P1.  If the result is exactly 0, jump to P2. 
  5536         -**
  5537         -** It is illegal to use this instruction on a register that does
  5538         -** not contain an integer.  An assertion fault will result if you try.
  5539   5557   */
  5540   5558   case OP_IfZero: {        /* jump, in1 */
  5541   5559     pIn1 = &aMem[pOp->p1];
  5542   5560     assert( pIn1->flags&MEM_Int );
  5543   5561     pIn1->u.i += pOp->p3;
  5544   5562     VdbeBranchTaken(pIn1->u.i==0, 2);
  5545   5563     if( pIn1->u.i==0 ){
................................................................................
  5560   5578   ** successors.
  5561   5579   */
  5562   5580   case OP_AggStep: {
  5563   5581     int n;
  5564   5582     int i;
  5565   5583     Mem *pMem;
  5566   5584     Mem *pRec;
         5585  +  Mem t;
  5567   5586     sqlite3_context ctx;
  5568   5587     sqlite3_value **apVal;
  5569   5588   
  5570   5589     n = pOp->p5;
  5571   5590     assert( n>=0 );
  5572   5591     pRec = &aMem[pOp->p2];
  5573   5592     apVal = p->apArg;
................................................................................
  5577   5596       apVal[i] = pRec;
  5578   5597       memAboutToChange(p, pRec);
  5579   5598     }
  5580   5599     ctx.pFunc = pOp->p4.pFunc;
  5581   5600     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5582   5601     ctx.pMem = pMem = &aMem[pOp->p3];
  5583   5602     pMem->n++;
  5584         -  ctx.s.flags = MEM_Null;
  5585         -  ctx.s.z = 0;
  5586         -  ctx.s.zMalloc = 0;
  5587         -  ctx.s.xDel = 0;
  5588         -  ctx.s.db = db;
         5603  +  t.flags = MEM_Null;
         5604  +  t.z = 0;
         5605  +  t.zMalloc = 0;
         5606  +  t.xDel = 0;
         5607  +  t.db = db;
         5608  +  ctx.pOut = &t;
  5589   5609     ctx.isError = 0;
  5590   5610     ctx.pColl = 0;
  5591   5611     ctx.skipFlag = 0;
  5592   5612     if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5593   5613       assert( pOp>p->aOp );
  5594   5614       assert( pOp[-1].p4type==P4_COLLSEQ );
  5595   5615       assert( pOp[-1].opcode==OP_CollSeq );
  5596   5616       ctx.pColl = pOp[-1].p4.pColl;
  5597   5617     }
  5598   5618     (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  5599   5619     if( ctx.isError ){
  5600         -    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
         5620  +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
  5601   5621       rc = ctx.isError;
  5602   5622     }
  5603   5623     if( ctx.skipFlag ){
  5604   5624       assert( pOp[-1].opcode==OP_CollSeq );
  5605   5625       i = pOp[-1].p1;
  5606   5626       if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
  5607   5627     }
  5608         -
  5609         -  sqlite3VdbeMemRelease(&ctx.s);
  5610         -
         5628  +  sqlite3VdbeMemRelease(&t);
  5611   5629     break;
  5612   5630   }
  5613   5631   
  5614   5632   /* Opcode: AggFinal P1 P2 * P4 *
  5615   5633   ** Synopsis: accum=r[P1] N=P2
  5616   5634   **
  5617   5635   ** Execute the finalizer function for an aggregate.  P1 is
................................................................................
  6053   6071       sqlite3VdbeMemSetNull(pDest);
  6054   6072       break;
  6055   6073     }
  6056   6074     pVtab = pCur->pVtabCursor->pVtab;
  6057   6075     pModule = pVtab->pModule;
  6058   6076     assert( pModule->xColumn );
  6059   6077     memset(&sContext, 0, sizeof(sContext));
  6060         -
  6061         -  /* The output cell may already have a buffer allocated. Move
  6062         -  ** the current contents to sContext.s so in case the user-function 
  6063         -  ** can use the already allocated buffer instead of allocating a 
  6064         -  ** new one.
  6065         -  */
  6066         -  sqlite3VdbeMemMove(&sContext.s, pDest);
  6067         -  MemSetTypeFlag(&sContext.s, MEM_Null);
  6068         -
         6078  +  sContext.pOut = pDest;
         6079  +  MemSetTypeFlag(pDest, MEM_Null);
  6069   6080     rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
  6070   6081     sqlite3VtabImportErrmsg(p, pVtab);
  6071   6082     if( sContext.isError ){
  6072   6083       rc = sContext.isError;
  6073   6084     }
  6074         -
  6075         -  /* Copy the result of the function to the P3 register. We
  6076         -  ** do this regardless of whether or not an error occurred to ensure any
  6077         -  ** dynamic allocation in sContext.s (a Mem struct) is  released.
  6078         -  */
  6079         -  sqlite3VdbeChangeEncoding(&sContext.s, encoding);
  6080         -  sqlite3VdbeMemMove(pDest, &sContext.s);
         6085  +  sqlite3VdbeChangeEncoding(pDest, encoding);
  6081   6086     REGISTER_TRACE(pOp->p3, pDest);
  6082   6087     UPDATE_MAX_BLOBSIZE(pDest);
  6083   6088   
  6084   6089     if( sqlite3VdbeMemTooBig(pDest) ){
  6085   6090       goto too_big;
  6086   6091     }
  6087   6092     break;

Changes to src/vdbeInt.h.

    64     64     BtCursor *pCursor;    /* The cursor structure of the backend */
    65     65     Btree *pBt;           /* Separate file holding temporary table */
    66     66     KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
    67     67     int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
    68     68     int pseudoTableReg;   /* Register holding pseudotable content. */
    69     69     i16 nField;           /* Number of fields in the header */
    70     70     u16 nHdrParsed;       /* Number of header fields parsed so far */
           71  +#ifdef SQLITE_DEBUG
           72  +  u8 seekOp;            /* Most recent seek operation on this cursor */
           73  +#endif
    71     74     i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
    72     75     u8 nullRow;           /* True if pointing to a row with no data */
    73     76     u8 rowidIsValid;      /* True if lastRowid is valid */
    74     77     u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
    75     78     Bool isEphemeral:1;   /* True for an ephemeral table */
    76     79     Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
    77     80     Bool isTable:1;       /* True if a table requiring integer keys */
................................................................................
   259    262   ** But this file is the only place where the internal details of this
   260    263   ** structure are known.
   261    264   **
   262    265   ** This structure is defined inside of vdbeInt.h because it uses substructures
   263    266   ** (Mem) which are only defined there.
   264    267   */
   265    268   struct sqlite3_context {
          269  +  Mem *pOut;            /* The return value is stored here */
   266    270     FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
   267         -  Mem s;                /* The return value is stored here */
   268    271     Mem *pMem;            /* Memory cell used to store aggregate context */
   269    272     CollSeq *pColl;       /* Collating sequence */
   270    273     Vdbe *pVdbe;          /* The VM that owns this context */
   271    274     int iOp;              /* Instruction number of OP_Function */
   272    275     int isError;          /* Error code returned by the function. */
   273    276     u8 skipFlag;          /* Skip skip accumulator loading if true */
   274    277     u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */
................................................................................
   411    414   #else
   412    415     void sqlite3VdbeMemSetDouble(Mem*, double);
   413    416   #endif
   414    417   void sqlite3VdbeMemSetNull(Mem*);
   415    418   void sqlite3VdbeMemSetZeroBlob(Mem*,int);
   416    419   void sqlite3VdbeMemSetRowSet(Mem*);
   417    420   int sqlite3VdbeMemMakeWriteable(Mem*);
   418         -int sqlite3VdbeMemStringify(Mem*, int);
          421  +int sqlite3VdbeMemStringify(Mem*, u8, u8);
   419    422   i64 sqlite3VdbeIntValue(Mem*);
   420    423   int sqlite3VdbeMemIntegerify(Mem*);
   421    424   double sqlite3VdbeRealValue(Mem*);
   422    425   void sqlite3VdbeIntegerAffinity(Mem*);
   423    426   int sqlite3VdbeMemRealify(Mem*);
   424    427   int sqlite3VdbeMemNumerify(Mem*);
          428  +void sqlite3VdbeMemCast(Mem*,u8,u8);
   425    429   int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
   426    430   void sqlite3VdbeMemRelease(Mem *p);
   427    431   void sqlite3VdbeMemReleaseExternal(Mem *p);
   428    432   #define VdbeMemDynamic(X)  \
   429    433     (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
   430         -#define VdbeMemRelease(X)  \
          434  +#define VdbeMemReleaseExtern(X)  \
   431    435     if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
   432    436   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   433    437   const char *sqlite3OpcodeName(int);
   434    438   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   435    439   int sqlite3VdbeCloseStatement(Vdbe *, int);
   436    440   void sqlite3VdbeFrameDelete(VdbeFrame*);
   437    441   int sqlite3VdbeFrameRestore(VdbeFrame *);

Changes to src/vdbeapi.c.

   219    219   static void setResultStrOrError(
   220    220     sqlite3_context *pCtx,  /* Function context */
   221    221     const char *z,          /* String pointer */
   222    222     int n,                  /* Bytes in string, or negative */
   223    223     u8 enc,                 /* Encoding of z.  0 for BLOBs */
   224    224     void (*xDel)(void*)     /* Destructor function */
   225    225   ){
   226         -  if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
          226  +  if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
   227    227       sqlite3_result_error_toobig(pCtx);
   228    228     }
   229    229   }
   230    230   void sqlite3_result_blob(
   231    231     sqlite3_context *pCtx, 
   232    232     const void *z, 
   233    233     int n, 
   234    234     void (*xDel)(void *)
   235    235   ){
   236    236     assert( n>=0 );
   237         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          237  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   238    238     setResultStrOrError(pCtx, z, n, 0, xDel);
   239    239   }
   240    240   void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
   241         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   242         -  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
          241  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          242  +  sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
   243    243   }
   244    244   void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
   245         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          245  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   246    246     pCtx->isError = SQLITE_ERROR;
   247    247     pCtx->fErrorOrAux = 1;
   248         -  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
          248  +  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
   249    249   }
   250    250   #ifndef SQLITE_OMIT_UTF16
   251    251   void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
   252         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          252  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   253    253     pCtx->isError = SQLITE_ERROR;
   254    254     pCtx->fErrorOrAux = 1;
   255         -  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
          255  +  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
   256    256   }
   257    257   #endif
   258    258   void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
   259         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   260         -  sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
          259  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          260  +  sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
   261    261   }
   262    262   void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
   263         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   264         -  sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
          263  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          264  +  sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
   265    265   }
   266    266   void sqlite3_result_null(sqlite3_context *pCtx){
   267         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   268         -  sqlite3VdbeMemSetNull(&pCtx->s);
          267  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          268  +  sqlite3VdbeMemSetNull(pCtx->pOut);
   269    269   }
   270    270   void sqlite3_result_text(
   271    271     sqlite3_context *pCtx, 
   272    272     const char *z, 
   273    273     int n,
   274    274     void (*xDel)(void *)
   275    275   ){
   276         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          276  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   277    277     setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
   278    278   }
   279    279   #ifndef SQLITE_OMIT_UTF16
   280    280   void sqlite3_result_text16(
   281    281     sqlite3_context *pCtx, 
   282    282     const void *z, 
   283    283     int n, 
   284    284     void (*xDel)(void *)
   285    285   ){
   286         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          286  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   287    287     setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
   288    288   }
   289    289   void sqlite3_result_text16be(
   290    290     sqlite3_context *pCtx, 
   291    291     const void *z, 
   292    292     int n, 
   293    293     void (*xDel)(void *)
   294    294   ){
   295         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          295  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   296    296     setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
   297    297   }
   298    298   void sqlite3_result_text16le(
   299    299     sqlite3_context *pCtx, 
   300    300     const void *z, 
   301    301     int n, 
   302    302     void (*xDel)(void *)
   303    303   ){
   304         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          304  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   305    305     setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
   306    306   }
   307    307   #endif /* SQLITE_OMIT_UTF16 */
   308    308   void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
   309         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   310         -  sqlite3VdbeMemCopy(&pCtx->s, pValue);
          309  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          310  +  sqlite3VdbeMemCopy(pCtx->pOut, pValue);
   311    311   }
   312    312   void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
   313         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   314         -  sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
          313  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          314  +  sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
   315    315   }
   316    316   void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
   317    317     pCtx->isError = errCode;
   318    318     pCtx->fErrorOrAux = 1;
   319         -  if( pCtx->s.flags & MEM_Null ){
   320         -    sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 
          319  +  if( pCtx->pOut->flags & MEM_Null ){
          320  +    sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 
   321    321                            SQLITE_UTF8, SQLITE_STATIC);
   322    322     }
   323    323   }
   324    324   
   325    325   /* Force an SQLITE_TOOBIG error. */
   326    326   void sqlite3_result_error_toobig(sqlite3_context *pCtx){
   327         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          327  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   328    328     pCtx->isError = SQLITE_TOOBIG;
   329    329     pCtx->fErrorOrAux = 1;
   330         -  sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
          330  +  sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 
   331    331                          SQLITE_UTF8, SQLITE_STATIC);
   332    332   }
   333    333   
   334    334   /* An SQLITE_NOMEM error. */
   335    335   void sqlite3_result_error_nomem(sqlite3_context *pCtx){
   336         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   337         -  sqlite3VdbeMemSetNull(&pCtx->s);
          336  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          337  +  sqlite3VdbeMemSetNull(pCtx->pOut);
   338    338     pCtx->isError = SQLITE_NOMEM;
   339    339     pCtx->fErrorOrAux = 1;
   340         -  pCtx->s.db->mallocFailed = 1;
          340  +  pCtx->pOut->db->mallocFailed = 1;
   341    341   }
   342    342   
   343    343   /*
   344    344   ** This function is called after a transaction has been committed. It 
   345    345   ** invokes callbacks registered with sqlite3_wal_hook() as required.
   346    346   */
   347    347   static int doWalCallbacks(sqlite3 *db){
................................................................................
   509    509     if( vdbeSafetyNotNull(v) ){
   510    510       return SQLITE_MISUSE_BKPT;
   511    511     }
   512    512     db = v->db;
   513    513     sqlite3_mutex_enter(db->mutex);
   514    514     v->doingRerun = 0;
   515    515     while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
   516         -         && cnt++ < SQLITE_MAX_SCHEMA_RETRY
   517         -         && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
          516  +         && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
          517  +    int savedPc = v->pc;
          518  +    rc2 = rc = sqlite3Reprepare(v);
          519  +    if( rc!=SQLITE_OK) break;
   518    520       sqlite3_reset(pStmt);
   519         -    v->doingRerun = 1;
          521  +    if( savedPc>=0 ) v->doingRerun = 1;
   520    522       assert( v->expired==0 );
   521    523     }
   522    524     if( rc2!=SQLITE_OK ){
   523    525       /* This case occurs after failing to recompile an sql statement. 
   524    526       ** The error message from the SQL compiler has already been loaded 
   525    527       ** into the database handle. This block copies the error message 
   526    528       ** from the database handle into the statement and sets the statement
................................................................................
   562    564   ** returns a copy of the pointer to the database connection (the 1st
   563    565   ** parameter) of the sqlite3_create_function() and
   564    566   ** sqlite3_create_function16() routines that originally registered the
   565    567   ** application defined function.
   566    568   */
   567    569   sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
   568    570     assert( p && p->pFunc );
   569         -  return p->s.db;
          571  +  return p->pOut->db;
   570    572   }
   571    573   
   572    574   /*
   573    575   ** Return the current time for a statement
   574    576   */
   575    577   sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
   576    578     Vdbe *v = p->pVdbe;
   577    579     int rc;
   578    580     if( v->iCurrentTime==0 ){
   579         -    rc = sqlite3OsCurrentTimeInt64(p->s.db->pVfs, &v->iCurrentTime);
          581  +    rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, &v->iCurrentTime);
   580    582       if( rc ) v->iCurrentTime = 0;
   581    583     }
   582    584     return v->iCurrentTime;
   583    585   }
   584    586   
   585    587   /*
   586    588   ** The following is the implementation of an SQL function that always
................................................................................
   599    601     char *zErr;
   600    602     UNUSED_PARAMETER2(NotUsed, NotUsed2);
   601    603     zErr = sqlite3_mprintf(
   602    604         "unable to use function %s in the requested context", zName);
   603    605     sqlite3_result_error(context, zErr, -1);
   604    606     sqlite3_free(zErr);
   605    607   }
          608  +
          609  +/*
          610  +** Create a new aggregate context for p and return a pointer to
          611  +** its pMem->z element.
          612  +*/
          613  +static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
          614  +  Mem *pMem = p->pMem;
          615  +  assert( (pMem->flags & MEM_Agg)==0 );
          616  +  if( nByte<=0 ){
          617  +    sqlite3VdbeMemReleaseExternal(pMem);
          618  +    pMem->flags = MEM_Null;
          619  +    pMem->z = 0;
          620  +  }else{
          621  +    sqlite3VdbeMemGrow(pMem, nByte, 0);
          622  +    pMem->flags = MEM_Agg;
          623  +    pMem->u.pDef = p->pFunc;
          624  +    if( pMem->z ){
          625  +      memset(pMem->z, 0, nByte);
          626  +    }
          627  +  }
          628  +  return (void*)pMem->z;
          629  +}
   606    630   
   607    631   /*
   608    632   ** Allocate or return the aggregate context for a user function.  A new
   609    633   ** context is allocated on the first call.  Subsequent calls return the
   610    634   ** same context that was returned on prior calls.
   611    635   */
   612    636   void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
   613         -  Mem *pMem;
   614    637     assert( p && p->pFunc && p->pFunc->xStep );
   615         -  assert( sqlite3_mutex_held(p->s.db->mutex) );
   616         -  pMem = p->pMem;
          638  +  assert( sqlite3_mutex_held(p->pOut->db->mutex) );
   617    639     testcase( nByte<0 );
   618         -  if( (pMem->flags & MEM_Agg)==0 ){
   619         -    if( nByte<=0 ){
   620         -      sqlite3VdbeMemReleaseExternal(pMem);
   621         -      pMem->flags = MEM_Null;
   622         -      pMem->z = 0;
   623         -    }else{
   624         -      sqlite3VdbeMemGrow(pMem, nByte, 0);
   625         -      pMem->flags = MEM_Agg;
   626         -      pMem->u.pDef = p->pFunc;
   627         -      if( pMem->z ){
   628         -        memset(pMem->z, 0, nByte);
   629         -      }
   630         -    }
          640  +  if( (p->pMem->flags & MEM_Agg)==0 ){
          641  +    return createAggContext(p, nByte);
          642  +  }else{
          643  +    return (void*)p->pMem->z;
   631    644     }
   632         -  return (void*)pMem->z;
   633    645   }
   634    646   
   635    647   /*
   636    648   ** Return the auxilary data pointer, if any, for the iArg'th argument to
   637    649   ** the user-function defined by pCtx.
   638    650   */
   639    651   void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
   640    652     AuxData *pAuxData;
   641    653   
   642         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          654  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   643    655     for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
   644    656       if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
   645    657     }
   646    658   
   647    659     return (pAuxData ? pAuxData->pAux : 0);
   648    660   }
   649    661   
................................................................................
   657    669     int iArg, 
   658    670     void *pAux, 
   659    671     void (*xDelete)(void*)
   660    672   ){
   661    673     AuxData *pAuxData;
   662    674     Vdbe *pVdbe = pCtx->pVdbe;
   663    675   
   664         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          676  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   665    677     if( iArg<0 ) goto failed;
   666    678   
   667    679     for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
   668    680       if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
   669    681     }
   670    682     if( pAuxData==0 ){
   671    683       pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
................................................................................
   764    776     pVm = (Vdbe *)pStmt;
   765    777     if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
   766    778       sqlite3_mutex_enter(pVm->db->mutex);
   767    779       pOut = &pVm->pResultSet[i];
   768    780     }else{
   769    781       if( pVm && ALWAYS(pVm->db) ){
   770    782         sqlite3_mutex_enter(pVm->db->mutex);
   771         -      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
          783  +      sqlite3Error(pVm->db, SQLITE_RANGE);
   772    784       }
   773    785       pOut = (Mem*)columnNullValue();
   774    786     }
   775    787     return pOut;
   776    788   }
   777    789   
   778    790   /*
................................................................................
  1029   1041   static int vdbeUnbind(Vdbe *p, int i){
  1030   1042     Mem *pVar;
  1031   1043     if( vdbeSafetyNotNull(p) ){
  1032   1044       return SQLITE_MISUSE_BKPT;
  1033   1045     }
  1034   1046     sqlite3_mutex_enter(p->db->mutex);
  1035   1047     if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
  1036         -    sqlite3Error(p->db, SQLITE_MISUSE, 0);
         1048  +    sqlite3Error(p->db, SQLITE_MISUSE);
  1037   1049       sqlite3_mutex_leave(p->db->mutex);
  1038   1050       sqlite3_log(SQLITE_MISUSE, 
  1039   1051           "bind on a busy prepared statement: [%s]", p->zSql);
  1040   1052       return SQLITE_MISUSE_BKPT;
  1041   1053     }
  1042   1054     if( i<1 || i>p->nVar ){
  1043         -    sqlite3Error(p->db, SQLITE_RANGE, 0);
         1055  +    sqlite3Error(p->db, SQLITE_RANGE);
  1044   1056       sqlite3_mutex_leave(p->db->mutex);
  1045   1057       return SQLITE_RANGE;
  1046   1058     }
  1047   1059     i--;
  1048   1060     pVar = &p->aVar[i];
  1049   1061     sqlite3VdbeMemRelease(pVar);
  1050   1062     pVar->flags = MEM_Null;
  1051         -  sqlite3Error(p->db, SQLITE_OK, 0);
         1063  +  sqlite3Error(p->db, SQLITE_OK);
  1052   1064   
  1053   1065     /* If the bit corresponding to this variable in Vdbe.expmask is set, then 
  1054   1066     ** binding a new value to this variable invalidates the current query plan.
  1055   1067     **
  1056   1068     ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
  1057   1069     ** parameter in the WHERE clause might influence the choice of query plan
  1058   1070     ** for a statement, then the statement will be automatically recompiled,
................................................................................
  1086   1098     if( rc==SQLITE_OK ){
  1087   1099       if( zData!=0 ){
  1088   1100         pVar = &p->aVar[i-1];
  1089   1101         rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
  1090   1102         if( rc==SQLITE_OK && encoding!=0 ){
  1091   1103           rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
  1092   1104         }
  1093         -      sqlite3Error(p->db, rc, 0);
         1105  +      sqlite3Error(p->db, rc);
  1094   1106         rc = sqlite3ApiExit(p->db, rc);
  1095   1107       }
  1096   1108       sqlite3_mutex_leave(p->db->mutex);
  1097   1109     }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
  1098   1110       xDel((void*)zData);
  1099   1111     }
  1100   1112     return rc;

Changes to src/vdbeaux.c.

    80     80     zTmp = pA->zSql;
    81     81     pA->zSql = pB->zSql;
    82     82     pB->zSql = zTmp;
    83     83     pB->isPrepareV2 = pA->isPrepareV2;
    84     84   }
    85     85   
    86     86   /*
    87         -** Resize the Vdbe.aOp array so that it is at least one op larger than 
    88         -** it was.
           87  +** Resize the Vdbe.aOp array so that it is at least nOp elements larger 
           88  +** than its current size. nOp is guaranteed to be less than or equal
           89  +** to 1024/sizeof(Op).
    89     90   **
    90     91   ** If an out-of-memory error occurs while resizing the array, return
    91         -** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 
           92  +** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain 
    92     93   ** unchanged (this is so that any opcodes already allocated can be 
    93     94   ** correctly deallocated along with the rest of the Vdbe).
    94     95   */
    95         -static int growOpArray(Vdbe *v){
           96  +static int growOpArray(Vdbe *v, int nOp){
    96     97     VdbeOp *pNew;
    97     98     Parse *p = v->pParse;
           99  +
          100  +  /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
          101  +  ** more frequent reallocs and hence provide more opportunities for 
          102  +  ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
          103  +  ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
          104  +  ** by the minimum* amount required until the size reaches 512.  Normal
          105  +  ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
          106  +  ** size of the op array or add 1KB of space, whichever is smaller. */
          107  +#ifdef SQLITE_TEST_REALLOC_STRESS
          108  +  int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
          109  +#else
    98    110     int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
          111  +  UNUSED_PARAMETER(nOp);
          112  +#endif
          113  +
          114  +  assert( nOp<=(1024/sizeof(Op)) );
          115  +  assert( nNew>=(p->nOpAlloc+nOp) );
    99    116     pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
   100    117     if( pNew ){
   101    118       p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
   102    119       v->aOp = pNew;
   103    120     }
   104    121     return (pNew ? SQLITE_OK : SQLITE_NOMEM);
   105    122   }
................................................................................
   135    152     int i;
   136    153     VdbeOp *pOp;
   137    154   
   138    155     i = p->nOp;
   139    156     assert( p->magic==VDBE_MAGIC_INIT );
   140    157     assert( op>0 && op<0xff );
   141    158     if( p->pParse->nOpAlloc<=i ){
   142         -    if( growOpArray(p) ){
          159  +    if( growOpArray(p, 1) ){
   143    160         return 1;
   144    161       }
   145    162     }
   146    163     p->nOp++;
   147    164     pOp = &p->aOp[i];
   148    165     pOp->opcode = (u8)op;
   149    166     pOp->p5 = 0;
................................................................................
   537    554   /*
   538    555   ** Add a whole list of operations to the operation stack.  Return the
   539    556   ** address of the first operation added.
   540    557   */
   541    558   int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
   542    559     int addr;
   543    560     assert( p->magic==VDBE_MAGIC_INIT );
   544         -  if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
          561  +  if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
   545    562       return 0;
   546    563     }
   547    564     addr = p->nOp;
   548    565     if( ALWAYS(nOp>0) ){
   549    566       int i;
   550    567       VdbeOpList const *pIn = aOp;
   551    568       for(i=0; i<nOp; i++, pIn++){
................................................................................
   722    739     pVdbe->pProgram = p;
   723    740   }
   724    741   
   725    742   /*
   726    743   ** Change the opcode at addr into OP_Noop
   727    744   */
   728    745   void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
   729         -  if( p->aOp ){
          746  +  if( addr<p->nOp ){
   730    747       VdbeOp *pOp = &p->aOp[addr];
   731    748       sqlite3 *db = p->db;
   732    749       freeP4(db, pOp->p4type, pOp->p4.p);
   733    750       memset(pOp, 0, sizeof(pOp[0]));
   734    751       pOp->opcode = OP_Noop;
   735    752       if( addr==p->nOp-1 ) p->nOp--;
   736    753     }
................................................................................
  2297   2314       int isSpecialError;            /* Set to true if a 'special' error */
  2298   2315   
  2299   2316       /* Lock all btrees used by the statement */
  2300   2317       sqlite3VdbeEnter(p);
  2301   2318   
  2302   2319       /* Check for one of the special errors */
  2303   2320       mrc = p->rc & 0xff;
  2304         -    assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
  2305   2321       isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
  2306   2322                        || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
  2307   2323       if( isSpecialError ){
  2308   2324         /* If the query was read-only and the error code is SQLITE_INTERRUPT, 
  2309   2325         ** no rollback is necessary. Otherwise, at least a savepoint 
  2310   2326         ** transaction must be rolled back to restore the database to a 
  2311   2327         ** consistent state.
................................................................................
  2477   2493       sqlite3BeginBenignMalloc();
  2478   2494       if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
  2479   2495       sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
  2480   2496       sqlite3EndBenignMalloc();
  2481   2497       db->mallocFailed = mallocFailed;
  2482   2498       db->errCode = rc;
  2483   2499     }else{
  2484         -    sqlite3Error(db, rc, 0);
         2500  +    sqlite3Error(db, rc);
  2485   2501     }
  2486   2502     return rc;
  2487   2503   }
  2488   2504   
  2489   2505   #ifdef SQLITE_ENABLE_SQLLOG
  2490   2506   /*
  2491   2507   ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 
................................................................................
  2540   2556       p->zErrMsg = 0;
  2541   2557       if( p->runOnlyOnce ) p->expired = 1;
  2542   2558     }else if( p->rc && p->expired ){
  2543   2559       /* The expired flag was set on the VDBE before the first call
  2544   2560       ** to sqlite3_step(). For consistency (since sqlite3_step() was
  2545   2561       ** called), set the database error in this case as well.
  2546   2562       */
  2547         -    sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
         2563  +    sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
  2548   2564       sqlite3DbFree(db, p->zErrMsg);
  2549   2565       p->zErrMsg = 0;
  2550   2566     }
  2551   2567   
  2552   2568     /* Reclaim all memory used by the VDBE
  2553   2569     */
  2554   2570     Cleanup(p);
................................................................................
  2692   2708     if( p->pNext ){
  2693   2709       p->pNext->pPrev = p->pPrev;
  2694   2710     }
  2695   2711     p->magic = VDBE_MAGIC_DEAD;
  2696   2712     p->db = 0;
  2697   2713     sqlite3DbFree(db, p);
  2698   2714   }
         2715  +
         2716  +/*
         2717  +** The cursor "p" has a pending seek operation that has not yet been
         2718  +** carried out.  Seek the cursor now.  If an error occurs, return
         2719  +** the appropriate error code.
         2720  +*/
         2721  +static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
         2722  +  int res, rc;
         2723  +#ifdef SQLITE_TEST
         2724  +  extern int sqlite3_search_count;
         2725  +#endif
         2726  +  assert( p->deferredMoveto );
         2727  +  assert( p->isTable );
         2728  +  rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
         2729  +  if( rc ) return rc;
         2730  +  p->lastRowid = p->movetoTarget;
         2731  +  if( res!=0 ) return SQLITE_CORRUPT_BKPT;
         2732  +  p->rowidIsValid = 1;
         2733  +#ifdef SQLITE_TEST
         2734  +  sqlite3_search_count++;
         2735  +#endif
         2736  +  p->deferredMoveto = 0;
         2737  +  p->cacheStatus = CACHE_STALE;
         2738  +  return SQLITE_OK;
         2739  +}
         2740  +
         2741  +/*
         2742  +** Something has moved cursor "p" out of place.  Maybe the row it was
         2743  +** pointed to was deleted out from under it.  Or maybe the btree was
         2744  +** rebalanced.  Whatever the cause, try to restore "p" to the place it
         2745  +** is suppose to be pointing.  If the row was deleted out from under the
         2746  +** cursor, set the cursor to point to a NULL row.
         2747  +*/
         2748  +static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
         2749  +  int isDifferentRow, rc;
         2750  +  assert( p->pCursor!=0 );
         2751  +  assert( sqlite3BtreeCursorHasMoved(p->pCursor) );
         2752  +  rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow);
         2753  +  p->cacheStatus = CACHE_STALE;
         2754  +  if( isDifferentRow ) p->nullRow = 1;
         2755  +  return rc;
         2756  +}
  2699   2757   
  2700   2758   /*
  2701   2759   ** Make sure the cursor p is ready to read or write the row to which it
  2702   2760   ** was last positioned.  Return an error code if an OOM fault or I/O error
  2703   2761   ** prevents us from positioning the cursor to its correct position.
  2704   2762   **
  2705   2763   ** If a MoveTo operation is pending on the given cursor, then do that
................................................................................
  2708   2766   ** a NULL row.
  2709   2767   **
  2710   2768   ** If the cursor is already pointing to the correct row and that row has
  2711   2769   ** not been deleted out from under the cursor, then this routine is a no-op.
  2712   2770   */
  2713   2771   int sqlite3VdbeCursorMoveto(VdbeCursor *p){
  2714   2772     if( p->deferredMoveto ){
  2715         -    int res, rc;
  2716         -#ifdef SQLITE_TEST
  2717         -    extern int sqlite3_search_count;
  2718         -#endif
  2719         -    assert( p->isTable );
  2720         -    rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
  2721         -    if( rc ) return rc;
  2722         -    p->lastRowid = p->movetoTarget;
  2723         -    if( res!=0 ) return SQLITE_CORRUPT_BKPT;
  2724         -    p->rowidIsValid = 1;
  2725         -#ifdef SQLITE_TEST
  2726         -    sqlite3_search_count++;
  2727         -#endif
  2728         -    p->deferredMoveto = 0;
  2729         -    p->cacheStatus = CACHE_STALE;
  2730         -  }else if( p->pCursor ){
  2731         -    int hasMoved;
  2732         -    int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
  2733         -    if( rc ) return rc;
  2734         -    if( hasMoved ){
  2735         -      p->cacheStatus = CACHE_STALE;
  2736         -      if( hasMoved==2 ) p->nullRow = 1;
  2737         -    }
         2773  +    return handleDeferredMoveto(p);
         2774  +  }
         2775  +  if( sqlite3BtreeCursorHasMoved(p->pCursor) ){
         2776  +    return handleMovedCursor(p);
  2738   2777     }
  2739   2778     return SQLITE_OK;
  2740   2779   }
  2741   2780   
  2742   2781   /*
  2743   2782   ** The following functions:
  2744   2783   **
................................................................................
  2782   2821   */
  2783   2822   
  2784   2823   /*
  2785   2824   ** Return the serial-type for the value stored in pMem.
  2786   2825   */
  2787   2826   u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
  2788   2827     int flags = pMem->flags;
  2789         -  int n;
         2828  +  u32 n;
  2790   2829   
  2791   2830     if( flags&MEM_Null ){
  2792   2831       return 0;
  2793   2832     }
  2794   2833     if( flags&MEM_Int ){
  2795   2834       /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
  2796   2835   #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
................................................................................
  2812   2851       if( u<=MAX_6BYTE ) return 5;
  2813   2852       return 6;
  2814   2853     }
  2815   2854     if( flags&MEM_Real ){
  2816   2855       return 7;
  2817   2856     }
  2818   2857     assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
  2819         -  n = pMem->n;
         2858  +  assert( pMem->n>=0 );
         2859  +  n = (u32)pMem->n;
  2820   2860     if( flags & MEM_Zero ){
  2821   2861       n += pMem->u.nZero;
  2822   2862     }
  2823         -  assert( n>=0 );
  2824   2863     return ((n*2) + 12 + ((flags&MEM_Str)!=0));
  2825   2864   }
  2826   2865   
  2827   2866   /*
  2828   2867   ** Return the length of the data corresponding to the supplied serial-type.
  2829   2868   */
  2830   2869   u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
................................................................................
  2913   2952         assert( sizeof(v)==sizeof(pMem->r) );
  2914   2953         memcpy(&v, &pMem->r, sizeof(v));
  2915   2954         swapMixedEndianFloat(v);
  2916   2955       }else{
  2917   2956         v = pMem->u.i;
  2918   2957       }
  2919   2958       len = i = sqlite3VdbeSerialTypeLen(serial_type);
  2920         -    while( i-- ){
  2921         -      buf[i] = (u8)(v&0xFF);
         2959  +    assert( i>0 );
         2960  +    do{
         2961  +      buf[--i] = (u8)(v&0xFF);
  2922   2962         v >>= 8;
  2923         -    }
         2963  +    }while( i );
  2924   2964       return len;
  2925   2965     }
  2926   2966   
  2927   2967     /* String or blob */
  2928   2968     if( serial_type>=12 ){
  2929   2969       assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
  2930   2970                == (int)sqlite3VdbeSerialTypeLen(serial_type) );
................................................................................
  2940   2980   /* Input "x" is a sequence of unsigned characters that represent a
  2941   2981   ** big-endian integer.  Return the equivalent native integer
  2942   2982   */
  2943   2983   #define ONE_BYTE_INT(x)    ((i8)(x)[0])
  2944   2984   #define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
  2945   2985   #define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
  2946   2986   #define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
         2987  +#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
  2947   2988   
  2948   2989   /*
  2949   2990   ** Deserialize the data blob pointed to by buf as serial type serial_type
  2950   2991   ** and store the result in pMem.  Return the number of bytes read.
         2992  +**
         2993  +** This function is implemented as two separate routines for performance.
         2994  +** The few cases that require local variables are broken out into a separate
         2995  +** routine so that in most cases the overhead of moving the stack pointer
         2996  +** is avoided.
  2951   2997   */ 
         2998  +static u32 SQLITE_NOINLINE serialGet(
         2999  +  const unsigned char *buf,     /* Buffer to deserialize from */
         3000  +  u32 serial_type,              /* Serial type to deserialize */
         3001  +  Mem *pMem                     /* Memory cell to write value into */
         3002  +){
         3003  +  u64 x = FOUR_BYTE_UINT(buf);
         3004  +  u32 y = FOUR_BYTE_UINT(buf+4);
         3005  +  x = (x<<32) + y;
         3006  +  if( serial_type==6 ){
         3007  +    pMem->u.i = *(i64*)&x;
         3008  +    pMem->flags = MEM_Int;
         3009  +    testcase( pMem->u.i<0 );
         3010  +  }else{
         3011  +#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
         3012  +    /* Verify that integers and floating point values use the same
         3013  +    ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
         3014  +    ** defined that 64-bit floating point values really are mixed
         3015  +    ** endian.
         3016  +    */
         3017  +    static const u64 t1 = ((u64)0x3ff00000)<<32;
         3018  +    static const double r1 = 1.0;
         3019  +    u64 t2 = t1;
         3020  +    swapMixedEndianFloat(t2);
         3021  +    assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
         3022  +#endif
         3023  +    assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
         3024  +    swapMixedEndianFloat(x);
         3025  +    memcpy(&pMem->r, &x, sizeof(x));
         3026  +    pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
         3027  +  }
         3028  +  return 8;
         3029  +}
  2952   3030   u32 sqlite3VdbeSerialGet(
  2953   3031     const unsigned char *buf,     /* Buffer to deserialize from */
  2954   3032     u32 serial_type,              /* Serial type to deserialize */
  2955   3033     Mem *pMem                     /* Memory cell to write value into */
  2956   3034   ){
  2957         -  u64 x;
  2958         -  u32 y;
  2959   3035     switch( serial_type ){
  2960   3036       case 10:   /* Reserved for future use */
  2961   3037       case 11:   /* Reserved for future use */
  2962   3038       case 0: {  /* NULL */
  2963   3039         pMem->flags = MEM_Null;
  2964   3040         break;
  2965   3041       }
................................................................................
  2978   3054       case 3: { /* 3-byte signed integer */
  2979   3055         pMem->u.i = THREE_BYTE_INT(buf);
  2980   3056         pMem->flags = MEM_Int;
  2981   3057         testcase( pMem->u.i<0 );
  2982   3058         return 3;
  2983   3059       }
  2984   3060       case 4: { /* 4-byte signed integer */
  2985         -      y = FOUR_BYTE_UINT(buf);
  2986         -      pMem->u.i = (i64)*(int*)&y;
         3061  +      pMem->u.i = FOUR_BYTE_INT(buf);
  2987   3062         pMem->flags = MEM_Int;
  2988   3063         testcase( pMem->u.i<0 );
  2989   3064         return 4;
  2990   3065       }
  2991   3066       case 5: { /* 6-byte signed integer */
  2992   3067         pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
  2993   3068         pMem->flags = MEM_Int;
  2994   3069         testcase( pMem->u.i<0 );
  2995   3070         return 6;
  2996   3071       }
  2997   3072       case 6:   /* 8-byte signed integer */
  2998   3073       case 7: { /* IEEE floating point */
  2999         -#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
  3000         -      /* Verify that integers and floating point values use the same
  3001         -      ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
  3002         -      ** defined that 64-bit floating point values really are mixed
  3003         -      ** endian.
  3004         -      */
  3005         -      static const u64 t1 = ((u64)0x3ff00000)<<32;
  3006         -      static const double r1 = 1.0;
  3007         -      u64 t2 = t1;
  3008         -      swapMixedEndianFloat(t2);
  3009         -      assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
  3010         -#endif
  3011         -      x = FOUR_BYTE_UINT(buf);
  3012         -      y = FOUR_BYTE_UINT(buf+4);
  3013         -      x = (x<<32) | y;
  3014         -      if( serial_type==6 ){
  3015         -        pMem->u.i = *(i64*)&x;
  3016         -        pMem->flags = MEM_Int;
  3017         -        testcase( pMem->u.i<0 );
  3018         -      }else{
  3019         -        assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
  3020         -        swapMixedEndianFloat(x);
  3021         -        memcpy(&pMem->r, &x, sizeof(x));
  3022         -        pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
  3023         -      }
  3024         -      return 8;
         3074  +      /* These use local variables, so do them in a separate routine
         3075  +      ** to avoid having to move the frame pointer in the common case */
         3076  +      return serialGet(buf,serial_type,pMem);
  3025   3077       }
  3026   3078       case 8:    /* Integer 0 */
  3027   3079       case 9: {  /* Integer 1 */
  3028   3080         pMem->u.i = serial_type-8;
  3029   3081         pMem->flags = MEM_Int;
  3030   3082         return 0;
  3031   3083       }
  3032   3084       default: {
  3033   3085         static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
  3034         -      u32 len = (serial_type-12)/2;
  3035   3086         pMem->z = (char *)buf;
  3036         -      pMem->n = len;
         3087  +      pMem->n = (serial_type-12)/2;
  3037   3088         pMem->xDel = 0;
  3038   3089         pMem->flags = aFlag[serial_type&1];
  3039         -      return len;
         3090  +      return pMem->n;
  3040   3091       }
  3041   3092     }
  3042   3093     return 0;
  3043   3094   }
  3044         -
  3045   3095   /*
  3046   3096   ** This routine is used to allocate sufficient space for an UnpackedRecord
  3047   3097   ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
  3048   3098   ** the first argument is a pointer to KeyInfo structure pKeyInfo.
  3049   3099   **
  3050   3100   ** The space is either allocated using sqlite3DbMallocRaw() or from within
  3051   3101   ** the unaligned buffer passed via the second and third arguments (presumably

Changes to src/vdbeblob.c.

   314    314   blob_open_out:
   315    315     if( rc==SQLITE_OK && db->mallocFailed==0 ){
   316    316       *ppBlob = (sqlite3_blob *)pBlob;
   317    317     }else{
   318    318       if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
   319    319       sqlite3DbFree(db, pBlob);
   320    320     }
   321         -  sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
          321  +  sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
   322    322     sqlite3DbFree(db, zErr);
   323    323     sqlite3ParserReset(pParse);
   324    324     sqlite3StackFree(db, pParse);
   325    325     rc = sqlite3ApiExit(db, rc);
   326    326     sqlite3_mutex_leave(db->mutex);
   327    327     return rc;
   328    328   }
................................................................................
   367    367     db = p->db;
   368    368     sqlite3_mutex_enter(db->mutex);
   369    369     v = (Vdbe*)p->pStmt;
   370    370   
   371    371     if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
   372    372       /* Request is out of range. Return a transient error. */
   373    373       rc = SQLITE_ERROR;
   374         -    sqlite3Error