/ Check-in [dfdc900f]
Login

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

Overview
Comment:Merge the recent performance enhancements implemented on trunk into the threads branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | threads
Files: files | file ages | folders
SHA1: dfdc900f5d1a31ee5c5f35a630c4a8253e69093b
User & Date: drh 2014-08-25 13:27:02
Context
2014-08-25
15:13
Query or change the maximum number of worker threads allowed on each database connection separately using the "PRAGMA threads" command. check-in: 29c5e8a7 user: drh tags: threads
13:27
Merge the recent performance enhancements implemented on trunk into the threads branch. check-in: dfdc900f user: drh tags: threads
11:33
Remove the pager_lookup() function since it is redundant with sqlite3PagerLookup(). check-in: 54164ce4 user: drh tags: trunk
2014-08-15
15:46
Merge the 3.8.6 release into the threads branch. check-in: 05807c41 user: drh tags: threads
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/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",

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

   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   **

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;
................................................................................
  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 );
................................................................................
  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 ){

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 ){

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

   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/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++;

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 );
................................................................................
    98     98           if( xCallback(pArg, nCol, azVals, azCols) ){
    99     99             /* EVIDENCE-OF: R-38229-40159 If the callback function to
   100    100             ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
   101    101             ** return SQLITE_ABORT. */
   102    102             rc = SQLITE_ABORT;
   103    103             sqlite3VdbeFinalize((Vdbe *)pStmt);
   104    104             pStmt = 0;
   105         -          sqlite3Error(db, SQLITE_ABORT, 0);
          105  +          sqlite3Error(db, SQLITE_ABORT);
   106    106             goto exec_out;
   107    107           }
   108    108         }
   109    109   
   110    110         if( rc!=SQLITE_ROW ){
   111    111           rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
   112    112           pStmt = 0;
................................................................................
   128    128     if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
   129    129       int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
   130    130       *pzErrMsg = sqlite3Malloc(nErrMsg);
   131    131       if( *pzErrMsg ){
   132    132         memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
   133    133       }else{
   134    134         rc = SQLITE_NOMEM;
   135         -      sqlite3Error(db, SQLITE_NOMEM, 0);
          135  +      sqlite3Error(db, SQLITE_NOMEM);
   136    136       }
   137    137     }else if( pzErrMsg ){
   138    138       *pzErrMsg = 0;
   139    139     }
   140    140   
   141    141     assert( (rc&db->errMask)==rc );
   142    142     sqlite3_mutex_leave(db->mutex);
   143    143     return rc;
   144    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.

   857    857     */
   858    858     sqlite3VtabRollback(db);
   859    859   
   860    860     /* Legacy behavior (sqlite3_close() behavior) is to return
   861    861     ** SQLITE_BUSY if the connection can not be closed immediately.
   862    862     */
   863    863     if( !forceZombie && connectionIsBusy(db) ){
   864         -    sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
          864  +    sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
   865    865          "statements or unfinished backups");
   866    866       sqlite3_mutex_leave(db->mutex);
   867    867       return SQLITE_BUSY;
   868    868     }
   869    869   
   870    870   #ifdef SQLITE_ENABLE_SQLLOG
   871    871     if( sqlite3GlobalConfig.xSqllog ){
................................................................................
   987    987         pMod->xDestroy(pMod->pAux);
   988    988       }
   989    989       sqlite3DbFree(db, pMod);
   990    990     }
   991    991     sqlite3HashClear(&db->aModule);
   992    992   #endif
   993    993   
   994         -  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
          994  +  sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
   995    995     sqlite3ValueFree(db->pErr);
   996    996     sqlite3CloseExtensions(db);
   997    997   
   998    998     db->magic = SQLITE_MAGIC_ERROR;
   999    999   
  1000   1000     /* The temp-database schema is allocated differently from the other schema
  1001   1001     ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
................................................................................
  1420   1420     ** and there are active VMs, then return SQLITE_BUSY. If a function
  1421   1421     ** is being overridden/deleted but there are no active VMs, allow the
  1422   1422     ** operation to continue but invalidate all precompiled statements.
  1423   1423     */
  1424   1424     p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
  1425   1425     if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
  1426   1426       if( db->nVdbeActive ){
  1427         -      sqlite3Error(db, SQLITE_BUSY, 
         1427  +      sqlite3ErrorWithMsg(db, SQLITE_BUSY, 
  1428   1428           "unable to delete/modify user-function due to active statements");
  1429   1429         assert( !db->mallocFailed );
  1430   1430         return SQLITE_BUSY;
  1431   1431       }else{
  1432   1432         sqlite3ExpirePreparedStatements(db);
  1433   1433       }
  1434   1434     }
................................................................................
  1758   1758   
  1759   1759     sqlite3_mutex_enter(db->mutex);
  1760   1760     if( zDb && zDb[0] ){
  1761   1761       iDb = sqlite3FindDbName(db, zDb);
  1762   1762     }
  1763   1763     if( iDb<0 ){
  1764   1764       rc = SQLITE_ERROR;
  1765         -    sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
         1765  +    sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
  1766   1766     }else{
  1767   1767       rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
  1768         -    sqlite3Error(db, rc, 0);
         1768  +    sqlite3Error(db, rc);
  1769   1769     }
  1770   1770     rc = sqlite3ApiExit(db, rc);
  1771   1771     sqlite3_mutex_leave(db->mutex);
  1772   1772     return rc;
  1773   1773   #endif
  1774   1774   }
  1775   1775   
................................................................................
  1916   1916     }
  1917   1917     sqlite3_mutex_enter(db->mutex);
  1918   1918     if( db->mallocFailed ){
  1919   1919       z = (void *)outOfMem;
  1920   1920     }else{
  1921   1921       z = sqlite3_value_text16(db->pErr);
  1922   1922       if( z==0 ){
  1923         -      sqlite3Error(db, db->errCode, sqlite3ErrStr(db->errCode));
         1923  +      sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
  1924   1924         z = sqlite3_value_text16(db->pErr);
  1925   1925       }
  1926   1926       /* A malloc() may have failed within the call to sqlite3_value_text16()
  1927   1927       ** above. If this is the case, then the db->mallocFailed flag needs to
  1928   1928       ** be cleared before returning. Do this directly, instead of via
  1929   1929       ** sqlite3ApiExit(), to avoid setting the database handle error message.
  1930   1930       */
................................................................................
  2003   2003     u8 enc,
  2004   2004     void* pCtx,
  2005   2005     int(*xCompare)(void*,int,const void*,int,const void*),
  2006   2006     void(*xDel)(void*)
  2007   2007   ){
  2008   2008     CollSeq *pColl;
  2009   2009     int enc2;
  2010         -  int nName = sqlite3Strlen30(zName);
  2011   2010     
  2012   2011     assert( sqlite3_mutex_held(db->mutex) );
  2013   2012   
  2014   2013     /* If SQLITE_UTF16 is specified as the encoding type, transform this
  2015   2014     ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
  2016   2015     ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
  2017   2016     */
................................................................................
  2028   2027     /* Check if this call is removing or replacing an existing collation 
  2029   2028     ** sequence. If so, and there are active VMs, return busy. If there
  2030   2029     ** are no active VMs, invalidate any pre-compiled statements.
  2031   2030     */
  2032   2031     pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
  2033   2032     if( pColl && pColl->xCmp ){
  2034   2033       if( db->nVdbeActive ){
  2035         -      sqlite3Error(db, SQLITE_BUSY, 
         2034  +      sqlite3ErrorWithMsg(db, SQLITE_BUSY, 
  2036   2035           "unable to delete/modify collation sequence due to active statements");
  2037   2036         return SQLITE_BUSY;
  2038   2037       }
  2039   2038       sqlite3ExpirePreparedStatements(db);
  2040   2039       invalidateCachedKeyInfo(db);
  2041   2040   
  2042   2041       /* If collation sequence pColl was created directly by a call to
  2043   2042       ** sqlite3_create_collation, and not generated by synthCollSeq(),
  2044   2043       ** then any copies made by synthCollSeq() need to be invalidated.
  2045   2044       ** Also, collation destructor - CollSeq.xDel() - function may need
  2046   2045       ** to be called.
  2047   2046       */ 
  2048   2047       if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
  2049         -      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
         2048  +      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
  2050   2049         int j;
  2051   2050         for(j=0; j<3; j++){
  2052   2051           CollSeq *p = &aColl[j];
  2053   2052           if( p->enc==pColl->enc ){
  2054   2053             if( p->xDel ){
  2055   2054               p->xDel(p->pUser);
  2056   2055             }
................................................................................
  2062   2061   
  2063   2062     pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
  2064   2063     if( pColl==0 ) return SQLITE_NOMEM;
  2065   2064     pColl->xCmp = xCompare;
  2066   2065     pColl->pUser = pCtx;
  2067   2066     pColl->xDel = xDel;
  2068   2067     pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
  2069         -  sqlite3Error(db, SQLITE_OK, 0);
         2068  +  sqlite3Error(db, SQLITE_OK);
  2070   2069     return SQLITE_OK;
  2071   2070   }
  2072   2071   
  2073   2072   
  2074   2073   /*
  2075   2074   ** This array defines hard upper bounds on limit values.  The
  2076   2075   ** initializer must be kept in sync with the SQLITE_LIMIT_*
................................................................................
  2548   2547     createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
  2549   2548   
  2550   2549     /* Parse the filename/URI argument. */
  2551   2550     db->openFlags = flags;
  2552   2551     rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
  2553   2552     if( rc!=SQLITE_OK ){
  2554   2553       if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
  2555         -    sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
         2554  +    sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
  2556   2555       sqlite3_free(zErrMsg);
  2557   2556       goto opendb_out;
  2558   2557     }
  2559   2558   
  2560   2559     /* Open the backend database driver */
  2561   2560     rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
  2562   2561                           flags | SQLITE_OPEN_MAIN_DB);
  2563   2562     if( rc!=SQLITE_OK ){
  2564   2563       if( rc==SQLITE_IOERR_NOMEM ){
  2565   2564         rc = SQLITE_NOMEM;
  2566   2565       }
  2567         -    sqlite3Error(db, rc, 0);
         2566  +    sqlite3Error(db, rc);
  2568   2567       goto opendb_out;
  2569   2568     }
  2570   2569     db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
  2571   2570     db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
  2572   2571   
  2573   2572   
  2574   2573     /* The default safety_level for the main database is 'full'; for the temp
................................................................................
  2584   2583       goto opendb_out;
  2585   2584     }
  2586   2585   
  2587   2586     /* Register all built-in functions, but do not attempt to read the
  2588   2587     ** database schema yet. This is delayed until the first time the database
  2589   2588     ** is accessed.
  2590   2589     */
  2591         -  sqlite3Error(db, SQLITE_OK, 0);
         2590  +  sqlite3Error(db, SQLITE_OK);
  2592   2591     sqlite3RegisterBuiltinFunctions(db);
  2593   2592   
  2594   2593     /* Load automatic extensions - extensions that have been registered
  2595   2594     ** using the sqlite3_automatic_extension() API.
  2596   2595     */
  2597   2596     rc = sqlite3_errcode(db);
  2598   2597     if( rc==SQLITE_OK ){
................................................................................
  2641   2640     */
  2642   2641   #ifdef SQLITE_DEFAULT_LOCKING_MODE
  2643   2642     db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
  2644   2643     sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
  2645   2644                             SQLITE_DEFAULT_LOCKING_MODE);
  2646   2645   #endif
  2647   2646   
  2648         -  if( rc ) sqlite3Error(db, rc, 0);
         2647  +  if( rc ) sqlite3Error(db, rc);
  2649   2648   
  2650   2649     /* Enable the lookaside-malloc subsystem */
  2651   2650     setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
  2652   2651                           sqlite3GlobalConfig.nLookaside);
  2653   2652   
  2654   2653     sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
  2655   2654   
................................................................................
  3003   3002   
  3004   3003     if( SQLITE_OK==rc && !pTab ){
  3005   3004       sqlite3DbFree(db, zErrMsg);
  3006   3005       zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
  3007   3006           zColumnName);
  3008   3007       rc = SQLITE_ERROR;
  3009   3008     }
  3010         -  sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
         3009  +  sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
  3011   3010     sqlite3DbFree(db, zErrMsg);
  3012   3011     rc = sqlite3ApiExit(db, rc);
  3013   3012     sqlite3_mutex_leave(db->mutex);
  3014   3013     return rc;
  3015   3014   }
  3016   3015   #endif
  3017   3016   

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_w32.c.

    95     95   static int winMutex_isInit = 0;
    96     96   static int winMutex_isNt = -1; /* <0 means "need to query" */
    97     97   
    98     98   /* As the winMutexInit() and winMutexEnd() functions are called as part
    99     99   ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
   100    100   ** "interlocked" magic used here is probably not strictly necessary.
   101    101   */
   102         -static LONG volatile winMutex_lock = 0;
          102  +static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
   103    103   
   104    104   int sqlite3_win32_is_nt(void); /* os_win.c */
   105    105   void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
   106    106   
   107    107   static int winMutexInit(void){
   108    108     /* The first to increment to 1 does actual initialization */
   109    109     if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){

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_win.c.

   406    406   ** 1:   Operating system is Win9x.
   407    407   ** 2:   Operating system is WinNT.
   408    408   **
   409    409   ** In order to facilitate testing on a WinNT system, the test fixture
   410    410   ** can manually set this value to 1 to emulate Win98 behavior.
   411    411   */
   412    412   #ifdef SQLITE_TEST
   413         -LONG volatile sqlite3_os_type = 0;
          413  +LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
   414    414   #else
   415         -static LONG volatile sqlite3_os_type = 0;
          415  +static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
   416    416   #endif
   417    417   
   418    418   #ifndef SYSCALL
   419    419   #  define SYSCALL sqlite3_syscall_ptr
   420    420   #endif
   421    421   
   422    422   /*
................................................................................
  1047   1047   #if defined(InterlockedCompareExchange)
  1048   1048     { "InterlockedCompareExchange", (SYSCALL)0,                    0 },
  1049   1049   
  1050   1050   #define osInterlockedCompareExchange InterlockedCompareExchange
  1051   1051   #else
  1052   1052     { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
  1053   1053   
  1054         -#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG volatile*, \
  1055         -        LONG,LONG))aSyscall[76].pCurrent)
         1054  +#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
         1055  +        SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
  1056   1056   #endif /* defined(InterlockedCompareExchange) */
  1057   1057   
  1058   1058   }; /* End of the overrideable system calls */
  1059   1059   
  1060   1060   /*
  1061   1061   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
  1062   1062   ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the

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     ));
................................................................................
  5430   5415   ** has ever happened.
  5431   5416   */
  5432   5417   DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
  5433   5418     PgHdr *pPg = 0;
  5434   5419     assert( pPager!=0 );
  5435   5420     assert( pgno!=0 );
  5436   5421     assert( pPager->pPCache!=0 );
  5437         -  assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
  5438   5422     sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
  5439   5423     return pPg;
  5440   5424   }
  5441   5425   
  5442   5426   /*
  5443   5427   ** Release a page reference.
  5444   5428   **
................................................................................
  5767   5751     /* Update the database size and return.
  5768   5752     */
  5769   5753     if( pPager->dbSize<pPg->pgno ){
  5770   5754       pPager->dbSize = pPg->pgno;
  5771   5755     }
  5772   5756     return rc;
  5773   5757   }
         5758  +
         5759  +/*
         5760  +** This is a variant of sqlite3PagerWrite() that runs when the sector size
         5761  +** is larger than the page size.  SQLite makes the (reasonable) assumption that
         5762  +** all bytes of a sector are written together by hardware.  Hence, all bytes of
         5763  +** a sector need to be journalled in case of a power loss in the middle of
         5764  +** a write.
         5765  +**
         5766  +** Usually, the sector size is less than or equal to the page size, in which
         5767  +** case pages can be individually written.  This routine only runs in the exceptional
         5768  +** case where the page size is smaller than the sector size.
         5769  +*/
         5770  +static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
         5771  +  int rc = SQLITE_OK;            /* Return code */
         5772  +  Pgno nPageCount;               /* Total number of pages in database file */
         5773  +  Pgno pg1;                      /* First page of the sector pPg is located on. */
         5774  +  int nPage = 0;                 /* Number of pages starting at pg1 to journal */
         5775  +  int ii;                        /* Loop counter */
         5776  +  int needSync = 0;              /* True if any page has PGHDR_NEED_SYNC */
         5777  +  Pager *pPager = pPg->pPager;   /* The pager that owns pPg */
         5778  +  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
         5779  +
         5780  +  /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
         5781  +  ** a journal header to be written between the pages journaled by
         5782  +  ** this function.
         5783  +  */
         5784  +  assert( !MEMDB );
         5785  +  assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
         5786  +  pPager->doNotSpill |= SPILLFLAG_NOSYNC;
         5787  +
         5788  +  /* This trick assumes that both the page-size and sector-size are
         5789  +  ** an integer power of 2. It sets variable pg1 to the identifier
         5790  +  ** of the first page of the sector pPg is located on.
         5791  +  */
         5792  +  pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
         5793  +
         5794  +  nPageCount = pPager->dbSize;
         5795  +  if( pPg->pgno>nPageCount ){
         5796  +    nPage = (pPg->pgno - pg1)+1;
         5797  +  }else if( (pg1+nPagePerSector-1)>nPageCount ){
         5798  +    nPage = nPageCount+1-pg1;
         5799  +  }else{
         5800  +    nPage = nPagePerSector;
         5801  +  }
         5802  +  assert(nPage>0);
         5803  +  assert(pg1<=pPg->pgno);
         5804  +  assert((pg1+nPage)>pPg->pgno);
         5805  +
         5806  +  for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
         5807  +    Pgno pg = pg1+ii;
         5808  +    PgHdr *pPage;
         5809  +    if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
         5810  +      if( pg!=PAGER_MJ_PGNO(pPager) ){
         5811  +        rc = sqlite3PagerGet(pPager, pg, &pPage);
         5812  +        if( rc==SQLITE_OK ){
         5813  +          rc = pager_write(pPage);
         5814  +          if( pPage->flags&PGHDR_NEED_SYNC ){
         5815  +            needSync = 1;
         5816  +          }
         5817  +          sqlite3PagerUnrefNotNull(pPage);
         5818  +        }
         5819  +      }
         5820  +    }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
         5821  +      if( pPage->flags&PGHDR_NEED_SYNC ){
         5822  +        needSync = 1;
         5823  +      }
         5824  +      sqlite3PagerUnrefNotNull(pPage);
         5825  +    }
         5826  +  }
         5827  +
         5828  +  /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
         5829  +  ** starting at pg1, then it needs to be set for all of them. Because
         5830  +  ** writing to any of these nPage pages may damage the others, the
         5831  +  ** journal file must contain sync()ed copies of all of them
         5832  +  ** before any of them can be written out to the database file.
         5833  +  */
         5834  +  if( rc==SQLITE_OK && needSync ){
         5835  +    assert( !MEMDB );
         5836  +    for(ii=0; ii<nPage; ii++){
         5837  +      PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
         5838  +      if( pPage ){
         5839  +        pPage->flags |= PGHDR_NEED_SYNC;
         5840  +        sqlite3PagerUnrefNotNull(pPage);
         5841  +      }
         5842  +    }
         5843  +  }
         5844  +
         5845  +  assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
         5846  +  pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
         5847  +  return rc;
         5848  +}
  5774   5849   
  5775   5850   /*
  5776   5851   ** Mark a data page as writeable. This routine must be called before 
  5777   5852   ** making changes to a page. The caller must check the return value 
  5778   5853   ** of this function and be careful not to change any page data unless 
  5779   5854   ** this routine returns SQLITE_OK.
  5780   5855   **
................................................................................
  5782   5857   ** function also deals with the special case where 2 or more pages
  5783   5858   ** fit on a single disk sector. In this case all co-resident pages
  5784   5859   ** must have been written to the journal file before returning.
  5785   5860   **
  5786   5861   ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
  5787   5862   ** as appropriate. Otherwise, SQLITE_OK.
  5788   5863   */
  5789         -int sqlite3PagerWrite(DbPage *pDbPage){
  5790         -  int rc = SQLITE_OK;
  5791         -
  5792         -  PgHdr *pPg = pDbPage;
  5793         -  Pager *pPager = pPg->pPager;
  5794         -
         5864  +int sqlite3PagerWrite(PgHdr *pPg){
  5795   5865     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;
         5866  +  assert( pPg->pPager->eState>=PAGER_WRITER_LOCKED );
         5867  +  assert( pPg->pPager->eState!=PAGER_ERROR );
         5868  +  assert( assert_pager_state(pPg->pPager) );
         5869  +  if( pPg->pPager->sectorSize > (u32)pPg->pPager->pageSize ){
         5870  +    return pagerWriteLargeSector(pPg);
  5875   5871     }else{
  5876         -    rc = pager_write(pDbPage);
         5872  +    return pager_write(pPg);
  5877   5873     }
  5878         -  return rc;
  5879   5874   }
  5880   5875   
  5881   5876   /*
  5882   5877   ** Return TRUE if the page given in the argument was previously passed
  5883   5878   ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
  5884   5879   ** to change the content of the page.
  5885   5880   */
................................................................................
  6767   6762   
  6768   6763     /* If the cache contains a page with page-number pgno, remove it
  6769   6764     ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
  6770   6765     ** page pgno before the 'move' operation, it needs to be retained 
  6771   6766     ** for the page moved there.
  6772   6767     */
  6773   6768     pPg->flags &= ~PGHDR_NEED_SYNC;
  6774         -  pPgOld = pager_lookup(pPager, pgno);
         6769  +  pPgOld = sqlite3PagerLookup(pPager, pgno);
  6775   6770     assert( !pPgOld || pPgOld->nRef==1 );
  6776   6771     if( pPgOld ){
  6777   6772       pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
  6778   6773       if( MEMDB ){
  6779   6774         /* Do not discard pages from an in-memory database since we might
  6780   6775         ** need to rollback later.  Just move the page out of the way. */
  6781   6776         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         -    }
    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;
    97         -    }
    98         -  }
    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) );
           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;
          126  +    }
          127  +    if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
          128  +      p->pSynced = pPage;
          129  +    }
          130  +    expensive_assert( pcacheCheckSynced(p) );
          131  +  }
   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);
   143    144     }
   144    145   }
   145    146   
   146    147   /*************************************************** General Interfaces ******
   147    148   **
   148    149   ** Initialize and shutdown the page cache subsystem. Neither of these 
   149    150   ** functions are threadsafe.
................................................................................
   328    329     return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
   329    330   }
   330    331   
   331    332   /*
   332    333   ** Decrement the reference count on a page. If the page is clean and the
   333    334   ** reference count drops to 0, then it is made elible for recycling.
   334    335   */
   335         -void sqlite3PcacheRelease(PgHdr *p){
          336  +void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
   336    337     assert( p->nRef>0 );
   337    338     p->nRef--;
   338    339     if( p->nRef==0 ){
   339         -    PCache *pCache = p->pCache;
   340         -    pCache->nRef--;
          340  +    p->pCache->nRef--;
   341    341       if( (p->flags&PGHDR_DIRTY)==0 ){
   342    342         pcacheUnpin(p);
   343    343       }else{
   344    344         /* Move the page to the head of the dirty list. */
   345         -      pcacheRemoveFromDirtyList(p);
   346         -      pcacheAddToDirtyList(p);
          345  +      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
   347    346       }
   348    347     }
   349    348   }
   350    349   
   351    350   /*
   352    351   ** Increase the reference count of a supplied page by 1.
   353    352   */
................................................................................
   358    357   
   359    358   /*
   360    359   ** Drop a page from the cache. There must be exactly one reference to the
   361    360   ** page. This function deletes that reference, so after it returns the
   362    361   ** page pointed to by p is invalid.
   363    362   */
   364    363   void sqlite3PcacheDrop(PgHdr *p){
   365         -  PCache *pCache;
   366    364     assert( p->nRef==1 );
   367    365     if( p->flags&PGHDR_DIRTY ){
   368         -    pcacheRemoveFromDirtyList(p);
          366  +    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   369    367     }
   370         -  pCache = p->pCache;
   371         -  pCache->nRef--;
          368  +  p->pCache->nRef--;
   372    369     if( p->pgno==1 ){
   373         -    pCache->pPage1 = 0;
          370  +    p->pCache->pPage1 = 0;
   374    371     }
   375         -  sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
          372  +  sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
   376    373   }
   377    374   
   378    375   /*
   379    376   ** Make sure the page is marked as dirty. If it isn't dirty already,
   380    377   ** make it so.
   381    378   */
   382    379   void sqlite3PcacheMakeDirty(PgHdr *p){
   383    380     p->flags &= ~PGHDR_DONT_WRITE;
   384    381     assert( p->nRef>0 );
   385    382     if( 0==(p->flags & PGHDR_DIRTY) ){
   386    383       p->flags |= PGHDR_DIRTY;
   387         -    pcacheAddToDirtyList( p);
          384  +    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
   388    385     }
   389    386   }
   390    387   
   391    388   /*
   392    389   ** Make sure the page is marked as clean. If it isn't clean already,
   393    390   ** make it so.
   394    391   */
   395    392   void sqlite3PcacheMakeClean(PgHdr *p){
   396    393     if( (p->flags & PGHDR_DIRTY) ){
   397         -    pcacheRemoveFromDirtyList(p);
          394  +    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   398    395       p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
   399    396       if( p->nRef==0 ){
   400    397         pcacheUnpin(p);
   401    398       }
   402    399     }
   403    400   }
   404    401   
................................................................................
   429    426   void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
   430    427     PCache *pCache = p->pCache;
   431    428     assert( p->nRef>0 );
   432    429     assert( newPgno>0 );
   433    430     sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
   434    431     p->pgno = newPgno;
   435    432     if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
   436         -    pcacheRemoveFromDirtyList(p);
   437         -    pcacheAddToDirtyList(p);
          433  +    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
   438    434     }
   439    435   }
   440    436   
   441    437   /*
   442    438   ** Drop every cache entry whose page number is greater than "pgno". The
   443    439   ** caller must ensure that there are no outstanding references to any pages
   444    440   ** other than page 1 with a page number greater than pgno.

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/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/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 */
................................................................................
   460    465     char *zDestTable;      /* Name of destination table when MODE_Insert */
   461    466     char separator[20];    /* Separator character for MODE_List */
   462    467     char newline[20];      /* Record separator in MODE_Csv */
   463    468     int colWidth[100];     /* Requested width of each column when in column mode*/
   464    469     int actualWidth[100];  /* Actual width of each column */
   465    470     char nullvalue[20];    /* The text to print when a NULL comes back from
   466    471                            ** the database */
   467         -  struct previous_mode_data explainPrev;
   468         -                         /* Holds the mode information just before
   469         -                         ** .explain ON */
          472  +  SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
   470    473     char outfile[FILENAME_MAX]; /* Filename for *out */
   471    474     const char *zDbFilename;    /* name of the database file */
   472    475     char *zFreeOnClose;         /* Filename to free when closing */
   473    476     const char *zVfs;           /* Name of VFS to use */
   474    477     sqlite3_stmt *pStmt;   /* Current statement if any. */
   475    478     FILE *pLog;            /* Write log output here */
   476    479     int *aiIndent;         /* Array of indents used in MODE_Explain */
................................................................................
   518    521     return 0x3fffffff & (int)(z2 - z);
   519    522   }
   520    523   
   521    524   /*
   522    525   ** A callback for the sqlite3_log() interface.
   523    526   */
   524    527   static void shellLog(void *pArg, int iErrCode, const char *zMsg){
   525         -  struct callback_data *p = (struct callback_data*)pArg;
          528  +  ShellState *p = (ShellState*)pArg;
   526    529     if( p->pLog==0 ) return;
   527    530     fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
   528    531     fflush(p->pLog);
   529    532   }
   530    533   
   531    534   /*
   532    535   ** Output the given string as a hex-encoded blob (eg. X'1234' )
................................................................................
   660    663   
   661    664   /*
   662    665   ** Output a single term of CSV.  Actually, p->separator is used for
   663    666   ** the separator, which may or may not be a comma.  p->nullvalue is
   664    667   ** the null value.  Strings are quoted if necessary.  The separator
   665    668   ** is only issued if bSep is true.
   666    669   */
   667         -static void output_csv(struct callback_data *p, const char *z, int bSep){
          670  +static void output_csv(ShellState *p, const char *z, int bSep){
   668    671     FILE *out = p->out;
   669    672     if( z==0 ){
   670    673       fprintf(out,"%s",p->nullvalue);
   671    674     }else{
   672    675       int i;
   673    676       int nSep = strlen30(p->separator);
   674    677       for(i=0; z[i]; i++){
................................................................................
   709    712   
   710    713   /*
   711    714   ** This is the callback routine that the shell
   712    715   ** invokes for each row of a query result.
   713    716   */
   714    717   static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
   715    718     int i;
   716         -  struct callback_data *p = (struct callback_data*)pArg;
          719  +  ShellState *p = (ShellState*)pArg;
   717    720   
   718    721     switch( p->mode ){
   719    722       case MODE_Line: {
   720    723         int w = 5;
   721    724         if( azArg==0 ) break;
   722    725         for(i=0; i<nArg; i++){
   723    726           int len = strlen30(azCol[i] ? azCol[i] : "");
................................................................................
   919    922   */
   920    923   static int callback(void *pArg, int nArg, char **azArg, char **azCol){
   921    924     /* since we don't have type info, call the shell_callback with a NULL value */
   922    925     return shell_callback(pArg, nArg, azArg, azCol, NULL);
   923    926   }
   924    927   
   925    928   /*
   926         -** Set the destination table field of the callback_data structure to
          929  +** Set the destination table field of the ShellState structure to
   927    930   ** the name of the table given.  Escape any quote characters in the
   928    931   ** table name.
   929    932   */
   930         -static void set_table_name(struct callback_data *p, const char *zName){
          933  +static void set_table_name(ShellState *p, const char *zName){
   931    934     int i, n;
   932    935     int needQuote;
   933    936     char *z;
   934    937   
   935    938     if( p->zDestTable ){
   936    939       free(p->zDestTable);
   937    940       p->zDestTable = 0;
................................................................................
  1013   1016   **
  1014   1017   ** If the number of columns is 1 and that column contains text "--"
  1015   1018   ** then write the semicolon on a separate line.  That way, if a 
  1016   1019   ** "--" comment occurs at the end of the statement, the comment
  1017   1020   ** won't consume the semicolon terminator.
  1018   1021   */
  1019   1022   static int run_table_dump_query(
  1020         -  struct callback_data *p, /* Query context */
         1023  +  ShellState *p,           /* Query context */
  1021   1024     const char *zSelect,     /* SELECT statement to extract content */
  1022   1025     const char *zFirstRow    /* Print before first row, if not NULL */
  1023   1026   ){
  1024   1027     sqlite3_stmt *pSelect;
  1025   1028     int rc;
  1026   1029     int nResult;
  1027   1030     int i;
................................................................................
  1076   1079   }
  1077   1080   
  1078   1081   /*
  1079   1082   ** Display memory stats.
  1080   1083   */
  1081   1084   static int display_stats(
  1082   1085     sqlite3 *db,                /* Database to query */
  1083         -  struct callback_data *pArg, /* Pointer to struct callback_data */
         1086  +  ShellState *pArg,           /* Pointer to ShellState */
  1084   1087     int bReset                  /* True to reset the stats */
  1085   1088   ){
  1086   1089     int iCur;
  1087   1090     int iHiwtr;
  1088   1091   
  1089   1092     if( pArg && pArg->out ){
  1090   1093       
................................................................................
  1183   1186       if( 0==strcmp(zStr, azArray[i]) ) return 1;
  1184   1187     }
  1185   1188     return 0;
  1186   1189   }
  1187   1190   
  1188   1191   /*
  1189   1192   ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
  1190         -** and populate the callback_data.aiIndent[] array with the number of
         1193  +** and populate the ShellState.aiIndent[] array with the number of
  1191   1194   ** spaces each opcode should be indented before it is output. 
  1192   1195   **
  1193   1196   ** The indenting rules are:
  1194   1197   **
  1195   1198   **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
  1196   1199   **       all opcodes that occur between the p2 jump destination and the opcode
  1197   1200   **       itself by 2 spaces.
................................................................................
  1199   1202   **     * For each "Goto", if the jump destination is earlier in the program
  1200   1203   **       and ends on one of:
  1201   1204   **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
  1202   1205   **       or if the P1 parameter is one instead of zero,
  1203   1206   **       then indent all opcodes between the earlier instruction
  1204   1207   **       and "Goto" by 2 spaces.
  1205   1208   */
  1206         -static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
         1209  +static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
  1207   1210     const char *zSql;               /* The text of the SQL statement */
  1208   1211     const char *z;                  /* Used to check if this is an EXPLAIN */
  1209   1212     int *abYield = 0;               /* True if op is an OP_Yield */
  1210   1213     int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
  1211   1214     int iOp;                        /* Index of operation in p->aiIndent[] */
  1212   1215   
  1213   1216     const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
................................................................................
  1259   1262     sqlite3_free(abYield);
  1260   1263     sqlite3_reset(pSql);
  1261   1264   }
  1262   1265   
  1263   1266   /*
  1264   1267   ** Free the array allocated by explain_data_prepare().
  1265   1268   */
  1266         -static void explain_data_delete(struct callback_data *p){
         1269  +static void explain_data_delete(ShellState *p){
  1267   1270     sqlite3_free(p->aiIndent);
  1268   1271     p->aiIndent = 0;
  1269   1272     p->nIndent = 0;
  1270   1273     p->iIndent = 0;
  1271   1274   }
  1272   1275   
  1273   1276   /*
................................................................................
  1276   1279   ** set via the supplied callback.
  1277   1280   **
  1278   1281   ** This is very similar to SQLite's built-in sqlite3_exec() 
  1279   1282   ** function except it takes a slightly different callback 
  1280   1283   ** and callback data argument.
  1281   1284   */
  1282   1285   static int shell_exec(
  1283         -  sqlite3 *db,                                /* An open database */
  1284         -  const char *zSql,                           /* SQL to be evaluated */
         1286  +  sqlite3 *db,                              /* An open database */
         1287  +  const char *zSql,                         /* SQL to be evaluated */
  1285   1288     int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
  1286         -                                              /* (not the same as sqlite3_exec) */
  1287         -  struct callback_data *pArg,                 /* Pointer to struct callback_data */
  1288         -  char **pzErrMsg                             /* Error msg written here */
         1289  +                                            /* (not the same as sqlite3_exec) */
         1290  +  ShellState *pArg,                         /* Pointer to ShellState */
         1291  +  char **pzErrMsg                           /* Error msg written here */
  1289   1292   ){
  1290   1293     sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
  1291   1294     int rc = SQLITE_OK;             /* Return Code */
  1292   1295     int rc2;
  1293   1296     const char *zLeftover;          /* Tail of unprocessed SQL */
  1294   1297   
  1295   1298     if( pzErrMsg ){
................................................................................
  1449   1452   */
  1450   1453   static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
  1451   1454     int rc;
  1452   1455     const char *zTable;
  1453   1456     const char *zType;
  1454   1457     const char *zSql;
  1455   1458     const char *zPrepStmt = 0;
  1456         -  struct callback_data *p = (struct callback_data *)pArg;
         1459  +  ShellState *p = (ShellState *)pArg;
  1457   1460   
  1458   1461     UNUSED_PARAMETER(azCol);
  1459   1462     if( nArg!=3 ) return 1;
  1460   1463     zTable = azArg[0];
  1461   1464     zType = azArg[1];
  1462   1465     zSql = azArg[2];
  1463   1466     
................................................................................
  1545   1548   ** Run zQuery.  Use dump_callback() as the callback routine so that
  1546   1549   ** the contents of the query are output as SQL statements.
  1547   1550   **
  1548   1551   ** If we get a SQLITE_CORRUPT error, rerun the query after appending
  1549   1552   ** "ORDER BY rowid DESC" to the end.
  1550   1553   */
  1551   1554   static int run_schema_dump_query(
  1552         -  struct callback_data *p, 
         1555  +  ShellState *p, 
  1553   1556     const char *zQuery
  1554   1557   ){
  1555   1558     int rc;
  1556   1559     char *zErr = 0;
  1557   1560     rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
  1558   1561     if( rc==SQLITE_CORRUPT ){
  1559   1562       char *zQ2;
................................................................................
  1645   1648     ".trace FILE|off        Output each SQL statement as it is run\n"
  1646   1649     ".vfsname ?AUX?         Print the name of the VFS stack\n"
  1647   1650     ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
  1648   1651     "                         Negative values right-justify\n"
  1649   1652   ;
  1650   1653   
  1651   1654   /* Forward reference */
  1652         -static int process_input(struct callback_data *p, FILE *in);
         1655  +static int process_input(ShellState *p, FILE *in);
  1653   1656   /*
  1654   1657   ** Implementation of the "readfile(X)" SQL function.  The entire content
  1655   1658   ** of the file named X is read and returned as a BLOB.  NULL is returned
  1656   1659   ** if the file does not exist or is unreadable.
  1657   1660   */
  1658   1661   static void readfileFunc(
  1659   1662     sqlite3_context *context,
................................................................................
  1711   1714     sqlite3_result_int64(context, rc);
  1712   1715   }
  1713   1716   
  1714   1717   /*
  1715   1718   ** Make sure the database is open.  If it is not, then open it.  If
  1716   1719   ** the database fails to open, print an error message and exit.
  1717   1720   */
  1718         -static void open_db(struct callback_data *p, int keepAlive){
         1721  +static void open_db(ShellState *p, int keepAlive){
  1719   1722     if( p->db==0 ){
  1720   1723       sqlite3_initialize();
  1721   1724       sqlite3_open(p->zDbFilename, &p->db);
  1722   1725       db = p->db;
  1723   1726       if( db && sqlite3_errcode(db)==SQLITE_OK ){
  1724   1727         sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
  1725   1728             shellstaticFunc, 0, 0);
................................................................................
  1892   1895   }
  1893   1896   
  1894   1897   /*
  1895   1898   ** A routine for handling output from sqlite3_trace().
  1896   1899   */
  1897   1900   static void sql_trace_callback(void *pArg, const char *z){
  1898   1901     FILE *f = (FILE*)pArg;
  1899         -  if( f ) fprintf(f, "%s\n", z);
         1902  +  if( f ){
         1903  +    int i = (int)strlen(z);
         1904  +    while( i>0 && z[i-1]==';' ){ i--; }
         1905  +    fprintf(f, "%.*s;\n", i, z);
         1906  +  }
  1900   1907   }
  1901   1908   
  1902   1909   /*
  1903   1910   ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
  1904   1911   ** a useful spot to set a debugger breakpoint.
  1905   1912   */
  1906   1913   static void test_breakpoint(void){
................................................................................
  2010   2017   
  2011   2018   /*
  2012   2019   ** Try to transfer data for table zTable.  If an error is seen while
  2013   2020   ** moving forward, try to go backwards.  The backwards movement won't
  2014   2021   ** work for WITHOUT ROWID tables.
  2015   2022   */
  2016   2023   static void tryToCloneData(
  2017         -  struct callback_data *p,
         2024  +  ShellState *p,
  2018   2025     sqlite3 *newDb,
  2019   2026     const char *zTable
  2020   2027   ){
  2021   2028     sqlite3_stmt *pQuery = 0; 
  2022   2029     sqlite3_stmt *pInsert = 0;
  2023   2030     char *zQuery = 0;
  2024   2031     char *zInsert = 0;
................................................................................
  2123   2130   /*
  2124   2131   ** Try to transfer all rows of the schema that match zWhere.  For
  2125   2132   ** each row, invoke xForEach() on the object defined by that row.
  2126   2133   ** If an error is encountered while moving forward through the
  2127   2134   ** sqlite_master table, try again moving backwards.
  2128   2135   */
  2129   2136   static void tryToCloneSchema(
  2130         -  struct callback_data *p,
         2137  +  ShellState *p,
  2131   2138     sqlite3 *newDb,
  2132   2139     const char *zWhere,
  2133         -  void (*xForEach)(struct callback_data*,sqlite3*,const char*)
         2140  +  void (*xForEach)(ShellState*,sqlite3*,const char*)
  2134   2141   ){
  2135   2142     sqlite3_stmt *pQuery = 0;
  2136   2143     char *zQuery = 0;
  2137   2144     int rc;
  2138   2145     const unsigned char *zName;
  2139   2146     const unsigned char *zSql;
  2140   2147     char *zErrMsg = 0;
................................................................................
  2197   2204   }
  2198   2205   
  2199   2206   /*
  2200   2207   ** Open a new database file named "zNewDb".  Try to recover as much information
  2201   2208   ** as possible out of the main database (which might be corrupt) and write it
  2202   2209   ** into zNewDb.
  2203   2210   */
  2204         -static void tryToClone(struct callback_data *p, const char *zNewDb){
         2211  +static void tryToClone(ShellState *p, const char *zNewDb){
  2205   2212     int rc;
  2206   2213     sqlite3 *newDb = 0;
  2207   2214     if( access(zNewDb,0)==0 ){
  2208   2215       fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
  2209   2216       return;
  2210   2217     }
  2211   2218     rc = sqlite3_open(zNewDb, &newDb);
................................................................................
  2222   2229     }
  2223   2230     sqlite3_close(newDb);
  2224   2231   }
  2225   2232   
  2226   2233   /*
  2227   2234   ** Change the output file back to stdout
  2228   2235   */
  2229         -static void output_reset(struct callback_data *p){
         2236  +static void output_reset(ShellState *p){
  2230   2237     if( p->outfile[0]=='|' ){
  2231   2238       pclose(p->out);
  2232   2239     }else{
  2233   2240       output_file_close(p->out);
  2234   2241     }
  2235   2242     p->outfile[0] = 0;
  2236   2243     p->out = stdout;
................................................................................
  2238   2245   
  2239   2246   /*
  2240   2247   ** If an input line begins with "." then invoke this routine to
  2241   2248   ** process that line.
  2242   2249   **
  2243   2250   ** Return 1 on error, 2 to exit, and 0 otherwise.
  2244   2251   */
  2245         -static int do_meta_command(char *zLine, struct callback_data *p){
         2252  +static int do_meta_command(char *zLine, ShellState *p){
  2246   2253     int i = 1;
  2247   2254     int nArg = 0;
  2248   2255     int n, c;
  2249   2256     int rc = 0;
  2250   2257     char *azArg[50];
  2251   2258   
  2252   2259     /* Parse the input line into tokens.
................................................................................
  2356   2363       }else{
  2357   2364         fprintf(stderr, "Usage: .clone FILENAME\n");
  2358   2365         rc = 1;
  2359   2366       }
  2360   2367     }else
  2361   2368   
  2362   2369     if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
  2363         -    struct callback_data data;
         2370  +    ShellState data;
  2364   2371       char *zErrMsg = 0;
  2365   2372       open_db(p, 0);
  2366   2373       memcpy(&data, p, sizeof(data));
  2367   2374       data.showHeader = 1;
  2368   2375       data.mode = MODE_Column;
  2369   2376       data.colWidth[0] = 3;
  2370   2377       data.colWidth[1] = 15;
................................................................................
  2454   2461       if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
  2455   2462       rc = 2;
  2456   2463     }else
  2457   2464   
  2458   2465     if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
  2459   2466       int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
  2460   2467       if(val == 1) {
  2461         -      if(!p->explainPrev.valid) {
  2462         -        p->explainPrev.valid = 1;
  2463         -        p->explainPrev.mode = p->mode;
  2464         -        p->explainPrev.showHeader = p->showHeader;
  2465         -        memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
         2468  +      if(!p->normalMode.valid) {
         2469  +        p->normalMode.valid = 1;
         2470  +        p->normalMode.mode = p->mode;
         2471  +        p->normalMode.showHeader = p->showHeader;
         2472  +        memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
  2466   2473         }
  2467   2474         /* We could put this code under the !p->explainValid
  2468   2475         ** condition so that it does not execute if we are already in
  2469   2476         ** explain mode. However, always executing it allows us an easy
  2470   2477         ** was to reset to explain mode in case the user previously
  2471   2478         ** did an .explain followed by a .width, .mode or .header
  2472   2479         ** command.
................................................................................
  2478   2485         p->colWidth[1] = 13;                 /* opcode */
  2479   2486         p->colWidth[2] = 4;                  /* P1 */
  2480   2487         p->colWidth[3] = 4;                  /* P2 */
  2481   2488         p->colWidth[4] = 4;                  /* P3 */
  2482   2489         p->colWidth[5] = 13;                 /* P4 */
  2483   2490         p->colWidth[6] = 2;                  /* P5 */
  2484   2491         p->colWidth[7] = 13;                  /* Comment */
  2485         -    }else if (p->explainPrev.valid) {
  2486         -      p->explainPrev.valid = 0;
  2487         -      p->mode = p->explainPrev.mode;
  2488         -      p->showHeader = p->explainPrev.showHeader;
  2489         -      memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
         2492  +    }else if (p->normalMode.valid) {
         2493  +      p->normalMode.valid = 0;
         2494  +      p->mode = p->normalMode.mode;
         2495  +      p->showHeader = p->normalMode.showHeader;
         2496  +      memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
  2490   2497       }
  2491   2498     }else
  2492   2499   
  2493   2500     if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
  2494         -    struct callback_data data;
         2501  +    ShellState data;
  2495   2502       char *zErrMsg = 0;
  2496   2503       int doStats = 0;
  2497   2504       if( nArg!=1 ){
  2498   2505         fprintf(stderr, "Usage: .fullschema\n");
  2499   2506         rc = 1;
  2500   2507         goto meta_command_exit;
  2501   2508       }
................................................................................
  2504   2511       data.showHeader = 0;
  2505   2512       data.mode = MODE_Semi;
  2506   2513       rc = sqlite3_exec(p->db,
  2507   2514          "SELECT sql FROM"
  2508   2515          "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
  2509   2516          "     FROM sqlite_master UNION ALL"
  2510   2517          "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
  2511         -       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
         2518  +       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
  2512   2519          "ORDER BY rowid",
  2513   2520          callback, &data, &zErrMsg
  2514   2521       );
  2515   2522       if( rc==SQLITE_OK ){
  2516   2523         sqlite3_stmt *pStmt;
  2517   2524         rc = sqlite3_prepare_v2(p->db,
  2518   2525                  "SELECT rowid FROM sqlite_master"
................................................................................
  2708   2715       xCloser(sCsv.in);
  2709   2716       sqlite3_free(sCsv.z);
  2710   2717       sqlite3_finalize(pStmt);
  2711   2718       if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
  2712   2719     }else
  2713   2720   
  2714   2721     if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
  2715         -    struct callback_data data;
         2722  +    ShellState data;
  2716   2723       char *zErrMsg = 0;
  2717   2724       open_db(p, 0);
  2718   2725       memcpy(&data, p, sizeof(data));
  2719   2726       data.showHeader = 0;
  2720   2727       data.mode = MODE_List;
  2721   2728       if( nArg==1 ){
  2722   2729         rc = sqlite3_exec(p->db,
................................................................................
  3002   3009         fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
  3003   3010         rc = 1;
  3004   3011       }
  3005   3012       sqlite3_close(pSrc);
  3006   3013     }else
  3007   3014   
  3008   3015     if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
  3009         -    struct callback_data data;
         3016  +    ShellState data;
  3010   3017       char *zErrMsg = 0;
  3011   3018       open_db(p, 0);
  3012   3019       memcpy(&data, p, sizeof(data));
  3013   3020       data.showHeader = 0;
  3014   3021       data.mode = MODE_Semi;
  3015   3022       if( nArg==2 ){
  3016   3023         int i;
................................................................................
  3058   3065         }
  3059   3066       }else if( nArg==1 ){
  3060   3067         rc = sqlite3_exec(p->db,
  3061   3068            "SELECT sql FROM "
  3062   3069            "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
  3063   3070            "     FROM sqlite_master UNION ALL"
  3064   3071            "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
  3065         -         "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
         3072  +         "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
  3066   3073            "ORDER BY rowid",
  3067   3074            callback, &data, &zErrMsg
  3068   3075         );
  3069   3076       }else{
  3070   3077         fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
  3071   3078         rc = 1;
  3072   3079         goto meta_command_exit;
................................................................................
  3144   3151       if( nArg!=1 ){
  3145   3152         fprintf(stderr, "Usage: .show\n");
  3146   3153         rc = 1;
  3147   3154         goto meta_command_exit;
  3148   3155       }
  3149   3156       fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
  3150   3157       fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
  3151         -    fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
         3158  +    fprintf(p->out,"%9.9s: %s\n","explain", p->normalMode.valid ? "on" :"off");
  3152   3159       fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
  3153   3160       fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
  3154   3161       fprintf(p->out,"%9.9s: ", "nullvalue");
  3155   3162         output_c_string(p->out, p->nullvalue);
  3156   3163         fprintf(p->out, "\n");
  3157   3164       fprintf(p->out,"%9.9s: %s\n","output",
  3158   3165               strlen30(p->outfile) ? p->outfile : "stdout");
................................................................................
  3536   3543   ** is interactive - the user is typing it it.  Otherwise, input
  3537   3544   ** is coming from a file or device.  A prompt is issued and history
  3538   3545   ** is saved only if input is interactive.  An interrupt signal will
  3539   3546   ** cause this routine to exit immediately, unless input is interactive.
  3540   3547   **
  3541   3548   ** Return the number of errors.
  3542   3549   */
  3543         -static int process_input(struct callback_data *p, FILE *in){
         3550  +static int process_input(ShellState *p, FILE *in){
  3544   3551     char *zLine = 0;          /* A single input line */
  3545   3552     char *zSql = 0;           /* Accumulated SQL text */
  3546   3553     int nLine;                /* Length of current line */
  3547   3554     int nSql = 0;             /* Bytes of zSql[] used */
  3548   3555     int nAlloc = 0;           /* Allocated zSql[] space */
  3549   3556     int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
  3550   3557     char *zErrMsg;            /* Error message returned */
................................................................................
  3715   3722   /*
  3716   3723   ** Read input from the file given by sqliterc_override.  Or if that
  3717   3724   ** parameter is NULL, take input from ~/.sqliterc
  3718   3725   **
  3719   3726   ** Returns the number of errors.
  3720   3727   */
  3721   3728   static int process_sqliterc(
  3722         -  struct callback_data *p,        /* Configuration data */
         3729  +  ShellState *p,                  /* Configuration data */
  3723   3730     const char *sqliterc_override   /* Name of config file. NULL to use default */
  3724   3731   ){
  3725   3732     char *home_dir = NULL;
  3726   3733     const char *sqliterc = sqliterc_override;
  3727   3734     char *zBuf = 0;
  3728   3735     FILE *in = NULL;
  3729   3736     int rc = 0;
................................................................................
  3798   3805     }
  3799   3806     exit(1);
  3800   3807   }
  3801   3808   
  3802   3809   /*
  3803   3810   ** Initialize the state information in data
  3804   3811   */
  3805         -static void main_init(struct callback_data *data) {
         3812  +static void main_init(ShellState *data) {
  3806   3813     memset(data, 0, sizeof(*data));
  3807   3814     data->mode = MODE_List;
  3808   3815     memcpy(data->separator,"|", 2);
  3809   3816     memcpy(data->newline,"\r\n", 3);
  3810   3817     data->showHeader = 0;
  3811   3818     sqlite3_config(SQLITE_CONFIG_URI, 1);
  3812   3819     sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
................................................................................
  3847   3854       exit(1);
  3848   3855     }
  3849   3856     return argv[i];
  3850   3857   }
  3851   3858   
  3852   3859   int main(int argc, char **argv){
  3853   3860     char *zErrMsg = 0;
  3854         -  struct callback_data data;
         3861  +  ShellState data;
  3855   3862     const char *zInitFile = 0;
  3856   3863     char *zFirstCmd = 0;
  3857   3864     int i;
  3858   3865     int rc = 0;
  3859   3866     int warnInmemoryDb = 0;
  3860   3867   
  3861   3868   #if USE_SYSTEM_SQLITE+0!=1

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.
................................................................................
  3314   3326   LogEst sqlite3LogEstFromDouble(double);
  3315   3327   #endif
  3316   3328   u64 sqlite3LogEstToInt(LogEst);
  3317   3329   
  3318   3330   /*
  3319   3331   ** Routines to read and write variable-length integers.  These used to
  3320   3332   ** be defined locally, but now we use the varint routines in the util.c
  3321         -** file.  Code should use the MACRO forms below, as the Varint32 versions
  3322         -** are coded to assume the single byte case is already handled (which 
  3323         -** the MACRO form does).
         3333  +** file.
  3324   3334   */
  3325   3335   int sqlite3PutVarint(unsigned char*, u64);
  3326         -int sqlite3PutVarint32(unsigned char*, u32);
  3327   3336   u8 sqlite3GetVarint(const unsigned char *, u64 *);
  3328   3337   u8 sqlite3GetVarint32(const unsigned char *, u32 *);
  3329   3338   int sqlite3VarintLen(u64 v);
  3330   3339   
  3331   3340   /*
  3332         -** The header of a record consists of a sequence variable-length integers.
  3333         -** These integers are almost always small and are encoded as a single byte.
  3334         -** The following macros take advantage this fact to provide a fast encode
  3335         -** and decode of the integers in a record header.  It is faster for the common
  3336         -** case where the integer is a single byte.  It is a little slower when the
  3337         -** integer is two or more bytes.  But overall it is faster.
  3338         -**
  3339         -** The following expressions are equivalent:
  3340         -**
  3341         -**     x = sqlite3GetVarint32( A, &B );
  3342         -**     x = sqlite3PutVarint32( A, B );
  3343         -**
  3344         -**     x = getVarint32( A, B );
  3345         -**     x = putVarint32( A, B );
  3346         -**
         3341  +** The common case is for a varint to be a single byte.  They following
         3342  +** macros handle the common case without a procedure call, but then call
         3343  +** the procedure for larger varints.
  3347   3344   */
  3348   3345   #define getVarint32(A,B)  \
  3349   3346     (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
  3350   3347   #define putVarint32(A,B)  \
  3351   3348     (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
  3352         -  sqlite3PutVarint32((A),(B)))
         3349  +  sqlite3PutVarint((A),(B)))
  3353   3350   #define getVarint    sqlite3GetVarint
  3354   3351   #define putVarint    sqlite3PutVarint
  3355   3352   
  3356   3353   
  3357   3354   const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
  3358   3355   void sqlite3TableAffinity(Vdbe*, Table*, int);
  3359   3356   char sqlite3CompareAffinity(Expr *pExpr, char aff2);
  3360   3357   int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
  3361   3358   char sqlite3ExprAffinity(Expr *pExpr);
  3362   3359   int sqlite3Atoi64(const char*, i64*, int, u8);
  3363   3360   int sqlite3DecOrHexToI64(const char*, i64*);
  3364         -void sqlite3Error(sqlite3*, int, const char*,...);
         3361  +void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
         3362  +void sqlite3Error(sqlite3*,int);
  3365   3363   void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  3366   3364   u8 sqlite3HexToInt(int h);
  3367   3365   int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  3368   3366   
  3369   3367   #if defined(SQLITE_TEST) 
  3370   3368   const char *sqlite3ErrName(int);
  3371   3369   #endif

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

   142    142   #endif
   143    143   
   144    144   /*
   145    145   ** Convert the given register into a string if it isn't one
   146    146   ** already. Return non-zero if a malloc() fails.
   147    147   */
   148    148   #define Stringify(P, enc) \
   149         -   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
          149  +   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
   150    150        { goto no_mem; }
   151    151   
   152    152   /*
   153    153   ** An ephemeral string value (signified by the MEM_Ephem flag) contains
   154    154   ** a pointer to a dynamically allocated string where some other entity
   155    155   ** is responsible for deallocating that string.  Because the register
   156    156   ** does not control the string, it might be deleted without the register
................................................................................
   224    224   }
   225    225   
   226    226   /*
   227    227   ** Try to convert a value into a numeric representation if we can
   228    228   ** do so without loss of information.  In other words, if the string
   229    229   ** looks like a number, convert it into a number.  If it does not
   230    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.
   231    240   */
   232         -static void applyNumericAffinity(Mem *pRec){
          241  +static void applyNumericAffinity(Mem *pRec, int bTryForInt){
   233    242     double rValue;
   234    243     i64 iValue;
   235    244     u8 enc = pRec->enc;
   236    245     if( (pRec->flags&MEM_Str)==0 ) return;
   237    246     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
   238    247     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
   239    248       pRec->u.i = iValue;
   240    249       pRec->flags |= MEM_Int;
   241    250     }else{
   242    251       pRec->r = rValue;
   243    252       pRec->flags |= MEM_Real;
          253  +    if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
   244    254     }
   245    255   }
   246         -#define ApplyNumericAffinity(X)  \
   247         -   if(((X)->flags&(MEM_Real|MEM_Int))==0){applyNumericAffinity(X);}
   248    256   
   249    257   /*
   250    258   ** Processing is determine by the affinity parameter:
   251    259   **
   252    260   ** SQLITE_AFF_INTEGER:
   253    261   ** SQLITE_AFF_REAL:
   254    262   ** SQLITE_AFF_NUMERIC:
................................................................................
   271    279   ){
   272    280     if( affinity==SQLITE_AFF_TEXT ){
   273    281       /* Only attempt the conversion to TEXT if there is an integer or real
   274    282       ** representation (blob and NULL do not get converted) but no string
   275    283       ** representation.
   276    284       */
   277    285       if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
   278         -      sqlite3VdbeMemStringify(pRec, enc);
          286  +      sqlite3VdbeMemStringify(pRec, enc, 1);
   279    287       }
   280         -    pRec->flags &= ~(MEM_Real|MEM_Int);
   281    288     }else if( affinity!=SQLITE_AFF_NONE ){
   282    289       assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
   283    290                || affinity==SQLITE_AFF_NUMERIC );
   284         -    ApplyNumericAffinity(pRec);
   285         -    if( pRec->flags & MEM_Real ){
   286         -      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  +      }
   287    297       }
   288    298     }
   289    299   }
   290    300   
   291    301   /*
   292    302   ** Try to convert the type of a function argument or a result column
   293    303   ** into a numeric representation.  Use either INTEGER or REAL whichever
................................................................................
   294    304   ** is appropriate.  But only do the conversion if it is possible without
   295    305   ** loss of information and return the revised type of the argument.
   296    306   */
   297    307   int sqlite3_value_numeric_type(sqlite3_value *pVal){
   298    308     int eType = sqlite3_value_type(pVal);
   299    309     if( eType==SQLITE_TEXT ){
   300    310       Mem *pMem = (Mem*)pVal;
   301         -    applyNumericAffinity(pMem);
          311  +    applyNumericAffinity(pMem, 0);
   302    312       eType = sqlite3_value_type(pVal);
   303    313     }
   304    314     return eType;
   305    315   }
   306    316   
   307    317   /*
   308    318   ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
................................................................................
   311    321   void sqlite3ValueApplyAffinity(
   312    322     sqlite3_value *pVal, 
   313    323     u8 affinity, 
   314    324     u8 enc
   315    325   ){
   316    326     applyAffinity((Mem *)pVal, affinity, enc);
   317    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  +}
   318    346   
   319    347   /*
   320    348   ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
   321    349   ** none.  
   322    350   **
   323    351   ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
   324    352   ** But it does set pMem->r and pMem->u.i appropriately.
   325    353   */
   326    354   static u16 numericType(Mem *pMem){
   327    355     if( pMem->flags & (MEM_Int|MEM_Real) ){
   328    356       return pMem->flags & (MEM_Int|MEM_Real);
   329    357     }
   330    358     if( pMem->flags & (MEM_Str|MEM_Blob) ){
   331         -    if( sqlite3AtoF(pMem->z, &pMem->r, pMem->n, pMem->enc)==0 ){
   332         -      return 0;
   333         -    }
   334         -    if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
   335         -      return MEM_Int;
   336         -    }
   337         -    return MEM_Real;
          359  +    return computeNumericType(pMem);
   338    360     }
   339    361     return 0;
   340    362   }
   341    363   
   342    364   #ifdef SQLITE_DEBUG
   343    365   /*
   344    366   ** Write a nice string representation of the contents of cell pMem
................................................................................
   614    636       */
   615    637       assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   616    638       if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   617    639         assert( pOp->p2>0 );
   618    640         assert( pOp->p2<=(p->nMem-p->nCursor) );
   619    641         pOut = &aMem[pOp->p2];
   620    642         memAboutToChange(p, pOut);
   621         -      VdbeMemRelease(pOut);
          643  +      VdbeMemReleaseExtern(pOut);
   622    644         pOut->flags = MEM_Int;
   623    645       }
   624    646   
   625    647       /* Sanity checking on other operands */
   626    648   #ifdef SQLITE_DEBUG
   627    649       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   628    650         assert( pOp->p1>0 );
................................................................................
  1053   1075     u16 nullFlag;
  1054   1076     cnt = pOp->p3-pOp->p2;
  1055   1077     assert( pOp->p3<=(p->nMem-p->nCursor) );
  1056   1078     pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
  1057   1079     while( cnt>0 ){
  1058   1080       pOut++;
  1059   1081       memAboutToChange(p, pOut);
  1060         -    VdbeMemRelease(pOut);
         1082  +    VdbeMemReleaseExtern(pOut);
  1061   1083       pOut->flags = nullFlag;
  1062   1084       cnt--;
  1063   1085     }
  1064   1086     break;
  1065   1087   }
  1066   1088   
  1067   1089   /* Opcode: SoftNull P1 * * * *
................................................................................
  2512   2534     */
  2513   2535     assert( p2<pC->nHdrParsed );
  2514   2536     assert( rc==SQLITE_OK );
  2515   2537     assert( sqlite3VdbeCheckMemInvariants(pDest) );
  2516   2538     if( pC->szRow>=aOffset[p2+1] ){
  2517   2539       /* This is the common case where the desired content fits on the original
  2518   2540       ** page - where the content is not on an overflow page */
  2519         -    VdbeMemRelease(pDest);
         2541  +    VdbeMemReleaseExtern(pDest);
  2520   2542       sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
  2521   2543     }else{
  2522   2544       /* This branch happens only when content is on overflow pages */
  2523   2545       t = aType[p2];
  2524   2546       if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
  2525   2547             && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
  2526   2548        || (len = sqlite3VdbeSerialTypeLen(t))==0
................................................................................
  3621   3643     pC->seekOp = pOp->opcode;
  3622   3644   #endif
  3623   3645     if( pC->isTable ){
  3624   3646       /* The input value in P3 might be of any type: integer, real, string,
  3625   3647       ** blob, or NULL.  But it needs to be an integer before we can do
  3626   3648       ** the seek, so covert it. */
  3627   3649       pIn3 = &aMem[pOp->p3];
  3628         -    ApplyNumericAffinity(pIn3);
         3650  +    if( (pIn3->flags & (MEM_Int|MEM_Real))==0 ){
         3651  +      applyNumericAffinity(pIn3, 0);
         3652  +    }
  3629   3653       iKey = sqlite3VdbeIntValue(pIn3);
  3630   3654       pC->rowidIsValid = 0;
  3631   3655   
  3632   3656       /* If the P3 value could not be converted into an integer without
  3633   3657       ** loss of information, then special processing is required... */
  3634   3658       if( (pIn3->flags & MEM_Int)==0 ){
  3635   3659         if( (pIn3->flags & MEM_Real)==0 ){

Changes to src/vdbeInt.h.

   414    414   #else
   415    415     void sqlite3VdbeMemSetDouble(Mem*, double);
   416    416   #endif
   417    417   void sqlite3VdbeMemSetNull(Mem*);
   418    418   void sqlite3VdbeMemSetZeroBlob(Mem*,int);
   419    419   void sqlite3VdbeMemSetRowSet(Mem*);
   420    420   int sqlite3VdbeMemMakeWriteable(Mem*);
   421         -int sqlite3VdbeMemStringify(Mem*, int);
          421  +int sqlite3VdbeMemStringify(Mem*, u8, u8);
   422    422   i64 sqlite3VdbeIntValue(Mem*);
   423    423   int sqlite3VdbeMemIntegerify(Mem*);
   424    424   double sqlite3VdbeRealValue(Mem*);
   425    425   void sqlite3VdbeIntegerAffinity(Mem*);
   426    426   int sqlite3VdbeMemRealify(Mem*);
   427    427   int sqlite3VdbeMemNumerify(Mem*);
   428    428   int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
   429    429   void sqlite3VdbeMemRelease(Mem *p);
   430    430   void sqlite3VdbeMemReleaseExternal(Mem *p);
   431    431   #define VdbeMemDynamic(X)  \
   432    432     (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
   433         -#define VdbeMemRelease(X)  \
          433  +#define VdbeMemReleaseExtern(X)  \
   434    434     if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
   435    435   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   436    436   const char *sqlite3OpcodeName(int);
   437    437   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   438    438   int sqlite3VdbeCloseStatement(Vdbe *, int);
   439    439   void sqlite3VdbeFrameDelete(VdbeFrame*);
   440    440   int sqlite3VdbeFrameRestore(VdbeFrame *);

Changes to src/vdbeapi.c.

   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
................................................................................
   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    638     assert( sqlite3_mutex_held(p->s.db->mutex) );
   616         -  pMem = p->pMem;
   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){
................................................................................
   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.

  2493   2493       sqlite3BeginBenignMalloc();
  2494   2494       if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
  2495   2495       sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
  2496   2496       sqlite3EndBenignMalloc();
  2497   2497       db->mallocFailed = mallocFailed;
  2498   2498       db->errCode = rc;
  2499   2499     }else{
  2500         -    sqlite3Error(db, rc, 0);
         2500  +    sqlite3Error(db, rc);
  2501   2501     }
  2502   2502     return rc;
  2503   2503   }
  2504   2504   
  2505   2505   #ifdef SQLITE_ENABLE_SQLLOG
  2506   2506   /*
  2507   2507   ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 
................................................................................
  2556   2556       p->zErrMsg = 0;
  2557   2557       if( p->runOnlyOnce ) p->expired = 1;
  2558   2558     }else if( p->rc && p->expired ){
  2559   2559       /* The expired flag was set on the VDBE before the first call
  2560   2560       ** to sqlite3_step(). For consistency (since sqlite3_step() was
  2561   2561       ** called), set the database error in this case as well.
  2562   2562       */
  2563         -    sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
         2563  +    sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
  2564   2564       sqlite3DbFree(db, p->zErrMsg);
  2565   2565       p->zErrMsg = 0;
  2566   2566     }
  2567   2567   
  2568   2568     /* Reclaim all memory used by the VDBE
  2569   2569     */
  2570   2570     Cleanup(p);
................................................................................
  2708   2708     if( p->pNext ){
  2709   2709       p->pNext->pPrev = p->pPrev;
  2710   2710     }
  2711   2711     p->magic = VDBE_MAGIC_DEAD;
  2712   2712     p->db = 0;
  2713   2713     sqlite3DbFree(db, p);
  2714   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  +}
  2715   2757   
  2716   2758   /*
  2717   2759   ** Make sure the cursor p is ready to read or write the row to which it
  2718   2760   ** was last positioned.  Return an error code if an OOM fault or I/O error
  2719   2761   ** prevents us from positioning the cursor to its correct position.
  2720   2762   **
  2721   2763   ** If a MoveTo operation is pending on the given cursor, then do that
................................................................................
  2724   2766   ** a NULL row.
  2725   2767   **
  2726   2768   ** If the cursor is already pointing to the correct row and that row has
  2727   2769   ** not been deleted out from under the cursor, then this routine is a no-op.
  2728   2770   */
  2729   2771   int sqlite3VdbeCursorMoveto(VdbeCursor *p){
  2730   2772     if( p->deferredMoveto ){
  2731         -    int res, rc;
  2732         -#ifdef SQLITE_TEST
  2733         -    extern int sqlite3_search_count;
  2734         -#endif
  2735         -    assert( p->isTable );
  2736         -    rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
  2737         -    if( rc ) return rc;
  2738         -    p->lastRowid = p->movetoTarget;
  2739         -    if( res!=0 ) return SQLITE_CORRUPT_BKPT;
  2740         -    p->rowidIsValid = 1;
  2741         -#ifdef SQLITE_TEST
  2742         -    sqlite3_search_count++;
  2743         -#endif
  2744         -    p->deferredMoveto = 0;
  2745         -    p->cacheStatus = CACHE_STALE;
  2746         -  }else if( p->pCursor ){
  2747         -    int hasMoved;
  2748         -    int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
  2749         -    if( rc ) return rc;
  2750         -    if( hasMoved ){
  2751         -      p->cacheStatus = CACHE_STALE;
  2752         -      if( hasMoved==2 ) p->nullRow = 1;
  2753         -    }
         2773  +    return handleDeferredMoveto(p);
         2774  +  }
         2775  +  if( sqlite3BtreeCursorHasMoved(p->pCursor) ){
         2776  +    return handleMovedCursor(p);
  2754   2777     }
  2755   2778     return SQLITE_OK;
  2756   2779   }
  2757   2780   
  2758   2781   /*
  2759   2782   ** The following functions:
  2760   2783   **
................................................................................
  2929   2952         assert( sizeof(v)==sizeof(pMem->r) );
  2930   2953         memcpy(&v, &pMem->r, sizeof(v));
  2931   2954         swapMixedEndianFloat(v);
  2932   2955       }else{
  2933   2956         v = pMem->u.i;
  2934   2957       }
  2935   2958       len = i = sqlite3VdbeSerialTypeLen(serial_type);
  2936         -    while( i-- ){
  2937         -      buf[i] = (u8)(v&0xFF);
         2959  +    assert( i>0 );
         2960  +    do{
         2961  +      buf[--i] = (u8)(v&0xFF);
  2938   2962         v >>= 8;
  2939         -    }
         2963  +    }while( i );
  2940   2964       return len;
  2941   2965     }
  2942   2966   
  2943   2967     /* String or blob */
  2944   2968     if( serial_type>=12 ){
  2945   2969       assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
  2946   2970                == (int)sqlite3VdbeSerialTypeLen(serial_type) );
................................................................................
  2956   2980   /* Input "x" is a sequence of unsigned characters that represent a
  2957   2981   ** big-endian integer.  Return the equivalent native integer
  2958   2982   */
  2959   2983   #define ONE_BYTE_INT(x)    ((i8)(x)[0])
  2960   2984   #define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
  2961   2985   #define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
  2962   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])
  2963   2988   
  2964   2989   /*
  2965   2990   ** Deserialize the data blob pointed to by buf as serial type serial_type
  2966   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.
  2967   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  +}
  2968   3030   u32 sqlite3VdbeSerialGet(
  2969   3031     const unsigned char *buf,     /* Buffer to deserialize from */
  2970   3032     u32 serial_type,              /* Serial type to deserialize */
  2971   3033     Mem *pMem                     /* Memory cell to write value into */
  2972   3034   ){
  2973         -  u64 x;
  2974         -  u32 y;
  2975   3035     switch( serial_type ){
  2976   3036       case 10:   /* Reserved for future use */
  2977   3037       case 11:   /* Reserved for future use */
  2978   3038       case 0: {  /* NULL */
  2979   3039         pMem->flags = MEM_Null;
  2980   3040         break;
  2981   3041       }
................................................................................
  2994   3054       case 3: { /* 3-byte signed integer */
  2995   3055         pMem->u.i = THREE_BYTE_INT(buf);
  2996   3056         pMem->flags = MEM_Int;
  2997   3057         testcase( pMem->u.i<0 );
  2998   3058         return 3;
  2999   3059       }
  3000   3060       case 4: { /* 4-byte signed integer */
  3001         -      y = FOUR_BYTE_UINT(buf);
  3002         -      pMem->u.i = (i64)*(int*)&y;
         3061  +      pMem->u.i = FOUR_BYTE_INT(buf);
  3003   3062         pMem->flags = MEM_Int;
  3004   3063         testcase( pMem->u.i<0 );
  3005   3064         return 4;
  3006   3065       }
  3007   3066       case 5: { /* 6-byte signed integer */
  3008   3067         pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
  3009   3068         pMem->flags = MEM_Int;
  3010   3069         testcase( pMem->u.i<0 );
  3011   3070         return 6;
  3012   3071       }
  3013   3072       case 6:   /* 8-byte signed integer */
  3014   3073       case 7: { /* IEEE floating point */
  3015         -#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
  3016         -      /* Verify that integers and floating point values use the same
  3017         -      ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
  3018         -      ** defined that 64-bit floating point values really are mixed
  3019         -      ** endian.
  3020         -      */
  3021         -      static const u64 t1 = ((u64)0x3ff00000)<<32;
  3022         -      static const double r1 = 1.0;
  3023         -      u64 t2 = t1;
  3024         -      swapMixedEndianFloat(t2);
  3025         -      assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
  3026         -#endif
  3027         -      x = FOUR_BYTE_UINT(buf);
  3028         -      y = FOUR_BYTE_UINT(buf+4);
  3029         -      x = (x<<32) | y;
  3030         -      if( serial_type==6 ){
  3031         -        pMem->u.i = *(i64*)&x;
  3032         -        pMem->flags = MEM_Int;
  3033         -        testcase( pMem->u.i<0 );
  3034         -      }else{
  3035         -        assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
  3036         -        swapMixedEndianFloat(x);
  3037         -        memcpy(&pMem->r, &x, sizeof(x));
  3038         -        pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
  3039         -      }
  3040         -      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);
  3041   3077       }
  3042   3078       case 8:    /* Integer 0 */
  3043   3079       case 9: {  /* Integer 1 */
  3044   3080         pMem->u.i = serial_type-8;
  3045   3081         pMem->flags = MEM_Int;
  3046   3082         return 0;
  3047   3083       }
  3048   3084       default: {
  3049   3085         static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
  3050         -      u32 len = (serial_type-12)/2;
  3051   3086         pMem->z = (char *)buf;
  3052         -      pMem->n = len;
         3087  +      pMem->n = (serial_type-12)/2;
  3053   3088         pMem->xDel = 0;
  3054   3089         pMem->flags = aFlag[serial_type&1];
  3055         -      return len;
         3090  +      return pMem->n;
  3056   3091       }
  3057   3092     }
  3058   3093     return 0;
  3059   3094   }
  3060         -
  3061   3095   /*
  3062   3096   ** This routine is used to allocate sufficient space for an UnpackedRecord
  3063   3097   ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
  3064   3098   ** the first argument is a pointer to KeyInfo structure pKeyInfo.
  3065   3099   **
  3066   3100   ** The space is either allocated using sqlite3DbMallocRaw() or from within
  3067   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(db, SQLITE_ERROR, 0);
          374  +    sqlite3Error(db, SQLITE_ERROR);
   375    375     }else if( v==0 ){
   376    376       /* If there is no statement handle, then the blob-handle has
   377    377       ** already been invalidated. Return SQLITE_ABORT in this case.
   378    378       */
   379    379       rc = SQLITE_ABORT;
   380    380     }else{
   381    381       /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
................................................................................
   447    447       ** already been invalidated. Return SQLITE_ABORT in this case.
   448    448       */
   449    449       rc = SQLITE_ABORT;
   450    450     }else{
   451    451       char *zErr;
   452    452       rc = blobSeekToRow(p, iRow, &zErr);
   453    453       if( rc!=SQLITE_OK ){
   454         -      sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
          454  +      sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
   455    455         sqlite3DbFree(db, zErr);
   456    456       }
   457    457       assert( rc!=SQLITE_SCHEMA );
   458    458     }
   459    459   
   460    460     rc = sqlite3ApiExit(db, rc);
   461    461     assert( rc==SQLITE_OK || p->pStmt==0 );
   462    462     sqlite3_mutex_leave(db->mutex);
   463    463     return rc;
   464    464   }
   465    465   
   466    466   #endif /* #ifndef SQLITE_OMIT_INCRBLOB */

Changes to src/vdbemem.c.

   117    117         pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
   118    118         bPreserve = 0;
   119    119       }else{
   120    120         sqlite3DbFree(pMem->db, pMem->zMalloc);
   121    121         pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
   122    122       }
   123    123       if( pMem->zMalloc==0 ){
   124         -      VdbeMemRelease(pMem);
          124  +      VdbeMemReleaseExtern(pMem);
   125    125         pMem->z = 0;
   126    126         pMem->flags = MEM_Null;  
   127    127         return SQLITE_NOMEM;
   128    128       }
   129    129     }
   130    130   
   131    131     if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
................................................................................
   219    219   }
   220    220   
   221    221   /*
   222    222   ** Add MEM_Str to the set of representations for the given Mem.  Numbers
   223    223   ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
   224    224   ** is a no-op.
   225    225   **
   226         -** Existing representations MEM_Int and MEM_Real are *not* invalidated.
          226  +** Existing representations MEM_Int and MEM_Real are invalidated if
          227  +** bForce is true but are retained if bForce is false.
   227    228   **
   228    229   ** A MEM_Null value will never be passed to this function. This function is
   229    230   ** used for converting values to text for returning to the user (i.e. via
   230    231   ** sqlite3_value_text()), or for ensuring that values to be used as btree
   231    232   ** keys are strings. In the former case a NULL pointer is returned the
   232    233   ** user and the later is an internal programming error.
   233    234   */
   234         -int sqlite3VdbeMemStringify(Mem *pMem, int enc){
   235         -  int rc = SQLITE_OK;
          235  +int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
   236    236     int fg = pMem->flags;
   237    237     const int nByte = 32;
   238    238   
   239    239     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   240    240     assert( !(fg&MEM_Zero) );
   241    241     assert( !(fg&(MEM_Str|MEM_Blob)) );
   242    242     assert( fg&(MEM_Int|MEM_Real) );
................................................................................
   244    244     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   245    245   
   246    246   
   247    247     if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
   248    248       return SQLITE_NOMEM;
   249    249     }
   250    250   
   251         -  /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
          251  +  /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
   252    252     ** string representation of the value. Then, if the required encoding
   253    253     ** is UTF-16le or UTF-16be do a translation.
   254    254     ** 
   255    255     ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
   256    256     */
   257    257     if( fg & MEM_Int ){
   258    258       sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
................................................................................
   259    259     }else{
   260    260       assert( fg & MEM_Real );
   261    261       sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
   262    262     }
   263    263     pMem->n = sqlite3Strlen30(pMem->z);
   264    264     pMem->enc = SQLITE_UTF8;
   265    265     pMem->flags |= MEM_Str|MEM_Term;
          266  +  if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
   266    267     sqlite3VdbeChangeEncoding(pMem, enc);
   267         -  return rc;
          268  +  return SQLITE_OK;
   268    269   }
   269    270   
   270    271   /*
   271    272   ** Memory cell pMem contains the context of an aggregate function.
   272    273   ** This routine calls the finalize method for that function.  The
   273    274   ** result of the aggregate is stored back into pMem.
   274    275   **
................................................................................
   295    296     return rc;
   296    297   }
   297    298   
   298    299   /*
   299    300   ** If the memory cell contains a string value that must be freed by
   300    301   ** invoking an external callback, free it now. Calling this function
   301    302   ** does not free any Mem.zMalloc buffer.
          303  +**
          304  +** The VdbeMemReleaseExtern() macro invokes this routine if only if there
          305  +** is work for this routine to do.
   302    306   */
   303    307   void sqlite3VdbeMemReleaseExternal(Mem *p){
   304    308     assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
   305    309     if( p->flags&MEM_Agg ){
   306    310       sqlite3VdbeMemFinalize(p, p->u.pDef);
   307    311       assert( (p->flags & MEM_Agg)==0 );
   308    312       sqlite3VdbeMemRelease(p);
................................................................................
   313    317       p->xDel = 0;
   314    318     }else if( p->flags&MEM_RowSet ){
   315    319       sqlite3RowSetClear(p->u.pRowSet);
   316    320     }else if( p->flags&MEM_Frame ){
   317    321       sqlite3VdbeMemSetNull(p);
   318    322     }
   319    323   }
          324  +
          325  +/*
          326  +** Release memory held by the Mem p, both external memory cleared
          327  +** by p->xDel and memory in p->zMalloc.
          328  +**
          329  +** This is a helper routine invoked by sqlite3VdbeMemRelease() in
          330  +** the uncommon case when there really is memory in p that is
          331  +** need of freeing.
          332  +*/
          333  +static SQLITE_NOINLINE void vdbeMemRelease(Mem *p){
          334  +  if( VdbeMemDynamic(p) ){
          335  +    sqlite3VdbeMemReleaseExternal(p);
          336  +  }
          337  +  if( p->zMalloc ){
          338  +    sqlite3DbFree(p->db, p->zMalloc);
          339  +    p->zMalloc = 0;
          340  +  }
          341  +  p->z = 0;
          342  +}
   320    343   
   321    344   /*
   322    345   ** Release any memory held by the Mem. This may leave the Mem in an
   323    346   ** inconsistent state, for example with (Mem.z==0) and
   324    347   ** (Mem.flags==MEM_Str).
   325    348   */
   326    349   void sqlite3VdbeMemRelease(Mem *p){
   327    350     assert( sqlite3VdbeCheckMemInvariants(p) );
   328         -  VdbeMemRelease(p);
   329         -  if( p->zMalloc ){
   330         -    sqlite3DbFree(p->db, p->zMalloc);
   331         -    p->zMalloc = 0;
          351  +  if( VdbeMemDynamic(p) || p->zMalloc ){
          352  +    vdbeMemRelease(p);
          353  +  }else{
          354  +    p->z = 0;
   332    355     }
   333         -  p->z = 0;
   334         -  assert( p->xDel==0 );  /* Zeroed by VdbeMemRelease() above */
          356  +  assert( p->xDel==0 );
   335    357   }
   336    358   
   337    359   /*
   338    360   ** Convert a 64-bit IEEE double into a 64-bit signed integer.
   339    361   ** If the double is out of range of a 64-bit signed integer then
   340    362   ** return the closest available 64-bit signed integer.
   341    363   */
................................................................................
   633    655   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   634    656   ** pTo are freed.  The pFrom->z field is not duplicated.  If
   635    657   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   636    658   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   637    659   */
   638    660   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   639    661     assert( (pFrom->flags & MEM_RowSet)==0 );
   640         -  VdbeMemRelease(pTo);
          662  +  VdbeMemReleaseExtern(pTo);
   641    663     memcpy(pTo, pFrom, MEMCELLSIZE);
   642    664     pTo->xDel = 0;
   643    665     if( (pFrom->flags&MEM_Static)==0 ){
   644    666       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   645    667       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   646    668       pTo->flags |= srcType;
   647    669     }
................................................................................
   651    673   ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   652    674   ** freed before the copy is made.
   653    675   */
   654    676   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   655    677     int rc = SQLITE_OK;
   656    678   
   657    679     assert( (pFrom->flags & MEM_RowSet)==0 );
   658         -  VdbeMemRelease(pTo);
          680  +  VdbeMemReleaseExtern(pTo);
   659    681     memcpy(pTo, pFrom, MEMCELLSIZE);
   660    682     pTo->flags &= ~MEM_Dyn;
   661    683     pTo->xDel = 0;
   662    684   
   663    685     if( pTo->flags&(MEM_Str|MEM_Blob) ){
   664    686       if( 0==(pFrom->flags&MEM_Static) ){
   665    687         pTo->flags |= MEM_Ephem;
................................................................................
   873    895         if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
   874    896           return 0;
   875    897         }
   876    898       }
   877    899       sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
   878    900     }else{
   879    901       assert( (pVal->flags&MEM_Blob)==0 );
   880         -    sqlite3VdbeMemStringify(pVal, enc);
          902  +    sqlite3VdbeMemStringify(pVal, enc, 0);
   881    903       assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
   882    904     }
   883    905     assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
   884    906                 || pVal->db->mallocFailed );
   885    907     if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
   886    908       return pVal->z;
   887    909     }else{
................................................................................
  1126   1148   
  1127   1149     nRet = 1 + nSerial + nVal;
  1128   1150     aRet = sqlite3DbMallocRaw(db, nRet);
  1129   1151     if( aRet==0 ){
  1130   1152       sqlite3_result_error_nomem(context);
  1131   1153     }else{
  1132   1154       aRet[0] = nSerial+1;
  1133         -    sqlite3PutVarint(&aRet[1], iSerial);
         1155  +    putVarint32(&aRet[1], iSerial);
  1134   1156       sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
  1135   1157       sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
  1136   1158       sqlite3DbFree(db, aRet);
  1137   1159     }
  1138   1160   }
  1139   1161   
  1140   1162   /*

Changes to src/vtab.c.

    39     39     void (*xDestroy)(void *)        /* Module destructor function */
    40     40   ){
    41     41     int rc = SQLITE_OK;
    42     42     int nName;
    43     43   
    44     44     sqlite3_mutex_enter(db->mutex);
    45     45     nName = sqlite3Strlen30(zName);
    46         -  if( sqlite3HashFind(&db->aModule, zName, nName) ){
           46  +  if( sqlite3HashFind(&db->aModule, zName) ){
    47     47       rc = SQLITE_MISUSE_BKPT;
    48     48     }else{
    49     49       Module *pMod;
    50     50       pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
    51     51       if( pMod ){
    52     52         Module *pDel;
    53     53         char *zCopy = (char *)(&pMod[1]);
    54     54         memcpy(zCopy, zName, nName+1);
    55     55         pMod->zName = zCopy;
    56     56         pMod->pModule = pModule;
    57     57         pMod->pAux = pAux;
    58     58         pMod->xDestroy = xDestroy;
    59         -      pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,nName,(void*)pMod);
           59  +      pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
    60     60         assert( pDel==0 || pDel==pMod );
    61     61         if( pDel ){
    62     62           db->mallocFailed = 1;
    63     63           sqlite3DbFree(db, pDel);
    64     64         }
    65     65       }
    66     66     }
................................................................................
   421    421     ** the first time the virtual table is used in an SQL statement. This
   422    422     ** allows a schema that contains virtual tables to be loaded before
   423    423     ** the required virtual table implementations are registered.  */
   424    424     else {
   425    425       Table *pOld;
   426    426       Schema *pSchema = pTab->pSchema;
   427    427       const char *zName = pTab->zName;
   428         -    int nName = sqlite3Strlen30(zName);
   429    428       assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
   430         -    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
          429  +    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
   431    430       if( pOld ){
   432    431         db->mallocFailed = 1;
   433    432         assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
   434    433         return;
   435    434       }
   436    435       pParse->pNewTable = 0;
   437    436     }
................................................................................
   589    588     assert( pTab );
   590    589     if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
   591    590       return SQLITE_OK;
   592    591     }
   593    592   
   594    593     /* Locate the required virtual table module */
   595    594     zMod = pTab->azModuleArg[0];
   596         -  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
          595  +  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
   597    596   
   598    597     if( !pMod ){
   599    598       const char *zModule = pTab->azModuleArg[0];
   600    599       sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
   601    600       rc = SQLITE_ERROR;
   602    601     }else{
   603    602       char *zErr = 0;
................................................................................
   657    656     const char *zMod;
   658    657   
   659    658     pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
   660    659     assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
   661    660   
   662    661     /* Locate the required virtual table module */
   663    662     zMod = pTab->azModuleArg[0];
   664         -  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
          663  +  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
   665    664   
   666    665     /* If the module has been registered and includes a Create method, 
   667    666     ** invoke it now. If the module has not been registered, return an 
   668    667     ** error. Otherwise, do nothing.
   669    668     */
   670    669     if( !pMod ){
   671    670       *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
................................................................................
   696    695   
   697    696     int rc = SQLITE_OK;
   698    697     Table *pTab;
   699    698     char *zErr = 0;
   700    699   
   701    700     sqlite3_mutex_enter(db->mutex);
   702    701     if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
   703         -    sqlite3Error(db, SQLITE_MISUSE, 0);
          702  +    sqlite3Error(db, SQLITE_MISUSE);
   704    703       sqlite3_mutex_leave(db->mutex);
   705    704       return SQLITE_MISUSE_BKPT;
   706    705     }
   707    706     assert( (pTab->tabFlags & TF_Virtual)!=0 );
   708    707   
   709    708     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
   710    709     if( pParse==0 ){
................................................................................
   724    723           pTab->aCol = pParse->pNewTable->aCol;
   725    724           pTab->nCol = pParse->pNewTable->nCol;
   726    725           pParse->pNewTable->nCol = 0;
   727    726           pParse->pNewTable->aCol = 0;
   728    727         }
   729    728         db->pVtabCtx->pTab = 0;
   730    729       }else{
   731         -      sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
          730  +      sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
   732    731         sqlite3DbFree(db, zErr);
   733    732         rc = SQLITE_ERROR;
   734    733       }
   735    734       pParse->declareVtab = 0;
   736    735     
   737    736       if( pParse->pVdbe ){
   738    737         sqlite3VdbeFinalize(pParse->pVdbe);
................................................................................
  1085   1084       }
  1086   1085       default:
  1087   1086         rc = SQLITE_MISUSE_BKPT;
  1088   1087         break;
  1089   1088     }
  1090   1089     va_end(ap);
  1091   1090   
  1092         -  if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
         1091  +  if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
  1093   1092     sqlite3_mutex_leave(db->mutex);
  1094   1093     return rc;
  1095   1094   }
  1096   1095   
  1097   1096   #endif /* SQLITE_OMIT_VIRTUALTABLE */

Changes to src/where.c.

  3777   3777     struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
  3778   3778     Table *pTab = pItem->pTab;
  3779   3779     sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
  3780   3780                        p->iTab, nb, p->maskSelf, nb, p->prereq);
  3781   3781     sqlite3DebugPrintf(" %12s",
  3782   3782                        pItem->zAlias ? pItem->zAlias : pTab->zName);
  3783   3783     if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
  3784         -     const char *zName;
  3785         -     if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
         3784  +    const char *zName;
         3785  +    if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
  3786   3786         if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
  3787   3787           int i = sqlite3Strlen30(zName) - 1;
  3788   3788           while( zName[i]!='_' ) i--;
  3789   3789           zName += i;
  3790   3790         }
  3791   3791         sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
  3792   3792       }else{
................................................................................
  3799   3799                   p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
  3800   3800       }else{
  3801   3801         z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
  3802   3802       }
  3803   3803       sqlite3DebugPrintf(" %-19s", z);
  3804   3804       sqlite3_free(z);
  3805   3805     }
  3806         -  sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
         3806  +  if( p->wsFlags & WHERE_SKIPSCAN ){
         3807  +    sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip);
         3808  +  }else{
         3809  +    sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
         3810  +  }
  3807   3811     sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
  3808   3812   #ifdef SQLITE_ENABLE_TREE_EXPLAIN
  3809   3813     /* If the 0x100 bit of wheretracing is set, then show all of the constraint
  3810   3814     ** expressions in the WhereLoop.aLTerm[] array.
  3811   3815     */
  3812   3816     if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){  /* WHERETRACE 0x100 */
  3813   3817       int i;
................................................................................
  4312   4316     ** The magic number 18 is selected on the basis that scanning 17 rows
  4313   4317     ** is almost always quicker than an index seek (even though if the index
  4314   4318     ** contains fewer than 2^17 rows we assume otherwise in other parts of
  4315   4319     ** the code). And, even if it is not, it should not be too much slower. 
  4316   4320     ** On the other hand, the extra seeks could end up being significantly
  4317   4321     ** more expensive.  */
  4318   4322     assert( 42==sqlite3LogEst(18) );
  4319         -  if( pTerm==0
  4320         -   && saved_nEq==saved_nSkip
         4323  +  if( saved_nEq==saved_nSkip
  4321   4324      && saved_nEq+1<pProbe->nKeyCol
  4322   4325      && pProbe->aiRowLogEst[saved_nEq+1]>=42  /* TUNING: Minimum for skip-scan */
  4323   4326      && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
  4324   4327     ){
  4325   4328       LogEst nIter;
  4326   4329       pNew->u.btree.nEq++;
  4327   4330       pNew->u.btree.nSkip++;
  4328   4331       pNew->aLTerm[pNew->nLTerm++] = 0;
  4329   4332       pNew->wsFlags |= WHERE_SKIPSCAN;
  4330   4333       nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
         4334  +    if( pTerm ){
         4335  +      /* TUNING:  When estimating skip-scan for a term that is also indexable,
         4336  +      ** increase the cost of the skip-scan by 2x, to make it a little less
         4337  +      ** desirable than the regular index lookup. */
         4338  +      nIter += 10;  assert( 10==sqlite3LogEst(2) );
         4339  +    }
  4331   4340       pNew->nOut -= nIter;
  4332   4341       whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
  4333   4342       pNew->nOut = saved_nOut;
         4343  +    pNew->u.btree.nEq = saved_nEq;
         4344  +    pNew->u.btree.nSkip = saved_nSkip;
  4334   4345     }
  4335   4346     for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
  4336   4347       u16 eOp = pTerm->eOperator;   /* Shorthand for pTerm->eOperator */
  4337   4348       LogEst rCostIdx;
  4338   4349       LogEst nOutUnadjusted;        /* nOut before IN() and WHERE adjustments */
  4339   4350       int nIn = 0;
  4340   4351   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4

Changes to test/memsubsys1.test.

   120    120   build_test_db memsubsys1-3.1 {PRAGMA page_size=1024}
   121    121   #show_memstats
   122    122   do_test memsubsys1-3.1.3 {
   123    123     set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
   124    124   } 0
   125    125   do_test memsubsys1-3.1.4 {
   126    126     set overflow [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
   127         -} $max_pagecache
          127  +  # Note:  The measured PAGECACHE_OVERFLOW is amount malloc() returns, not what
          128  +  # was requested.  System malloc() implementations might (arbitrarily) return
          129  +  # slightly different oversize buffers, which can result in slightly different
          130  +  # PAGECACHE_OVERFLOW sizes between consecutive runs.  So we cannot do an
          131  +  # exact comparison.  Simply verify that the amount is within 5%.
          132  +  expr {$overflow>=$max_pagecache*0.95 && $overflow<=$max_pagecache*1.05}
          133  +} 1
   128    134   do_test memsubsys1-3.1.5 {
   129    135     set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
   130    136   } 0
   131    137   db close
   132    138   sqlite3_shutdown
   133    139   sqlite3_config_pagecache [expr 2048+$xtra_size] 20
   134    140   sqlite3_initialize

Added test/skipscan3.test.

            1  +# 2014-08-20
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file implements tests of the "skip-scan" query strategy.
           13  +# In particular, this file looks at skipping intermediate terms
           14  +# in an index.  For example, if (a,b,c) are indexed, and we have
           15  +# "WHERE a=?1 AND c=?2" - verify that skip-scan can still be used.
           16  +#
           17  +
           18  +set testdir [file dirname $argv0]
           19  +source $testdir/tester.tcl
           20  +
           21  +do_execsql_test skipscan3-1.1 {
           22  +  CREATE TABLE t1(a,b,c,d,PRIMARY KEY(a,b,c));
           23  +  WITH RECURSIVE
           24  +    c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000)
           25  +  INSERT INTO t1(a,b,c,d)
           26  +    SELECT 1, 1, x, printf('x%04d',x) FROM c;
           27  +  ANALYZE;
           28  +} {}
           29  +
           30  +# This version has long used skip-scan because of the "+a"
           31  +#
           32  +do_execsql_test skipscan3-1.2eqp {
           33  +  EXPLAIN QUERY PLAN SELECT d FROM t1 WHERE +a=1 AND c=32;
           34  +} {/*ANY(a) AND ANY(b)*/}
           35  +do_execsql_test skipscan3-1.2 {
           36  +  SELECT d FROM t1 WHERE +a=1 AND c=32;
           37  +} {x0032}
           38  +
           39  +# This version (with "a" instead of "+a") should use skip-scan but
           40  +# did not prior to changes implemented on 2014-08-20
           41  +#
           42  +do_execsql_test skipscan3-1.3eqp {
           43  +  EXPLAIN QUERY PLAN SELECT d FROM t1 WHERE a=1 AND c=32;
           44  +} {/*ANY(a) AND ANY(b)*/}
           45  +do_execsql_test skipscan3-1.3 {
           46  +  SELECT d FROM t1 WHERE a=1 AND c=32;
           47  +} {x0032}
           48  +
           49  +# Repeat the test on a WITHOUT ROWID table
           50  +#
           51  +do_execsql_test skipscan3-2.1 {
           52  +  CREATE TABLE t2(a,b,c,d,PRIMARY KEY(a,b,c)) WITHOUT ROWID;
           53  +  WITH RECURSIVE
           54  +    c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000)
           55  +  INSERT INTO t2(a,b,c,d)
           56  +    SELECT 1, 1, x, printf('x%04d',x) FROM c;
           57  +  ANALYZE;
           58  +} {}
           59  +do_execsql_test skipscan3-2.2eqp {
           60  +  EXPLAIN QUERY PLAN SELECT d FROM t2 WHERE +a=1 AND c=32;
           61  +} {/*ANY(a) AND ANY(b)*/}
           62  +do_execsql_test skipscan3-2.2 {
           63  +  SELECT d FROM t2 WHERE +a=1 AND c=32;
           64  +} {x0032}
           65  +do_execsql_test skipscan3-2.3eqp {
           66  +  EXPLAIN QUERY PLAN SELECT d FROM t2 WHERE a=1 AND c=32;
           67  +} {/*ANY(a) AND ANY(b)*/}
           68  +do_execsql_test skipscan3-2.3 {
           69  +  SELECT d FROM t2 WHERE a=1 AND c=32;
           70  +} {x0032}
           71  +
           72  +  
           73  +finish_test

Changes to test/spellfix.test.

   120    120   do_execsql_test 1.22 {
   121    121     SELECT next_char('AB','vocab2','w',null,'NOCASE');
   122    122   } {cDeF}
   123    123   do_execsql_test 1.23 {
   124    124     SELECT next_char('ab','vocab2','w',null,'binary');
   125    125   } {c}
   126    126   
          127  +do_execsql_test 1.30 {
          128  +  SELECT rowid FROM t1 WHERE word='rabbit';
          129  +} {2}
          130  +do_execsql_test 1.31 {
          131  +  UPDATE t1 SET rowid=2000 WHERE word='rabbit';
          132  +  SELECT rowid FROM t1 WHERE word='rabbit';
          133  +} {2000}
          134  +do_execsql_test 1.32 {
          135  +  INSERT INTO t1(rowid, word) VALUES(3000,'melody');
          136  +  SELECT rowid, word, matchlen FROM t1 WHERE word MATCH 'melotti'
          137  +   ORDER BY score LIMIT 3;
          138  +} {3000 melody 6}
          139  +do_test 1.33 {
          140  +  catchsql {INSERT INTO t1(rowid, word) VALUES(3000,'garden');}
          141  +} {1 {constraint failed}}
          142  +
   127    143   do_execsql_test 2.1 {
   128    144     CREATE VIRTUAL TABLE t2 USING spellfix1;
   129    145     INSERT INTO t2 (word, soundslike) VALUES('school', 'skuul');
   130    146     INSERT INTO t2 (word, soundslike) VALUES('psalm', 'sarm');
   131    147     SELECT word, matchlen FROM t2 WHERE word MATCH 'sar*' LIMIT 5;
   132    148   } {psalm 4}
   133    149   

Changes to test/trace.test.

    44     44   do_test trace-1.4 {
    45     45     set ::stmtlist
    46     46   } {{CREATE TABLE t1(a,b);} {INSERT INTO t1 VALUES(1,2);} {SELECT * FROM t1;}}
    47     47   do_test trace-1.5 {
    48     48     db trace {}
    49     49     db trace
    50     50   } {}
           51  +do_test trace-1.6 {
           52  +  db eval {
           53  +     CREATE TABLE t1b(x TEXT PRIMARY KEY, y);
           54  +     INSERT INTO t1b VALUES('abc','def'),('ghi','jkl'),('mno','pqr');
           55  +  }
           56  +  set ::stmtlist {}
           57  +  set xyzzy a*
           58  +  db trace trace_proc
           59  +  db eval {
           60  +     SELECT y FROM t1b WHERE x GLOB $xyzzy
           61  +  }
           62  +} {def}
           63  +do_test trace-1.7 {
           64  +  set ::stmtlist
           65  +} {{SELECT y FROM t1b WHERE x GLOB 'a*'}}
           66  +db trace {}
    51     67   
    52     68   # If we prepare a statement and execute it multiple times, the trace
    53     69   # happens on each execution.
    54     70   #
    55     71   db close
    56     72   sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
    57     73   do_test trace-2.1 {

Changes to tool/mkautoconfamal.sh.

    58     58   autoconf
    59     59   automake
    60     60   
    61     61   mkdir -p tea/generic
    62     62   echo "#ifdef USE_SYSTEM_SQLITE"      > tea/generic/tclsqlite3.c 
    63     63   echo "# include <sqlite3.h>"        >> tea/generic/tclsqlite3.c
    64     64   echo "#else"                        >> tea/generic/tclsqlite3.c
    65         -echo "#include \"../../sqlite3.c\"" >> tea/generic/tclsqlite3.c
           65  +echo "#include \"sqlite3.c\""       >> tea/generic/tclsqlite3.c
    66     66   echo "#endif"                       >> tea/generic/tclsqlite3.c
    67     67   cat  $TOP/src/tclsqlite.c           >> tea/generic/tclsqlite3.c
    68     68   
    69     69   cat tea/configure.in | 
    70     70     sed "s/AC_INIT(\[sqlite\], .*)/AC_INIT([sqlite], [$VERSION])/" > tmp
    71     71   mv tmp tea/configure.in
    72     72   

Changes to tool/showdb.c.

   953    953       "    NNN..end        Show hex of pages NNN through end of file\n"
   954    954       "    NNNb            Decode btree page NNN\n"
   955    955       "    NNNbc           Decode btree page NNN and show content\n"
   956    956       "    NNNbm           Decode btree page NNN and show a layout map\n"
   957    957       "    NNNbdCCC        Decode cell CCC on btree page NNN\n"
   958    958       "    NNNt            Decode freelist trunk page NNN\n"
   959    959       "    NNNtd           Show leaf freelist pages on the decode\n"
   960         -    "    NNNtr           Recurisvely decode freelist starting at NNN\n"
          960  +    "    NNNtr           Recursively decode freelist starting at NNN\n"
   961    961     );
   962    962   }
   963    963   
   964    964   int main(int argc, char **argv){
   965    965     struct stat sbuf;
   966    966     unsigned char zPgSz[2];
   967    967     if( argc<2 ){