/ Check-in [9bd3e445]
Login

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

Overview
Comment:Merge all recent trunk enhancements and fixes into the ota-update branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | ota-update
Files: files | file ages | folders
SHA1: 9bd3e4453d4ad416f7e3f08f0bd283d34f1c319c
User & Date: drh 2015-04-15 14:26:04
Context
2015-04-16
10:55
Avoid attempting to rename the *-oal file while it is still open. check-in: 658c3b0c user: dan tags: ota-update
2015-04-15
14:26
Merge all recent trunk enhancements and fixes into the ota-update branch. check-in: 9bd3e445 user: drh tags: ota-update
09:16
Remove an incorrect assert() statement from sqlite3Fts3Dequote(). check-in: bd06eeb8 user: dan tags: trunk
2015-04-08
14:01
Merge all changes for version 3.8.9 into the ota-update branch. check-in: ec9d907a user: drh tags: ota-update
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   532    532   		-avoid-version
   533    533   
   534    534   sqlite3$(TEXE):	$(TOP)/src/shell.c libsqlite3.la sqlite3.h
   535    535   	$(LTLINK) $(READLINE_FLAGS) \
   536    536   		-o $@ $(TOP)/src/shell.c libsqlite3.la \
   537    537   		$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
   538    538   
          539  +sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
          540  +	$(LTLINK) -o $@ $(TOP)/tool/sqldiff.c	sqlite3.c $(TLIBS)
          541  +
   539    542   mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
   540    543   	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   541    544   		$(TLIBS) -rpath "$(libdir)"
   542    545   
   543    546   MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
   544    547   MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
   545    548   mptest:	mptester$(EXE)

Changes to Makefile.msc.

    38     38   
    39     39   # If necessary, create a list of harmless compiler warnings to disable when
    40     40   # compiling the various tools.  For the SQLite source code itself, warnings,
    41     41   # if any, will be disabled from within it.
    42     42   #
    43     43   !IFNDEF NO_WARN
    44     44   !IF $(USE_FULLWARN)!=0
    45         -NO_WARN = -wd4054 -wd4055 -wd4100 -wd4127 -wd4152 -wd4189 -wd4206 -wd4210
    46         -NO_WARN = $(NO_WARN) -wd4232 -wd4244 -wd4305 -wd4306 -wd4702 -wd4706
           45  +NO_WARN = -wd4054 -wd4055 -wd4100 -wd4127 -wd4130 -wd4152 -wd4189 -wd4206
           46  +NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4244 -wd4305 -wd4306 -wd4702 -wd4706
    47     47   !ENDIF
    48     48   !ENDIF
    49     49   
    50     50   # Set this non-0 to use the library paths and other options necessary for
    51     51   # Windows Phone 8.1.
    52     52   #
    53     53   !IFNDEF USE_WP81_OPTS
................................................................................
   449    449   !ENDIF
   450    450   
   451    451   # The mksqlite3c.tcl script accepts some options on the command
   452    452   # line.  When compiling with debugging enabled, some of these
   453    453   # options are necessary in order to allow debugging symbols to
   454    454   # work correctly with Visual Studio when using the amalgamation.
   455    455   #
          456  +!IFNDEF MKSQLITE3C_ARGS
   456    457   !IF $(DEBUG)>1
   457    458   MKSQLITE3C_ARGS = --linemacros
   458    459   !ELSE
   459    460   MKSQLITE3C_ARGS =
          461  +!ENDIF
   460    462   !ENDIF
   461    463   
   462    464   # Define -DNDEBUG to compile without debugging (i.e., for production usage)
   463    465   # Omitting the define will cause extra debugging code to be inserted and
   464    466   # includes extra comments when "EXPLAIN stmt" is used.
   465    467   #
   466    468   !IF $(DEBUG)==0
................................................................................
  1149   1151   
  1150   1152   libtclsqlite3.lib:	tclsqlite.lo libsqlite3.lib
  1151   1153   	$(LTLIB) $(LTLIBOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS)
  1152   1154   
  1153   1155   sqlite3.exe:	$(TOP)\src\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h
  1154   1156   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c \
  1155   1157   		/link /pdb:sqlite3sh.pdb $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
         1158  +
         1159  +sqldiff.exe:	$(TOP)\tool\sqldiff.c sqlite3.c sqlite3.h
         1160  +	$(LTLINK) $(TOP)\tool\sqldiff.c sqlite3.c
  1156   1161   
  1157   1162   mptester.exe:	$(TOP)\mptest\mptest.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h
  1158   1163   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(TOP)\mptest\mptest.c \
  1159   1164   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(SHELL_LINK_OPTS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
  1160   1165   
  1161   1166   MPTEST1 = mptester mptest.db $(TOP)/mptest/crash01.test --repeat 20
  1162   1167   MPTEST2 = mptester mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20

Changes to VERSION.

     1         -3.8.9
            1  +3.8.10

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.9.
            3  +# Generated by GNU Autoconf 2.62 for sqlite 3.8.10.
     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.9'
   747         -PACKAGE_STRING='sqlite 3.8.9'
          746  +PACKAGE_VERSION='3.8.10'
          747  +PACKAGE_STRING='sqlite 3.8.10'
   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>
................................................................................
  1477   1477   #
  1478   1478   # Report the --help message.
  1479   1479   #
  1480   1480   if test "$ac_init_help" = "long"; then
  1481   1481     # Omit some internal or obsolete options to make the list less imposing.
  1482   1482     # This message is too long to be a string in the A/UX 3.1 sh.
  1483   1483     cat <<_ACEOF
  1484         -\`configure' configures sqlite 3.8.9 to adapt to many kinds of systems.
         1484  +\`configure' configures sqlite 3.8.10 to adapt to many kinds of systems.
  1485   1485   
  1486   1486   Usage: $0 [OPTION]... [VAR=VALUE]...
  1487   1487   
  1488   1488   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1489   1489   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1490   1490   
  1491   1491   Defaults for the options are specified in brackets.
................................................................................
  1542   1542     --build=BUILD     configure for building on BUILD [guessed]
  1543   1543     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1544   1544   _ACEOF
  1545   1545   fi
  1546   1546   
  1547   1547   if test -n "$ac_init_help"; then
  1548   1548     case $ac_init_help in
  1549         -     short | recursive ) echo "Configuration of sqlite 3.8.9:";;
         1549  +     short | recursive ) echo "Configuration of sqlite 3.8.10:";;
  1550   1550      esac
  1551   1551     cat <<\_ACEOF
  1552   1552   
  1553   1553   Optional Features:
  1554   1554     --disable-option-checking  ignore unrecognized --enable/--with options
  1555   1555     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1556   1556     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1656   1656       cd "$ac_pwd" || { ac_status=$?; break; }
  1657   1657     done
  1658   1658   fi
  1659   1659   
  1660   1660   test -n "$ac_init_help" && exit $ac_status
  1661   1661   if $ac_init_version; then
  1662   1662     cat <<\_ACEOF
  1663         -sqlite configure 3.8.9
         1663  +sqlite configure 3.8.10
  1664   1664   generated by GNU Autoconf 2.62
  1665   1665   
  1666   1666   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
  1667   1667   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  1668   1668   This configure script is free software; the Free Software Foundation
  1669   1669   gives unlimited permission to copy, distribute and modify it.
  1670   1670   _ACEOF
  1671   1671     exit
  1672   1672   fi
  1673   1673   cat >config.log <<_ACEOF
  1674   1674   This file contains any messages produced by compilers while
  1675   1675   running configure, to aid debugging if configure makes a mistake.
  1676   1676   
  1677         -It was created by sqlite $as_me 3.8.9, which was
         1677  +It was created by sqlite $as_me 3.8.10, which was
  1678   1678   generated by GNU Autoconf 2.62.  Invocation command line was
  1679   1679   
  1680   1680     $ $0 $@
  1681   1681   
  1682   1682   _ACEOF
  1683   1683   exec 5>>config.log
  1684   1684   {
................................................................................
 13949  13949   
 13950  13950   exec 6>&1
 13951  13951   
 13952  13952   # Save the log message, to keep $[0] and so on meaningful, and to
 13953  13953   # report actual input values of CONFIG_FILES etc. instead of their
 13954  13954   # values after options handling.
 13955  13955   ac_log="
 13956         -This file was extended by sqlite $as_me 3.8.9, which was
        13956  +This file was extended by sqlite $as_me 3.8.10, which was
 13957  13957   generated by GNU Autoconf 2.62.  Invocation command line was
 13958  13958   
 13959  13959     CONFIG_FILES    = $CONFIG_FILES
 13960  13960     CONFIG_HEADERS  = $CONFIG_HEADERS
 13961  13961     CONFIG_LINKS    = $CONFIG_LINKS
 13962  13962     CONFIG_COMMANDS = $CONFIG_COMMANDS
 13963  13963     $ $0 $@
................................................................................
 14002  14002   $config_commands
 14003  14003   
 14004  14004   Report bugs to <bug-autoconf@gnu.org>."
 14005  14005   
 14006  14006   _ACEOF
 14007  14007   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 14008  14008   ac_cs_version="\\
 14009         -sqlite config.status 3.8.9
        14009  +sqlite config.status 3.8.10
 14010  14010   configured by $0, generated by GNU Autoconf 2.62,
 14011  14011     with options \\"`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"
 14012  14012   
 14013  14013   Copyright (C) 2008 Free Software Foundation, Inc.
 14014  14014   This config.status script is free software; the Free Software Foundation
 14015  14015   gives unlimited permission to copy, distribute and modify it."
 14016  14016   

Changes to ext/fts3/fts3.c.

   309    309   #endif
   310    310   
   311    311   static int fts3EvalNext(Fts3Cursor *pCsr);
   312    312   static int fts3EvalStart(Fts3Cursor *pCsr);
   313    313   static int fts3TermSegReaderCursor(
   314    314       Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
   315    315   
          316  +#ifndef SQLITE_AMALGAMATION
          317  +# if defined(SQLITE_DEBUG)
          318  +int sqlite3Fts3Always(int b) { assert( b ); return b; }
          319  +int sqlite3Fts3Never(int b)  { assert( !b ); return b; }
          320  +# endif
          321  +#endif
          322  +
   316    323   /* 
   317    324   ** Write a 64-bit variable-length integer to memory starting at p[0].
   318    325   ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
   319    326   ** The number of bytes written is returned.
   320    327   */
   321    328   int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
   322    329     unsigned char *q = (unsigned char *) p;
................................................................................
   418    425     if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
   419    426       int iIn = 1;                  /* Index of next byte to read from input */
   420    427       int iOut = 0;                 /* Index of next byte to write to output */
   421    428   
   422    429       /* If the first byte was a '[', then the close-quote character is a ']' */
   423    430       if( quote=='[' ) quote = ']';  
   424    431   
   425         -    while( ALWAYS(z[iIn]) ){
          432  +    while( z[iIn] ){
   426    433         if( z[iIn]==quote ){
   427    434           if( z[iIn+1]!=quote ) break;
   428    435           z[iOut++] = quote;
   429    436           iIn += 2;
   430    437         }else{
   431    438           z[iOut++] = z[iIn++];
   432    439         }
................................................................................
  1015   1022   */
  1016   1023   static int fts3ContentColumns(
  1017   1024     sqlite3 *db,                    /* Database handle */
  1018   1025     const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
  1019   1026     const char *zTbl,               /* Name of content table */
  1020   1027     const char ***pazCol,           /* OUT: Malloc'd array of column names */
  1021   1028     int *pnCol,                     /* OUT: Size of array *pazCol */
  1022         -  int *pnStr                      /* OUT: Bytes of string content */
         1029  +  int *pnStr,                     /* OUT: Bytes of string content */
         1030  +  char **pzErr                    /* OUT: error message */
  1023   1031   ){
  1024   1032     int rc = SQLITE_OK;             /* Return code */
  1025   1033     char *zSql;                     /* "SELECT *" statement on zTbl */  
  1026   1034     sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
  1027   1035   
  1028   1036     zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
  1029   1037     if( !zSql ){
  1030   1038       rc = SQLITE_NOMEM;
  1031   1039     }else{
  1032   1040       rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
         1041  +    if( rc!=SQLITE_OK ){
         1042  +      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
         1043  +    }
  1033   1044     }
  1034   1045     sqlite3_free(zSql);
  1035   1046   
  1036   1047     if( rc==SQLITE_OK ){
  1037   1048       const char **azCol;           /* Output array */
  1038   1049       int nStr = 0;                 /* Size of all column names (incl. 0x00) */
  1039   1050       int nCol;                     /* Number of table columns */
................................................................................
  1277   1288       sqlite3_free(zCompress); 
  1278   1289       sqlite3_free(zUncompress); 
  1279   1290       zCompress = 0;
  1280   1291       zUncompress = 0;
  1281   1292       if( nCol==0 ){
  1282   1293         sqlite3_free((void*)aCol); 
  1283   1294         aCol = 0;
  1284         -      rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
         1295  +      rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
  1285   1296   
  1286   1297         /* If a languageid= option was specified, remove the language id
  1287   1298         ** column from the aCol[] array. */ 
  1288   1299         if( rc==SQLITE_OK && zLanguageid ){
  1289   1300           int j;
  1290   1301           for(j=0; j<nCol; j++){
  1291   1302             if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
................................................................................
  3519   3530       case 5: iCol = sqlite3_value_int(apVal[4]);
  3520   3531       case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
  3521   3532       case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
  3522   3533       case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
  3523   3534     }
  3524   3535     if( !zEllipsis || !zEnd || !zStart ){
  3525   3536       sqlite3_result_error_nomem(pContext);
         3537  +  }else if( nToken==0 ){
         3538  +    sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
  3526   3539     }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
  3527   3540       sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
  3528   3541     }
  3529   3542   }
  3530   3543   
  3531   3544   /*
  3532   3545   ** Implementation of the offsets() function for FTS3

Changes to ext/fts3/fts3Int.h.

   130    130   /*
   131    131   ** Macros indicating that conditional expressions are always true or
   132    132   ** false.
   133    133   */
   134    134   #ifdef SQLITE_COVERAGE_TEST
   135    135   # define ALWAYS(x) (1)
   136    136   # define NEVER(X)  (0)
          137  +#elif defined(SQLITE_DEBUG)
          138  +# define ALWAYS(x) sqlite3Fts3Always((x)!=0)
          139  +# define NEVER(x) sqlite3Fts3Never((x)!=0)
          140  +int sqlite3Fts3Always(int b);
          141  +int sqlite3Fts3Never(int b);
   137    142   #else
   138    143   # define ALWAYS(x) (x)
   139    144   # define NEVER(x)  (x)
   140    145   #endif
   141    146   
   142    147   /*
   143    148   ** Internal types used by SQLite.

Changes to main.mk.

   403    403   	$(AR) libsqlite3.a $(LIBOBJ)
   404    404   	$(RANLIB) libsqlite3.a
   405    405   
   406    406   sqlite3$(EXE):	$(TOP)/src/shell.c libsqlite3.a sqlite3.h
   407    407   	$(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE)                  \
   408    408   		$(TOP)/src/shell.c                                  \
   409    409   		libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
          410  +
          411  +sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
          412  +	$(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \
          413  +		$(TOP)/tool/sqldiff.c	sqlite3.c $(TLIBS) $(THREADLIB)
   410    414   
   411    415   mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
   412    416   	$(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   413    417   		$(TLIBS) $(THREADLIB)
   414    418   
   415    419   MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
   416    420   MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20

Changes to mkopcodeh.awk.

    68     68   # Scan for "case OP_aaaa:" lines in the vdbe.c file
    69     69   /^case OP_/ {
    70     70     name = $2
    71     71     sub(/:/,"",name)
    72     72     sub("\r","",name)
    73     73     op[name] = -1       # op[x] holds the numeric value for OP symbol x
    74     74     jump[name] = 0
    75         -  out2_prerelease[name] = 0
    76     75     in1[name] = 0
    77     76     in2[name] = 0
    78     77     in3[name] = 0
    79     78     out2[name] = 0
    80     79     out3[name] = 0
    81     80     for(i=3; i<NF; i++){
    82     81       if($i=="same" && $(i+1)=="as"){
................................................................................
    88     87         sameas[val] = sym
    89     88         def[val] = name
    90     89       }
    91     90       x = $i
    92     91       sub(",","",x)
    93     92       if(x=="jump"){
    94     93         jump[name] = 1
    95         -    }else if(x=="out2-prerelease"){
    96         -      out2_prerelease[name] = 1
    97     94       }else if(x=="in1"){
    98     95         in1[name] = 1
    99     96       }else if(x=="in2"){
   100     97         in2[name] = 1
   101     98       }else if(x=="in3"){
   102     99         in3[name] = 1
   103    100       }else if(x=="out2"){
................................................................................
   190    187     #  bit 1:     pushes a result onto stack
   191    188     #  bit 2:     output to p1.  release p1 before opcode runs
   192    189     #
   193    190     for(i=0; i<=max; i++){
   194    191       name = def[i]
   195    192       a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0
   196    193       if( jump[name] ) a0 = 1;
   197         -    if( out2_prerelease[name] ) a1 = 2;
   198         -    if( in1[name] ) a2 = 4;
   199         -    if( in2[name] ) a3 = 8;
   200         -    if( in3[name] ) a4 = 16;
   201         -    if( out2[name] ) a5 = 32;
   202         -    if( out3[name] ) a6 = 64;
   203         -    bv[i] = a0+a1+a2+a3+a4+a5+a6+a7;
          194  +    if( in1[name] ) a2 = 2;
          195  +    if( in2[name] ) a3 = 4;
          196  +    if( in3[name] ) a4 = 8;
          197  +    if( out2[name] ) a5 = 16;
          198  +    if( out3[name] ) a6 = 32;
          199  +    bv[i] = a0+a1+a2+a3+a4+a5+a6;
   204    200     }
   205    201     print "\n"
   206    202     print "/* Properties such as \"out2\" or \"jump\" that are specified in"
   207    203     print "** comments following the \"case\" for each opcode in the vdbe.c"
   208    204     print "** are encoded into bitvectors as follows:"
   209    205     print "*/"
   210    206     print "#define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */"
   211         -  print "#define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */"
   212         -  print "#define OPFLG_IN1             0x0004  /* in1:   P1 is an input */"
   213         -  print "#define OPFLG_IN2             0x0008  /* in2:   P2 is an input */"
   214         -  print "#define OPFLG_IN3             0x0010  /* in3:   P3 is an input */"
   215         -  print "#define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */"
   216         -  print "#define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */"
          207  +  print "#define OPFLG_IN1             0x0002  /* in1:   P1 is an input */"
          208  +  print "#define OPFLG_IN2             0x0004  /* in2:   P2 is an input */"
          209  +  print "#define OPFLG_IN3             0x0008  /* in3:   P3 is an input */"
          210  +  print "#define OPFLG_OUT2            0x0010  /* out2:  P2 is an output */"
          211  +  print "#define OPFLG_OUT3            0x0020  /* out3:  P3 is an output */"
   217    212     print "#define OPFLG_INITIALIZER {\\"
   218    213     for(i=0; i<=max; i++){
   219    214       if( i%8==0 ) printf("/* %3d */",i)
   220    215       printf " 0x%02x,", bv[i]
   221    216       if( i%8==7 ) printf("\\\n");
   222    217     }
   223    218     print "}"

Changes to src/attach.c.

   294    294       sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
   295    295       goto detach_error;
   296    296     }
   297    297   
   298    298     sqlite3BtreeClose(pDb->pBt);
   299    299     pDb->pBt = 0;
   300    300     pDb->pSchema = 0;
   301         -  sqlite3ResetAllSchemasOfConnection(db);
          301  +  sqlite3CollapseDatabaseArray(db);
   302    302     return;
   303    303   
   304    304   detach_error:
   305    305     sqlite3_result_error(context, zErr, -1);
   306    306   }
   307    307   
   308    308   /*

Changes to src/btree.c.

  6731   6731     }else{
  6732   6732       assert( bBulk==0 || bBulk==1 );
  6733   6733       if( iParentIdx==0 ){                 
  6734   6734         nxDiv = 0;
  6735   6735       }else if( iParentIdx==i ){
  6736   6736         nxDiv = i-2+bBulk;
  6737   6737       }else{
  6738         -      assert( bBulk==0 );
  6739   6738         nxDiv = iParentIdx-1;
  6740   6739       }
  6741   6740       i = 2-bBulk;
  6742   6741     }
  6743   6742     nOld = i+1;
  6744   6743     if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
  6745   6744       pRight = &pParent->aData[pParent->hdrOffset+8];

Changes to src/build.c.

   222    222         sqlite3VdbeAddOp2(v, OP_Goto, 0, 1);
   223    223       }
   224    224     }
   225    225   
   226    226   
   227    227     /* Get the VDBE program ready for execution
   228    228     */
   229         -  if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
          229  +  if( v && pParse->nErr==0 && !db->mallocFailed ){
   230    230       assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
   231    231       /* A minimum of one cursor is required if autoincrement is used
   232    232       *  See ticket [a696379c1f08866] */
   233    233       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
   234    234       sqlite3VdbeMakeReady(v, pParse);
   235    235       pParse->rc = SQLITE_DONE;
   236    236       pParse->colNamesSet = 0;
................................................................................
  2759   2759       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
  2760   2760                            pIndex->nKeyCol); VdbeCoverage(v);
  2761   2761       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
  2762   2762     }else{
  2763   2763       addr2 = sqlite3VdbeCurrentAddr(v);
  2764   2764     }
  2765   2765     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
  2766         -  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
         2766  +  sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
         2767  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
  2767   2768     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  2768   2769     sqlite3ReleaseTempReg(pParse, regRecord);
  2769   2770     sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
  2770   2771     sqlite3VdbeJumpHere(v, addr1);
  2771   2772   
  2772   2773     sqlite3VdbeAddOp1(v, OP_Close, iTab);
  2773   2774     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
................................................................................
  3772   3773   ** The operator is "natural cross join".  The A and B operands are stored
  3773   3774   ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
  3774   3775   ** operator with A.  This routine shifts that operator over to B.
  3775   3776   */
  3776   3777   void sqlite3SrcListShiftJoinType(SrcList *p){
  3777   3778     if( p ){
  3778   3779       int i;
  3779         -    assert( p->a || p->nSrc==0 );
  3780   3780       for(i=p->nSrc-1; i>0; i--){
  3781   3781         p->a[i].jointype = p->a[i-1].jointype;
  3782   3782       }
  3783   3783       p->a[0].jointype = 0;
  3784   3784     }
  3785   3785   }
  3786   3786   

Changes to src/complete.c.

   265    265   ** This routine is the same as the sqlite3_complete() routine described
   266    266   ** above, except that the parameter is required to be UTF-16 encoded, not
   267    267   ** UTF-8.
   268    268   */
   269    269   int sqlite3_complete16(const void *zSql){
   270    270     sqlite3_value *pVal;
   271    271     char const *zSql8;
   272         -  int rc = SQLITE_NOMEM;
          272  +  int rc;
   273    273   
   274    274   #ifndef SQLITE_OMIT_AUTOINIT
   275    275     rc = sqlite3_initialize();
   276    276     if( rc ) return rc;
   277    277   #endif
   278    278     pVal = sqlite3ValueNew(0);
   279    279     sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);

Changes to src/expr.c.

  1687   1687     mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
  1688   1688   
  1689   1689     /* Check to see if an existing table or index can be used to
  1690   1690     ** satisfy the query.  This is preferable to generating a new 
  1691   1691     ** ephemeral table.
  1692   1692     */
  1693   1693     p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
  1694         -  if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
         1694  +  if( pParse->nErr==0 && isCandidateForInOpt(p) ){
  1695   1695       sqlite3 *db = pParse->db;              /* Database connection */
  1696   1696       Table *pTab;                           /* Table <table>. */
  1697   1697       Expr *pExpr;                           /* Expression <column> */
  1698   1698       i16 iCol;                              /* Index of column <column> */
  1699   1699       i16 iDb;                               /* Database idx for pTab */
  1700   1700   
  1701   1701       assert( p );                        /* Because of isCandidateForInOpt(p) */
................................................................................
  2012   2012           sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
  2013   2013           VdbeComment((v, "Init EXISTS result"));
  2014   2014         }
  2015   2015         sqlite3ExprDelete(pParse->db, pSel->pLimit);
  2016   2016         pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
  2017   2017                                     &sqlite3IntTokens[1]);
  2018   2018         pSel->iLimit = 0;
         2019  +      pSel->selFlags &= ~SF_AllValues;
  2019   2020         if( sqlite3Select(pParse, pSel, &dest) ){
  2020   2021           return 0;
  2021   2022         }
  2022   2023         rReg = dest.iSDParm;
  2023   2024         ExprSetVVAProperty(pExpr, EP_NoReduce);
  2024   2025         break;
  2025   2026       }

Changes to src/fkey.c.

  1180   1180         Token tFromCol;             /* Name of column in child table */
  1181   1181         Token tToCol;               /* Name of column in parent table */
  1182   1182         int iFromCol;               /* Idx of column in child table */
  1183   1183         Expr *pEq;                  /* tFromCol = OLD.tToCol */
  1184   1184   
  1185   1185         iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
  1186   1186         assert( iFromCol>=0 );
  1187         -      tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
         1187  +      assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
         1188  +      tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName;
  1188   1189         tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
  1189   1190   
  1190   1191         tToCol.n = sqlite3Strlen30(tToCol.z);
  1191   1192         tFromCol.n = sqlite3Strlen30(tFromCol.z);
  1192   1193   
  1193   1194         /* Create the expression "OLD.zToCol = zFromCol". It is important
  1194   1195         ** that the "OLD.zToCol" term is on the LHS of the = operator, so

Changes to src/insert.c.

  1761   1761   static int xferOptimization(
  1762   1762     Parse *pParse,        /* Parser context */
  1763   1763     Table *pDest,         /* The table we are inserting into */
  1764   1764     Select *pSelect,      /* A SELECT statement to use as the data source */
  1765   1765     int onError,          /* How to handle constraint errors */
  1766   1766     int iDbDest           /* The database of pDest */
  1767   1767   ){
         1768  +  sqlite3 *db = pParse->db;
  1768   1769     ExprList *pEList;                /* The result set of the SELECT */
  1769   1770     Table *pSrc;                     /* The table in the FROM clause of SELECT */
  1770   1771     Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
  1771   1772     struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
  1772   1773     int i;                           /* Loop counter */
  1773   1774     int iDbSrc;                      /* The database of pSrc */
  1774   1775     int iSrc, iDest;                 /* Cursors from source and destination */
................................................................................
  1908   1909     /* Disallow the transfer optimization if the destination table constains
  1909   1910     ** any foreign key constraints.  This is more restrictive than necessary.
  1910   1911     ** But the main beneficiary of the transfer optimization is the VACUUM 
  1911   1912     ** command, and the VACUUM command disables foreign key constraints.  So
  1912   1913     ** the extra complication to make this rule less restrictive is probably
  1913   1914     ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
  1914   1915     */
  1915         -  if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
         1916  +  if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
  1916   1917       return 0;
  1917   1918     }
  1918   1919   #endif
  1919         -  if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
         1920  +  if( (db->flags & SQLITE_CountRows)!=0 ){
  1920   1921       return 0;  /* xfer opt does not play well with PRAGMA count_changes */
  1921   1922     }
  1922   1923   
  1923   1924     /* If we get this far, it means that the xfer optimization is at
  1924   1925     ** least a possibility, though it might only work if the destination
  1925   1926     ** table (tab1) is initially empty.
  1926   1927     */
  1927   1928   #ifdef SQLITE_TEST
  1928   1929     sqlite3_xferopt_count++;
  1929   1930   #endif
  1930         -  iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
         1931  +  iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
  1931   1932     v = sqlite3GetVdbe(pParse);
  1932   1933     sqlite3CodeVerifySchema(pParse, iDbSrc);
  1933   1934     iSrc = pParse->nTab++;
  1934   1935     iDest = pParse->nTab++;
  1935   1936     regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
  1936   1937     regData = sqlite3GetTempReg(pParse);
  1937   1938     regRowid = sqlite3GetTempReg(pParse);
  1938   1939     sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
  1939   1940     assert( HasRowid(pDest) || destHasUniqueIdx );
  1940         -  if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
         1941  +  if( (db->flags & SQLITE_Vacuum)==0 && (
         1942  +      (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
  1941   1943      || destHasUniqueIdx                              /* (2) */
  1942   1944      || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
  1943         -  ){
         1945  +  )){
  1944   1946       /* In some circumstances, we are able to run the xfer optimization
  1945         -    ** only if the destination table is initially empty.  This code makes
  1946         -    ** that determination.  Conditions under which the destination must
  1947         -    ** be empty:
         1947  +    ** only if the destination table is initially empty. Unless the
         1948  +    ** SQLITE_Vacuum flag is set, this block generates code to make
         1949  +    ** that determination. If SQLITE_Vacuum is set, then the destination
         1950  +    ** table is always empty.
         1951  +    **
         1952  +    ** Conditions under which the destination must be empty:
  1948   1953       **
  1949   1954       ** (1) There is no INTEGER PRIMARY KEY but there are indices.
  1950   1955       **     (If the destination is not initially empty, the rowid fields
  1951   1956       **     of index entries might need to change.)
  1952   1957       **
  1953   1958       ** (2) The destination has a unique index.  (The xfer optimization 
  1954   1959       **     is unable to test uniqueness.)
................................................................................
  1983   1988       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  1984   1989       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1985   1990     }else{
  1986   1991       sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
  1987   1992       sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
  1988   1993     }
  1989   1994     for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
         1995  +    u8 useSeekResult = 0;
  1990   1996       for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
  1991   1997         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
  1992   1998       }
  1993   1999       assert( pSrcIdx );
  1994   2000       sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
  1995   2001       sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
  1996   2002       VdbeComment((v, "%s", pSrcIdx->zName));
  1997   2003       sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
  1998   2004       sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
  1999   2005       sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
  2000   2006       VdbeComment((v, "%s", pDestIdx->zName));
  2001   2007       addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
  2002   2008       sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
         2009  +    if( db->flags & SQLITE_Vacuum ){
         2010  +      /* This INSERT command is part of a VACUUM operation, which guarantees
         2011  +      ** that the destination table is empty. If all indexed columns use
         2012  +      ** collation sequence BINARY, then it can also be assumed that the
         2013  +      ** index will be populated by inserting keys in strictly sorted 
         2014  +      ** order. In this case, instead of seeking within the b-tree as part
         2015  +      ** of every OP_IdxInsert opcode, an OP_Last is added before the
         2016  +      ** OP_IdxInsert to seek to the point within the b-tree where each key 
         2017  +      ** should be inserted. This is faster.
         2018  +      **
         2019  +      ** If any of the indexed columns use a collation sequence other than
         2020  +      ** BINARY, this optimization is disabled. This is because the user 
         2021  +      ** might change the definition of a collation sequence and then run
         2022  +      ** a VACUUM command. In that case keys may not be written in strictly
         2023  +      ** sorted order.  */
         2024  +      int i;
         2025  +      for(i=0; i<pSrcIdx->nColumn; i++){
         2026  +        char *zColl = pSrcIdx->azColl[i];
         2027  +        assert( zColl!=0 );
         2028  +        if( sqlite3_stricmp("BINARY", zColl) ) break;
         2029  +      }
         2030  +      if( i==pSrcIdx->nColumn ){
         2031  +        useSeekResult = OPFLAG_USESEEKRESULT;
         2032  +        sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
         2033  +      }
         2034  +    }
  2003   2035       sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
         2036  +    sqlite3VdbeChangeP5(v, useSeekResult);
  2004   2037       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
  2005   2038       sqlite3VdbeJumpHere(v, addr1);
  2006   2039       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  2007   2040       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  2008   2041     }
  2009   2042     if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
  2010   2043     sqlite3ReleaseTempReg(pParse, regRowid);

Changes to src/msvc.h.

    16     16   #define _MSVC_H_
    17     17   
    18     18   #if defined(_MSC_VER)
    19     19   #pragma warning(disable : 4054)
    20     20   #pragma warning(disable : 4055)
    21     21   #pragma warning(disable : 4100)
    22     22   #pragma warning(disable : 4127)
           23  +#pragma warning(disable : 4130)
    23     24   #pragma warning(disable : 4152)
    24     25   #pragma warning(disable : 4189)
    25     26   #pragma warning(disable : 4206)
    26     27   #pragma warning(disable : 4210)
    27     28   #pragma warning(disable : 4232)
    28     29   #pragma warning(disable : 4244)
    29     30   #pragma warning(disable : 4305)
    30     31   #pragma warning(disable : 4306)
    31     32   #pragma warning(disable : 4702)
    32     33   #pragma warning(disable : 4706)
    33     34   #endif /* defined(_MSC_VER) */
    34     35   
    35     36   #endif /* _MSVC_H_ */

Changes to src/os_unix.c.

    86     86   #endif
    87     87   
    88     88   #if SQLITE_ENABLE_LOCKING_STYLE
    89     89   # include <sys/ioctl.h>
    90     90   # include <sys/file.h>
    91     91   # include <sys/param.h>
    92     92   #endif /* SQLITE_ENABLE_LOCKING_STYLE */
           93  +
           94  +#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
           95  +                           (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
           96  +#  if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
           97  +       && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
           98  +#    define HAVE_GETHOSTUUID 1
           99  +#  else
          100  +#    warning "gethostuuid() is disabled."
          101  +#  endif
          102  +#endif
          103  +
    93    104   
    94    105   #if OS_VXWORKS
    95    106   # include <sys/ioctl.h>
    96    107   # include <semaphore.h>
    97    108   # include <limits.h>
    98    109   #endif /* OS_VXWORKS */
    99    110   
................................................................................
  6598   6609   #ifdef SQLITE_TEST
  6599   6610   /* simulate multiple hosts by creating unique hostid file paths */
  6600   6611   int sqlite3_hostid_num = 0;
  6601   6612   #endif
  6602   6613   
  6603   6614   #define PROXY_HOSTIDLEN    16  /* conch file host id length */
  6604   6615   
         6616  +#ifdef HAVE_GETHOSTUUID
  6605   6617   /* Not always defined in the headers as it ought to be */
  6606   6618   extern int gethostuuid(uuid_t id, const struct timespec *wait);
         6619  +#endif
  6607   6620   
  6608   6621   /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
  6609   6622   ** bytes of writable memory.
  6610   6623   */
  6611   6624   static int proxyGetHostID(unsigned char *pHostID, int *pError){
  6612   6625     assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
  6613   6626     memset(pHostID, 0, PROXY_HOSTIDLEN);
  6614         -# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
  6615         -                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
         6627  +#ifdef HAVE_GETHOSTUUID
  6616   6628     {
  6617   6629       struct timespec timeout = {1, 0}; /* 1 sec timeout */
  6618   6630       if( gethostuuid(pHostID, &timeout) ){
  6619   6631         int err = errno;
  6620   6632         if( pError ){
  6621   6633           *pError = err;
  6622   6634         }

Changes to src/pragma.c.

  1037   1037             sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
  1038   1038           }
  1039   1039           if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
  1040   1040             k = 0;
  1041   1041           }else if( pPk==0 ){
  1042   1042             k = 1;
  1043   1043           }else{
  1044         -          for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
         1044  +          for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
  1045   1045           }
  1046   1046           sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
  1047   1047           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
  1048   1048         }
  1049   1049       }
  1050   1050     }
  1051   1051     break;

Changes to src/printf.c.

   822    822   }
   823    823   
   824    824   /*
   825    825   ** Append N bytes of text from z to the StrAccum object.  Increase the
   826    826   ** size of the memory allocation for StrAccum if necessary.
   827    827   */
   828    828   void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
   829         -  assert( z!=0 );
          829  +  assert( z!=0 || N==0 );
   830    830     assert( p->zText!=0 || p->nChar==0 || p->accError );
   831    831     assert( N>=0 );
   832    832     assert( p->accError==0 || p->nAlloc==0 );
   833    833     if( p->nChar+N >= p->nAlloc ){
   834    834       enlargeAndAppend(p,z,N);
   835    835     }else{
   836    836       assert( p->zText );

Changes to src/resolve.c.

   456    456     **
   457    457     ** Because no reference was made to outer contexts, the pNC->nRef
   458    458     ** fields are not changed in any context.
   459    459     */
   460    460     if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
   461    461       pExpr->op = TK_STRING;
   462    462       pExpr->pTab = 0;
          463  +    pExpr->iTable = -1;
   463    464       return WRC_Prune;
   464    465     }
   465    466   
   466    467     /*
   467    468     ** cnt==0 means there was not match.  cnt>1 means there were two or
   468    469     ** more matches.  Either way, we have an error.
   469    470     */
................................................................................
   989    990           Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
   990    991           if( pNew==0 ) return 1;
   991    992           pNew->flags |= EP_IntValue;
   992    993           pNew->u.iValue = iCol;
   993    994           if( pItem->pExpr==pE ){
   994    995             pItem->pExpr = pNew;
   995    996           }else{
   996         -          assert( pItem->pExpr->op==TK_COLLATE );
   997         -          assert( pItem->pExpr->pLeft==pE );
   998         -          pItem->pExpr->pLeft = pNew;
          997  +          Expr *pParent = pItem->pExpr;
          998  +          assert( pParent->op==TK_COLLATE );
          999  +          while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
         1000  +          assert( pParent->pLeft==pE );
         1001  +          pParent->pLeft = pNew;
   999   1002           }
  1000   1003           sqlite3ExprDelete(db, pE);
  1001   1004           pItem->u.x.iOrderByCol = (u16)iCol;
  1002   1005           pItem->done = 1;
  1003   1006         }else{
  1004   1007           moreToDo = 1;
  1005   1008         }
................................................................................
  1191   1194       ** was created by the convertCompoundSelectToSubquery() function.
  1192   1195       ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
  1193   1196       ** as if it were part of the sub-query, not the parent. This block
  1194   1197       ** moves the pOrderBy down to the sub-query. It will be moved back
  1195   1198       ** after the names have been resolved.  */
  1196   1199       if( p->selFlags & SF_Converted ){
  1197   1200         Select *pSub = p->pSrc->a[0].pSelect;
  1198         -      assert( p->pSrc->nSrc==1 && isCompound==0 && p->pOrderBy );
         1201  +      assert( p->pSrc->nSrc==1 && p->pOrderBy );
  1199   1202         assert( pSub->pPrior && pSub->pOrderBy==0 );
  1200   1203         pSub->pOrderBy = p->pOrderBy;
  1201   1204         p->pOrderBy = 0;
  1202   1205       }
  1203   1206     
  1204   1207       /* Recursively resolve names in all subqueries
  1205   1208       */

Changes to src/select.c.

  3067   3067     }
  3068   3068     p->pPrior = pPrior;
  3069   3069     pPrior->pNext = p;
  3070   3070   
  3071   3071     /*** TBD:  Insert subroutine calls to close cursors on incomplete
  3072   3072     **** subqueries ****/
  3073   3073     explainComposite(pParse, p->op, iSub1, iSub2, 0);
  3074         -  return SQLITE_OK;
         3074  +  return pParse->nErr!=0;
  3075   3075   }
  3076   3076   #endif
  3077   3077   
  3078   3078   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  3079   3079   /* Forward Declarations */
  3080   3080   static void substExprList(sqlite3*, ExprList*, int, ExprList*);
  3081   3081   static void substSelect(sqlite3*, Select *, int, ExprList *);
................................................................................
  3879   3879     p->op = TK_SELECT;
  3880   3880     p->pWhere = 0;
  3881   3881     pNew->pGroupBy = 0;
  3882   3882     pNew->pHaving = 0;
  3883   3883     pNew->pOrderBy = 0;
  3884   3884     p->pPrior = 0;
  3885   3885     p->pNext = 0;
         3886  +  p->pWith = 0;
  3886   3887     p->selFlags &= ~SF_Compound;
  3887   3888     assert( (p->selFlags & SF_Converted)==0 );
  3888   3889     p->selFlags |= SF_Converted;
  3889   3890     assert( pNew->pPrior!=0 );
  3890   3891     pNew->pPrior->pNext = pNew;
  3891   3892     pNew->pLimit = 0;
  3892   3893     pNew->pOffset = 0;

Changes to src/sqlite.h.in.

   266    266   */
   267    267   #ifdef SQLITE_OMIT_FLOATING_POINT
   268    268   # define double sqlite3_int64
   269    269   #endif
   270    270   
   271    271   /*
   272    272   ** CAPI3REF: Closing A Database Connection
          273  +** DESTRUCTOR: sqlite3
   273    274   **
   274    275   ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   275    276   ** for the [sqlite3] object.
   276    277   ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
   277    278   ** the [sqlite3] object is successfully destroyed and all associated
   278    279   ** resources are deallocated.
   279    280   **
................................................................................
   317    318   ** This is legacy and deprecated.  It is included for historical
   318    319   ** compatibility and is not documented.
   319    320   */
   320    321   typedef int (*sqlite3_callback)(void*,int,char**, char**);
   321    322   
   322    323   /*
   323    324   ** CAPI3REF: One-Step Query Execution Interface
          325  +** METHOD: sqlite3
   324    326   **
   325    327   ** The sqlite3_exec() interface is a convenience wrapper around
   326    328   ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
   327    329   ** that allows an application to run multiple statements of SQL
   328    330   ** without having to use a lot of C code. 
   329    331   **
   330    332   ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
................................................................................
  1379   1381   ** ^If the option is unknown or SQLite is unable to set the option
  1380   1382   ** then this routine returns a non-zero [error code].
  1381   1383   */
  1382   1384   int sqlite3_config(int, ...);
  1383   1385   
  1384   1386   /*
  1385   1387   ** CAPI3REF: Configure database connections
         1388  +** METHOD: sqlite3
  1386   1389   **
  1387   1390   ** The sqlite3_db_config() interface is used to make configuration
  1388   1391   ** changes to a [database connection].  The interface is similar to
  1389   1392   ** [sqlite3_config()] except that the changes apply to a single
  1390   1393   ** [database connection] (specified in the first argument).
  1391   1394   **
  1392   1395   ** The second argument to sqlite3_db_config(D,V,...)  is the
................................................................................
  1876   1879   #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
  1877   1880   #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
  1878   1881   #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
  1879   1882   
  1880   1883   
  1881   1884   /*
  1882   1885   ** CAPI3REF: Enable Or Disable Extended Result Codes
         1886  +** METHOD: sqlite3
  1883   1887   **
  1884   1888   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  1885   1889   ** [extended result codes] feature of SQLite. ^The extended result
  1886   1890   ** codes are disabled by default for historical compatibility.
  1887   1891   */
  1888   1892   int sqlite3_extended_result_codes(sqlite3*, int onoff);
  1889   1893   
  1890   1894   /*
  1891   1895   ** CAPI3REF: Last Insert Rowid
         1896  +** METHOD: sqlite3
  1892   1897   **
  1893   1898   ** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
  1894   1899   ** has a unique 64-bit signed
  1895   1900   ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  1896   1901   ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  1897   1902   ** names are not also used by explicitly declared columns. ^If
  1898   1903   ** the table has a column of type [INTEGER PRIMARY KEY] then that column
................................................................................
  1936   1941   ** unpredictable and might not equal either the old or the new
  1937   1942   ** last insert [rowid].
  1938   1943   */
  1939   1944   sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  1940   1945   
  1941   1946   /*
  1942   1947   ** CAPI3REF: Count The Number Of Rows Modified
         1948  +** METHOD: sqlite3
  1943   1949   **
  1944   1950   ** ^This function returns the number of rows modified, inserted or
  1945   1951   ** deleted by the most recently completed INSERT, UPDATE or DELETE
  1946   1952   ** statement on the database connection specified by the only parameter.
  1947   1953   ** ^Executing any other type of SQL statement does not modify the value
  1948   1954   ** returned by this function.
  1949   1955   **
................................................................................
  1988   1994   ** while [sqlite3_changes()] is running then the value returned
  1989   1995   ** is unpredictable and not meaningful.
  1990   1996   */
  1991   1997   int sqlite3_changes(sqlite3*);
  1992   1998   
  1993   1999   /*
  1994   2000   ** CAPI3REF: Total Number Of Rows Modified
         2001  +** METHOD: sqlite3
  1995   2002   **
  1996   2003   ** ^This function returns the total number of rows inserted, modified or
  1997   2004   ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
  1998   2005   ** since the database connection was opened, including those executed as
  1999   2006   ** part of trigger programs. ^Executing any other type of SQL statement
  2000   2007   ** does not affect the value returned by sqlite3_total_changes().
  2001   2008   ** 
................................................................................
  2011   2018   ** while [sqlite3_total_changes()] is running then the value
  2012   2019   ** returned is unpredictable and not meaningful.
  2013   2020   */
  2014   2021   int sqlite3_total_changes(sqlite3*);
  2015   2022   
  2016   2023   /*
  2017   2024   ** CAPI3REF: Interrupt A Long-Running Query
         2025  +** METHOD: sqlite3
  2018   2026   **
  2019   2027   ** ^This function causes any pending database operation to abort and
  2020   2028   ** return at its earliest opportunity. This routine is typically
  2021   2029   ** called in response to a user action such as pressing "Cancel"
  2022   2030   ** or Ctrl-C where the user wants a long query operation to halt
  2023   2031   ** immediately.
  2024   2032   **
................................................................................
  2087   2095   */
  2088   2096   int sqlite3_complete(const char *sql);
  2089   2097   int sqlite3_complete16(const void *sql);
  2090   2098   
  2091   2099   /*
  2092   2100   ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  2093   2101   ** KEYWORDS: {busy-handler callback} {busy handler}
         2102  +** METHOD: sqlite3
  2094   2103   **
  2095   2104   ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
  2096   2105   ** that might be invoked with argument P whenever
  2097   2106   ** an attempt is made to access a database table associated with
  2098   2107   ** [database connection] D when another thread
  2099   2108   ** or process has the table locked.
  2100   2109   ** The sqlite3_busy_handler() interface is used to implement
................................................................................
  2146   2155   ** A busy handler must not close the database connection
  2147   2156   ** or [prepared statement] that invoked the busy handler.
  2148   2157   */
  2149   2158   int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  2150   2159   
  2151   2160   /*
  2152   2161   ** CAPI3REF: Set A Busy Timeout
         2162  +** METHOD: sqlite3
  2153   2163   **
  2154   2164   ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  2155   2165   ** for a specified amount of time when a table is locked.  ^The handler
  2156   2166   ** will sleep multiple times until at least "ms" milliseconds of sleeping
  2157   2167   ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
  2158   2168   ** the handler returns 0 which causes [sqlite3_step()] to return
  2159   2169   ** [SQLITE_BUSY].
................................................................................
  2168   2178   **
  2169   2179   ** See also:  [PRAGMA busy_timeout]
  2170   2180   */
  2171   2181   int sqlite3_busy_timeout(sqlite3*, int ms);
  2172   2182   
  2173   2183   /*
  2174   2184   ** CAPI3REF: Convenience Routines For Running Queries
         2185  +** METHOD: sqlite3
  2175   2186   **
  2176   2187   ** This is a legacy interface that is preserved for backwards compatibility.
  2177   2188   ** Use of this interface is not recommended.
  2178   2189   **
  2179   2190   ** Definition: A <b>result table</b> is memory data structure created by the
  2180   2191   ** [sqlite3_get_table()] interface.  A result table records the
  2181   2192   ** complete query results from one or more queries.
................................................................................
  2503   2514   ** internally and without recourse to the [sqlite3_vfs] xRandomness
  2504   2515   ** method.
  2505   2516   */
  2506   2517   void sqlite3_randomness(int N, void *P);
  2507   2518   
  2508   2519   /*
  2509   2520   ** CAPI3REF: Compile-Time Authorization Callbacks
         2521  +** METHOD: sqlite3
  2510   2522   **
  2511   2523   ** ^This routine registers an authorizer callback with a particular
  2512   2524   ** [database connection], supplied in the first argument.
  2513   2525   ** ^The authorizer callback is invoked as SQL statements are being compiled
  2514   2526   ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  2515   2527   ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
  2516   2528   ** points during the compilation process, as logic is being created
................................................................................
  2659   2671   #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
  2660   2672   #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
  2661   2673   #define SQLITE_COPY                  0   /* No longer used */
  2662   2674   #define SQLITE_RECURSIVE            33   /* NULL            NULL            */
  2663   2675   
  2664   2676   /*
  2665   2677   ** CAPI3REF: Tracing And Profiling Functions
         2678  +** METHOD: sqlite3
  2666   2679   **
  2667   2680   ** These routines register callback functions that can be used for
  2668   2681   ** tracing and profiling the execution of SQL statements.
  2669   2682   **
  2670   2683   ** ^The callback function registered by sqlite3_trace() is invoked at
  2671   2684   ** various times when an SQL statement is being run by [sqlite3_step()].
  2672   2685   ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
................................................................................
  2691   2704   */
  2692   2705   void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  2693   2706   SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
  2694   2707      void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  2695   2708   
  2696   2709   /*
  2697   2710   ** CAPI3REF: Query Progress Callbacks
         2711  +** METHOD: sqlite3
  2698   2712   **
  2699   2713   ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  2700   2714   ** function X to be invoked periodically during long running calls to
  2701   2715   ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
  2702   2716   ** database connection D.  An example use for this
  2703   2717   ** interface is to keep a GUI updated during a large query.
  2704   2718   **
................................................................................
  2724   2738   ** database connections for the meaning of "modify" in this paragraph.
  2725   2739   **
  2726   2740   */
  2727   2741   void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
  2728   2742   
  2729   2743   /*
  2730   2744   ** CAPI3REF: Opening A New Database Connection
         2745  +** CONSTRUCTOR: sqlite3
  2731   2746   **
  2732   2747   ** ^These routines open an SQLite database file as specified by the 
  2733   2748   ** filename argument. ^The filename argument is interpreted as UTF-8 for
  2734   2749   ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
  2735   2750   ** order for sqlite3_open16(). ^(A [database connection] handle is usually
  2736   2751   ** returned in *ppDb, even if an error occurs.  The only exception is that
  2737   2752   ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
................................................................................
  3009   3024   const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
  3010   3025   int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
  3011   3026   sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
  3012   3027   
  3013   3028   
  3014   3029   /*
  3015   3030   ** CAPI3REF: Error Codes And Messages
         3031  +** METHOD: sqlite3
  3016   3032   **
  3017   3033   ** ^If the most recent sqlite3_* API call associated with 
  3018   3034   ** [database connection] D failed, then the sqlite3_errcode(D) interface
  3019   3035   ** returns the numeric [result code] or [extended result code] for that
  3020   3036   ** API call.
  3021   3037   ** If the most recent API call was successful,
  3022   3038   ** then the return value from sqlite3_errcode() is undefined.
................................................................................
  3054   3070   int sqlite3_errcode(sqlite3 *db);
  3055   3071   int sqlite3_extended_errcode(sqlite3 *db);
  3056   3072   const char *sqlite3_errmsg(sqlite3*);
  3057   3073   const void *sqlite3_errmsg16(sqlite3*);
  3058   3074   const char *sqlite3_errstr(int);
  3059   3075   
  3060   3076   /*
  3061         -** CAPI3REF: SQL Statement Object
         3077  +** CAPI3REF: Prepared Statement Object
  3062   3078   ** KEYWORDS: {prepared statement} {prepared statements}
  3063   3079   **
  3064         -** An instance of this object represents a single SQL statement.
  3065         -** This object is variously known as a "prepared statement" or a
  3066         -** "compiled SQL statement" or simply as a "statement".
         3080  +** An instance of this object represents a single SQL statement that
         3081  +** has been compiled into binary form and is ready to be evaluated.
  3067   3082   **
  3068         -** The life of a statement object goes something like this:
         3083  +** Think of each SQL statement as a separate computer program.  The
         3084  +** original SQL text is source code.  A prepared statement object 
         3085  +** is the compiled object code.  All SQL must be converted into a
         3086  +** prepared statement before it can be run.
         3087  +**
         3088  +** The life-cycle of a prepared statement object usually goes like this:
  3069   3089   **
  3070   3090   ** <ol>
  3071         -** <li> Create the object using [sqlite3_prepare_v2()] or a related
  3072         -**      function.
  3073         -** <li> Bind values to [host parameters] using the sqlite3_bind_*()
         3091  +** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
         3092  +** <li> Bind values to [parameters] using the sqlite3_bind_*()
  3074   3093   **      interfaces.
  3075   3094   ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
  3076         -** <li> Reset the statement using [sqlite3_reset()] then go back
         3095  +** <li> Reset the prepared statement using [sqlite3_reset()] then go back
  3077   3096   **      to step 2.  Do this zero or more times.
  3078   3097   ** <li> Destroy the object using [sqlite3_finalize()].
  3079   3098   ** </ol>
  3080         -**
  3081         -** Refer to documentation on individual methods above for additional
  3082         -** information.
  3083   3099   */
  3084   3100   typedef struct sqlite3_stmt sqlite3_stmt;
  3085   3101   
  3086   3102   /*
  3087   3103   ** CAPI3REF: Run-time Limits
         3104  +** METHOD: sqlite3
  3088   3105   **
  3089   3106   ** ^(This interface allows the size of various constructs to be limited
  3090   3107   ** on a connection by connection basis.  The first parameter is the
  3091   3108   ** [database connection] whose limit is to be set or queried.  The
  3092   3109   ** second parameter is one of the [limit categories] that define a
  3093   3110   ** class of constructs to be size limited.  The third parameter is the
  3094   3111   ** new limit for that construct.)^
................................................................................
  3192   3209   #define SQLITE_LIMIT_VARIABLE_NUMBER           9
  3193   3210   #define SQLITE_LIMIT_TRIGGER_DEPTH            10
  3194   3211   #define SQLITE_LIMIT_WORKER_THREADS           11
  3195   3212   
  3196   3213   /*
  3197   3214   ** CAPI3REF: Compiling An SQL Statement
  3198   3215   ** KEYWORDS: {SQL statement compiler}
         3216  +** METHOD: sqlite3
         3217  +** CONSTRUCTOR: sqlite3_stmt
  3199   3218   **
  3200   3219   ** To execute an SQL query, it must first be compiled into a byte-code
  3201   3220   ** program using one of these routines.
  3202   3221   **
  3203   3222   ** The first argument, "db", is a [database connection] obtained from a
  3204   3223   ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
  3205   3224   ** [sqlite3_open16()].  The database connection must not have been closed.
................................................................................
  3299   3318     int nByte,              /* Maximum length of zSql in bytes. */
  3300   3319     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3301   3320     const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  3302   3321   );
  3303   3322   
  3304   3323   /*
  3305   3324   ** CAPI3REF: Retrieving Statement SQL
         3325  +** METHOD: sqlite3_stmt
  3306   3326   **
  3307   3327   ** ^This interface can be used to retrieve a saved copy of the original
  3308   3328   ** SQL text used to create a [prepared statement] if that statement was
  3309   3329   ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  3310   3330   */
  3311   3331   const char *sqlite3_sql(sqlite3_stmt *pStmt);
  3312   3332   
  3313   3333   /*
  3314   3334   ** CAPI3REF: Determine If An SQL Statement Writes The Database
         3335  +** METHOD: sqlite3_stmt
  3315   3336   **
  3316   3337   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3317   3338   ** and only if the [prepared statement] X makes no direct changes to
  3318   3339   ** the content of the database file.
  3319   3340   **
  3320   3341   ** Note that [application-defined SQL functions] or
  3321   3342   ** [virtual tables] might change the database indirectly as a side effect.  
................................................................................
  3339   3360   ** change the configuration of a database connection, they do not make 
  3340   3361   ** changes to the content of the database files on disk.
  3341   3362   */
  3342   3363   int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  3343   3364   
  3344   3365   /*
  3345   3366   ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
         3367  +** METHOD: sqlite3_stmt
  3346   3368   **
  3347   3369   ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  3348   3370   ** [prepared statement] S has been stepped at least once using 
  3349   3371   ** [sqlite3_step(S)] but has not run to completion and/or has not 
  3350   3372   ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
  3351   3373   ** interface returns false if S is a NULL pointer.  If S is not a 
  3352   3374   ** NULL pointer and is not a pointer to a valid [prepared statement]
................................................................................
  3413   3435   */
  3414   3436   typedef struct sqlite3_context sqlite3_context;
  3415   3437   
  3416   3438   /*
  3417   3439   ** CAPI3REF: Binding Values To Prepared Statements
  3418   3440   ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
  3419   3441   ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
         3442  +** METHOD: sqlite3_stmt
  3420   3443   **
  3421   3444   ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
  3422   3445   ** literals may be replaced by a [parameter] that matches one of following
  3423   3446   ** templates:
  3424   3447   **
  3425   3448   ** <ul>
  3426   3449   ** <li>  ?
................................................................................
  3531   3554   int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
  3532   3555                            void(*)(void*), unsigned char encoding);
  3533   3556   int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3534   3557   int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3535   3558   
  3536   3559   /*
  3537   3560   ** CAPI3REF: Number Of SQL Parameters
         3561  +** METHOD: sqlite3_stmt
  3538   3562   **
  3539   3563   ** ^This routine can be used to find the number of [SQL parameters]
  3540   3564   ** in a [prepared statement].  SQL parameters are tokens of the
  3541   3565   ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
  3542   3566   ** placeholders for values that are [sqlite3_bind_blob | bound]
  3543   3567   ** to the parameters at a later time.
  3544   3568   **
................................................................................
  3551   3575   ** [sqlite3_bind_parameter_name()], and
  3552   3576   ** [sqlite3_bind_parameter_index()].
  3553   3577   */
  3554   3578   int sqlite3_bind_parameter_count(sqlite3_stmt*);
  3555   3579   
  3556   3580   /*
  3557   3581   ** CAPI3REF: Name Of A Host Parameter
         3582  +** METHOD: sqlite3_stmt
  3558   3583   **
  3559   3584   ** ^The sqlite3_bind_parameter_name(P,N) interface returns
  3560   3585   ** the name of the N-th [SQL parameter] in the [prepared statement] P.
  3561   3586   ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3562   3587   ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3563   3588   ** respectively.
  3564   3589   ** In other words, the initial ":" or "$" or "@" or "?"
................................................................................
  3578   3603   ** [sqlite3_bind_parameter_count()], and
  3579   3604   ** [sqlite3_bind_parameter_index()].
  3580   3605   */
  3581   3606   const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  3582   3607   
  3583   3608   /*
  3584   3609   ** CAPI3REF: Index Of A Parameter With A Given Name
         3610  +** METHOD: sqlite3_stmt
  3585   3611   **
  3586   3612   ** ^Return the index of an SQL parameter given its name.  ^The
  3587   3613   ** index value returned is suitable for use as the second
  3588   3614   ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
  3589   3615   ** is returned if no matching parameter is found.  ^The parameter
  3590   3616   ** name must be given in UTF-8 even if the original statement
  3591   3617   ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
................................................................................
  3594   3620   ** [sqlite3_bind_parameter_count()], and
  3595   3621   ** [sqlite3_bind_parameter_index()].
  3596   3622   */
  3597   3623   int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
  3598   3624   
  3599   3625   /*
  3600   3626   ** CAPI3REF: Reset All Bindings On A Prepared Statement
         3627  +** METHOD: sqlite3_stmt
  3601   3628   **
  3602   3629   ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
  3603   3630   ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
  3604   3631   ** ^Use this routine to reset all host parameters to NULL.
  3605   3632   */
  3606   3633   int sqlite3_clear_bindings(sqlite3_stmt*);
  3607   3634   
  3608   3635   /*
  3609   3636   ** CAPI3REF: Number Of Columns In A Result Set
         3637  +** METHOD: sqlite3_stmt
  3610   3638   **
  3611   3639   ** ^Return the number of columns in the result set returned by the
  3612   3640   ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  3613   3641   ** statement that does not return data (for example an [UPDATE]).
  3614   3642   **
  3615   3643   ** See also: [sqlite3_data_count()]
  3616   3644   */
  3617   3645   int sqlite3_column_count(sqlite3_stmt *pStmt);
  3618   3646   
  3619   3647   /*
  3620   3648   ** CAPI3REF: Column Names In A Result Set
         3649  +** METHOD: sqlite3_stmt
  3621   3650   **
  3622   3651   ** ^These routines return the name assigned to a particular column
  3623   3652   ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
  3624   3653   ** interface returns a pointer to a zero-terminated UTF-8 string
  3625   3654   ** and sqlite3_column_name16() returns a pointer to a zero-terminated
  3626   3655   ** UTF-16 string.  ^The first parameter is the [prepared statement]
  3627   3656   ** that implements the [SELECT] statement. ^The second parameter is the
................................................................................
  3643   3672   ** one release of SQLite to the next.
  3644   3673   */
  3645   3674   const char *sqlite3_column_name(sqlite3_stmt*, int N);
  3646   3675   const void *sqlite3_column_name16(sqlite3_stmt*, int N);
  3647   3676   
  3648   3677   /*
  3649   3678   ** CAPI3REF: Source Of Data In A Query Result
         3679  +** METHOD: sqlite3_stmt
  3650   3680   **
  3651   3681   ** ^These routines provide a means to determine the database, table, and
  3652   3682   ** table column that is the origin of a particular result column in
  3653   3683   ** [SELECT] statement.
  3654   3684   ** ^The name of the database or table or column can be returned as
  3655   3685   ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
  3656   3686   ** the database name, the _table_ routines return the table name, and
................................................................................
  3695   3725   const char *sqlite3_column_table_name(sqlite3_stmt*,int);
  3696   3726   const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
  3697   3727   const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
  3698   3728   const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
  3699   3729   
  3700   3730   /*
  3701   3731   ** CAPI3REF: Declared Datatype Of A Query Result
         3732  +** METHOD: sqlite3_stmt
  3702   3733   **
  3703   3734   ** ^(The first parameter is a [prepared statement].
  3704   3735   ** If this statement is a [SELECT] statement and the Nth column of the
  3705   3736   ** returned result set of that [SELECT] is a table column (not an
  3706   3737   ** expression or subquery) then the declared type of the table
  3707   3738   ** column is returned.)^  ^If the Nth column of the result set is an
  3708   3739   ** expression or subquery, then a NULL pointer is returned.
................................................................................
  3727   3758   ** used to hold those values.
  3728   3759   */
  3729   3760   const char *sqlite3_column_decltype(sqlite3_stmt*,int);
  3730   3761   const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
  3731   3762   
  3732   3763   /*
  3733   3764   ** CAPI3REF: Evaluate An SQL Statement
         3765  +** METHOD: sqlite3_stmt
  3734   3766   **
  3735   3767   ** After a [prepared statement] has been prepared using either
  3736   3768   ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
  3737   3769   ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
  3738   3770   ** must be called one or more times to evaluate the statement.
  3739   3771   **
  3740   3772   ** The details of the behavior of the sqlite3_step() interface depend
................................................................................
  3806   3838   ** then the more specific [error codes] are returned directly
  3807   3839   ** by sqlite3_step().  The use of the "v2" interface is recommended.
  3808   3840   */
  3809   3841   int sqlite3_step(sqlite3_stmt*);
  3810   3842   
  3811   3843   /*
  3812   3844   ** CAPI3REF: Number of columns in a result set
         3845  +** METHOD: sqlite3_stmt
  3813   3846   **
  3814   3847   ** ^The sqlite3_data_count(P) interface returns the number of columns in the
  3815   3848   ** current row of the result set of [prepared statement] P.
  3816   3849   ** ^If prepared statement P does not have results ready to return
  3817   3850   ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  3818   3851   ** interfaces) then sqlite3_data_count(P) returns 0.
  3819   3852   ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
................................................................................
  3859   3892   # define SQLITE_TEXT     3
  3860   3893   #endif
  3861   3894   #define SQLITE3_TEXT     3
  3862   3895   
  3863   3896   /*
  3864   3897   ** CAPI3REF: Result Values From A Query
  3865   3898   ** KEYWORDS: {column access functions}
         3899  +** METHOD: sqlite3_stmt
  3866   3900   **
  3867   3901   ** These routines form the "result set" interface.
  3868   3902   **
  3869   3903   ** ^These routines return information about a single column of the current
  3870   3904   ** result row of a query.  ^In every case the first argument is a pointer
  3871   3905   ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  3872   3906   ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
................................................................................
  4031   4065   const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  4032   4066   const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
  4033   4067   int sqlite3_column_type(sqlite3_stmt*, int iCol);
  4034   4068   sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
  4035   4069   
  4036   4070   /*
  4037   4071   ** CAPI3REF: Destroy A Prepared Statement Object
         4072  +** DESTRUCTOR: sqlite3_stmt
  4038   4073   **
  4039   4074   ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
  4040   4075   ** ^If the most recent evaluation of the statement encountered no errors
  4041   4076   ** or if the statement is never been evaluated, then sqlite3_finalize() returns
  4042   4077   ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
  4043   4078   ** sqlite3_finalize(S) returns the appropriate [error code] or
  4044   4079   ** [extended error code].
................................................................................
  4058   4093   ** statement after it has been finalized can result in undefined and
  4059   4094   ** undesirable behavior such as segfaults and heap corruption.
  4060   4095   */
  4061   4096   int sqlite3_finalize(sqlite3_stmt *pStmt);
  4062   4097   
  4063   4098   /*
  4064   4099   ** CAPI3REF: Reset A Prepared Statement Object
         4100  +** METHOD: sqlite3_stmt
  4065   4101   **
  4066   4102   ** The sqlite3_reset() function is called to reset a [prepared statement]
  4067   4103   ** object back to its initial state, ready to be re-executed.
  4068   4104   ** ^Any SQL statement variables that had values bound to them using
  4069   4105   ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
  4070   4106   ** Use [sqlite3_clear_bindings()] to reset the bindings.
  4071   4107   **
................................................................................
  4087   4123   int sqlite3_reset(sqlite3_stmt *pStmt);
  4088   4124   
  4089   4125   /*
  4090   4126   ** CAPI3REF: Create Or Redefine SQL Functions
  4091   4127   ** KEYWORDS: {function creation routines}
  4092   4128   ** KEYWORDS: {application-defined SQL function}
  4093   4129   ** KEYWORDS: {application-defined SQL functions}
         4130  +** METHOD: sqlite3
  4094   4131   **
  4095   4132   ** ^These functions (collectively known as "function creation routines")
  4096   4133   ** are used to add SQL functions or aggregates or to redefine the behavior
  4097   4134   ** of existing SQL functions or aggregates.  The only differences between
  4098   4135   ** these routines are the text encoding expected for
  4099   4136   ** the second parameter (the name of the function being created)
  4100   4137   ** and the presence or absence of a destructor callback for
................................................................................
  4256   4293   SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
  4257   4294   SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
  4258   4295                         void*,sqlite3_int64);
  4259   4296   #endif
  4260   4297   
  4261   4298   /*
  4262   4299   ** CAPI3REF: Obtaining SQL Function Parameter Values
         4300  +** METHOD: sqlite3_value
  4263   4301   **
  4264   4302   ** The C-language implementation of SQL functions and aggregates uses
  4265   4303   ** this set of interface routines to access the parameter values on
  4266   4304   ** the function or aggregate.
  4267   4305   **
  4268   4306   ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  4269   4307   ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
................................................................................
  4314   4352   const void *sqlite3_value_text16le(sqlite3_value*);
  4315   4353   const void *sqlite3_value_text16be(sqlite3_value*);
  4316   4354   int sqlite3_value_type(sqlite3_value*);
  4317   4355   int sqlite3_value_numeric_type(sqlite3_value*);
  4318   4356   
  4319   4357   /*
  4320   4358   ** CAPI3REF: Obtain Aggregate Function Context
         4359  +** METHOD: sqlite3_context
  4321   4360   **
  4322   4361   ** Implementations of aggregate SQL functions use this
  4323   4362   ** routine to allocate memory for storing their state.
  4324   4363   **
  4325   4364   ** ^The first time the sqlite3_aggregate_context(C,N) routine is called 
  4326   4365   ** for a particular aggregate function, SQLite
  4327   4366   ** allocates N of memory, zeroes out that memory, and returns a pointer
................................................................................
  4358   4397   ** This routine must be called from the same thread in which
  4359   4398   ** the aggregate SQL function is running.
  4360   4399   */
  4361   4400   void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
  4362   4401   
  4363   4402   /*
  4364   4403   ** CAPI3REF: User Data For Functions
         4404  +** METHOD: sqlite3_context
  4365   4405   **
  4366   4406   ** ^The sqlite3_user_data() interface returns a copy of
  4367   4407   ** the pointer that was the pUserData parameter (the 5th parameter)
  4368   4408   ** of the [sqlite3_create_function()]
  4369   4409   ** and [sqlite3_create_function16()] routines that originally
  4370   4410   ** registered the application defined function.
  4371   4411   **
................................................................................
  4372   4412   ** This routine must be called from the same thread in which
  4373   4413   ** the application-defined function is running.
  4374   4414   */
  4375   4415   void *sqlite3_user_data(sqlite3_context*);
  4376   4416   
  4377   4417   /*
  4378   4418   ** CAPI3REF: Database Connection For Functions
         4419  +** METHOD: sqlite3_context
  4379   4420   **
  4380   4421   ** ^The sqlite3_context_db_handle() interface returns a copy of
  4381   4422   ** the pointer to the [database connection] (the 1st parameter)
  4382   4423   ** of the [sqlite3_create_function()]
  4383   4424   ** and [sqlite3_create_function16()] routines that originally
  4384   4425   ** registered the application defined function.
  4385   4426   */
  4386   4427   sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
  4387   4428   
  4388   4429   /*
  4389   4430   ** CAPI3REF: Function Auxiliary Data
         4431  +** METHOD: sqlite3_context
  4390   4432   **
  4391   4433   ** These functions may be used by (non-aggregate) SQL functions to
  4392   4434   ** associate metadata with argument values. If the same value is passed to
  4393   4435   ** multiple invocations of the same SQL function during query execution, under
  4394   4436   ** some circumstances the associated metadata may be preserved.  An example
  4395   4437   ** of where this might be useful is in a regular-expression matching
  4396   4438   ** function. The compiled version of the regular expression can be stored as
................................................................................
  4455   4497   */
  4456   4498   typedef void (*sqlite3_destructor_type)(void*);
  4457   4499   #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
  4458   4500   #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
  4459   4501   
  4460   4502   /*
  4461   4503   ** CAPI3REF: Setting The Result Of An SQL Function
         4504  +** METHOD: sqlite3_context
  4462   4505   **
  4463   4506   ** These routines are used by the xFunc or xFinal callbacks that
  4464   4507   ** implement SQL functions and aggregates.  See
  4465   4508   ** [sqlite3_create_function()] and [sqlite3_create_function16()]
  4466   4509   ** for additional information.
  4467   4510   **
  4468   4511   ** These functions work very much like the [parameter binding] family of
................................................................................
  4590   4633   void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  4591   4634   void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  4592   4635   void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  4593   4636   void sqlite3_result_zeroblob(sqlite3_context*, int n);
  4594   4637   
  4595   4638   /*
  4596   4639   ** CAPI3REF: Define New Collating Sequences
         4640  +** METHOD: sqlite3
  4597   4641   **
  4598   4642   ** ^These functions add, remove, or modify a [collation] associated
  4599   4643   ** with the [database connection] specified as the first argument.
  4600   4644   **
  4601   4645   ** ^The name of the collation is a UTF-8 string
  4602   4646   ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
  4603   4647   ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
................................................................................
  4692   4736     int eTextRep, 
  4693   4737     void *pArg,
  4694   4738     int(*xCompare)(void*,int,const void*,int,const void*)
  4695   4739   );
  4696   4740   
  4697   4741   /*
  4698   4742   ** CAPI3REF: Collation Needed Callbacks
         4743  +** METHOD: sqlite3
  4699   4744   **
  4700   4745   ** ^To avoid having to register all collation sequences before a database
  4701   4746   ** can be used, a single callback function may be registered with the
  4702   4747   ** [database connection] to be invoked whenever an undefined collation
  4703   4748   ** sequence is required.
  4704   4749   **
  4705   4750   ** ^If the function is registered using the sqlite3_collation_needed() API,
................................................................................
  4899   4944   ** or else the use of the [data_store_directory pragma] should be avoided.
  4900   4945   */
  4901   4946   SQLITE_EXTERN char *sqlite3_data_directory;
  4902   4947   
  4903   4948   /*
  4904   4949   ** CAPI3REF: Test For Auto-Commit Mode
  4905   4950   ** KEYWORDS: {autocommit mode}
         4951  +** METHOD: sqlite3
  4906   4952   **
  4907   4953   ** ^The sqlite3_get_autocommit() interface returns non-zero or
  4908   4954   ** zero if the given database connection is or is not in autocommit mode,
  4909   4955   ** respectively.  ^Autocommit mode is on by default.
  4910   4956   ** ^Autocommit mode is disabled by a [BEGIN] statement.
  4911   4957   ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
  4912   4958   **
................................................................................
  4921   4967   ** connection while this routine is running, then the return value
  4922   4968   ** is undefined.
  4923   4969   */
  4924   4970   int sqlite3_get_autocommit(sqlite3*);
  4925   4971   
  4926   4972   /*
  4927   4973   ** CAPI3REF: Find The Database Handle Of A Prepared Statement
         4974  +** METHOD: sqlite3_stmt
  4928   4975   **
  4929   4976   ** ^The sqlite3_db_handle interface returns the [database connection] handle
  4930   4977   ** to which a [prepared statement] belongs.  ^The [database connection]
  4931   4978   ** returned by sqlite3_db_handle is the same [database connection]
  4932   4979   ** that was the first argument
  4933   4980   ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  4934   4981   ** create the statement in the first place.
  4935   4982   */
  4936   4983   sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
  4937   4984   
  4938   4985   /*
  4939   4986   ** CAPI3REF: Return The Filename For A Database Connection
         4987  +** METHOD: sqlite3
  4940   4988   **
  4941   4989   ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  4942   4990   ** associated with database N of connection D.  ^The main database file
  4943   4991   ** has the name "main".  If there is no attached database N on the database
  4944   4992   ** connection D, or if database N is a temporary or in-memory database, then
  4945   4993   ** a NULL pointer is returned.
  4946   4994   **
................................................................................
  4949   4997   ** will be an absolute pathname, even if the filename used
  4950   4998   ** to open the database originally was a URI or relative pathname.
  4951   4999   */
  4952   5000   const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
  4953   5001   
  4954   5002   /*
  4955   5003   ** CAPI3REF: Determine if a database is read-only
         5004  +** METHOD: sqlite3
  4956   5005   **
  4957   5006   ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
  4958   5007   ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
  4959   5008   ** the name of a database on connection D.
  4960   5009   */
  4961   5010   int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
  4962   5011   
  4963   5012   /*
  4964   5013   ** CAPI3REF: Find the next prepared statement
         5014  +** METHOD: sqlite3
  4965   5015   **
  4966   5016   ** ^This interface returns a pointer to the next [prepared statement] after
  4967   5017   ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
  4968   5018   ** then this interface returns a pointer to the first prepared statement
  4969   5019   ** associated with the database connection pDb.  ^If no prepared statement
  4970   5020   ** satisfies the conditions of this routine, it returns NULL.
  4971   5021   **
................................................................................
  4973   5023   ** [sqlite3_next_stmt(D,S)] must refer to an open database
  4974   5024   ** connection and in particular must not be a NULL pointer.
  4975   5025   */
  4976   5026   sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
  4977   5027   
  4978   5028   /*
  4979   5029   ** CAPI3REF: Commit And Rollback Notification Callbacks
         5030  +** METHOD: sqlite3
  4980   5031   **
  4981   5032   ** ^The sqlite3_commit_hook() interface registers a callback
  4982   5033   ** function to be invoked whenever a transaction is [COMMIT | committed].
  4983   5034   ** ^Any callback set by a previous call to sqlite3_commit_hook()
  4984   5035   ** for the same database connection is overridden.
  4985   5036   ** ^The sqlite3_rollback_hook() interface registers a callback
  4986   5037   ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
................................................................................
  5022   5073   ** See also the [sqlite3_update_hook()] interface.
  5023   5074   */
  5024   5075   void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
  5025   5076   void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  5026   5077   
  5027   5078   /*
  5028   5079   ** CAPI3REF: Data Change Notification Callbacks
         5080  +** METHOD: sqlite3
  5029   5081   **
  5030   5082   ** ^The sqlite3_update_hook() interface registers a callback function
  5031   5083   ** with the [database connection] identified by the first argument
  5032   5084   ** to be invoked whenever a row is updated, inserted or deleted in
  5033   5085   ** a rowid table.
  5034   5086   ** ^Any callback set by a previous call to this function
  5035   5087   ** for the same database connection is overridden.
................................................................................
  5128   5180   **
  5129   5181   ** See also: [sqlite3_db_release_memory()]
  5130   5182   */
  5131   5183   int sqlite3_release_memory(int);
  5132   5184   
  5133   5185   /*
  5134   5186   ** CAPI3REF: Free Memory Used By A Database Connection
         5187  +** METHOD: sqlite3
  5135   5188   **
  5136   5189   ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
  5137   5190   ** memory as possible from database connection D. Unlike the
  5138   5191   ** [sqlite3_release_memory()] interface, this interface is in effect even
  5139   5192   ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
  5140   5193   ** omitted.
  5141   5194   **
................................................................................
  5205   5258   ** [sqlite3_soft_heap_limit64()] interface rather than this one.
  5206   5259   */
  5207   5260   SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
  5208   5261   
  5209   5262   
  5210   5263   /*
  5211   5264   ** CAPI3REF: Extract Metadata About A Column Of A Table
         5265  +** METHOD: sqlite3
  5212   5266   **
  5213   5267   ** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
  5214   5268   ** information about column C of table T in database D
  5215   5269   ** on [database connection] X.)^  ^The sqlite3_table_column_metadata()
  5216   5270   ** interface returns SQLITE_OK and fills in the non-NULL pointers in
  5217   5271   ** the final five arguments with appropriate values if the specified
  5218   5272   ** column exists.  ^The sqlite3_table_column_metadata() interface returns
................................................................................
  5283   5337     int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  5284   5338     int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  5285   5339     int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  5286   5340   );
  5287   5341   
  5288   5342   /*
  5289   5343   ** CAPI3REF: Load An Extension
         5344  +** METHOD: sqlite3
  5290   5345   **
  5291   5346   ** ^This interface loads an SQLite extension library from the named file.
  5292   5347   **
  5293   5348   ** ^The sqlite3_load_extension() interface attempts to load an
  5294   5349   ** [SQLite extension] library contained in the file zFile.  If
  5295   5350   ** the file cannot be loaded directly, attempts are made to load
  5296   5351   ** with various operating-system specific extensions added.
................................................................................
  5324   5379     const char *zFile,    /* Name of the shared library containing extension */
  5325   5380     const char *zProc,    /* Entry point.  Derived from zFile if 0 */
  5326   5381     char **pzErrMsg       /* Put error message here if not 0 */
  5327   5382   );
  5328   5383   
  5329   5384   /*
  5330   5385   ** CAPI3REF: Enable Or Disable Extension Loading
         5386  +** METHOD: sqlite3
  5331   5387   **
  5332   5388   ** ^So as not to open security holes in older applications that are
  5333   5389   ** unprepared to deal with [extension loading], and as a means of disabling
  5334   5390   ** [extension loading] while evaluating user-entered SQL, the following API
  5335   5391   ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
  5336   5392   **
  5337   5393   ** ^Extension loading is off by default.
................................................................................
  5573   5629   #define SQLITE_INDEX_CONSTRAINT_LE    8
  5574   5630   #define SQLITE_INDEX_CONSTRAINT_LT    16
  5575   5631   #define SQLITE_INDEX_CONSTRAINT_GE    32
  5576   5632   #define SQLITE_INDEX_CONSTRAINT_MATCH 64
  5577   5633   
  5578   5634   /*
  5579   5635   ** CAPI3REF: Register A Virtual Table Implementation
         5636  +** METHOD: sqlite3
  5580   5637   **
  5581   5638   ** ^These routines are used to register a new [virtual table module] name.
  5582   5639   ** ^Module names must be registered before
  5583   5640   ** creating a new [virtual table] using the module and before using a
  5584   5641   ** preexisting [virtual table] for the module.
  5585   5642   **
  5586   5643   ** ^The module name is registered on the [database connection] specified
................................................................................
  5669   5726   ** to declare the format (the names and datatypes of the columns) of
  5670   5727   ** the virtual tables they implement.
  5671   5728   */
  5672   5729   int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
  5673   5730   
  5674   5731   /*
  5675   5732   ** CAPI3REF: Overload A Function For A Virtual Table
         5733  +** METHOD: sqlite3
  5676   5734   **
  5677   5735   ** ^(Virtual tables can provide alternative implementations of functions
  5678   5736   ** using the [xFindFunction] method of the [virtual table module].  
  5679   5737   ** But global versions of those functions
  5680   5738   ** must exist in order to be overloaded.)^
  5681   5739   **
  5682   5740   ** ^(This API makes sure a global version of a function with a particular
................................................................................
  5711   5769   ** can be used to read or write small subsections of the BLOB.
  5712   5770   ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
  5713   5771   */
  5714   5772   typedef struct sqlite3_blob sqlite3_blob;
  5715   5773   
  5716   5774   /*
  5717   5775   ** CAPI3REF: Open A BLOB For Incremental I/O
         5776  +** METHOD: sqlite3
         5777  +** CONSTRUCTOR: sqlite3_blob
  5718   5778   **
  5719   5779   ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
  5720   5780   ** in row iRow, column zColumn, table zTable in database zDb;
  5721   5781   ** in other words, the same BLOB that would be selected by:
  5722   5782   **
  5723   5783   ** <pre>
  5724   5784   **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
................................................................................
  5792   5852     sqlite3_int64 iRow,
  5793   5853     int flags,
  5794   5854     sqlite3_blob **ppBlob
  5795   5855   );
  5796   5856   
  5797   5857   /*
  5798   5858   ** CAPI3REF: Move a BLOB Handle to a New Row
         5859  +** METHOD: sqlite3_blob
  5799   5860   **
  5800   5861   ** ^This function is used to move an existing blob handle so that it points
  5801   5862   ** to a different row of the same database table. ^The new row is identified
  5802   5863   ** by the rowid value passed as the second argument. Only the row can be
  5803   5864   ** changed. ^The database, table and column on which the blob handle is open
  5804   5865   ** remain the same. Moving an existing blob handle to a new row can be
  5805   5866   ** faster than closing the existing handle and opening a new one.
................................................................................
  5816   5877   **
  5817   5878   ** ^This function sets the database handle error code and message.
  5818   5879   */
  5819   5880   SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
  5820   5881   
  5821   5882   /*
  5822   5883   ** CAPI3REF: Close A BLOB Handle
         5884  +** DESTRUCTOR: sqlite3_blob
  5823   5885   **
  5824   5886   ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
  5825   5887   ** unconditionally.  Even if this routine returns an error code, the 
  5826   5888   ** handle is still closed.)^
  5827   5889   **
  5828   5890   ** ^If the blob handle being closed was opened for read-write access, and if
  5829   5891   ** the database is in auto-commit mode and there are no other open read-write
................................................................................
  5838   5900   ** is passed a valid open blob handle, the values returned by the 
  5839   5901   ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
  5840   5902   */
  5841   5903   int sqlite3_blob_close(sqlite3_blob *);
  5842   5904   
  5843   5905   /*
  5844   5906   ** CAPI3REF: Return The Size Of An Open BLOB
         5907  +** METHOD: sqlite3_blob
  5845   5908   **
  5846   5909   ** ^Returns the size in bytes of the BLOB accessible via the 
  5847   5910   ** successfully opened [BLOB handle] in its only argument.  ^The
  5848   5911   ** incremental blob I/O routines can only read or overwriting existing
  5849   5912   ** blob content; they cannot change the size of a blob.
  5850   5913   **
  5851   5914   ** This routine only works on a [BLOB handle] which has been created
................................................................................
  5853   5916   ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  5854   5917   ** to this routine results in undefined and probably undesirable behavior.
  5855   5918   */
  5856   5919   int sqlite3_blob_bytes(sqlite3_blob *);
  5857   5920   
  5858   5921   /*
  5859   5922   ** CAPI3REF: Read Data From A BLOB Incrementally
         5923  +** METHOD: sqlite3_blob
  5860   5924   **
  5861   5925   ** ^(This function is used to read data from an open [BLOB handle] into a
  5862   5926   ** caller-supplied buffer. N bytes of data are copied into buffer Z
  5863   5927   ** from the open BLOB, starting at offset iOffset.)^
  5864   5928   **
  5865   5929   ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  5866   5930   ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
................................................................................
  5881   5945   **
  5882   5946   ** See also: [sqlite3_blob_write()].
  5883   5947   */
  5884   5948   int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
  5885   5949   
  5886   5950   /*
  5887   5951   ** CAPI3REF: Write Data Into A BLOB Incrementally
         5952  +** METHOD: sqlite3_blob
  5888   5953   **
  5889   5954   ** ^(This function is used to write data into an open [BLOB handle] from a
  5890   5955   ** caller-supplied buffer. N bytes of data are copied from the buffer Z
  5891   5956   ** into the open BLOB, starting at offset iOffset.)^
  5892   5957   **
  5893   5958   ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
  5894   5959   ** Otherwise, an  [error code] or an [extended error code] is returned.)^
................................................................................
  6208   6273   #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  6209   6274   #define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
  6210   6275   #define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
  6211   6276   #define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
  6212   6277   
  6213   6278   /*
  6214   6279   ** CAPI3REF: Retrieve the mutex for a database connection
         6280  +** METHOD: sqlite3
  6215   6281   **
  6216   6282   ** ^This interface returns a pointer the [sqlite3_mutex] object that 
  6217   6283   ** serializes access to the [database connection] given in the argument
  6218   6284   ** when the [threading mode] is Serialized.
  6219   6285   ** ^If the [threading mode] is Single-thread or Multi-thread then this
  6220   6286   ** routine returns a NULL pointer.
  6221   6287   */
  6222   6288   sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
  6223   6289   
  6224   6290   /*
  6225   6291   ** CAPI3REF: Low-Level Control Of Database Files
         6292  +** METHOD: sqlite3
  6226   6293   **
  6227   6294   ** ^The [sqlite3_file_control()] interface makes a direct call to the
  6228   6295   ** xFileControl method for the [sqlite3_io_methods] object associated
  6229   6296   ** with a particular database identified by the second argument. ^The
  6230   6297   ** name of the database is "main" for the main database or "temp" for the
  6231   6298   ** TEMP database, or the name that appears after the AS keyword for
  6232   6299   ** databases that are added using the [ATTACH] SQL command.
................................................................................
  6435   6502   #define SQLITE_STATUS_PARSER_STACK         6
  6436   6503   #define SQLITE_STATUS_PAGECACHE_SIZE       7
  6437   6504   #define SQLITE_STATUS_SCRATCH_SIZE         8
  6438   6505   #define SQLITE_STATUS_MALLOC_COUNT         9
  6439   6506   
  6440   6507   /*
  6441   6508   ** CAPI3REF: Database Connection Status
         6509  +** METHOD: sqlite3
  6442   6510   **
  6443   6511   ** ^This interface is used to retrieve runtime status information 
  6444   6512   ** about a single [database connection].  ^The first argument is the
  6445   6513   ** database connection object to be interrogated.  ^The second argument
  6446   6514   ** is an integer constant, taken from the set of
  6447   6515   ** [SQLITE_DBSTATUS options], that
  6448   6516   ** determines the parameter to interrogate.  The set of 
................................................................................
  6563   6631   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  6564   6632   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  6565   6633   #define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
  6566   6634   
  6567   6635   
  6568   6636   /*
  6569   6637   ** CAPI3REF: Prepared Statement Status
         6638  +** METHOD: sqlite3_stmt
  6570   6639   **
  6571   6640   ** ^(Each prepared statement maintains various
  6572   6641   ** [SQLITE_STMTSTATUS counters] that measure the number
  6573   6642   ** of times it has performed specific operations.)^  These counters can
  6574   6643   ** be used to monitor the performance characteristics of the prepared
  6575   6644   ** statements.  For example, if the number of table steps greatly exceeds
  6576   6645   ** the number of table searches or result rows, that would tend to indicate
................................................................................
  7066   7135   int sqlite3_backup_step(sqlite3_backup *p, int nPage);
  7067   7136   int sqlite3_backup_finish(sqlite3_backup *p);
  7068   7137   int sqlite3_backup_remaining(sqlite3_backup *p);
  7069   7138   int sqlite3_backup_pagecount(sqlite3_backup *p);
  7070   7139   
  7071   7140   /*
  7072   7141   ** CAPI3REF: Unlock Notification
         7142  +** METHOD: sqlite3
  7073   7143   **
  7074   7144   ** ^When running in shared-cache mode, a database operation may fail with
  7075   7145   ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
  7076   7146   ** individual tables within the shared-cache cannot be obtained. See
  7077   7147   ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
  7078   7148   ** ^This API may be used to register a callback that SQLite will invoke 
  7079   7149   ** when the connection currently holding the required lock relinquishes it.
................................................................................
  7236   7306   ** a few hundred characters, it will be truncated to the length of the
  7237   7307   ** buffer.
  7238   7308   */
  7239   7309   void sqlite3_log(int iErrCode, const char *zFormat, ...);
  7240   7310   
  7241   7311   /*
  7242   7312   ** CAPI3REF: Write-Ahead Log Commit Hook
         7313  +** METHOD: sqlite3
  7243   7314   **
  7244   7315   ** ^The [sqlite3_wal_hook()] function is used to register a callback that
  7245   7316   ** is invoked each time data is committed to a database in wal mode.
  7246   7317   **
  7247   7318   ** ^(The callback is invoked by SQLite after the commit has taken place and 
  7248   7319   ** the associated write-lock on the database released)^, so the implementation 
  7249   7320   ** may read, write or [checkpoint] the database as required.
................................................................................
  7275   7346     sqlite3*, 
  7276   7347     int(*)(void *,sqlite3*,const char*,int),
  7277   7348     void*
  7278   7349   );
  7279   7350   
  7280   7351   /*
  7281   7352   ** CAPI3REF: Configure an auto-checkpoint
         7353  +** METHOD: sqlite3
  7282   7354   **
  7283   7355   ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
  7284   7356   ** [sqlite3_wal_hook()] that causes any database on [database connection] D
  7285   7357   ** to automatically [checkpoint]
  7286   7358   ** after committing a transaction if there are N or
  7287   7359   ** more frames in the [write-ahead log] file.  ^Passing zero or 
  7288   7360   ** a negative value as the nFrame parameter disables automatic
................................................................................
  7305   7377   ** is only necessary if the default setting is found to be suboptimal
  7306   7378   ** for a particular application.
  7307   7379   */
  7308   7380   int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  7309   7381   
  7310   7382   /*
  7311   7383   ** CAPI3REF: Checkpoint a database
         7384  +** METHOD: sqlite3
  7312   7385   **
  7313   7386   ** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
  7314   7387   ** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
  7315   7388   **
  7316   7389   ** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the 
  7317   7390   ** [write-ahead log] for database X on [database connection] D to be
  7318   7391   ** transferred into the database file and for the write-ahead log to
................................................................................
  7326   7399   ** start a callback but which do not need the full power (and corresponding
  7327   7400   ** complication) of [sqlite3_wal_checkpoint_v2()].
  7328   7401   */
  7329   7402   int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
  7330   7403   
  7331   7404   /*
  7332   7405   ** CAPI3REF: Checkpoint a database
         7406  +** METHOD: sqlite3
  7333   7407   **
  7334   7408   ** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
  7335   7409   ** operation on database X of [database connection] D in mode M.  Status
  7336   7410   ** information is written back into integers pointed to by L and C.)^
  7337   7411   ** ^(The M parameter must be a valid [checkpoint mode]:)^
  7338   7412   **
  7339   7413   ** <dl>
................................................................................
  7580   7654   #define SQLITE_SCANSTAT_EST      2
  7581   7655   #define SQLITE_SCANSTAT_NAME     3
  7582   7656   #define SQLITE_SCANSTAT_EXPLAIN  4
  7583   7657   #define SQLITE_SCANSTAT_SELECTID 5
  7584   7658   
  7585   7659   /*
  7586   7660   ** CAPI3REF: Prepared Statement Scan Status
         7661  +** METHOD: sqlite3_stmt
  7587   7662   **
  7588   7663   ** This interface returns information about the predicted and measured
  7589   7664   ** performance for pStmt.  Advanced applications can use this
  7590   7665   ** interface to compare the predicted and the measured performance and
  7591   7666   ** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
  7592   7667   **
  7593   7668   ** Since this interface is expected to be rarely used, it is only
................................................................................
  7617   7692     int idx,                  /* Index of loop to report on */
  7618   7693     int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
  7619   7694     void *pOut                /* Result written here */
  7620   7695   );     
  7621   7696   
  7622   7697   /*
  7623   7698   ** CAPI3REF: Zero Scan-Status Counters
         7699  +** METHOD: sqlite3_stmt
  7624   7700   **
  7625   7701   ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
  7626   7702   **
  7627   7703   ** This API is only available if the library is built with pre-processor
  7628   7704   ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
  7629   7705   */
  7630   7706   SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);

Changes to src/sqliteInt.h.

  1222   1222   #define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
  1223   1223   #define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
  1224   1224   #define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
  1225   1225   #define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
  1226   1226   #define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
  1227   1227   #define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
  1228   1228   #define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
         1229  +#define SQLITE_Vacuum         0x08000000  /* Currently in a VACUUM */
  1229   1230   
  1230   1231   
  1231   1232   /*
  1232   1233   ** Bits of the sqlite3.dbOptFlags field that are used by the
  1233   1234   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
  1234   1235   ** selectively disable various optimizations.
  1235   1236   */

Changes to src/tokenize.c.

   426    426             sqlite3ErrorMsg(pParse, "interrupt");
   427    427             pParse->rc = SQLITE_INTERRUPT;
   428    428             goto abort_parse;
   429    429           }
   430    430           break;
   431    431         }
   432    432         case TK_ILLEGAL: {
   433         -        sqlite3DbFree(db, *pzErrMsg);
   434         -        *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
          433  +        sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
   435    434                           &pParse->sLastToken);
   436         -        nErr++;
   437    435           goto abort_parse;
   438    436         }
   439    437         case TK_SEMI: {
   440    438           pParse->zTail = &zSql[i];
   441    439           /* Fall thru into the default case */
   442    440         }
   443    441         default: {
................................................................................
   447    445             goto abort_parse;
   448    446           }
   449    447           break;
   450    448         }
   451    449       }
   452    450     }
   453    451   abort_parse:
   454         -  if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
          452  +  assert( nErr==0 );
          453  +  if( zSql[i]==0 && pParse->rc==SQLITE_OK ){
   455    454       if( lastTokenParsed!=TK_SEMI ){
   456    455         sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
   457    456         pParse->zTail = &zSql[i];
   458    457       }
   459    458       sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
   460    459     }
   461    460   #ifdef YYTRACKMAXSTACKDEPTH

Changes to src/trigger.c.

   676    676   ){
   677    677     int iDb;             /* Index of the database to use */
   678    678     SrcList *pSrc;       /* SrcList to be returned */
   679    679   
   680    680     pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
   681    681     if( pSrc ){
   682    682       assert( pSrc->nSrc>0 );
   683         -    assert( pSrc->a!=0 );
   684    683       iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
   685    684       if( iDb==0 || iDb>=2 ){
   686    685         sqlite3 *db = pParse->db;
   687    686         assert( iDb<pParse->db->nDb );
   688    687         pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
   689    688       }
   690    689     }

Changes to src/vacuum.c.

   246    246         "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
   247    247     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   248    248   
   249    249     /* Loop through the tables in the main database. For each, do
   250    250     ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
   251    251     ** the contents to the temporary database.
   252    252     */
          253  +  assert( (db->flags & SQLITE_Vacuum)==0 );
          254  +  db->flags |= SQLITE_Vacuum;
   253    255     rc = execExecSql(db, pzErrMsg,
   254    256         "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
   255    257         "|| ' SELECT * FROM main.' || quote(name) || ';'"
   256    258         "FROM main.sqlite_master "
   257    259         "WHERE type = 'table' AND name!='sqlite_sequence' "
   258    260         "  AND coalesce(rootpage,1)>0"
   259    261     );
          262  +  assert( (db->flags & SQLITE_Vacuum)!=0 );
          263  +  db->flags &= ~SQLITE_Vacuum;
   260    264     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   261    265   
   262    266     /* Copy over the sequence table
   263    267     */
   264    268     rc = execExecSql(db, pzErrMsg,
   265    269         "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
   266    270         "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "

Changes to src/vdbe.c.

   510    510     Savepoint *p;
   511    511     for(p=db->pSavepoint; p; p=p->pNext) n++;
   512    512     assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
   513    513     return 1;
   514    514   }
   515    515   #endif
   516    516   
          517  +/*
          518  +** Return the register of pOp->p2 after first preparing it to be
          519  +** overwritten with an integer value.
          520  +*/ 
          521  +static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
          522  +  Mem *pOut;
          523  +  assert( pOp->p2>0 );
          524  +  assert( pOp->p2<=(p->nMem-p->nCursor) );
          525  +  pOut = &p->aMem[pOp->p2];
          526  +  memAboutToChange(p, pOut);
          527  +  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
          528  +  pOut->flags = MEM_Int;
          529  +  return pOut;
          530  +}
          531  +
   517    532   
   518    533   /*
   519    534   ** Execute as much of a VDBE program as we can.
   520    535   ** This is the core of sqlite3_step().  
   521    536   */
   522    537   int sqlite3VdbeExec(
   523    538     Vdbe *p                    /* The VDBE */
   524    539   ){
   525         -  int pc=0;                  /* The program counter */
   526    540     Op *aOp = p->aOp;          /* Copy of p->aOp */
   527         -  Op *pOp;                   /* Current operation */
          541  +  Op *pOp = aOp;             /* Current operation */
   528    542     int rc = SQLITE_OK;        /* Value to return */
   529    543     sqlite3 *db = p->db;       /* The database */
   530    544     u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
   531    545     u8 encoding = ENC(db);     /* The database encoding */
   532    546     int iCompare = 0;          /* Result of last OP_Compare operation */
   533    547     unsigned nVmStep = 0;      /* Number of virtual machine steps */
   534    548   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
................................................................................
   596    610           }
   597    611         }
   598    612       }
   599    613       if( p->db->flags & SQLITE_VdbeTrace )  printf("VDBE Trace:\n");
   600    614     }
   601    615     sqlite3EndBenignMalloc();
   602    616   #endif
   603         -  for(pc=p->pc; rc==SQLITE_OK; pc++){
   604         -    assert( pc>=0 && pc<p->nOp );
          617  +  for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
          618  +    assert( pOp>=aOp && pOp<&aOp[p->nOp]);
   605    619       if( db->mallocFailed ) goto no_mem;
   606    620   #ifdef VDBE_PROFILE
   607    621       start = sqlite3Hwtime();
   608    622   #endif
   609    623       nVmStep++;
   610         -    pOp = &aOp[pc];
   611    624   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
   612         -    if( p->anExec ) p->anExec[pc]++;
          625  +    if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
   613    626   #endif
   614    627   
   615    628       /* Only allow tracing if SQLITE_DEBUG is defined.
   616    629       */
   617    630   #ifdef SQLITE_DEBUG
   618    631       if( db->flags & SQLITE_VdbeTrace ){
   619         -      sqlite3VdbePrintOp(stdout, pc, pOp);
          632  +      sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
   620    633       }
   621    634   #endif
   622    635         
   623    636   
   624    637       /* Check to see if we need to simulate an interrupt.  This only happens
   625    638       ** if we have a special test build.
   626    639       */
................................................................................
   629    642         sqlite3_interrupt_count--;
   630    643         if( sqlite3_interrupt_count==0 ){
   631    644           sqlite3_interrupt(db);
   632    645         }
   633    646       }
   634    647   #endif
   635    648   
   636         -    /* On any opcode with the "out2-prerelease" tag, free any
   637         -    ** external allocations out of mem[p2] and set mem[p2] to be
   638         -    ** an undefined integer.  Opcodes will either fill in the integer
   639         -    ** value or convert mem[p2] to a different type.
   640         -    */
   641         -    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   642         -    if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   643         -      assert( pOp->p2>0 );
   644         -      assert( pOp->p2<=(p->nMem-p->nCursor) );
   645         -      pOut = &aMem[pOp->p2];
   646         -      memAboutToChange(p, pOut);
   647         -      if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
   648         -      pOut->flags = MEM_Int;
   649         -    }
   650         -
   651    649       /* Sanity checking on other operands */
   652    650   #ifdef SQLITE_DEBUG
          651  +    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   653    652       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   654    653         assert( pOp->p1>0 );
   655    654         assert( pOp->p1<=(p->nMem-p->nCursor) );
   656    655         assert( memIsValid(&aMem[pOp->p1]) );
   657    656         assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
   658    657         REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
   659    658       }
................................................................................
   701    700   ** opcode and the opcodes.c file is filled with an array of strings where
   702    701   ** each string is the symbolic name for the corresponding opcode.  If the
   703    702   ** case statement is followed by a comment of the form "/# same as ... #/"
   704    703   ** that comment is used to determine the particular value of the opcode.
   705    704   **
   706    705   ** Other keywords in the comment that follows each case are used to
   707    706   ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
   708         -** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
          707  +** Keywords include: in1, in2, in3, out2, out3.  See
   709    708   ** the mkopcodeh.awk script for additional information.
   710    709   **
   711    710   ** Documentation about VDBE opcodes is generated by scanning this file
   712    711   ** for lines of that contain "Opcode:".  That line and all subsequent
   713    712   ** comment lines are used in the generation of the opcode.html documentation
   714    713   ** file.
   715    714   **
................................................................................
   729    728   **
   730    729   ** The P1 parameter is not actually used by this opcode.  However, it
   731    730   ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
   732    731   ** that this Goto is the bottom of a loop and that the lines from P2 down
   733    732   ** to the current line should be indented for EXPLAIN output.
   734    733   */
   735    734   case OP_Goto: {             /* jump */
   736         -  pc = pOp->p2 - 1;
          735  +jump_to_p2_and_check_for_interrupt:
          736  +  pOp = &aOp[pOp->p2 - 1];
   737    737   
   738    738     /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
   739    739     ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
   740    740     ** completion.  Check to see if sqlite3_interrupt() has been called
   741    741     ** or if the progress callback needs to be invoked. 
   742    742     **
   743    743     ** This code uses unstructured "goto" statements and does not look clean.
................................................................................
   774    774   */
   775    775   case OP_Gosub: {            /* jump */
   776    776     assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
   777    777     pIn1 = &aMem[pOp->p1];
   778    778     assert( VdbeMemDynamic(pIn1)==0 );
   779    779     memAboutToChange(p, pIn1);
   780    780     pIn1->flags = MEM_Int;
   781         -  pIn1->u.i = pc;
          781  +  pIn1->u.i = (int)(pOp-aOp);
   782    782     REGISTER_TRACE(pOp->p1, pIn1);
   783         -  pc = pOp->p2 - 1;
          783  +
          784  +  /* Most jump operations do a goto to this spot in order to update
          785  +  ** the pOp pointer. */
          786  +jump_to_p2:
          787  +  pOp = &aOp[pOp->p2 - 1];
   784    788     break;
   785    789   }
   786    790   
   787    791   /* Opcode:  Return P1 * * * *
   788    792   **
   789    793   ** Jump to the next instruction after the address in register P1.  After
   790    794   ** the jump, register P1 becomes undefined.
   791    795   */
   792    796   case OP_Return: {           /* in1 */
   793    797     pIn1 = &aMem[pOp->p1];
   794    798     assert( pIn1->flags==MEM_Int );
   795         -  pc = (int)pIn1->u.i;
          799  +  pOp = &aOp[pIn1->u.i];
   796    800     pIn1->flags = MEM_Undefined;
   797    801     break;
   798    802   }
   799    803   
   800    804   /* Opcode: InitCoroutine P1 P2 P3 * *
   801    805   **
   802    806   ** Set up register P1 so that it will Yield to the coroutine
................................................................................
   812    816     assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
   813    817     assert( pOp->p2>=0 && pOp->p2<p->nOp );
   814    818     assert( pOp->p3>=0 && pOp->p3<p->nOp );
   815    819     pOut = &aMem[pOp->p1];
   816    820     assert( !VdbeMemDynamic(pOut) );
   817    821     pOut->u.i = pOp->p3 - 1;
   818    822     pOut->flags = MEM_Int;
   819         -  if( pOp->p2 ) pc = pOp->p2 - 1;
          823  +  if( pOp->p2 ) goto jump_to_p2;
   820    824     break;
   821    825   }
   822    826   
   823    827   /* Opcode:  EndCoroutine P1 * * * *
   824    828   **
   825    829   ** The instruction at the address in register P1 is a Yield.
   826    830   ** Jump to the P2 parameter of that Yield.
................................................................................
   832    836     VdbeOp *pCaller;
   833    837     pIn1 = &aMem[pOp->p1];
   834    838     assert( pIn1->flags==MEM_Int );
   835    839     assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
   836    840     pCaller = &aOp[pIn1->u.i];
   837    841     assert( pCaller->opcode==OP_Yield );
   838    842     assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
   839         -  pc = pCaller->p2 - 1;
          843  +  pOp = &aOp[pCaller->p2 - 1];
   840    844     pIn1->flags = MEM_Undefined;
   841    845     break;
   842    846   }
   843    847   
   844    848   /* Opcode:  Yield P1 P2 * * *
   845    849   **
   846    850   ** Swap the program counter with the value in register P1.  This
................................................................................
   856    860   */
   857    861   case OP_Yield: {            /* in1, jump */
   858    862     int pcDest;
   859    863     pIn1 = &aMem[pOp->p1];
   860    864     assert( VdbeMemDynamic(pIn1)==0 );
   861    865     pIn1->flags = MEM_Int;
   862    866     pcDest = (int)pIn1->u.i;
   863         -  pIn1->u.i = pc;
          867  +  pIn1->u.i = (int)(pOp - aOp);
   864    868     REGISTER_TRACE(pOp->p1, pIn1);
   865         -  pc = pcDest;
          869  +  pOp = &aOp[pcDest];
   866    870     break;
   867    871   }
   868    872   
   869    873   /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
   870    874   ** Synopsis:  if r[P3]=null halt
   871    875   **
   872    876   ** Check the value in register P3.  If it is NULL then Halt using
................................................................................
   909    913   ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
   910    914   ** every program.  So a jump past the last instruction of the program
   911    915   ** is the same as executing Halt.
   912    916   */
   913    917   case OP_Halt: {
   914    918     const char *zType;
   915    919     const char *zLogFmt;
          920  +  VdbeFrame *pFrame;
          921  +  int pcx;
   916    922   
          923  +  pcx = (int)(pOp - aOp);
   917    924     if( pOp->p1==SQLITE_OK && p->pFrame ){
   918    925       /* Halt the sub-program. Return control to the parent frame. */
   919         -    VdbeFrame *pFrame = p->pFrame;
          926  +    pFrame = p->pFrame;
   920    927       p->pFrame = pFrame->pParent;
   921    928       p->nFrame--;
   922    929       sqlite3VdbeSetChanges(db, p->nChange);
   923         -    pc = sqlite3VdbeFrameRestore(pFrame);
          930  +    pcx = sqlite3VdbeFrameRestore(pFrame);
   924    931       lastRowid = db->lastRowid;
   925    932       if( pOp->p2==OE_Ignore ){
   926         -      /* Instruction pc is the OP_Program that invoked the sub-program 
          933  +      /* Instruction pcx is the OP_Program that invoked the sub-program 
   927    934         ** currently being halted. If the p2 instruction of this OP_Halt
   928    935         ** instruction is set to OE_Ignore, then the sub-program is throwing
   929    936         ** an IGNORE exception. In this case jump to the address specified
   930    937         ** as the p2 of the calling OP_Program.  */
   931         -      pc = p->aOp[pc].p2-1;
          938  +      pcx = p->aOp[pcx].p2-1;
   932    939       }
   933    940       aOp = p->aOp;
   934    941       aMem = p->aMem;
          942  +    pOp = &aOp[pcx];
   935    943       break;
   936    944     }
   937    945     p->rc = pOp->p1;
   938    946     p->errorAction = (u8)pOp->p2;
   939         -  p->pc = pc;
          947  +  p->pc = pcx;
   940    948     if( p->rc ){
   941    949       if( pOp->p5 ){
   942    950         static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
   943    951                                                "FOREIGN KEY" };
   944    952         assert( pOp->p5>=1 && pOp->p5<=4 );
   945    953         testcase( pOp->p5==1 );
   946    954         testcase( pOp->p5==2 );
................................................................................
   956    964         sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s", 
   957    965                          zType, pOp->p4.z);
   958    966       }else if( pOp->p4.z ){
   959    967         sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
   960    968       }else{
   961    969         sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
   962    970       }
   963         -    sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
          971  +    sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
   964    972     }
   965    973     rc = sqlite3VdbeHalt(p);
   966    974     assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
   967    975     if( rc==SQLITE_BUSY ){
   968    976       p->rc = rc = SQLITE_BUSY;
   969    977     }else{
   970    978       assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
   971    979       assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
   972    980       rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
   973    981     }
          982  +  pOp = &aOp[pcx];
   974    983     goto vdbe_return;
   975    984   }
   976    985   
   977    986   /* Opcode: Integer P1 P2 * * *
   978    987   ** Synopsis: r[P2]=P1
   979    988   **
   980    989   ** The 32-bit integer value P1 is written into register P2.
   981    990   */
   982         -case OP_Integer: {         /* out2-prerelease */
          991  +case OP_Integer: {         /* out2 */
          992  +  pOut = out2Prerelease(p, pOp);
   983    993     pOut->u.i = pOp->p1;
   984    994     break;
   985    995   }
   986    996   
   987    997   /* Opcode: Int64 * P2 * P4 *
   988    998   ** Synopsis: r[P2]=P4
   989    999   **
   990   1000   ** P4 is a pointer to a 64-bit integer value.
   991   1001   ** Write that value into register P2.
   992   1002   */
   993         -case OP_Int64: {           /* out2-prerelease */
         1003  +case OP_Int64: {           /* out2 */
         1004  +  pOut = out2Prerelease(p, pOp);
   994   1005     assert( pOp->p4.pI64!=0 );
   995   1006     pOut->u.i = *pOp->p4.pI64;
   996   1007     break;
   997   1008   }
   998   1009   
   999   1010   #ifndef SQLITE_OMIT_FLOATING_POINT
  1000   1011   /* Opcode: Real * P2 * P4 *
  1001   1012   ** Synopsis: r[P2]=P4
  1002   1013   **
  1003   1014   ** P4 is a pointer to a 64-bit floating point value.
  1004   1015   ** Write that value into register P2.
  1005   1016   */
  1006         -case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
         1017  +case OP_Real: {            /* same as TK_FLOAT, out2 */
         1018  +  pOut = out2Prerelease(p, pOp);
  1007   1019     pOut->flags = MEM_Real;
  1008   1020     assert( !sqlite3IsNaN(*pOp->p4.pReal) );
  1009   1021     pOut->u.r = *pOp->p4.pReal;
  1010   1022     break;
  1011   1023   }
  1012   1024   #endif
  1013   1025   
................................................................................
  1015   1027   ** Synopsis: r[P2]='P4'
  1016   1028   **
  1017   1029   ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
  1018   1030   ** into a String opcode before it is executed for the first time.  During
  1019   1031   ** this transformation, the length of string P4 is computed and stored
  1020   1032   ** as the P1 parameter.
  1021   1033   */
  1022         -case OP_String8: {         /* same as TK_STRING, out2-prerelease */
         1034  +case OP_String8: {         /* same as TK_STRING, out2 */
  1023   1035     assert( pOp->p4.z!=0 );
         1036  +  pOut = out2Prerelease(p, pOp);
  1024   1037     pOp->opcode = OP_String;
  1025   1038     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
  1026   1039   
  1027   1040   #ifndef SQLITE_OMIT_UTF16
  1028   1041     if( encoding!=SQLITE_UTF8 ){
  1029   1042       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
  1030   1043       if( rc==SQLITE_TOOBIG ) goto too_big;
................................................................................
  1053   1066   ** The string value P4 of length P1 (bytes) is stored in register P2.
  1054   1067   **
  1055   1068   ** If P5!=0 and the content of register P3 is greater than zero, then
  1056   1069   ** the datatype of the register P2 is converted to BLOB.  The content is
  1057   1070   ** the same sequence of bytes, it is merely interpreted as a BLOB instead
  1058   1071   ** of a string, as if it had been CAST.
  1059   1072   */
  1060         -case OP_String: {          /* out2-prerelease */
         1073  +case OP_String: {          /* out2 */
  1061   1074     assert( pOp->p4.z!=0 );
         1075  +  pOut = out2Prerelease(p, pOp);
  1062   1076     pOut->flags = MEM_Str|MEM_Static|MEM_Term;
  1063   1077     pOut->z = pOp->p4.z;
  1064   1078     pOut->n = pOp->p1;
  1065   1079     pOut->enc = encoding;
  1066   1080     UPDATE_MAX_BLOBSIZE(pOut);
  1067   1081     if( pOp->p5 ){
  1068   1082       assert( pOp->p3>0 );
................................................................................
  1082   1096   ** is less than P2 (typically P3 is zero) then only register P2 is
  1083   1097   ** set to NULL.
  1084   1098   **
  1085   1099   ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
  1086   1100   ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
  1087   1101   ** OP_Ne or OP_Eq.
  1088   1102   */
  1089         -case OP_Null: {           /* out2-prerelease */
         1103  +case OP_Null: {           /* out2 */
  1090   1104     int cnt;
  1091   1105     u16 nullFlag;
         1106  +  pOut = out2Prerelease(p, pOp);
  1092   1107     cnt = pOp->p3-pOp->p2;
  1093   1108     assert( pOp->p3<=(p->nMem-p->nCursor) );
  1094   1109     pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
  1095   1110     while( cnt>0 ){
  1096   1111       pOut++;
  1097   1112       memAboutToChange(p, pOut);
  1098   1113       sqlite3VdbeMemSetNull(pOut);
................................................................................
  1119   1134   
  1120   1135   /* Opcode: Blob P1 P2 * P4 *
  1121   1136   ** Synopsis: r[P2]=P4 (len=P1)
  1122   1137   **
  1123   1138   ** P4 points to a blob of data P1 bytes long.  Store this
  1124   1139   ** blob in register P2.
  1125   1140   */
  1126         -case OP_Blob: {                /* out2-prerelease */
         1141  +case OP_Blob: {                /* out2 */
  1127   1142     assert( pOp->p1 <= SQLITE_MAX_LENGTH );
         1143  +  pOut = out2Prerelease(p, pOp);
  1128   1144     sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
  1129   1145     pOut->enc = encoding;
  1130   1146     UPDATE_MAX_BLOBSIZE(pOut);
  1131   1147     break;
  1132   1148   }
  1133   1149   
  1134   1150   /* Opcode: Variable P1 P2 * P4 *
................................................................................
  1135   1151   ** Synopsis: r[P2]=parameter(P1,P4)
  1136   1152   **
  1137   1153   ** Transfer the values of bound parameter P1 into register P2
  1138   1154   **
  1139   1155   ** If the parameter is named, then its name appears in P4.
  1140   1156   ** The P4 value is used by sqlite3_bind_parameter_name().
  1141   1157   */
  1142         -case OP_Variable: {            /* out2-prerelease */
         1158  +case OP_Variable: {            /* out2 */
  1143   1159     Mem *pVar;       /* Value being transferred */
  1144   1160   
  1145   1161     assert( pOp->p1>0 && pOp->p1<=p->nVar );
  1146   1162     assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
  1147   1163     pVar = &p->aVar[pOp->p1 - 1];
  1148   1164     if( sqlite3VdbeMemTooBig(pVar) ){
  1149   1165       goto too_big;
  1150   1166     }
         1167  +  pOut = out2Prerelease(p, pOp);
  1151   1168     sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
  1152   1169     UPDATE_MAX_BLOBSIZE(pOut);
  1153   1170     break;
  1154   1171   }
  1155   1172   
  1156   1173   /* Opcode: Move P1 P2 P3 * *
  1157   1174   ** Synopsis:  r[P2@P3]=r[P1@P3]
................................................................................
  1320   1337       sqlite3VdbeMemNulTerminate(&pMem[i]);
  1321   1338       REGISTER_TRACE(pOp->p1+i, &pMem[i]);
  1322   1339     }
  1323   1340     if( db->mallocFailed ) goto no_mem;
  1324   1341   
  1325   1342     /* Return SQLITE_ROW
  1326   1343     */
  1327         -  p->pc = pc + 1;
         1344  +  p->pc = (int)(pOp - aOp) + 1;
  1328   1345     rc = SQLITE_ROW;
  1329   1346     goto vdbe_return;
  1330   1347   }
  1331   1348   
  1332   1349   /* Opcode: Concat P1 P2 P3 * *
  1333   1350   ** Synopsis: r[P3]=r[P2]+r[P1]
  1334   1351   **
................................................................................
  1566   1583       apVal[i] = pArg;
  1567   1584       Deephemeralize(pArg);
  1568   1585       REGISTER_TRACE(pOp->p2+i, pArg);
  1569   1586     }
  1570   1587   
  1571   1588     assert( pOp->p4type==P4_FUNCDEF );
  1572   1589     ctx.pFunc = pOp->p4.pFunc;
  1573         -  ctx.iOp = pc;
         1590  +  ctx.iOp = (int)(pOp - aOp);
  1574   1591     ctx.pVdbe = p;
  1575   1592     MemSetTypeFlag(ctx.pOut, MEM_Null);
  1576   1593     ctx.fErrorOrAux = 0;
  1577   1594     db->lastRowid = lastRowid;
  1578   1595     (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  1579   1596     lastRowid = db->lastRowid;  /* Remember rowid changes made by xFunc */
  1580   1597   
  1581   1598     /* If the function returned an error, throw an exception */
  1582   1599     if( ctx.fErrorOrAux ){
  1583   1600       if( ctx.isError ){
  1584   1601         sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
  1585   1602         rc = ctx.isError;
  1586   1603       }
  1587         -    sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
         1604  +    sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
  1588   1605     }
  1589   1606   
  1590   1607     /* Copy the result of the function into register P3 */
  1591   1608     sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
  1592   1609     if( sqlite3VdbeMemTooBig(ctx.pOut) ){
  1593   1610       goto too_big;
  1594   1611     }
................................................................................
  1709   1726       applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
  1710   1727       VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
  1711   1728       if( (pIn1->flags & MEM_Int)==0 ){
  1712   1729         if( pOp->p2==0 ){
  1713   1730           rc = SQLITE_MISMATCH;
  1714   1731           goto abort_due_to_error;
  1715   1732         }else{
  1716         -        pc = pOp->p2 - 1;
  1717         -        break;
         1733  +        goto jump_to_p2;
  1718   1734         }
  1719   1735       }
  1720   1736     }
  1721   1737     MemSetTypeFlag(pIn1, MEM_Int);
  1722   1738     break;
  1723   1739   }
  1724   1740   
................................................................................
  1896   1912         if( pOp->p5 & SQLITE_STOREP2 ){
  1897   1913           pOut = &aMem[pOp->p2];
  1898   1914           MemSetTypeFlag(pOut, MEM_Null);
  1899   1915           REGISTER_TRACE(pOp->p2, pOut);
  1900   1916         }else{
  1901   1917           VdbeBranchTaken(2,3);
  1902   1918           if( pOp->p5 & SQLITE_JUMPIFNULL ){
  1903         -          pc = pOp->p2-1;
         1919  +          goto jump_to_p2;
  1904   1920           }
  1905   1921         }
  1906   1922         break;
  1907   1923       }
  1908   1924     }else{
  1909   1925       /* Neither operand is NULL.  Do a comparison. */
  1910   1926       affinity = pOp->p5 & SQLITE_AFF_MASK;
................................................................................
  1947   1963       case OP_Eq:    res = res==0;     break;
  1948   1964       case OP_Ne:    res = res!=0;     break;
  1949   1965       case OP_Lt:    res = res<0;      break;
  1950   1966       case OP_Le:    res = res<=0;     break;
  1951   1967       case OP_Gt:    res = res>0;      break;
  1952   1968       default:       res = res>=0;     break;
  1953   1969     }
         1970  +
         1971  +  /* Undo any changes made by applyAffinity() to the input registers. */
         1972  +  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
         1973  +  pIn1->flags = flags1;
         1974  +  assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
         1975  +  pIn3->flags = flags3;
  1954   1976   
  1955   1977     if( pOp->p5 & SQLITE_STOREP2 ){
  1956   1978       pOut = &aMem[pOp->p2];
  1957   1979       memAboutToChange(p, pOut);
  1958   1980       MemSetTypeFlag(pOut, MEM_Int);
  1959   1981       pOut->u.i = res;
  1960   1982       REGISTER_TRACE(pOp->p2, pOut);
  1961   1983     }else{
  1962   1984       VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
  1963   1985       if( res ){
  1964         -      pc = pOp->p2-1;
         1986  +      goto jump_to_p2;
  1965   1987       }
  1966   1988     }
  1967         -  /* Undo any changes made by applyAffinity() to the input registers. */
  1968         -  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
  1969         -  pIn1->flags = flags1;
  1970         -  assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
  1971         -  pIn3->flags = flags3;
  1972   1989     break;
  1973   1990   }
  1974   1991   
  1975   1992   /* Opcode: Permutation * * * P4 *
  1976   1993   **
  1977   1994   ** Set the permutation used by the OP_Compare operator to be the array
  1978   1995   ** of integers in P4.
................................................................................
  2059   2076   **
  2060   2077   ** Jump to the instruction at address P1, P2, or P3 depending on whether
  2061   2078   ** in the most recent OP_Compare instruction the P1 vector was less than
  2062   2079   ** equal to, or greater than the P2 vector, respectively.
  2063   2080   */
  2064   2081   case OP_Jump: {             /* jump */
  2065   2082     if( iCompare<0 ){
  2066         -    pc = pOp->p1 - 1;  VdbeBranchTaken(0,3);
         2083  +    VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
  2067   2084     }else if( iCompare==0 ){
  2068         -    pc = pOp->p2 - 1;  VdbeBranchTaken(1,3);
         2085  +    VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
  2069   2086     }else{
  2070         -    pc = pOp->p3 - 1;  VdbeBranchTaken(2,3);
         2087  +    VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
  2071   2088     }
  2072   2089     break;
  2073   2090   }
  2074   2091   
  2075   2092   /* Opcode: And P1 P2 P3 * *
  2076   2093   ** Synopsis: r[P3]=(r[P1] && r[P2])
  2077   2094   **
................................................................................
  2173   2190   ** All "once" flags are initially cleared whenever a prepared statement
  2174   2191   ** first begins to run.
  2175   2192   */
  2176   2193   case OP_Once: {             /* jump */
  2177   2194     assert( pOp->p1<p->nOnceFlag );
  2178   2195     VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
  2179   2196     if( p->aOnceFlag[pOp->p1] ){
  2180         -    pc = pOp->p2-1;
         2197  +    goto jump_to_p2;
  2181   2198     }else{
  2182   2199       p->aOnceFlag[pOp->p1] = 1;
  2183   2200     }
  2184   2201     break;
  2185   2202   }
  2186   2203   
  2187   2204   /* Opcode: If P1 P2 P3 * *
................................................................................
  2208   2225   #else
  2209   2226       c = sqlite3VdbeRealValue(pIn1)!=0.0;
  2210   2227   #endif
  2211   2228       if( pOp->opcode==OP_IfNot ) c = !c;
  2212   2229     }
  2213   2230     VdbeBranchTaken(c!=0, 2);
  2214   2231     if( c ){
  2215         -    pc = pOp->p2-1;
         2232  +    goto jump_to_p2;
  2216   2233     }
  2217   2234     break;
  2218   2235   }
  2219   2236   
  2220   2237   /* Opcode: IsNull P1 P2 * * *
  2221   2238   ** Synopsis:  if r[P1]==NULL goto P2
  2222   2239   **
  2223   2240   ** Jump to P2 if the value in register P1 is NULL.
  2224   2241   */
  2225   2242   case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
  2226   2243     pIn1 = &aMem[pOp->p1];
  2227   2244     VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
  2228   2245     if( (pIn1->flags & MEM_Null)!=0 ){
  2229         -    pc = pOp->p2 - 1;
         2246  +    goto jump_to_p2;
  2230   2247     }
  2231   2248     break;
  2232   2249   }
  2233   2250   
  2234   2251   /* Opcode: NotNull P1 P2 * * *
  2235   2252   ** Synopsis: if r[P1]!=NULL goto P2
  2236   2253   **
  2237   2254   ** Jump to P2 if the value in register P1 is not NULL.  
  2238   2255   */
  2239   2256   case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
  2240   2257     pIn1 = &aMem[pOp->p1];
  2241   2258     VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
  2242   2259     if( (pIn1->flags & MEM_Null)==0 ){
  2243         -    pc = pOp->p2 - 1;
         2260  +    goto jump_to_p2;
  2244   2261     }
  2245   2262     break;
  2246   2263   }
  2247   2264   
  2248   2265   /* Opcode: Column P1 P2 P3 P4 P5
  2249   2266   ** Synopsis:  r[P3]=PX
  2250   2267   **
................................................................................
  2574   2591   */
  2575   2592   case OP_MakeRecord: {
  2576   2593     u8 *zNewRecord;        /* A buffer to hold the data for the new record */
  2577   2594     Mem *pRec;             /* The new record */
  2578   2595     u64 nData;             /* Number of bytes of data space */
  2579   2596     int nHdr;              /* Number of bytes of header space */
  2580   2597     i64 nByte;             /* Data space required for this record */
  2581         -  int nZero;             /* Number of zero bytes at the end of the record */
         2598  +  i64 nZero;             /* Number of zero bytes at the end of the record */
  2582   2599     int nVarint;           /* Number of bytes in a varint */
  2583   2600     u32 serial_type;       /* Type field */
  2584   2601     Mem *pData0;           /* First field to be combined into the record */
  2585   2602     Mem *pLast;            /* Last field of the record */
  2586   2603     int nField;            /* Number of fields in the record */
  2587   2604     char *zAffinity;       /* The affinity string for the record */
  2588   2605     int file_format;       /* File format to use for encoding */
................................................................................
  2666   2683     }else{
  2667   2684       /* Rare case of a really large header */
  2668   2685       nVarint = sqlite3VarintLen(nHdr);
  2669   2686       nHdr += nVarint;
  2670   2687       if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
  2671   2688     }
  2672   2689     nByte = nHdr+nData;
  2673         -  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
         2690  +  if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2674   2691       goto too_big;
  2675   2692     }
  2676   2693   
  2677   2694     /* Make sure the output register has a buffer large enough to store 
  2678   2695     ** the new record. The output register (pOp->p3) is not allowed to
  2679   2696     ** be one of the input registers (because the following call to
  2680   2697     ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
................................................................................
  2717   2734   /* Opcode: Count P1 P2 * * *
  2718   2735   ** Synopsis: r[P2]=count()
  2719   2736   **
  2720   2737   ** Store the number of entries (an integer value) in the table or index 
  2721   2738   ** opened by cursor P1 in register P2
  2722   2739   */
  2723   2740   #ifndef SQLITE_OMIT_BTREECOUNT
  2724         -case OP_Count: {         /* out2-prerelease */
         2741  +case OP_Count: {         /* out2 */
  2725   2742     i64 nEntry;
  2726   2743     BtCursor *pCrsr;
  2727   2744   
  2728   2745     pCrsr = p->apCsr[pOp->p1]->pCursor;
  2729   2746     assert( pCrsr );
  2730   2747     nEntry = 0;  /* Not needed.  Only used to silence a warning. */
  2731   2748     rc = sqlite3BtreeCount(pCrsr, &nEntry);
         2749  +  pOut = out2Prerelease(p, pOp);
  2732   2750     pOut->u.i = nEntry;
  2733   2751     break;
  2734   2752   }
  2735   2753   #endif
  2736   2754   
  2737   2755   /* Opcode: Savepoint P1 * * P4 *
  2738   2756   **
................................................................................
  2838   2856         int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
  2839   2857         if( isTransaction && p1==SAVEPOINT_RELEASE ){
  2840   2858           if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
  2841   2859             goto vdbe_return;
  2842   2860           }
  2843   2861           db->autoCommit = 1;
  2844   2862           if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
  2845         -          p->pc = pc;
         2863  +          p->pc = (int)(pOp - aOp);
  2846   2864             db->autoCommit = 0;
  2847   2865             p->rc = rc = SQLITE_BUSY;
  2848   2866             goto vdbe_return;
  2849   2867           }
  2850   2868           db->isTransactionSavepoint = 0;
  2851   2869           rc = p->rc;
  2852   2870         }else{
................................................................................
  2957   2975         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
  2958   2976         db->autoCommit = 1;
  2959   2977       }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
  2960   2978         goto vdbe_return;
  2961   2979       }else{
  2962   2980         db->autoCommit = (u8)desiredAutoCommit;
  2963   2981         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
  2964         -        p->pc = pc;
         2982  +        p->pc = (int)(pOp - aOp);
  2965   2983           db->autoCommit = (u8)(1-desiredAutoCommit);
  2966   2984           p->rc = rc = SQLITE_BUSY;
  2967   2985           goto vdbe_return;
  2968   2986         }
  2969   2987       }
  2970   2988       assert( db->nStatement==0 );
  2971   2989       sqlite3CloseSavepoints(db);
................................................................................
  3034   3052       goto abort_due_to_error;
  3035   3053     }
  3036   3054     pBt = db->aDb[pOp->p1].pBt;
  3037   3055   
  3038   3056     if( pBt ){
  3039   3057       rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
  3040   3058       if( rc==SQLITE_BUSY ){
  3041         -      p->pc = pc;
         3059  +      p->pc = (int)(pOp - aOp);
  3042   3060         p->rc = rc = SQLITE_BUSY;
  3043   3061         goto vdbe_return;
  3044   3062       }
  3045   3063       if( rc!=SQLITE_OK ){
  3046   3064         goto abort_due_to_error;
  3047   3065       }
  3048   3066   
................................................................................
  3113   3131   ** the main database file and P1==1 is the database file used to store
  3114   3132   ** temporary tables.
  3115   3133   **
  3116   3134   ** There must be a read-lock on the database (either a transaction
  3117   3135   ** must be started or there must be an open cursor) before
  3118   3136   ** executing this instruction.
  3119   3137   */
  3120         -case OP_ReadCookie: {               /* out2-prerelease */
         3138  +case OP_ReadCookie: {               /* out2 */
  3121   3139     int iMeta;
  3122   3140     int iDb;
  3123   3141     int iCookie;
  3124   3142   
  3125   3143     assert( p->bIsReader );
  3126   3144     iDb = pOp->p1;
  3127   3145     iCookie = pOp->p3;
  3128   3146     assert( pOp->p3<SQLITE_N_BTREE_META );
  3129   3147     assert( iDb>=0 && iDb<db->nDb );
  3130   3148     assert( db->aDb[iDb].pBt!=0 );
  3131   3149     assert( DbMaskTest(p->btreeMask, iDb) );
  3132   3150   
  3133   3151     sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
         3152  +  pOut = out2Prerelease(p, pOp);
  3134   3153     pOut->u.i = iMeta;
  3135   3154     break;
  3136   3155   }
  3137   3156   
  3138   3157   /* Opcode: SetCookie P1 P2 P3 * *
  3139   3158   **
  3140   3159   ** Write the content of register P3 (interpreted as an integer)
................................................................................
  3448   3467   */
  3449   3468   case OP_SequenceTest: {
  3450   3469     VdbeCursor *pC;
  3451   3470     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3452   3471     pC = p->apCsr[pOp->p1];
  3453   3472     assert( pC->pSorter );
  3454   3473     if( (pC->seqCount++)==0 ){
  3455         -    pc = pOp->p2 - 1;
         3474  +    goto jump_to_p2;
  3456   3475     }
  3457   3476     break;
  3458   3477   }
  3459   3478   
  3460   3479   /* Opcode: OpenPseudo P1 P2 P3 * *
  3461   3480   ** Synopsis: P3 columns in r[P2]
  3462   3481   **
................................................................................
  3625   3644   
  3626   3645       /* If the P3 value could not be converted into an integer without
  3627   3646       ** loss of information, then special processing is required... */
  3628   3647       if( (pIn3->flags & MEM_Int)==0 ){
  3629   3648         if( (pIn3->flags & MEM_Real)==0 ){
  3630   3649           /* If the P3 value cannot be converted into any kind of a number,
  3631   3650           ** then the seek is not possible, so jump to P2 */
  3632         -        pc = pOp->p2 - 1;  VdbeBranchTaken(1,2);
         3651  +        VdbeBranchTaken(1,2); goto jump_to_p2;
  3633   3652           break;
  3634   3653         }
  3635   3654   
  3636   3655         /* If the approximation iKey is larger than the actual real search
  3637   3656         ** term, substitute >= for > and < for <=. e.g. if the search term
  3638   3657         ** is 4.9 and the integer approximation 5:
  3639   3658         **
................................................................................
  3716   3735         */
  3717   3736         res = sqlite3BtreeEof(pC->pCursor);
  3718   3737       }
  3719   3738     }
  3720   3739     assert( pOp->p2>0 );
  3721   3740     VdbeBranchTaken(res!=0,2);
  3722   3741     if( res ){
  3723         -    pc = pOp->p2 - 1;
         3742  +    goto jump_to_p2;
  3724   3743     }
  3725   3744     break;
  3726   3745   }
  3727   3746   
  3728   3747   /* Opcode: Seek P1 P2 * * *
  3729   3748   ** Synopsis:  intkey=r[P2]
  3730   3749   **
................................................................................
  3810   3829   **
  3811   3830   ** See also: NotFound, Found, NotExists
  3812   3831   */
  3813   3832   case OP_NoConflict:     /* jump, in3 */
  3814   3833   case OP_NotFound:       /* jump, in3 */
  3815   3834   case OP_Found: {        /* jump, in3 */
  3816   3835     int alreadyExists;
         3836  +  int takeJump;
  3817   3837     int ii;
  3818   3838     VdbeCursor *pC;
  3819   3839     int res;
  3820   3840     char *pFree;
  3821   3841     UnpackedRecord *pIdxKey;
  3822   3842     UnpackedRecord r;
  3823   3843     char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
................................................................................
  3832   3852     assert( pC!=0 );
  3833   3853   #ifdef SQLITE_DEBUG
  3834   3854     pC->seekOp = pOp->opcode;
  3835   3855   #endif
  3836   3856     pIn3 = &aMem[pOp->p3];
  3837   3857     assert( pC->pCursor!=0 );
  3838   3858     assert( pC->isTable==0 );
  3839         -  pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
         3859  +  pFree = 0;
  3840   3860     if( pOp->p4.i>0 ){
  3841   3861       r.pKeyInfo = pC->pKeyInfo;
  3842   3862       r.nField = (u16)pOp->p4.i;
  3843   3863       r.aMem = pIn3;
  3844   3864       for(ii=0; ii<r.nField; ii++){
  3845   3865         assert( memIsValid(&r.aMem[ii]) );
  3846   3866         ExpandBlob(&r.aMem[ii]);
................................................................................
  3855   3875       );
  3856   3876       if( pIdxKey==0 ) goto no_mem;
  3857   3877       assert( pIn3->flags & MEM_Blob );
  3858   3878       ExpandBlob(pIn3);
  3859   3879       sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
  3860   3880     }
  3861   3881     pIdxKey->default_rc = 0;
         3882  +  takeJump = 0;
  3862   3883     if( pOp->opcode==OP_NoConflict ){
  3863   3884       /* For the OP_NoConflict opcode, take the jump if any of the
  3864   3885       ** input fields are NULL, since any key with a NULL will not
  3865   3886       ** conflict */
  3866   3887       for(ii=0; ii<pIdxKey->nField; ii++){
  3867   3888         if( pIdxKey->aMem[ii].flags & MEM_Null ){
  3868         -        pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
         3889  +        takeJump = 1;
  3869   3890           break;
  3870   3891         }
  3871   3892       }
  3872   3893     }
  3873   3894     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
  3874         -  if( pOp->p4.i==0 ){
  3875         -    sqlite3DbFree(db, pFree);
  3876         -  }
         3895  +  sqlite3DbFree(db, pFree);
  3877   3896     if( rc!=SQLITE_OK ){
  3878   3897       break;
  3879   3898     }
  3880   3899     pC->seekResult = res;
  3881   3900     alreadyExists = (res==0);
  3882   3901     pC->nullRow = 1-alreadyExists;
  3883   3902     pC->deferredMoveto = 0;
  3884   3903     pC->cacheStatus = CACHE_STALE;
  3885   3904     if( pOp->opcode==OP_Found ){
  3886   3905       VdbeBranchTaken(alreadyExists!=0,2);
  3887         -    if( alreadyExists ) pc = pOp->p2 - 1;
         3906  +    if( alreadyExists ) goto jump_to_p2;
  3888   3907     }else{
  3889         -    VdbeBranchTaken(alreadyExists==0,2);
  3890         -    if( !alreadyExists ) pc = pOp->p2 - 1;
         3908  +    VdbeBranchTaken(takeJump||alreadyExists==0,2);
         3909  +    if( takeJump || !alreadyExists ) goto jump_to_p2;
  3891   3910     }
  3892   3911     break;
  3893   3912   }
  3894   3913   
  3895   3914   /* Opcode: NotExists P1 P2 P3 * *
  3896   3915   ** Synopsis: intkey=r[P3]
  3897   3916   **
................................................................................
  3932   3951     iKey = pIn3->u.i;
  3933   3952     rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
  3934   3953     pC->movetoTarget = iKey;  /* Used by OP_Delete */
  3935   3954     pC->nullRow = 0;
  3936   3955     pC->cacheStatus = CACHE_STALE;
  3937   3956     pC->deferredMoveto = 0;
  3938   3957     VdbeBranchTaken(res!=0,2);
  3939         -  if( res!=0 ){
  3940         -    pc = pOp->p2 - 1;
  3941         -  }
  3942   3958     pC->seekResult = res;
         3959  +  if( res!=0 ) goto jump_to_p2;
  3943   3960     break;
  3944   3961   }
  3945   3962   
  3946   3963   /* Opcode: Sequence P1 P2 * * *
  3947   3964   ** Synopsis: r[P2]=cursor[P1].ctr++
  3948   3965   **
  3949   3966   ** Find the next available sequence number for cursor P1.
  3950   3967   ** Write the sequence number into register P2.
  3951   3968   ** The sequence number on the cursor is incremented after this
  3952   3969   ** instruction.  
  3953   3970   */
  3954         -case OP_Sequence: {           /* out2-prerelease */
         3971  +case OP_Sequence: {           /* out2 */
  3955   3972     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3956   3973     assert( p->apCsr[pOp->p1]!=0 );
         3974  +  pOut = out2Prerelease(p, pOp);
  3957   3975     pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
  3958   3976     break;
  3959   3977   }
  3960   3978   
  3961   3979   
  3962   3980   /* Opcode: NewRowid P1 P2 P3 * *
  3963   3981   ** Synopsis: r[P2]=rowid
................................................................................
  3970   3988   ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
  3971   3989   ** the largest previously generated record number. No new record numbers are
  3972   3990   ** allowed to be less than this value. When this value reaches its maximum, 
  3973   3991   ** an SQLITE_FULL error is generated. The P3 register is updated with the '
  3974   3992   ** generated record number. This P3 mechanism is used to help implement the
  3975   3993   ** AUTOINCREMENT feature.
  3976   3994   */
  3977         -case OP_NewRowid: {           /* out2-prerelease */
         3995  +case OP_NewRowid: {           /* out2 */
  3978   3996     i64 v;                 /* The new rowid */
  3979   3997     VdbeCursor *pC;        /* Cursor of table to get the new rowid */
  3980   3998     int res;               /* Result of an sqlite3BtreeLast() */
  3981   3999     int cnt;               /* Counter to limit the number of searches */
  3982   4000     Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
  3983   4001     VdbeFrame *pFrame;     /* Root frame of VDBE */
  3984   4002   
  3985   4003     v = 0;
  3986   4004     res = 0;
         4005  +  pOut = out2Prerelease(p, pOp);
  3987   4006     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3988   4007     pC = p->apCsr[pOp->p1];
  3989   4008     assert( pC!=0 );
  3990   4009     if( NEVER(pC->pCursor==0) ){
  3991   4010       /* The zero initialization above is all that is needed */
  3992   4011     }else{
  3993   4012       /* The next rowid or record number (different terms for the same
................................................................................
  4293   4312     assert( isSorter(pC) );
  4294   4313     assert( pOp->p4type==P4_INT32 );
  4295   4314     pIn3 = &aMem[pOp->p3];
  4296   4315     nKeyCol = pOp->p4.i;
  4297   4316     res = 0;
  4298   4317     rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
  4299   4318     VdbeBranchTaken(res!=0,2);
  4300         -  if( res ){
  4301         -    pc = pOp->p2-1;
  4302         -  }
         4319  +  if( res ) goto jump_to_p2;
  4303   4320     break;
  4304   4321   };
  4305   4322   
  4306   4323   /* Opcode: SorterData P1 P2 P3 * *
  4307   4324   ** Synopsis: r[P2]=data
  4308   4325   **
  4309   4326   ** Write into register P2 the current sorter data for sorter cursor P1.
................................................................................
  4424   4441   ** Store in register P2 an integer which is the key of the table entry that
  4425   4442   ** P1 is currently point to.
  4426   4443   **
  4427   4444   ** P1 can be either an ordinary table or a virtual table.  There used to
  4428   4445   ** be a separate OP_VRowid opcode for use with virtual tables, but this
  4429   4446   ** one opcode now works for both table types.
  4430   4447   */
  4431         -case OP_Rowid: {                 /* out2-prerelease */
         4448  +case OP_Rowid: {                 /* out2 */
  4432   4449     VdbeCursor *pC;
  4433   4450     i64 v;
  4434   4451     sqlite3_vtab *pVtab;
  4435   4452     const sqlite3_module *pModule;
  4436   4453   
         4454  +  pOut = out2Prerelease(p, pOp);
  4437   4455     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4438   4456     pC = p->apCsr[pOp->p1];
  4439   4457     assert( pC!=0 );
  4440   4458     assert( pC->pseudoTableReg==0 || pC->nullRow );
  4441   4459     if( pC->nullRow ){
  4442   4460       pOut->flags = MEM_Null;
  4443   4461       break;
................................................................................
  4482   4500     pC->cacheStatus = CACHE_STALE;
  4483   4501     if( pC->pCursor ){
  4484   4502       sqlite3BtreeClearCursor(pC->pCursor);
  4485   4503     }
  4486   4504     break;
  4487   4505   }
  4488   4506   
  4489         -/* Opcode: Last P1 P2 * * *
         4507  +/* Opcode: Last P1 P2 P3 * *
  4490   4508   **
  4491   4509   ** The next use of the Rowid or Column or Prev instruction for P1 
  4492   4510   ** will refer to the last entry in the database table or index.
  4493   4511   ** If the table or index is empty and P2>0, then jump immediately to P2.
  4494   4512   ** If P2 is 0 or if the table or index is not empty, fall through
  4495   4513   ** to the following instruction.
  4496   4514   **
................................................................................
  4509   4527     pCrsr = pC->pCursor;
  4510   4528     res = 0;
  4511   4529     assert( pCrsr!=0 );
  4512   4530     rc = sqlite3BtreeLast(pCrsr, &res);
  4513   4531     pC->nullRow = (u8)res;
  4514   4532     pC->deferredMoveto = 0;
  4515   4533     pC->cacheStatus = CACHE_STALE;
         4534  +  pC->seekResult = pOp->p3;
  4516   4535   #ifdef SQLITE_DEBUG
  4517   4536     pC->seekOp = OP_Last;
  4518   4537   #endif
  4519   4538     if( pOp->p2>0 ){
  4520   4539       VdbeBranchTaken(res!=0,2);
  4521         -    if( res ) pc = pOp->p2 - 1;
         4540  +    if( res ) goto jump_to_p2;
  4522   4541     }
  4523   4542     break;
  4524   4543   }
  4525   4544   
  4526   4545   
  4527   4546   /* Opcode: Sort P1 P2 * * *
  4528   4547   **
................................................................................
  4578   4597       rc = sqlite3BtreeFirst(pCrsr, &res);
  4579   4598       pC->deferredMoveto = 0;
  4580   4599       pC->cacheStatus = CACHE_STALE;
  4581   4600     }
  4582   4601     pC->nullRow = (u8)res;
  4583   4602     assert( pOp->p2>0 && pOp->p2<p->nOp );
  4584   4603     VdbeBranchTaken(res!=0,2);
  4585         -  if( res ){
  4586         -    pc = pOp->p2 - 1;
  4587         -  }
         4604  +  if( res ) goto jump_to_p2;
  4588   4605     break;
  4589   4606   }
  4590   4607   
  4591   4608   /* Opcode: Next P1 P2 P3 P4 P5
  4592   4609   **
  4593   4610   ** Advance cursor P1 so that it points to the next key/data pair in its
  4594   4611   ** table or index.  If there are no more key/value pairs then fall through
................................................................................
  4691   4708   
  4692   4709     rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4693   4710   next_tail:
  4694   4711     pC->cacheStatus = CACHE_STALE;
  4695   4712     VdbeBranchTaken(res==0,2);
  4696   4713     if( res==0 ){
  4697   4714       pC->nullRow = 0;
  4698         -    pc = pOp->p2 - 1;
  4699   4715       p->aCounter[pOp->p5]++;
  4700   4716   #ifdef SQLITE_TEST
  4701   4717       sqlite3_search_count++;
  4702   4718   #endif
         4719  +    goto jump_to_p2_and_check_for_interrupt;
  4703   4720     }else{
  4704   4721       pC->nullRow = 1;
  4705   4722     }
  4706   4723     goto check_for_interrupt;
  4707   4724   }
  4708   4725   
  4709   4726   /* Opcode: IdxInsert P1 P2 P3 * P5
................................................................................
  4803   4820   **
  4804   4821   ** Write into register P2 an integer which is the last entry in the record at
  4805   4822   ** the end of the index key pointed to by cursor P1.  This integer should be
  4806   4823   ** the rowid of the table entry to which this index entry points.
  4807   4824   **
  4808   4825   ** See also: Rowid, MakeRecord.
  4809   4826   */
  4810         -case OP_IdxRowid: {              /* out2-prerelease */
         4827  +case OP_IdxRowid: {              /* out2 */
  4811   4828     BtCursor *pCrsr;
  4812   4829     VdbeCursor *pC;
  4813   4830     i64 rowid;
  4814   4831   
         4832  +  pOut = out2Prerelease(p, pOp);
  4815   4833     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4816   4834     pC = p->apCsr[pOp->p1];
  4817   4835     assert( pC!=0 );
  4818   4836     pCrsr = pC->pCursor;
  4819   4837     assert( pCrsr!=0 );
  4820   4838     pOut->flags = MEM_Null;
  4821   4839     assert( pC->isTable==0 );
................................................................................
  4920   4938       assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
  4921   4939       res = -res;
  4922   4940     }else{
  4923   4941       assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
  4924   4942       res++;
  4925   4943     }
  4926   4944     VdbeBranchTaken(res>0,2);
  4927         -  if( res>0 ){
  4928         -    pc = pOp->p2 - 1 ;
  4929         -  }
         4945  +  if( res>0 ) goto jump_to_p2;
  4930   4946     break;
  4931   4947   }
  4932   4948   
  4933   4949   /* Opcode: Destroy P1 P2 P3 * *
  4934   4950   **
  4935   4951   ** Delete an entire database table or index whose root page in the database
  4936   4952   ** file is given by P1.
................................................................................
  4946   4962   ** is stored in register P2.  If no page 
  4947   4963   ** movement was required (because the table being dropped was already 
  4948   4964   ** the last one in the database) then a zero is stored in register P2.
  4949   4965   ** If AUTOVACUUM is disabled then a zero is stored in register P2.
  4950   4966   **
  4951   4967   ** See also: Clear
  4952   4968   */
  4953         -case OP_Destroy: {     /* out2-prerelease */
         4969  +case OP_Destroy: {     /* out2 */
  4954   4970     int iMoved;
  4955   4971     int iDb;
  4956   4972   
  4957   4973     assert( p->readOnly==0 );
         4974  +  pOut = out2Prerelease(p, pOp);
  4958   4975     pOut->flags = MEM_Null;
  4959   4976     if( db->nVdbeRead > db->nVDestroy+1 ){
  4960   4977       rc = SQLITE_LOCKED;
  4961   4978       p->errorAction = OE_Abort;
  4962   4979     }else{
  4963   4980       iDb = pOp->p3;
  4964   4981       assert( DbMaskTest(p->btreeMask, iDb) );
................................................................................
  5059   5076   ** Allocate a new index in the main database file if P1==0 or in the
  5060   5077   ** auxiliary database file if P1==1 or in an attached database if
  5061   5078   ** P1>1.  Write the root page number of the new table into
  5062   5079   ** register P2.
  5063   5080   **
  5064   5081   ** See documentation on OP_CreateTable for additional information.
  5065   5082   */
  5066         -case OP_CreateIndex:            /* out2-prerelease */
  5067         -case OP_CreateTable: {          /* out2-prerelease */
         5083  +case OP_CreateIndex:            /* out2 */
         5084  +case OP_CreateTable: {          /* out2 */
  5068   5085     int pgno;
  5069   5086     int flags;
  5070   5087     Db *pDb;
  5071   5088   
         5089  +  pOut = out2Prerelease(p, pOp);
  5072   5090     pgno = 0;
  5073   5091     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5074   5092     assert( DbMaskTest(p->btreeMask, pOp->p1) );
  5075   5093     assert( p->readOnly==0 );
  5076   5094     pDb = &db->aDb[pOp->p1];
  5077   5095     assert( pDb->pBt!=0 );
  5078   5096     if( pOp->opcode==OP_CreateTable ){
................................................................................
  5290   5308   
  5291   5309     pIn1 = &aMem[pOp->p1];
  5292   5310     if( (pIn1->flags & MEM_RowSet)==0 
  5293   5311      || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
  5294   5312     ){
  5295   5313       /* The boolean index is empty */
  5296   5314       sqlite3VdbeMemSetNull(pIn1);
  5297         -    pc = pOp->p2 - 1;
  5298   5315       VdbeBranchTaken(1,2);
         5316  +    goto jump_to_p2_and_check_for_interrupt;
  5299   5317     }else{
  5300   5318       /* A value was pulled from the index */
  5301         -    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
  5302   5319       VdbeBranchTaken(0,2);
         5320  +    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
  5303   5321     }
  5304   5322     goto check_for_interrupt;
  5305   5323   }
  5306   5324   
  5307   5325   /* Opcode: RowSetTest P1 P2 P3 P4
  5308   5326   ** Synopsis: if r[P3] in rowset(P1) goto P2
  5309   5327   **
................................................................................
  5346   5364     }
  5347   5365   
  5348   5366     assert( pOp->p4type==P4_INT32 );
  5349   5367     assert( iSet==-1 || iSet>=0 );
  5350   5368     if( iSet ){
  5351   5369       exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
  5352   5370       VdbeBranchTaken(exists!=0,2);
  5353         -    if( exists ){
  5354         -      pc = pOp->p2 - 1;
  5355         -      break;
  5356         -    }
         5371  +    if( exists ) goto jump_to_p2;
  5357   5372     }
  5358   5373     if( iSet>=0 ){
  5359   5374       sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
  5360   5375     }
  5361   5376     break;
  5362   5377   }
  5363   5378   
................................................................................
  5438   5453       sqlite3VdbeMemRelease(pRt);
  5439   5454       pRt->flags = MEM_Frame;
  5440   5455       pRt->u.pFrame = pFrame;
  5441   5456   
  5442   5457       pFrame->v = p;
  5443   5458       pFrame->nChildMem = nMem;
  5444   5459       pFrame->nChildCsr = pProgram->nCsr;
  5445         -    pFrame->pc = pc;
         5460  +    pFrame->pc = (int)(pOp - aOp);
  5446   5461       pFrame->aMem = p->aMem;
  5447   5462       pFrame->nMem = p->nMem;
  5448   5463       pFrame->apCsr = p->apCsr;
  5449   5464       pFrame->nCursor = p->nCursor;
  5450   5465       pFrame->aOp = p->aOp;
  5451   5466       pFrame->nOp = p->nOp;
  5452   5467       pFrame->token = pProgram->token;
................................................................................
  5461   5476         pMem->flags = MEM_Undefined;
  5462   5477         pMem->db = db;
  5463   5478       }
  5464   5479     }else{
  5465   5480       pFrame = pRt->u.pFrame;
  5466   5481       assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
  5467   5482       assert( pProgram->nCsr==pFrame->nChildCsr );
  5468         -    assert( pc==pFrame->pc );
         5483  +    assert( (int)(pOp - aOp)==pFrame->pc );
  5469   5484     }
  5470   5485   
  5471   5486     p->nFrame++;
  5472   5487     pFrame->pParent = p->pFrame;
  5473   5488     pFrame->lastRowid = lastRowid;
  5474   5489     pFrame->nChange = p->nChange;
  5475   5490     pFrame->nDbChange = p->db->nChange;
................................................................................
  5482   5497     p->aOp = aOp = pProgram->aOp;
  5483   5498     p->nOp = pProgram->nOp;
  5484   5499     p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
  5485   5500     p->nOnceFlag = pProgram->nOnce;
  5486   5501   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  5487   5502     p->anExec = 0;
  5488   5503   #endif
  5489         -  pc = -1;
         5504  +  pOp = &aOp[-1];
  5490   5505     memset(p->aOnceFlag, 0, p->nOnceFlag);
  5491   5506   
  5492   5507     break;
  5493   5508   }
  5494   5509   
  5495   5510   /* Opcode: Param P1 P2 * * *
  5496   5511   **
................................................................................
  5500   5515   ** address space. This is used by trigger programs to access the new.* 
  5501   5516   ** and old.* values.
  5502   5517   **
  5503   5518   ** The address of the cell in the parent frame is determined by adding
  5504   5519   ** the value of the P1 argument to the value of the P1 argument to the
  5505   5520   ** calling OP_Program instruction.
  5506   5521   */
  5507         -case OP_Param: {           /* out2-prerelease */
         5522  +case OP_Param: {           /* out2 */
  5508   5523     VdbeFrame *pFrame;
  5509   5524     Mem *pIn;
         5525  +  pOut = out2Prerelease(p, pOp);
  5510   5526     pFrame = p->pFrame;
  5511   5527     pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];   
  5512   5528     sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
  5513   5529     break;
  5514   5530   }
  5515   5531   
  5516   5532   #endif /* #ifndef SQLITE_OMIT_TRIGGER */
................................................................................
  5546   5562   ** is zero (the one that counts deferred constraint violations). If P1 is
  5547   5563   ** zero, the jump is taken if the statement constraint-counter is zero
  5548   5564   ** (immediate foreign key constraint violations).
  5549   5565   */
  5550   5566   case OP_FkIfZero: {         /* jump */
  5551   5567     if( pOp->p1 ){
  5552   5568       VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
  5553         -    if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
         5569  +    if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
  5554   5570     }else{
  5555   5571       VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
  5556         -    if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
         5572  +    if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
  5557   5573     }
  5558   5574     break;
  5559   5575   }
  5560   5576   #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
  5561   5577   
  5562   5578   #ifndef SQLITE_OMIT_AUTOINCREMENT
  5563   5579   /* Opcode: MemMax P1 P2 * * *
................................................................................
  5600   5616   ** If the initial value of register P1 is less than 1, then the
  5601   5617   ** value is unchanged and control passes through to the next instruction.
  5602   5618   */
  5603   5619   case OP_IfPos: {        /* jump, in1 */
  5604   5620     pIn1 = &aMem[pOp->p1];
  5605   5621     assert( pIn1->flags&MEM_Int );
  5606   5622     VdbeBranchTaken( pIn1->u.i>0, 2);
  5607         -  if( pIn1->u.i>0 ){
  5608         -     pc = pOp->p2 - 1;
  5609         -  }
         5623  +  if( pIn1->u.i>0 ) goto jump_to_p2;
  5610   5624     break;
  5611   5625   }
  5612   5626   
  5613   5627   /* Opcode: IfNeg P1 P2 P3 * *
  5614   5628   ** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
  5615   5629   **
  5616   5630   ** Register P1 must contain an integer.  Add literal P3 to the value in
................................................................................
  5617   5631   ** register P1 then if the value of register P1 is less than zero, jump to P2. 
  5618   5632   */
  5619   5633   case OP_IfNeg: {        /* jump, in1 */
  5620   5634     pIn1 = &aMem[pOp->p1];
  5621   5635     assert( pIn1->flags&MEM_Int );
  5622   5636     pIn1->u.i += pOp->p3;
  5623   5637     VdbeBranchTaken(pIn1->u.i<0, 2);
  5624         -  if( pIn1->u.i<0 ){
  5625         -     pc = pOp->p2 - 1;
  5626         -  }
         5638  +  if( pIn1->u.i<0 ) goto jump_to_p2;
  5627   5639     break;
  5628   5640   }
  5629   5641   
  5630   5642   /* Opcode: IfNotZero P1 P2 P3 * *
  5631   5643   ** Synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2
  5632   5644   **
  5633   5645   ** Register P1 must contain an integer.  If the content of register P1 is
................................................................................
  5636   5648   */
  5637   5649   case OP_IfNotZero: {        /* jump, in1 */
  5638   5650     pIn1 = &aMem[pOp->p1];
  5639   5651     assert( pIn1->flags&MEM_Int );
  5640   5652     VdbeBranchTaken(pIn1->u.i<0, 2);
  5641   5653     if( pIn1->u.i ){
  5642   5654        pIn1->u.i += pOp->p3;
  5643         -     pc = pOp->p2 - 1;
         5655  +     goto jump_to_p2;
  5644   5656     }
  5645   5657     break;
  5646   5658   }
  5647   5659   
  5648   5660   /* Opcode: DecrJumpZero P1 P2 * * *
  5649   5661   ** Synopsis: if (--r[P1])==0 goto P2
  5650   5662   **
................................................................................
  5652   5664   ** then jump to P2 if the new value is exactly zero.
  5653   5665   */
  5654   5666   case OP_DecrJumpZero: {      /* jump, in1 */
  5655   5667     pIn1 = &aMem[pOp->p1];
  5656   5668     assert( pIn1->flags&MEM_Int );
  5657   5669     pIn1->u.i--;
  5658   5670     VdbeBranchTaken(pIn1->u.i==0, 2);
  5659         -  if( pIn1->u.i==0 ){
  5660         -     pc = pOp->p2 - 1;
  5661         -  }
         5671  +  if( pIn1->u.i==0 ) goto jump_to_p2;
  5662   5672     break;
  5663   5673   }
  5664   5674   
  5665   5675   
  5666   5676   /* Opcode: JumpZeroIncr P1 P2 * * *
  5667   5677   ** Synopsis: if (r[P1]++)==0 ) goto P2
  5668   5678   **
................................................................................
  5670   5680   ** zero, then jump to P2.  Increment register P1 regardless of whether or
  5671   5681   ** not the jump is taken.
  5672   5682   */
  5673   5683   case OP_JumpZeroIncr: {        /* jump, in1 */
  5674   5684     pIn1 = &aMem[pOp->p1];
  5675   5685     assert( pIn1->flags&MEM_Int );
  5676   5686     VdbeBranchTaken(pIn1->u.i==0, 2);
  5677         -  if( (pIn1->u.i++)==0 ){
  5678         -     pc = pOp->p2 - 1;
  5679         -  }
         5687  +  if( (pIn1->u.i++)==0 ) goto jump_to_p2;
  5680   5688     break;
  5681   5689   }
  5682   5690   
  5683   5691   /* Opcode: AggStep * P2 P3 P4 P5
  5684   5692   ** Synopsis: accum=r[P3] step(r[P2@P5])
  5685   5693   **
  5686   5694   ** Execute the step function for an aggregate.  The
................................................................................
  5714   5722     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5715   5723     ctx.pMem = pMem = &aMem[pOp->p3];
  5716   5724     pMem->n++;
  5717   5725     sqlite3VdbeMemInit(&t, db, MEM_Null);
  5718   5726     ctx.pOut = &t;
  5719   5727     ctx.isError = 0;
  5720   5728     ctx.pVdbe = p;
  5721         -  ctx.iOp = pc;
         5729  +  ctx.iOp = (int)(pOp - aOp);
  5722   5730     ctx.skipFlag = 0;
  5723   5731     (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  5724   5732     if( ctx.isError ){
  5725   5733       sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
  5726   5734       rc = ctx.isError;
  5727   5735     }
  5728   5736     if( ctx.skipFlag ){
................................................................................
  5809   5817   ** modes (delete, truncate, persist, off and memory), this is a simple
  5810   5818   ** operation. No IO is required.
  5811   5819   **
  5812   5820   ** If changing into or out of WAL mode the procedure is more complicated.
  5813   5821   **
  5814   5822   ** Write a string containing the final journal-mode to register P2.
  5815   5823   */
  5816         -case OP_JournalMode: {    /* out2-prerelease */
         5824  +case OP_JournalMode: {    /* out2 */
  5817   5825     Btree *pBt;                     /* Btree to change journal mode of */
  5818   5826     Pager *pPager;                  /* Pager associated with pBt */
  5819   5827     int eNew;                       /* New journal mode */
  5820   5828     int eOld;                       /* The old journal mode */
  5821   5829   #ifndef SQLITE_OMIT_WAL
  5822   5830     const char *zFilename;          /* Name of database file for pPager */
  5823   5831   #endif
  5824   5832   
         5833  +  pOut = out2Prerelease(p, pOp);
  5825   5834     eNew = pOp->p3;
  5826   5835     assert( eNew==PAGER_JOURNALMODE_DELETE 
  5827   5836          || eNew==PAGER_JOURNALMODE_TRUNCATE 
  5828   5837          || eNew==PAGER_JOURNALMODE_PERSIST 
  5829   5838          || eNew==PAGER_JOURNALMODE_OFF
  5830   5839          || eNew==PAGER_JOURNALMODE_MEMORY
  5831   5840          || eNew==PAGER_JOURNALMODE_WAL
................................................................................
  5934   5943     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5935   5944     assert( DbMaskTest(p->btreeMask, pOp->p1) );
  5936   5945     assert( p->readOnly==0 );
  5937   5946     pBt = db->aDb[pOp->p1].pBt;
  5938   5947     rc = sqlite3BtreeIncrVacuum(pBt);
  5939   5948     VdbeBranchTaken(rc==SQLITE_DONE,2);
  5940   5949     if( rc==SQLITE_DONE ){
  5941         -    pc = pOp->p2 - 1;
  5942   5950       rc = SQLITE_OK;
         5951  +    goto jump_to_p2;
  5943   5952     }
  5944   5953     break;
  5945   5954   }
  5946   5955   #endif
  5947   5956   
  5948   5957   /* Opcode: Expire P1 * * * *
  5949   5958   **
................................................................................
  6145   6154   
  6146   6155     /* Grab the index number and argc parameters */
  6147   6156     assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
  6148   6157     nArg = (int)pArgc->u.i;
  6149   6158     iQuery = (int)pQuery->u.i;
  6150   6159   
  6151   6160     /* Invoke the xFilter method */
  6152         -  {
  6153         -    res = 0;
  6154         -    apArg = p->apArg;
  6155         -    for(i = 0; i<nArg; i++){
  6156         -      apArg[i] = &pArgc[i+1];
  6157         -    }
  6158         -
  6159         -    rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
  6160         -    sqlite3VtabImportErrmsg(p, pVtab);
  6161         -    if( rc==SQLITE_OK ){
  6162         -      res = pModule->xEof(pVtabCursor);
  6163         -    }
  6164         -    VdbeBranchTaken(res!=0,2);
  6165         -    if( res ){
  6166         -      pc = pOp->p2 - 1;
  6167         -    }
         6161  +  res = 0;
         6162  +  apArg = p->apArg;
         6163  +  for(i = 0; i<nArg; i++){
         6164  +    apArg[i] = &pArgc[i+1];
         6165  +  }
         6166  +  rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
         6167  +  sqlite3VtabImportErrmsg(p, pVtab);
         6168  +  if( rc==SQLITE_OK ){
         6169  +    res = pModule->xEof(pVtabCursor);
  6168   6170     }
  6169   6171     pCur->nullRow = 0;
  6170         -
         6172  +  VdbeBranchTaken(res!=0,2);
         6173  +  if( res ) goto jump_to_p2;
  6171   6174     break;
  6172   6175   }
  6173   6176   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6174   6177   
  6175   6178   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6176   6179   /* Opcode: VColumn P1 P2 P3 * *
  6177   6180   ** Synopsis: r[P3]=vcolumn(P2)
................................................................................
  6250   6253     sqlite3VtabImportErrmsg(p, pVtab);
  6251   6254     if( rc==SQLITE_OK ){
  6252   6255       res = pModule->xEof(pCur->pVtabCursor);
  6253   6256     }
  6254   6257     VdbeBranchTaken(!res,2);
  6255   6258     if( !res ){
  6256   6259       /* If there is data, jump to P2 */
  6257         -    pc = pOp->p2 - 1;
         6260  +    goto jump_to_p2_and_check_for_interrupt;
  6258   6261     }
  6259   6262     goto check_for_interrupt;
  6260   6263   }
  6261   6264   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6262   6265   
  6263   6266   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6264   6267   /* Opcode: VRename P1 * * P4 *
................................................................................
  6373   6376   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6374   6377   
  6375   6378   #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
  6376   6379   /* Opcode: Pagecount P1 P2 * * *
  6377   6380   **
  6378   6381   ** Write the current number of pages in database P1 to memory cell P2.
  6379   6382   */
  6380         -case OP_Pagecount: {            /* out2-prerelease */
         6383  +case OP_Pagecount: {            /* out2 */
         6384  +  pOut = out2Prerelease(p, pOp);
  6381   6385     pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
  6382   6386     break;
  6383   6387   }
  6384   6388   #endif
  6385   6389   
  6386   6390   
  6387   6391   #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
................................................................................
  6389   6393   **
  6390   6394   ** Try to set the maximum page count for database P1 to the value in P3.
  6391   6395   ** Do not let the maximum page count fall below the current page count and
  6392   6396   ** do not change the maximum page count value if P3==0.
  6393   6397   **
  6394   6398   ** Store the maximum page count after the change in register P2.
  6395   6399   */
  6396         -case OP_MaxPgcnt: {            /* out2-prerelease */
         6400  +case OP_MaxPgcnt: {            /* out2 */
  6397   6401     unsigned int newMax;
  6398   6402     Btree *pBt;
  6399   6403   
         6404  +  pOut = out2Prerelease(p, pOp);
  6400   6405     pBt = db->aDb[pOp->p1].pBt;
  6401   6406     newMax = 0;
  6402   6407     if( pOp->p3 ){
  6403   6408       newMax = sqlite3BtreeLastPage(pBt);
  6404   6409       if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
  6405   6410     }
  6406   6411     pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
................................................................................
  6421   6426   **
  6422   6427   ** If P2 is not zero, jump to instruction P2.
  6423   6428   */
  6424   6429   case OP_Init: {          /* jump */
  6425   6430     char *zTrace;
  6426   6431     char *z;
  6427   6432   
  6428         -  if( pOp->p2 ){
  6429         -    pc = pOp->p2 - 1;
  6430         -  }
  6431   6433   #ifndef SQLITE_OMIT_TRACE
  6432   6434     if( db->xTrace
  6433   6435      && !p->doingRerun
  6434   6436      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
  6435   6437     ){
  6436   6438       z = sqlite3VdbeExpandSql(p, zTrace);
  6437   6439       db->xTrace(db->pTraceArg, z);
................................................................................
  6451   6453     if( (db->flags & SQLITE_SqlTrace)!=0
  6452   6454      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
  6453   6455     ){
  6454   6456       sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
  6455   6457     }
  6456   6458   #endif /* SQLITE_DEBUG */
  6457   6459   #endif /* SQLITE_OMIT_TRACE */
         6460  +  if( pOp->p2 ) goto jump_to_p2;
  6458   6461     break;
  6459   6462   }
  6460   6463   
  6461   6464   
  6462   6465   /* Opcode: Noop * * * * *
  6463   6466   **
  6464   6467   ** Do nothing.  This instruction is often useful as a jump
................................................................................
  6493   6496   
  6494   6497       /* The following code adds nothing to the actual functionality
  6495   6498       ** of the program.  It is only here for testing and debugging.
  6496   6499       ** On the other hand, it does burn CPU cycles every time through
  6497   6500       ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
  6498   6501       */
  6499   6502   #ifndef NDEBUG
  6500         -    assert( pc>=-1 && pc<p->nOp );
         6503  +    assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp] );
  6501   6504   
  6502   6505   #ifdef SQLITE_DEBUG
  6503   6506       if( db->flags & SQLITE_VdbeTrace ){
  6504   6507         if( rc!=0 ) printf("rc=%d\n",rc);
  6505         -      if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
         6508  +      if( pOp->opflags & (OPFLG_OUT2) ){
  6506   6509           registerTrace(pOp->p2, &aMem[pOp->p2]);
  6507   6510         }
  6508   6511         if( pOp->opflags & OPFLG_OUT3 ){
  6509   6512           registerTrace(pOp->p3, &aMem[pOp->p3]);
  6510   6513         }
  6511   6514       }
  6512   6515   #endif  /* SQLITE_DEBUG */
................................................................................
  6517   6520     ** an error of some kind.
  6518   6521     */
  6519   6522   vdbe_error_halt:
  6520   6523     assert( rc );
  6521   6524     p->rc = rc;
  6522   6525     testcase( sqlite3GlobalConfig.xLog!=0 );
  6523   6526     sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
  6524         -                   pc, p->zSql, p->zErrMsg);
         6527  +                   (int)(pOp - aOp), p->zSql, p->zErrMsg);
  6525   6528     sqlite3VdbeHalt(p);
  6526   6529     if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
  6527   6530     rc = SQLITE_ERROR;
  6528   6531     if( resetSchemaOnFault>0 ){
  6529   6532       sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
  6530   6533     }
  6531   6534   

Changes to src/vdbe.h.

   209    209   #ifndef SQLITE_OMIT_TRACE
   210    210     char *sqlite3VdbeExpandSql(Vdbe*, const char*);
   211    211   #endif
   212    212   int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
   213    213   
   214    214   void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
   215    215   int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
          216  +int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
   216    217   UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
   217    218   
   218    219   typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
   219    220   RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
   220    221   
   221    222   #ifndef SQLITE_OMIT_TRIGGER
   222    223   void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);

Changes to src/vdbeaux.c.

  3581   3581   ** returned.
  3582   3582   **
  3583   3583   ** If database corruption is discovered, set pPKey2->errCode to 
  3584   3584   ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, 
  3585   3585   ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
  3586   3586   ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
  3587   3587   */
  3588         -static int vdbeRecordCompareWithSkip(
         3588  +int sqlite3VdbeRecordCompareWithSkip(
  3589   3589     int nKey1, const void *pKey1,   /* Left key */
  3590   3590     UnpackedRecord *pPKey2,         /* Right key */
  3591   3591     int bSkip                       /* If true, skip the first field */
  3592   3592   ){
  3593   3593     u32 d1;                         /* Offset into aKey[] of next data element */
  3594   3594     int i;                          /* Index of next field to compare */
  3595   3595     u32 szHdr1;                     /* Size of record header in bytes */
................................................................................
  3767   3767     );
  3768   3768     return pPKey2->default_rc;
  3769   3769   }
  3770   3770   int sqlite3VdbeRecordCompare(
  3771   3771     int nKey1, const void *pKey1,   /* Left key */
  3772   3772     UnpackedRecord *pPKey2          /* Right key */
  3773   3773   ){
  3774         -  return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
         3774  +  return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
  3775   3775   }
  3776   3776   
  3777   3777   
  3778   3778   /*
  3779   3779   ** This function is an optimized version of sqlite3VdbeRecordCompare() 
  3780   3780   ** that (a) the first field of pPKey2 is an integer, and (b) the 
  3781   3781   ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
................................................................................
  3855   3855     if( v>lhs ){
  3856   3856       res = pPKey2->r1;
  3857   3857     }else if( v<lhs ){
  3858   3858       res = pPKey2->r2;
  3859   3859     }else if( pPKey2->nField>1 ){
  3860   3860       /* The first fields of the two keys are equal. Compare the trailing 
  3861   3861       ** fields.  */
  3862         -    res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
         3862  +    res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
  3863   3863     }else{
  3864   3864       /* The first fields of the two keys are equal and there are no trailing
  3865   3865       ** fields. Return pPKey2->default_rc in this case. */
  3866   3866       res = pPKey2->default_rc;
  3867   3867     }
  3868   3868   
  3869   3869     assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
................................................................................
  3903   3903       nCmp = MIN( pPKey2->aMem[0].n, nStr );
  3904   3904       res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
  3905   3905   
  3906   3906       if( res==0 ){
  3907   3907         res = nStr - pPKey2->aMem[0].n;
  3908   3908         if( res==0 ){
  3909   3909           if( pPKey2->nField>1 ){
  3910         -          res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
         3910  +          res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
  3911   3911           }else{
  3912   3912             res = pPKey2->default_rc;
  3913   3913           }
  3914   3914         }else if( res>0 ){
  3915   3915           res = pPKey2->r2;
  3916   3916         }else{
  3917   3917           res = pPKey2->r1;

Changes to src/vdbesort.c.

   287    287   **      to sqlite3ThreadJoin() is likely to block. Cases that are likely to
   288    288   **      block provoke debugging output.
   289    289   **
   290    290   ** In both cases, the effects of the main thread seeing (bDone==0) even
   291    291   ** after the thread has finished are not dire. So we don't worry about
   292    292   ** memory barriers and such here.
   293    293   */
          294  +typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
   294    295   struct SortSubtask {
   295    296     SQLiteThread *pThread;          /* Background thread, if any */
   296    297     int bDone;                      /* Set if thread is finished but not joined */
   297    298     VdbeSorter *pSorter;            /* Sorter that owns this sub-task */
   298    299     UnpackedRecord *pUnpacked;      /* Space to unpack a record */
   299    300     SorterList list;                /* List for thread to write to a PMA */
   300    301     int nPMA;                       /* Number of PMAs currently in file */
          302  +  SorterCompare xCompare;         /* Compare function to use */
   301    303     SorterFile file;                /* Temp file for level-0 PMAs */
   302    304     SorterFile file2;               /* Space for other PMAs */
   303    305   };
          306  +
   304    307   
   305    308   /*
   306    309   ** Main sorter structure. A single instance of this is allocated for each 
   307    310   ** sorter cursor created by the VDBE.
   308    311   **
   309    312   ** mxKeysize:
   310    313   **   As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
................................................................................
   324    327     SorterList list;                /* List of in-memory records */
   325    328     int iMemory;                    /* Offset of free space in list.aMemory */
   326    329     int nMemory;                    /* Size of list.aMemory allocation in bytes */
   327    330     u8 bUsePMA;                     /* True if one or more PMAs created */
   328    331     u8 bUseThreads;                 /* True to use background threads */
   329    332     u8 iPrev;                       /* Previous thread used to flush PMA */
   330    333     u8 nTask;                       /* Size of aTask[] array */
          334  +  u8 typeMask;
   331    335     SortSubtask aTask[1];           /* One or more subtasks */
   332    336   };
          337  +
          338  +#define SORTER_TYPE_INTEGER 0x01
          339  +#define SORTER_TYPE_TEXT    0x02
   333    340   
   334    341   /*
   335    342   ** An instance of the following object is used to read records out of a
   336    343   ** PMA, in sorted order.  The next key to be read is cached in nKey/aKey.
   337    344   ** aKey might point into aMap or into aBuffer.  If neither of those locations
   338    345   ** contain a contiguous representation of the key, then aAlloc is allocated
   339    346   ** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
................................................................................
   738    745   
   739    746     if( rc==SQLITE_OK ){
   740    747       rc = vdbePmaReaderNext(pReadr);
   741    748     }
   742    749     return rc;
   743    750   }
   744    751   
          752  +/*
          753  +** A version of vdbeSorterCompare() that assumes that it has already been
          754  +** determined that the first field of key1 is equal to the first field of 
          755  +** key2.
          756  +*/
          757  +static int vdbeSorterCompareTail(
          758  +  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
          759  +  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
          760  +  const void *pKey1, int nKey1,   /* Left side of comparison */
          761  +  const void *pKey2, int nKey2    /* Right side of comparison */
          762  +){
          763  +  UnpackedRecord *r2 = pTask->pUnpacked;
          764  +  if( *pbKey2Cached==0 ){
          765  +    sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
          766  +    *pbKey2Cached = 1;
          767  +  }
          768  +  return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
          769  +}
   745    770   
   746    771   /*
   747    772   ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
   748    773   ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
   749    774   ** used by the comparison. Return the result of the comparison.
   750    775   **
   751         -** Before returning, object (pTask->pUnpacked) is populated with the
   752         -** unpacked version of key2. Or, if pKey2 is passed a NULL pointer, then it 
   753         -** is assumed that the (pTask->pUnpacked) structure already contains the 
   754         -** unpacked key to use as key2.
          776  +** If IN/OUT parameter *pbKey2Cached is true when this function is called,
          777  +** it is assumed that (pTask->pUnpacked) contains the unpacked version
          778  +** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
          779  +** version of key2 and *pbKey2Cached set to true before returning.
   755    780   **
   756    781   ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
   757    782   ** to SQLITE_NOMEM.
   758    783   */
   759    784   static int vdbeSorterCompare(
   760    785     SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
          786  +  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
   761    787     const void *pKey1, int nKey1,   /* Left side of comparison */
   762    788     const void *pKey2, int nKey2    /* Right side of comparison */
   763    789   ){
   764    790     UnpackedRecord *r2 = pTask->pUnpacked;
   765         -  if( pKey2 ){
          791  +  if( !*pbKey2Cached ){
   766    792       sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
          793  +    *pbKey2Cached = 1;
   767    794     }
   768    795     return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
   769    796   }
          797  +
          798  +/*
          799  +** A specially optimized version of vdbeSorterCompare() that assumes that
          800  +** the first field of each key is a TEXT value and that the collation
          801  +** sequence to compare them with is BINARY.
          802  +*/
          803  +static int vdbeSorterCompareText(
          804  +  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
          805  +  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
          806  +  const void *pKey1, int nKey1,   /* Left side of comparison */
          807  +  const void *pKey2, int nKey2    /* Right side of comparison */
          808  +){
          809  +  const u8 * const p1 = (const u8 * const)pKey1;
          810  +  const u8 * const p2 = (const u8 * const)pKey2;
          811  +  const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
          812  +  const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
          813  +
          814  +  int n1;
          815  +  int n2;
          816  +  int res;
          817  +
          818  +  getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2;
          819  +  getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2;
          820  +  res = memcmp(v1, v2, MIN(n1, n2));
          821  +  if( res==0 ){
          822  +    res = n1 - n2;
          823  +  }
          824  +
          825  +  if( res==0 ){
          826  +    if( pTask->pSorter->pKeyInfo->nField>1 ){
          827  +      res = vdbeSorterCompareTail(
          828  +          pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
          829  +      );
          830  +    }
          831  +  }else{
          832  +    if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
          833  +      res = res * -1;
          834  +    }
          835  +  }
          836  +
          837  +  return res;
          838  +}
          839  +
          840  +/*
          841  +** A specially optimized version of vdbeSorterCompare() that assumes that
          842  +** the first field of each key is an INTEGER value.
          843  +*/
          844  +static int vdbeSorterCompareInt(
          845  +  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
          846  +  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
          847  +  const void *pKey1, int nKey1,   /* Left side of comparison */
          848  +  const void *pKey2, int nKey2    /* Right side of comparison */
          849  +){
          850  +  const u8 * const p1 = (const u8 * const)pKey1;
          851  +  const u8 * const p2 = (const u8 * const)pKey2;
          852  +  const int s1 = p1[1];                 /* Left hand serial type */
          853  +  const int s2 = p2[1];                 /* Right hand serial type */
          854  +  const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
          855  +  const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
          856  +  int res;                              /* Return value */
          857  +
          858  +  assert( (s1>0 && s1<7) || s1==8 || s1==9 );
          859  +  assert( (s2>0 && s2<7) || s2==8 || s2==9 );
          860  +
          861  +  if( s1>7 && s2>7 ){
          862  +    res = s1 - s2;
          863  +  }else{
          864  +    if( s1==s2 ){
          865  +      if( (*v1 ^ *v2) & 0x80 ){
          866  +        /* The two values have different signs */
          867  +        res = (*v1 & 0x80) ? -1 : +1;
          868  +      }else{
          869  +        /* The two values have the same sign. Compare using memcmp(). */
          870  +        static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 };
          871  +        int i;
          872  +        res = 0;
          873  +        for(i=0; i<aLen[s1]; i++){
          874  +          if( (res = v1[i] - v2[i]) ) break;
          875  +        }
          876  +      }
          877  +    }else{
          878  +      if( s2>7 ){
          879  +        res = +1;
          880  +      }else if( s1>7 ){
          881  +        res = -1;
          882  +      }else{
          883  +        res = s1 - s2;
          884  +      }
          885  +      assert( res!=0 );
          886  +
          887  +      if( res>0 ){
          888  +        if( *v1 & 0x80 ) res = -1;
          889  +      }else{
          890  +        if( *v2 & 0x80 ) res = +1;
          891  +      }
          892  +    }
          893  +  }
          894  +
          895  +  if( res==0 ){
          896  +    if( pTask->pSorter->pKeyInfo->nField>1 ){
          897  +      res = vdbeSorterCompareTail(
          898  +          pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
          899  +      );
          900  +    }
          901  +  }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
          902  +    res = res * -1;
          903  +  }
          904  +
          905  +  return res;
          906  +}
   770    907   
   771    908   /*
   772    909   ** Initialize the temporary index cursor just opened as a sorter cursor.
   773    910   **
   774    911   ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
   775    912   ** to determine the number of fields that should be compared from the
   776    913   ** records being sorted. However, if the value passed as argument nField
................................................................................
   831    968     pCsr->pSorter = pSorter;
   832    969     if( pSorter==0 ){
   833    970       rc = SQLITE_NOMEM;
   834    971     }else{
   835    972       pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
   836    973       memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
   837    974       pKeyInfo->db = 0;
   838         -    if( nField && nWorker==0 ) pKeyInfo->nField = nField;
          975  +    if( nField && nWorker==0 ){
          976  +      pKeyInfo->nXField += (pKeyInfo->nField - nField);
          977  +      pKeyInfo->nField = nField;
          978  +    }
   839    979       pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
   840    980       pSorter->nTask = nWorker + 1;
          981  +    pSorter->iPrev = nWorker-1;
   841    982       pSorter->bUseThreads = (pSorter->nTask>1);
   842    983       pSorter->db = db;
   843    984       for(i=0; i<pSorter->nTask; i++){
   844    985         SortSubtask *pTask = &pSorter->aTask[i];
   845    986         pTask->pSorter = pSorter;
   846    987       }
   847    988   
................................................................................
   859   1000         if( sqlite3GlobalConfig.pScratch==0 ){
   860   1001           assert( pSorter->iMemory==0 );
   861   1002           pSorter->nMemory = pgsz;
   862   1003           pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
   863   1004           if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM;
   864   1005         }
   865   1006       }
         1007  +
         1008  +    if( (pKeyInfo->nField+pKeyInfo->nXField)<13 
         1009  +     && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
         1010  +    ){
         1011  +      pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
         1012  +    }
   866   1013     }
   867   1014   
   868   1015     return rc;
   869   1016   }
   870   1017   #undef nWorker   /* Defined at the top of this function */
   871   1018   
   872   1019   /*
................................................................................
   883   1030   
   884   1031   /*
   885   1032   ** Free all resources owned by the object indicated by argument pTask. All 
   886   1033   ** fields of *pTask are zeroed before returning.
   887   1034   */
   888   1035   static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
   889   1036     sqlite3DbFree(db, pTask->pUnpacked);
   890         -  pTask->pUnpacked = 0;
   891   1037   #if SQLITE_MAX_WORKER_THREADS>0
   892   1038     /* pTask->list.aMemory can only be non-zero if it was handed memory
   893   1039     ** from the main thread.  That only occurs SQLITE_MAX_WORKER_THREADS>0 */
   894   1040     if( pTask->list.aMemory ){
   895   1041       sqlite3_free(pTask->list.aMemory);
   896         -    pTask->list.aMemory = 0;
   897   1042     }else
   898   1043   #endif
   899   1044     {
   900   1045       assert( pTask->list.aMemory==0 );
   901   1046       vdbeSorterRecordFree(0, pTask->list.pList);
   902   1047     }
   903         -  pTask->list.pList = 0;
   904   1048     if( pTask->file.pFd ){
   905   1049       sqlite3OsCloseFree(pTask->file.pFd);
   906         -    pTask->file.pFd = 0;
   907         -    pTask->file.iEof = 0;
   908   1050     }
   909   1051     if( pTask->file2.pFd ){
   910   1052       sqlite3OsCloseFree(pTask->file2.pFd);
   911         -    pTask->file2.pFd = 0;
   912         -    pTask->file2.iEof = 0;
   913   1053     }
         1054  +  memset(pTask, 0, sizeof(SortSubtask));
   914   1055   }
   915   1056   
   916   1057   #ifdef SQLITE_DEBUG_SORTER_THREADS
   917   1058   static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
   918   1059     i64 t;
   919   1060     int iTask = (pTask - pTask->pSorter->aTask);
   920   1061     sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
................................................................................
  1086   1227     }
  1087   1228   #endif
  1088   1229     vdbeMergeEngineFree(pSorter->pMerger);
  1089   1230     pSorter->pMerger = 0;
  1090   1231     for(i=0; i<pSorter->nTask; i++){
  1091   1232       SortSubtask *pTask = &pSorter->aTask[i];
  1092   1233       vdbeSortSubtaskCleanup(db, pTask);
         1234  +    pTask->pSorter = pSorter;
  1093   1235     }
  1094   1236     if( pSorter->list.aMemory==0 ){
  1095   1237       vdbeSorterRecordFree(0, pSorter->list.pList);
  1096   1238     }
  1097   1239     pSorter->list.pList = 0;
  1098   1240     pSorter->list.szPMA = 0;
  1099   1241     pSorter->bUsePMA = 0;
................................................................................
  1195   1337     SortSubtask *pTask,             /* Calling thread context */
  1196   1338     SorterRecord *p1,               /* First list to merge */
  1197   1339     SorterRecord *p2,               /* Second list to merge */
  1198   1340     SorterRecord **ppOut            /* OUT: Head of merged list */
  1199   1341   ){
  1200   1342     SorterRecord *pFinal = 0;
  1201   1343     SorterRecord **pp = &pFinal;
  1202         -  void *pVal2 = p2 ? SRVAL(p2) : 0;
         1344  +  int bCached = 0;
  1203   1345   
  1204   1346     while( p1 && p2 ){
  1205   1347       int res;
  1206         -    res = vdbeSorterCompare(pTask, SRVAL(p1), p1->nVal, pVal2, p2->nVal);
         1348  +    res = pTask->xCompare(
         1349  +        pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
         1350  +    );
         1351  +
  1207   1352       if( res<=0 ){
  1208   1353         *pp = p1;
  1209   1354         pp = &p1->u.pNext;
  1210   1355         p1 = p1->u.pNext;
  1211         -      pVal2 = 0;
  1212   1356       }else{
  1213   1357         *pp = p2;
  1214         -       pp = &p2->u.pNext;
         1358  +      pp = &p2->u.pNext;
  1215   1359         p2 = p2->u.pNext;
  1216         -      if( p2==0 ) break;
  1217         -      pVal2 = SRVAL(p2);
         1360  +      bCached = 0;
  1218   1361       }
  1219   1362     }
  1220   1363     *pp = p1 ? p1 : p2;
  1221   1364     *ppOut = pFinal;
  1222   1365   }
         1366  +
         1367  +/*
         1368  +** Return the SorterCompare function to compare values collected by the
         1369  +** sorter object passed as the only argument.
         1370  +*/
         1371  +static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
         1372  +  if( p->typeMask==SORTER_TYPE_INTEGER ){
         1373  +    return vdbeSorterCompareInt;
         1374  +  }else if( p->typeMask==SORTER_TYPE_TEXT ){
         1375  +    return vdbeSorterCompareText; 
         1376  +  }
         1377  +  return vdbeSorterCompare;
         1378  +}
  1223   1379   
  1224   1380   /*
  1225   1381   ** Sort the linked list of records headed at pTask->pList. Return 
  1226   1382   ** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if 
  1227   1383   ** an error occurs.
  1228   1384   */
  1229   1385   static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
................................................................................
  1231   1387     SorterRecord **aSlot;
  1232   1388     SorterRecord *p;
  1233   1389     int rc;
  1234   1390   
  1235   1391     rc = vdbeSortAllocUnpacked(pTask);
  1236   1392     if( rc!=SQLITE_OK ) return rc;
  1237   1393   
         1394  +  p = pList->pList;
         1395  +  pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
         1396  +
  1238   1397     aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
  1239   1398     if( !aSlot ){
  1240   1399       return SQLITE_NOMEM;
  1241   1400     }
  1242   1401   
  1243         -  p = pList->pList;
  1244   1402     while( p ){
  1245   1403       SorterRecord *pNext;
  1246   1404       if( pList->aMemory ){
  1247   1405         if( (u8*)p==pList->aMemory ){
  1248   1406           pNext = 0;
  1249   1407         }else{
  1250   1408           assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
................................................................................
  1450   1608     rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
  1451   1609   
  1452   1610     /* Update contents of aTree[] */
  1453   1611     if( rc==SQLITE_OK ){
  1454   1612       int i;                      /* Index of aTree[] to recalculate */
  1455   1613       PmaReader *pReadr1;         /* First PmaReader to compare */
  1456   1614       PmaReader *pReadr2;         /* Second PmaReader to compare */
  1457         -    u8 *pKey2;                  /* To pReadr2->aKey, or 0 if record cached */
         1615  +    int bCached = 0;
  1458   1616   
  1459   1617       /* Find the first two PmaReaders to compare. The one that was just
  1460   1618       ** advanced (iPrev) and the one next to it in the array.  */
  1461   1619       pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
  1462   1620       pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
  1463         -    pKey2 = pReadr2->aKey;
  1464   1621   
  1465   1622       for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
  1466   1623         /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
  1467   1624         int iRes;
  1468   1625         if( pReadr1->pFd==0 ){
  1469   1626           iRes = +1;
  1470   1627         }else if( pReadr2->pFd==0 ){
  1471   1628           iRes = -1;
  1472   1629         }else{
  1473         -        iRes = vdbeSorterCompare(pTask, 
  1474         -            pReadr1->aKey, pReadr1->nKey, pKey2, pReadr2->nKey
         1630  +        iRes = pTask->xCompare(pTask, &bCached,
         1631  +            pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
  1475   1632           );
  1476   1633         }
  1477   1634   
  1478   1635         /* If pReadr1 contained the smaller value, set aTree[i] to its index.
  1479   1636         ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
  1480   1637         ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
  1481   1638         ** pKey2 to point to the record belonging to pReadr2.
................................................................................
  1489   1646         ** If the two values were equal, then the value from the oldest
  1490   1647         ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
  1491   1648         ** is sorted from oldest to newest, so pReadr1 contains older values
  1492   1649         ** than pReadr2 iff (pReadr1<pReadr2).  */
  1493   1650         if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
  1494   1651           pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
  1495   1652           pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
  1496         -        pKey2 = pReadr2->aKey;
         1653  +        bCached = 0;
  1497   1654         }else{
  1498         -        if( pReadr1->pFd ) pKey2 = 0;
         1655  +        if( pReadr1->pFd ) bCached = 0;
  1499   1656           pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
  1500   1657           pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
  1501   1658         }
  1502   1659       }
  1503   1660       *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
  1504   1661     }
  1505   1662   
................................................................................
  1598   1755     VdbeSorter *pSorter = pCsr->pSorter;
  1599   1756     int rc = SQLITE_OK;             /* Return Code */
  1600   1757     SorterRecord *pNew;             /* New list element */
  1601   1758   
  1602   1759     int bFlush;                     /* True to flush contents of memory to PMA */
  1603   1760     int nReq;                       /* Bytes of memory required */
  1604   1761     int nPMA;                       /* Bytes of PMA space required */
         1762  +  int t;                          /* serial type of first record field */
         1763  +
         1764  +  getVarint32((const u8*)&pVal->z[1], t);
         1765  +  if( t>0 && t<10 && t!=7 ){
         1766  +    pSorter->typeMask &= SORTER_TYPE_INTEGER;
         1767  +  }else if( t>10 && (t & 0x01) ){
         1768  +    pSorter->typeMask &= SORTER_TYPE_TEXT;
         1769  +  }else{
         1770  +    pSorter->typeMask = 0;
         1771  +  }
  1605   1772   
  1606   1773     assert( pSorter );
  1607   1774   
  1608   1775     /* Figure out whether or not the current contents of memory should be
  1609   1776     ** flushed to a PMA before continuing. If so, do so.
  1610   1777     **
  1611   1778     ** If using the single large allocation mode (pSorter->aMemory!=0), then
................................................................................
  1863   2030     p2 = &pMerger->aReadr[i2];
  1864   2031   
  1865   2032     if( p1->pFd==0 ){
  1866   2033       iRes = i2;
  1867   2034     }else if( p2->pFd==0 ){
  1868   2035       iRes = i1;
  1869   2036     }else{
         2037  +    SortSubtask *pTask = pMerger->pTask;
         2038  +    int bCached = 0;
  1870   2039       int res;
  1871         -    assert( pMerger->pTask->pUnpacked!=0 );  /* from vdbeSortSubtaskMain() */
  1872         -    res = vdbeSorterCompare(
  1873         -        pMerger->pTask, p1->aKey, p1->nKey, p2->aKey, p2->nKey
         2040  +    assert( pTask->pUnpacked!=0 );  /* from vdbeSortSubtaskMain() */
         2041  +    res = pTask->xCompare(
         2042  +        pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
  1874   2043       );
  1875   2044       if( res<=0 ){
  1876   2045         iRes = i1;
  1877   2046       }else{
  1878   2047         iRes = i2;
  1879   2048       }
  1880   2049     }
................................................................................
  2284   2453   */
  2285   2454   static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
  2286   2455     int rc;                         /* Return code */
  2287   2456     SortSubtask *pTask0 = &pSorter->aTask[0];
  2288   2457     MergeEngine *pMain = 0;
  2289   2458   #if SQLITE_MAX_WORKER_THREADS
  2290   2459     sqlite3 *db = pTask0->pSorter->db;
         2460  +  int i;
         2461  +  SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
         2462  +  for(i=0; i<pSorter->nTask; i++){
         2463  +    pSorter->aTask[i].xCompare = xCompare;
         2464  +  }
  2291   2465   #endif
  2292   2466   
  2293   2467     rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
  2294   2468     if( rc==SQLITE_OK ){
  2295   2469   #if SQLITE_MAX_WORKER_THREADS
  2296   2470       assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
  2297   2471       if( pSorter->bUseThreads ){

Changes to src/vtab.c.

    20     20   ** this struct allocated on the stack. It is used by the implementation of 
    21     21   ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
    22     22   ** are invoked only from within xCreate and xConnect methods.
    23     23   */
    24     24   struct VtabCtx {
    25     25     VTable *pVTable;    /* The virtual table being constructed */
    26     26     Table *pTab;        /* The Table object to which the virtual table belongs */
           27  +  VtabCtx *pPrior;    /* Parent context (if any) */
           28  +  int bDeclared;      /* True after sqlite3_declare_vtab() is called */
    27     29   };
    28     30   
    29     31   /*
    30     32   ** The actual function that does the work of creating a new module.
    31     33   ** This function implements the sqlite3_create_module() and
    32     34   ** sqlite3_create_module_v2() interfaces.
    33     35   */
................................................................................
   483    485   static int vtabCallConstructor(
   484    486     sqlite3 *db, 
   485    487     Table *pTab,
   486    488     Module *pMod,
   487    489     int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
   488    490     char **pzErr
   489    491   ){
   490         -  VtabCtx sCtx, *pPriorCtx;
          492  +  VtabCtx sCtx;
   491    493     VTable *pVTable;
   492    494     int rc;
   493    495     const char *const*azArg = (const char *const*)pTab->azModuleArg;
   494    496     int nArg = pTab->nModuleArg;
   495    497     char *zErr = 0;
   496         -  char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
          498  +  char *zModuleName;
   497    499     int iDb;
          500  +  VtabCtx *pCtx;
   498    501   
          502  +  /* Check that the virtual-table is not already being initialized */
          503  +  for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
          504  +    if( pCtx->pTab==pTab ){
          505  +      *pzErr = sqlite3MPrintf(db, 
          506  +          "vtable constructor called recursively: %s", pTab->zName
          507  +      );
          508  +      return SQLITE_LOCKED;
          509  +    }
          510  +  }
          511  +
          512  +  zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
   499    513     if( !zModuleName ){
   500    514       return SQLITE_NOMEM;
   501    515     }
   502    516   
   503    517     pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
   504    518     if( !pVTable ){
   505    519       sqlite3DbFree(db, zModuleName);
................................................................................
   512    526     pTab->azModuleArg[1] = db->aDb[iDb].zName;
   513    527   
   514    528     /* Invoke the virtual table constructor */
   515    529     assert( &db->pVtabCtx );
   516    530     assert( xConstruct );
   517    531     sCtx.pTab = pTab;
   518    532     sCtx.pVTable = pVTable;
   519         -  pPriorCtx = db->pVtabCtx;
          533  +  sCtx.pPrior = db->pVtabCtx;
          534  +  sCtx.bDeclared = 0;
   520    535     db->pVtabCtx = &sCtx;
   521    536     rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
   522         -  db->pVtabCtx = pPriorCtx;
          537  +  db->pVtabCtx = sCtx.pPrior;
   523    538     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
          539  +  assert( sCtx.pTab==pTab );
   524    540   
   525    541     if( SQLITE_OK!=rc ){
   526    542       if( zErr==0 ){
   527    543         *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
   528    544       }else {
   529    545         *pzErr = sqlite3MPrintf(db, "%s", zErr);
   530    546         sqlite3_free(zErr);
................................................................................
   532    548       sqlite3DbFree(db, pVTable);
   533    549     }else if( ALWAYS(pVTable->pVtab) ){
   534    550       /* Justification of ALWAYS():  A correct vtab constructor must allocate
   535    551       ** the sqlite3_vtab object if successful.  */
   536    552       memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
   537    553       pVTable->pVtab->pModule = pMod->pModule;
   538    554       pVTable->nRef = 1;
   539         -    if( sCtx.pTab ){
          555  +    if( sCtx.bDeclared==0 ){
   540    556         const char *zFormat = "vtable constructor did not declare schema: %s";
   541    557         *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
   542    558         sqlite3VtabUnlock(pVTable);
   543    559         rc = SQLITE_ERROR;
   544    560       }else{
   545    561         int iCol;
   546    562         /* If everything went according to plan, link the new VTable structure
................................................................................
   702    718   
   703    719   /*
   704    720   ** This function is used to set the schema of a virtual table.  It is only
   705    721   ** valid to call this function from within the xCreate() or xConnect() of a
   706    722   ** virtual table module.
   707    723   */
   708    724   int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
          725  +  VtabCtx *pCtx;
   709    726     Parse *pParse;
   710         -
   711    727     int rc = SQLITE_OK;
   712    728     Table *pTab;
   713    729     char *zErr = 0;
   714    730   
   715    731   #ifdef SQLITE_ENABLE_API_ARMOR
   716    732     if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
   717    733       return SQLITE_MISUSE_BKPT;
   718    734     }
   719    735   #endif
   720    736     sqlite3_mutex_enter(db->mutex);
   721         -  if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
          737  +  pCtx = db->pVtabCtx;
          738  +  if( !pCtx || pCtx->bDeclared ){
   722    739       sqlite3Error(db, SQLITE_MISUSE);
   723    740       sqlite3_mutex_leave(db->mutex);
   724    741       return SQLITE_MISUSE_BKPT;
   725    742     }
          743  +  pTab = pCtx->pTab;
   726    744     assert( (pTab->tabFlags & TF_Virtual)!=0 );
   727    745   
   728    746     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
   729    747     if( pParse==0 ){
   730    748       rc = SQLITE_NOMEM;
   731    749     }else{
   732    750       pParse->declareVtab = 1;
................................................................................
   741    759       ){
   742    760         if( !pTab->aCol ){
   743    761           pTab->aCol = pParse->pNewTable->aCol;
   744    762           pTab->nCol = pParse->pNewTable->nCol;
   745    763           pParse->pNewTable->nCol = 0;
   746    764           pParse->pNewTable->aCol = 0;
   747    765         }
   748         -      db->pVtabCtx->pTab = 0;
          766  +      pCtx->bDeclared = 1;
   749    767       }else{
   750    768         sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
   751    769         sqlite3DbFree(db, zErr);
   752    770         rc = SQLITE_ERROR;
   753    771       }
   754    772       pParse->declareVtab = 0;
   755    773     

Changes to src/wal.c.

  1726   1726       ** safe to write into the database.  Frames beyond mxSafeFrame might
  1727   1727       ** overwrite database pages that are in use by active readers and thus
  1728   1728       ** cannot be backfilled from the WAL.
  1729   1729       */
  1730   1730       mxSafeFrame = pWal->hdr.mxFrame;
  1731   1731       mxPage = pWal->hdr.nPage;
  1732   1732       for(i=1; i<WAL_NREADER; i++){
         1733  +      /* Thread-sanitizer reports that the following is an unsafe read,
         1734  +      ** as some other thread may be in the process of updating the value
         1735  +      ** of the aReadMark[] slot. The assumption here is that if that is
         1736  +      ** happening, the other client may only be increasing the value,
         1737  +      ** not decreasing it. So assuming either that either the "old" or
         1738  +      ** "new" version of the value is read, and not some arbitrary value
         1739  +      ** that would never be written by a real client, things are still 
         1740  +      ** safe.  */
  1733   1741         u32 y = pInfo->aReadMark[i];
  1734   1742         if( mxSafeFrame>y ){
  1735   1743           assert( y<=pWal->hdr.mxFrame );
  1736   1744           rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
  1737   1745           if( rc==SQLITE_OK ){
  1738   1746             pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
  1739   1747             walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);

Changes to src/where.c.

  1530   1530     for(i=0; i<pList->nExpr; i++){
  1531   1531       Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
  1532   1532       if( p->op==TK_COLUMN
  1533   1533        && p->iColumn==pIdx->aiColumn[iCol]
  1534   1534        && p->iTable==iBase
  1535   1535       ){
  1536   1536         CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
  1537         -      if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
         1537  +      if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){
  1538   1538           return i;
  1539   1539         }
  1540   1540       }
  1541   1541     }
  1542   1542   
  1543   1543     return -1;
  1544   1544   }
................................................................................
  1804   1804         testcase( iCol==BMS-1 );
  1805   1805         testcase( iCol==BMS );
  1806   1806         if( (idxCols & cMask)==0 ){
  1807   1807           Expr *pX = pTerm->pExpr;
  1808   1808           idxCols |= cMask;
  1809   1809           pIdx->aiColumn[n] = pTerm->u.leftColumn;
  1810   1810           pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
  1811         -        pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
         1811  +        pIdx->azColl[n] = pColl ? pColl->zName : "BINARY";
  1812   1812           n++;
  1813   1813         }
  1814   1814       }
  1815   1815     }
  1816   1816     assert( (u32)n==pLoop->u.btree.nEq );
  1817   1817   
  1818   1818     /* Add additional columns needed to make the automatic index into
................................................................................
  4782   4782         }
  4783   4783         assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
  4784   4784                           ** changes "x IN (?)" into "x=?". */
  4785   4785   
  4786   4786       }else if( eOp & (WO_EQ) ){
  4787   4787         pNew->wsFlags |= WHERE_COLUMN_EQ;
  4788   4788         if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
  4789         -        if( iCol>=0 && !IsUniqueIndex(pProbe) ){
         4789  +        if( iCol>=0 && pProbe->uniqNotNull==0 ){
  4790   4790             pNew->wsFlags |= WHERE_UNQ_WANTED;
  4791   4791           }else{
  4792   4792             pNew->wsFlags |= WHERE_ONEROW;
  4793   4793           }
  4794   4794         }
  4795   4795       }else if( eOp & WO_ISNULL ){
  4796   4796         pNew->wsFlags |= WHERE_COLUMN_NULL;
................................................................................
  6241   6241         }
  6242   6242       }else{
  6243   6243         pWInfo->nOBSat = pFrom->isOrdered;
  6244   6244         if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
  6245   6245         pWInfo->revMask = pFrom->revLoop;
  6246   6246       }
  6247   6247       if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
  6248         -        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr
         6248  +        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
  6249   6249       ){
  6250   6250         Bitmask revMask = 0;
  6251   6251         int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, 
  6252   6252             pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
  6253   6253         );
  6254   6254         assert( pWInfo->sorted==0 );
  6255   6255         if( nOrder==pWInfo->pOrderBy->nExpr ){

Changes to test/autoindex1.test.

   508    508     CREATE VIEW agg2 AS SELECT a, sum(b) AS m FROM t2 GROUP BY a;
   509    509     EXPLAIN QUERY PLAN
   510    510     SELECT t1.z, agg2.m
   511    511       FROM t1 JOIN agg2 ON t1.y=agg2.m
   512    512      WHERE t1.x IN (1,2,3);
   513    513   } {/USING AUTOMATIC COVERING INDEX/}
   514    514   
          515  +# 2015-04-15:  A NULL CollSeq pointer in automatic index creation.
          516  +#
          517  +do_execsql_test autoindex1-920 {
          518  +  CREATE TABLE t920(x);
          519  +  INSERT INTO t920 VALUES(3),(4),(5);
          520  +  SELECT * FROM t920,(SELECT 0 FROM t920),(VALUES(9)) WHERE 5 IN (x);
          521  +} {5 0 9 5 0 9 5 0 9}
   515    522   
   516    523   finish_test

Changes to test/collate1.test.

   381    381   
   382    382     INSERT INTO c1 VALUES(2, 'abb');
   383    383     INSERT INTO c1 VALUES(3, 'wxz');
   384    384     INSERT INTO c1 VALUES(4, 'WXY');
   385    385     SELECT x, y FROM c1 ORDER BY y COLLATE """""""";
   386    386   } {2 abb 1 ABC 4 WXY 3 wxz}
   387    387   
   388         -finish_test
          388  +# 2015-04-15:  Nested COLLATE operators
          389  +#
          390  +do_execsql_test 7.0 {
          391  +   SELECT 'abc' UNION ALL SELECT 'DEF'
          392  +    ORDER BY 1 COLLATE nocase COLLATE nocase COLLATE nocase COLLATE nocase;
          393  +} {abc DEF}
          394  +do_execsql_test 7.1 {
          395  +   SELECT 'abc' UNION ALL SELECT 'DEF'
          396  +    ORDER BY 1 COLLATE nocase COLLATE nocase COLLATE nocase COLLATE binary;
          397  +} {DEF abc}
          398  +do_execsql_test 7.2 {
          399  +   SELECT 'abc' UNION ALL SELECT 'DEF'
          400  +    ORDER BY 1 COLLATE binary COLLATE binary COLLATE binary COLLATE nocase;
          401  +} {abc DEF}
   389    402   
   390    403   
          404  +finish_test

Changes to test/collate3.test.

    28     28   
    29     29   #
    30     30   # These tests ensure that when a user executes a statement with an 
    31     31   # unknown collation sequence an error is returned.
    32     32   #
    33     33   do_test collate3-1.0 {
    34     34     execsql {
    35         -    CREATE TABLE collate3t1(c1);
           35  +    CREATE TABLE collate3t1(c1 UNIQUE);
    36     36     }
    37     37   } {}
    38     38   do_test collate3-1.1 {
    39     39     catchsql {
    40     40       SELECT * FROM collate3t1 ORDER BY 1 collate garbage;
    41     41     }
           42  +} {1 {no such collation sequence: garbage}}
           43  +do_test collate3-1.1.2 {
           44  +  catchsql {
           45  +    SELECT DISTINCT c1 COLLATE garbage FROM collate3t1;
           46  +  }
    42     47   } {1 {no such collation sequence: garbage}}
    43     48   do_test collate3-1.2 {
    44     49     catchsql {
    45     50       CREATE TABLE collate3t2(c1 collate garbage);
    46     51     }
    47     52   } {1 {no such collation sequence: garbage}}
    48     53   do_test collate3-1.3 {

Changes to test/fkey2.test.

   742    742   
   743    743   #-------------------------------------------------------------------------
   744    744   # The following tests, fkey2-11.*, test CASCADE actions.
   745    745   #
   746    746   drop_all_tables
   747    747   do_test fkey2-11.1.1 {
   748    748     execsql {
   749         -    CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
          749  +    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, rowid, _rowid_, oid);
   750    750       CREATE TABLE t2(c, d, FOREIGN KEY(c) REFERENCES t1(a) ON UPDATE CASCADE);
   751    751   
   752         -    INSERT INTO t1 VALUES(10, 100);
          752  +    INSERT INTO t1 VALUES(10, 100, 'abc', 'def', 'ghi');
   753    753       INSERT INTO t2 VALUES(10, 100);
   754    754       UPDATE t1 SET a = 15;
   755    755       SELECT * FROM t2;
   756    756     }
   757    757   } {15 100}
   758    758   
   759    759   #-------------------------------------------------------------------------

Changes to test/fts3snippet.test.

   533    533   } {{[one <b>two</b> three]}}
   534    534   do_execsql_test 3.3 {
   535    535     SELECT snippet(t3) FROM t3 WHERE t3 MATCH 'three';
   536    536   } {{[one two <b>three</b>]}}
   537    537   do_execsql_test 3.4 {
   538    538     SELECT snippet(t3) FROM t3 WHERE t3 MATCH 'one OR two OR three';
   539    539   } {{[<b>one</b> <b>two</b> <b>three</b>]}}
          540  +
          541  +#-------------------------------------------------------------------------
          542  +# Request a snippet 0 tokens in size. This is always an empty string.
          543  +do_execsql_test 4.1 {
          544  +  CREATE VIRTUAL TABLE t4 USING fts4;
          545  +  INSERT INTO t4 VALUES('a b c d');
          546  +  SELECT snippet(t4, '[', ']', '...', 0, 0) FROM t4 WHERE t4 MATCH 'b';
          547  +} {{}}
          548  +
          549  +do_test 4.2 {
          550  +  set x35 [string trim [string repeat "x " 35]]
          551  +  execsql "INSERT INTO t4 VALUES('$x35 E $x35 F $x35 G $x35');"
          552  +  llength [db one {
          553  +    SELECT snippet(t4, '', '', '', 0, 64) FROM t4 WHERE t4 MATCH 'E'
          554  +  }]
          555  +} {64}
          556  +
          557  +
          558  +
   540    559   
   541    560   set sqlite_fts3_enable_parentheses 0
   542    561   finish_test
   543    562   

Changes to test/fts3tok1.test.

   105    105     CREATE VIRTUAL TABLE tX USING fts3tokenize(nosuchtokenizer);
   106    106   } {1 {unknown tokenizer: nosuchtokenizer}}
   107    107   
   108    108   do_catchsql_test 2.1 {
   109    109     CREATE VIRTUAL TABLE t4 USING fts3tokenize;
   110    110     SELECT * FROM t4;
   111    111   } {1 {SQL logic error or missing database}}
          112  +
          113  +do_catchsql_test 2.2 {
          114  +  CREATE VIRTUAL TABLE t USING fts4(tokenize=simple""); 
          115  +} {0 {}}
          116  +
          117  +ifcapable fts3_unicode {
          118  +  do_catchsql_test 2.3 {
          119  +    CREATE VIRTUAL TABLE u USING fts4(tokenize=unicode61""); 
          120  +  } {1 {unknown tokenizer}}
          121  +}
   112    122   
   113    123   
   114    124   finish_test

Changes to test/fts4content.test.

    44     44   #         SELECT statements.
    45     45   #
    46     46   #   8.* - Test that if the content=xxx and prefix options are used together,
    47     47   #         the 'rebuild' command still works.
    48     48   #
    49     49   #   9.* - Test using content=xxx where xxx is a virtual table.
    50     50   #
           51  +#   11.* - Test that circular references (e.g. "t1(content=t1)") are
           52  +#          detected.
           53  +#
    51     54   
    52     55   do_execsql_test 1.1.1 {
    53     56     CREATE TABLE t1(a, b, c);
    54     57     INSERT INTO t1 VALUES('w x', 'x y', 'y z');
    55     58     CREATE VIRTUAL TABLE ft1 USING fts4(content=t1);
    56     59   }
    57     60   
................................................................................
   402    405   }
   403    406   
   404    407   #-------------------------------------------------------------------------
   405    408   # Test cases 6.* test 
   406    409   # 
   407    410   do_catchsql_test 6.1.1 {
   408    411     CREATE VIRTUAL TABLE ft7 USING fts4(content=t7);
   409         -} {1 {vtable constructor failed: ft7}}
          412  +} {1 {no such table: main.t7}}
   410    413   
   411    414   do_execsql_test 6.2.1 {
   412    415     CREATE TABLE t7(one, two);
   413    416     CREATE VIRTUAL TABLE ft7 USING fts4(content=t7);
   414    417     INSERT INTO t7 VALUES('A B', 'B A');
   415    418     INSERT INTO t7 VALUES('C D', 'A A');
   416    419     SELECT * FROM ft7;
................................................................................
   429    432     SELECT name FROM sqlite_master WHERE name LIKE '%t7%'
   430    433   } {
   431    434     ft7 ft7_segments ft7_segdir sqlite_autoindex_ft7_segdir_1 
   432    435     ft7_docsize ft7_stat
   433    436   }
   434    437   do_catchsql_test 6.2.4 {
   435    438     SELECT * FROM ft7;
   436         -} {1 {vtable constructor failed: ft7}}
          439  +} {1 {no such table: main.t7}}
   437    440   do_execsql_test 6.2.5 {
   438    441     CREATE TABLE t7(x, y);
   439    442     INSERT INTO t7 VALUES('A B', 'B A');
   440    443     INSERT INTO t7 VALUES('C D', 'A A');
   441    444     SELECT * FROM ft7;
   442    445   } {
   443    446     {A B} {B A} {C D} {A A}
................................................................................
   617    620   do_execsql_test 10.6 { DELETE FROM ft WHERE docid=2 }
   618    621   
   619    622   do_execsql_test 10.7 {
   620    623     SELECT snippet(ft, '[', ']', '...', -1, 5) FROM ft WHERE ft MATCH 'e'
   621    624   } {
   622    625     {...c d [e] f g...}
   623    626   }
          627  +
          628  +#-------------------------------------------------------------------------
          629  +# Test cases 11.*
          630  +# 
          631  +reset_db
          632  +
          633  +do_catchsql_test 11.1 {
          634  +  CREATE VIRTUAL TABLE x1 USING fts4(content=x1);
          635  +} {1 {vtable constructor called recursively: x1}}
          636  +
   624    637   
   625    638   finish_test
          639  +

Changes to test/hexlit.test.

   105    105   } {0}
   106    106   
   107    107   # Oversized hex literals are rejected
   108    108   #
   109    109   do_catchsql_test hexlist-400 {
   110    110     SELECT 0x10000000000000000;
   111    111   } {1 {hex literal too big: 0x10000000000000000}}
          112  +do_catchsql_test hexlist-410 {
          113  +  DROP TABLE IF EXISTS t1;
          114  +  CREATE TABLE t1(x);
          115  +  INSERT INTO t1 VALUES(1+0x10000000000000000);
          116  +} {1 {hex literal too big: 0x10000000000000000}}
   112    117   
   113    118   
   114    119   finish_test

Changes to test/in.test.

   611    611   do_test in-13.14 {
   612    612     execsql {
   613    613       CREATE INDEX i5 ON b(id);
   614    614       SELECT * FROM a WHERE id NOT IN (SELECT id FROM b);
   615    615     }
   616    616   } {}
   617    617   
          618  +do_test in-13.15 {
          619  +  catchsql {
          620  +    SELECT 0 WHERE (SELECT 0,0) OR (0 IN (1,2));
          621  +  }
          622  +} {1 {only a single result allowed for a SELECT that is part of an expression}}
          623  +
   618    624   
   619    625   do_test in-13.X {
   620    626     db nullvalue ""
   621    627   } {}
   622    628   
   623    629   finish_test

Changes to test/misc1.test.

   634    634   #
   635    635   do_catchsql_test misc1-21.1 {
   636    636     select''like''like''like#0;
   637    637   } {1 {near "#0": syntax error}}
   638    638   do_catchsql_test misc1-21.2 {
   639    639     VALUES(0,0x0MATCH#0;
   640    640   } {1 {near ";": syntax error}}
          641  +
          642  +# 2015-04-15
          643  +do_execsql_test misc1-22.1 {
          644  +  SELECT ""+3 FROM (SELECT ""+5);
          645  +} {3}
   641    646   
   642    647   finish_test

Changes to test/null.test.

   274    274   } {1}
   275    275   do_test null-8.15 {
   276    276     execsql {
   277    277       SELECT x FROM t4 WHERE y!=33 ORDER BY x;
   278    278     }
   279    279   } {1}
   280    280   
          281  +do_execsql_test null-9.1 {
          282  +  CREATE TABLE t5(a, b, c);
          283  +  CREATE UNIQUE INDEX t5ab ON t5(a, b);
          284  +
          285  +  INSERT INTO t5 VALUES(1, NULL, 'one');
          286  +  INSERT INTO t5 VALUES(1, NULL, 'i');
          287  +  INSERT INTO t5 VALUES(NULL, 'x', 'two');
          288  +  INSERT INTO t5 VALUES(NULL, 'x', 'ii');
          289  +}
          290  +
          291  +do_execsql_test null-9.2 {
          292  +  SELECT * FROM t5 WHERE a = 1 AND b IS NULL;
          293  +} {1 {} one 1 {} i}
          294  +
          295  +do_execsql_test null-9.3 {
          296  +  SELECT * FROM t5 WHERE a IS NULL AND b = 'x';
          297  +} {{} x two {} x ii}
   281    298   
   282    299   
   283    300   finish_test

Changes to test/orderby1.test.

   459    459   } {}
   460    460   do_execsql_test 5.1 {
   461    461     EXPLAIN QUERY PLAN SELECT 5 UNION ALL SELECT 3 ORDER BY 1
   462    462   } {~/B-TREE/}
   463    463   do_execsql_test 5.2 {
   464    464     SELECT 5 UNION ALL SELECT 3 ORDER BY 1
   465    465   } {3 5}
          466  +do_execsql_test 5.3 {
          467  +  SELECT 986 AS x GROUP BY X ORDER BY X
          468  +} {986}
   466    469   
   467    470   # The following test (originally derived from a single test within fuzz.test)
   468    471   # verifies that a PseudoTable cursor is not closed prematurely in a deeply
   469    472   # nested query.  This test caused a segfault on 3.8.5 beta.
   470    473   #
   471    474   do_execsql_test 6.0 {
   472    475     CREATE TABLE abc(a, b, c);
................................................................................
   491    494     CREATE TABLE t7(a,b);
   492    495     CREATE INDEX t7a ON t7(a);
   493    496     CREATE INDEX t7ab ON t7(a,b);
   494    497     EXPLAIN QUERY PLAN
   495    498     SELECT * FROM t7 WHERE a=?1 ORDER BY rowid;
   496    499   } {~/ORDER BY/}
   497    500   
          501  +#-------------------------------------------------------------------------
          502  +# Test a partial sort large enough to cause the sorter to spill data
          503  +# to disk.
          504  +#
          505  +reset_db
          506  +do_execsql_test 8.0 {
          507  +  PRAGMA cache_size = 5;
          508  +  CREATE TABLE t1(a, b);
          509  +  CREATE INDEX i1 ON t1(a);
          510  +}
          511  +
          512  +do_eqp_test 8.1 {
          513  +  SELECT * FROM t1 ORDER BY a, b;
          514  +} {
          515  +  0 0 0 {SCAN TABLE t1 USING INDEX i1} 
          516  +  0 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY}
          517  +}
          518  +
          519  +do_execsql_test 8.2 {
          520  +  WITH cnt(i) AS (
          521  +    SELECT 1 UNION ALL SELECT i+1 FROM cnt WHERE i<10000
          522  +  )
          523  +  INSERT INTO t1 SELECT i%2, randomblob(500) FROM cnt;
          524  +}
          525  +
          526  +do_test 8.3 {
          527  +  db eval { SELECT * FROM t1 ORDER BY a, b } { incr res $a }
          528  +  set res
          529  +} 5000
   498    530   
   499    531   finish_test

Changes to test/pragma.test.

   747    747               ORDER BY cid}
   748    748   } [concat \
   749    749     {0 one INT 1 -1 0} \
   750    750     {1 two text 0 {} 0} \
   751    751     {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
   752    752     {3 four REAL 0 X'abcdef' 0} \
   753    753     {4 five {} 0 CURRENT_TIME 0} \
          754  +]
          755  +do_test pragma-6.8 {
          756  +  execsql {
          757  +    CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
          758  +    PRAGMA table_info(t68);
          759  +  }
          760  +} [concat \
          761  +  {0 a {} 0 {} 1} \
          762  +  {1 b {} 0 {} 2} \
          763  +  {2 c {} 0 {} 4} \
   754    764   ]
   755    765   } ;# ifcapable schema_pragmas
   756    766   # Miscellaneous tests
   757    767   #
   758    768   ifcapable schema_pragmas {
   759    769   # EVIDENCE-OF: R-63500-32024 PRAGMA database.index_list(table-name);
   760    770   # This pragma returns one row for each index associated with the given

Changes to test/printf2.test.

    54     54   } {314159}
    55     55   do_execsql_test printf2-1.10 {
    56     56     SELECT printf('%lld',314159.2653);
    57     57   } {314159}
    58     58   do_execsql_test printf2-1.11 {
    59     59     SELECT printf('%lld%n',314159.2653,'hi');
    60     60   } {314159}
           61  +do_execsql_test printf2-1.12 {
           62  +  SELECT printf('%n',0);
           63  +} {{}}
    61     64   
    62     65   # EVIDENCE-OF: R-17002-27534 The %z format is interchangeable with %s.
    63     66   #
    64     67   do_execsql_test printf2-1.12 {
    65     68     SELECT printf('%.*z',5,'abcdefghijklmnop');
    66     69   } {abcde}
    67     70   do_execsql_test printf2-1.13 {

Changes to test/select4.test.

   795    795       SELECT x FROM t2
   796    796       UNION ALL
   797    797       SELECT x FROM t2
   798    798       EXCEPT
   799    799       SELECT x FROM t2
   800    800     }
   801    801   } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
          802  +do_test select4-11.16 {
          803  +  catchsql {
          804  +    INSERT INTO t2(rowid) VALUES(2) UNION SELECT 3,4 UNION SELECT 5,6 ORDER BY 1;
          805  +  }
          806  +} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
   802    807   
   803    808   do_test select4-12.1 {
   804    809     sqlite3 db2 :memory:
   805    810     catchsql {
   806    811       SELECT 1 UNION SELECT 2,3 UNION SELECT 4,5 ORDER BY 1;
   807    812     } db2
   808    813   } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
................................................................................
   859    864   } {}
   860    865   do_execsql_test select4-14.8 {
   861    866     SELECT * FROM t14 EXCEPT VALUES('a','b','c') EXCEPT VALUES(4,5,6)
   862    867   } {1 2 3}
   863    868   do_execsql_test select4-14.9 {
   864    869     SELECT * FROM t14 UNION ALL VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3);
   865    870   } {1 2 3 4 5 6 3 2 1 2 3 1 1 2 3 2 1 3}
          871  +do_execsql_test select4-14.10 {
          872  +  SELECT (VALUES(1),(2),(3),(4))
          873  +} {1}
          874  +do_execsql_test select4-14.11 {
          875  +  SELECT (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4)
          876  +} {1}
          877  +
   866    878   
   867    879   finish_test

Changes to test/selectE.test.

    87     87   } {}
    88     88   do_test selectE-2.2 {
    89     89     db eval {
    90     90       SELECT a COLLATE nocase FROM t2 EXCEPT SELECT a FROM t3
    91     91        ORDER BY 1 COLLATE binary
    92     92     }
    93     93   } {}
           94  +
           95  +do_catchsql_test selectE-3.1 {
           96  +  SELECT 1 EXCEPT SELECT 2 ORDER BY 1 COLLATE nocase EXCEPT SELECT 3;
           97  +} {1 {ORDER BY clause should come after EXCEPT not before}}
           98  +
    94     99   
    95    100   finish_test

Changes to test/vacuum2.test.

    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this file is testing the VACUUM statement.
    13     13   #
    14     14   # $Id: vacuum2.test,v 1.10 2009/02/18 20:31:18 drh Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
           18  +set testprefix vacuum2
    18     19   
    19     20   # Do not use a codec for tests in this file, as the database file is
    20     21   # manipulated directly using tcl scripts (using the [hexio_write] command).
    21     22   #
    22     23   do_not_use_codec
    23     24   
    24     25   # If the VACUUM statement is disabled in the current build, skip all
................................................................................
   223    224     db eval {SELECT a, b FROM t1 WHERE a<=10} {
   224    225       if {$a==6} { set res [catchsql VACUUM] }
   225    226       lappend res2 $a
   226    227     }
   227    228     lappend res2 $res
   228    229   } {1 2 3 4 5 6 7 8 9 10 {1 {cannot VACUUM - SQL statements in progress}}}
   229    230   
          231  +#-------------------------------------------------------------------------
          232  +# Check that if the definition of a collation sequence is changed and
          233  +# VACUUM run, records are store in the (new) correct order following the
          234  +# VACUUM. Even if the modified collation is attached to a PK of a WITHOUT
          235  +# ROWID table.
          236  +
          237  +proc cmp {lhs rhs} { string compare $lhs $rhs }
          238  +db collate cmp cmp
          239  +do_execsql_test 6.0 {
          240  +  CREATE TABLE t6(x PRIMARY KEY COLLATE cmp, y) WITHOUT ROWID;
          241  +  CREATE INDEX t6y ON t6(y);
          242  +  INSERT INTO t6 VALUES('i', 'one');
          243  +  INSERT INTO t6 VALUES('ii', 'one');
          244  +  INSERT INTO t6 VALUES('iii', 'one');
          245  +}
          246  +integrity_check 6.1
          247  +proc cmp {lhs rhs} { string compare $rhs $lhs }
          248  +do_execsql_test 6.2 VACUUM
          249  +integrity_check 6.3
   230    250   
   231    251   finish_test

Changes to test/whereD.test.

   125    125     CREATE TABLE t4(x PRIMARY KEY, y);
   126    126     INSERT INTO t4 VALUES('a', 'one');
   127    127     INSERT INTO t4 VALUES('b', 'two');
   128    128   }
   129    129   
   130    130   do_searchcount_test 3.1 {
   131    131     SELECT a, b FROM t3 WHERE (a=1 AND b='one') OR (a=2 AND b='two')
   132         -} {1 one 2 two search 2}
          132  +} {1 one 2 two search 4}
   133    133   
   134    134   do_searchcount_test 3.2 {
   135    135     SELECT a, c FROM t3 WHERE (a=1 AND b='one') OR (a=2 AND b='two')
   136         -} {1 i 2 ii search 4}
          136  +} {1 i 2 ii search 6}
   137    137   
   138    138   do_searchcount_test 3.4.1 {
   139    139     SELECT y FROM t4 WHERE x='a'
   140    140   } {one search 2}
   141    141   do_searchcount_test 3.4.2 {
   142    142     SELECT a, b FROM t3 WHERE 
   143    143           (a=1 AND b=(SELECT y FROM t4 WHERE x='a')) 
   144    144        OR (a=2 AND b='two')
   145         -} {1 one 2 two search 4}
          145  +} {1 one 2 two search 6}
   146    146   do_searchcount_test 3.4.3 {
   147    147     SELECT a, b FROM t3 WHERE 
   148    148           (a=2 AND b='two')
   149    149        OR (a=1 AND b=(SELECT y FROM t4 WHERE x='a')) 
   150         -} {2 two 1 one search 4}
          150  +} {2 two 1 one search 6}
   151    151   do_searchcount_test 3.4.4 {
   152    152     SELECT a, b FROM t3 WHERE 
   153    153           (a=2 AND b=(SELECT y FROM t4 WHERE x='b')) 
   154    154        OR (a=1 AND b=(SELECT y FROM t4 WHERE x='a')) 
   155         -} {2 two 1 one search 6}
          155  +} {2 two 1 one search 8}
   156    156   
   157    157   do_searchcount_test 3.5.1 {
   158    158     SELECT a, b FROM t3 WHERE (a=1 AND b='one') OR rowid=4
   159         -} {1 one 2 two search 2}
          159  +} {1 one 2 two search 3}
   160    160   do_searchcount_test 3.5.2 {
   161    161     SELECT a, c FROM t3 WHERE (a=1 AND b='one') OR rowid=4
   162         -} {1 i 2 ii search 2}
          162  +} {1 i 2 ii search 3}
   163    163   
   164    164   # Ticket [d02e1406a58ea02d] (2012-10-04)
   165    165   # LEFT JOIN with an OR in the ON clause causes segfault 
   166    166   #
   167    167   do_test 4.1 {
   168    168     db eval {
   169    169       CREATE TABLE t41(a,b,c);

Changes to test/with1.test.

   838    838     WITH RECURSIVE c(i) AS (SELECT 5,* UNION ALL SELECT i+1 FROM c WHERE i<10)
   839    839     SELECT i FROM c;
   840    840   } {1 {no tables specified}}
   841    841   do_catchsql_test 13.3 {
   842    842     WITH RECURSIVE c(i,j) AS (SELECT 5,* UNION ALL SELECT i+1,11 FROM c WHERE i<10)
   843    843     SELECT i FROM c;
   844    844   } {1 {table c has 1 values for 2 columns}}
          845  +
          846  +# 2015-04-12
          847  +#
          848  +do_execsql_test 14.1 {
          849  +  WITH x AS (SELECT * FROM t) SELECT 0 EXCEPT SELECT 0 ORDER BY 1 COLLATE binary;
          850  +} {}
   845    851   
   846    852   finish_test

Changes to test/zeroblob.test.

   251    251   do_test zeroblob-9.7 {
   252    252     db eval {SELECT zeroblob(2) IN (zeroblob(3))}
   253    253   } {0}
   254    254   do_test zeroblob-9.8 {
   255    255     db eval {SELECT zeroblob(2) IN (zeroblob(2))}
   256    256   } {1}
   257    257   
          258  +# Oversized zeroblob records
          259  +#
          260  +do_test zeroblob-10.1 {
          261  +  db eval {
          262  +    CREATE TABLE t10(a,b,c);
          263  +  }
          264  +  catchsql {INSERT INTO t10 VALUES(zeroblob(1e9),zeroblob(1e9),zeroblob(1e9))}
          265  +} {1 {string or blob too big}}
          266  +
   258    267   
   259    268   finish_test

Added tool/sqldiff.c.

            1  +/*
            2  +** 2015-04-06
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This is a utility problem that computes the differences in content
           14  +** between two SQLite databases.
           15  +*/
           16  +#include <stdio.h>
           17  +#include <stdlib.h>
           18  +#include <stdarg.h>
           19  +#include <ctype.h>
           20  +#include <string.h>
           21  +#include "sqlite3.h"
           22  +
           23  +/*
           24  +** All global variables are gathered into the "g" singleton.
           25  +*/
           26  +struct GlobalVars {
           27  +  const char *zArgv0;       /* Name of program */
           28  +  int bSchemaOnly;          /* Only show schema differences */
           29  +  int bSchemaPK;            /* Use the schema-defined PK, not the true PK */
           30  +  unsigned fDebug;          /* Debug flags */
           31  +  sqlite3 *db;              /* The database connection */
           32  +} g;
           33  +
           34  +/*
           35  +** Allowed values for g.fDebug
           36  +*/
           37  +#define DEBUG_COLUMN_NAMES  0x000001
           38  +#define DEBUG_DIFF_SQL      0x000002
           39  +
           40  +/*
           41  +** Dynamic string object
           42  +*/
           43  +typedef struct Str Str;
           44  +struct Str {
           45  +  char *z;        /* Text of the string */
           46  +  int nAlloc;     /* Bytes allocated in z[] */
           47  +  int nUsed;      /* Bytes actually used in z[] */
           48  +};
           49  +
           50  +/*
           51  +** Initialize a Str object
           52  +*/
           53  +static void strInit(Str *p){
           54  +  p->z = 0;
           55  +  p->nAlloc = 0;
           56  +  p->nUsed = 0;
           57  +}
           58  +  
           59  +/*
           60  +** Print an error resulting from faulting command-line arguments and
           61  +** abort the program.
           62  +*/
           63  +static void cmdlineError(const char *zFormat, ...){
           64  +  va_list ap;
           65  +  fprintf(stderr, "%s: ", g.zArgv0);
           66  +  va_start(ap, zFormat);
           67  +  vfprintf(stderr, zFormat, ap);
           68  +  va_end(ap);
           69  +  fprintf(stderr, "\n\"%s --help\" for more help\n", g.zArgv0);
           70  +  exit(1);
           71  +}
           72  +
           73  +/*
           74  +** Print an error message for an error that occurs at runtime, then
           75  +** abort the program.
           76  +*/
           77  +static void runtimeError(const char *zFormat, ...){
           78  +  va_list ap;
           79  +  fprintf(stderr, "%s: ", g.zArgv0);
           80  +  va_start(ap, zFormat);
           81  +  vfprintf(stderr, zFormat, ap);
           82  +  va_end(ap);
           83  +  fprintf(stderr, "\n");
           84  +  exit(1);
           85  +}
           86  +
           87  +/*
           88  +** Free all memory held by a Str object
           89  +*/
           90  +static void strFree(Str *p){
           91  +  sqlite3_free(p->z);
           92  +  strInit(p);
           93  +}
           94  +
           95  +/*
           96  +** Add formatted text to the end of a Str object
           97  +*/
           98  +static void strPrintf(Str *p, const char *zFormat, ...){
           99  +  int nNew;
          100  +  for(;;){
          101  +    if( p->z ){
          102  +      va_list ap;
          103  +      va_start(ap, zFormat);
          104  +      sqlite3_vsnprintf(p->nAlloc-p->nUsed, p->z+p->nUsed, zFormat, ap);
          105  +      va_end(ap);
          106  +      nNew = (int)strlen(p->z + p->nUsed);
          107  +    }else{
          108  +      nNew = p->nAlloc;
          109  +    }
          110  +    if( p->nUsed+nNew < p->nAlloc-1 ){
          111  +      p->nUsed += nNew;
          112  +      break;
          113  +    }
          114  +    p->nAlloc = p->nAlloc*2 + 1000;
          115  +    p->z = sqlite3_realloc(p->z, p->nAlloc);
          116  +    if( p->z==0 ) runtimeError("out of memory");
          117  +  }
          118  +}
          119  +
          120  +
          121  +
          122  +/* Safely quote an SQL identifier.  Use the minimum amount of transformation
          123  +** necessary to allow the string to be used with %s.
          124  +**
          125  +** Space to hold the returned string is obtained from sqlite3_malloc().  The
          126  +** caller is responsible for ensuring this space is freed when no longer
          127  +** needed.
          128  +*/
          129  +static char *safeId(const char *zId){
          130  +  /* All SQLite keywords, in alphabetical order */
          131  +  static const char *azKeywords[] = {
          132  +    "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
          133  +    "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
          134  +    "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
          135  +    "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
          136  +    "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
          137  +    "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
          138  +    "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
          139  +    "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
          140  +    "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
          141  +    "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
          142  +    "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
          143  +    "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
          144  +    "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
          145  +    "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
          146  +    "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
          147  +    "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
          148  +    "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
          149  +    "WITH", "WITHOUT",
          150  +  };
          151  +  int lwr, upr, mid, c, i, x;
          152  +  for(i=x=0; (c = zId[i])!=0; i++){
          153  +    if( !isalpha(c) && c!='_' ){
          154  +      if( i>0 && isdigit(c) ){
          155  +        x++;
          156  +      }else{
          157  +        return sqlite3_mprintf("\"%w\"", zId);
          158  +      }
          159  +    }
          160  +  }
          161  +  if( x ) return sqlite3_mprintf("%s", zId);
          162  +  lwr = 0;
          163  +  upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
          164  +  while( lwr<=upr ){
          165  +    mid = (lwr+upr)/2;
          166  +    c = sqlite3_stricmp(azKeywords[mid], zId);
          167  +    if( c==0 ) return sqlite3_mprintf("\"%w\"", zId);
          168  +    if( c<0 ){
          169  +      lwr = mid+1;
          170  +    }else{
          171  +      upr = mid-1;
          172  +    }
          173  +  }
          174  +  return sqlite3_mprintf("%s", zId);
          175  +}
          176  +
          177  +/*
          178  +** Prepare a new SQL statement.  Print an error and abort if anything
          179  +** goes wrong.
          180  +*/
          181  +static sqlite3_stmt *db_vprepare(const char *zFormat, va_list ap){
          182  +  char *zSql;
          183  +  int rc;
          184  +  sqlite3_stmt *pStmt;
          185  +
          186  +  zSql = sqlite3_vmprintf(zFormat, ap);
          187  +  if( zSql==0 ) runtimeError("out of memory");
          188  +  rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0);
          189  +  if( rc ){
          190  +    runtimeError("SQL statement error: %s\n\"%s\"", sqlite3_errmsg(g.db),
          191  +                 zSql);
          192  +  }
          193  +  sqlite3_free(zSql);
          194  +  return pStmt;
          195  +}
          196  +static sqlite3_stmt *db_prepare(const char *zFormat, ...){
          197  +  va_list ap;
          198  +  sqlite3_stmt *pStmt;
          199  +  va_start(ap, zFormat);
          200  +  pStmt = db_vprepare(zFormat, ap);
          201  +  va_end(ap);
          202  +  return pStmt;
          203  +}
          204  +
          205  +/*
          206  +** Free a list of strings
          207  +*/
          208  +static void namelistFree(char **az){
          209  +  if( az ){
          210  +    int i;
          211  +    for(i=0; az[i]; i++) sqlite3_free(az[i]);
          212  +    sqlite3_free(az);
          213  +  }
          214  +}
          215  +
          216  +/*
          217  +** Return a list of column names for the table zDb.zTab.  Space to
          218  +** hold the list is obtained from sqlite3_malloc() and should released
          219  +** using namelistFree() when no longer needed.
          220  +**
          221  +** Primary key columns are listed first, followed by data columns.
          222  +** The number of columns in the primary key is returned in *pnPkey.
          223  +**
          224  +** Normally, the "primary key" in the previous sentence is the true
          225  +** primary key - the rowid or INTEGER PRIMARY KEY for ordinary tables
          226  +** or the declared PRIMARY KEY for WITHOUT ROWID tables.  However, if
          227  +** the g.bSchemaPK flag is set, then the schema-defined PRIMARY KEY is
          228  +** used in all cases.  In that case, entries that have NULL values in
          229  +** any of their primary key fields will be excluded from the analysis.
          230  +**
          231  +** If the primary key for a table is the rowid but rowid is inaccessible,
          232  +** then this routine returns a NULL pointer.
          233  +**
          234  +** Examples:
          235  +**    CREATE TABLE t1(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(c));
          236  +**    *pnPKey = 1;
          237  +**    az = { "rowid", "a", "b", "c", 0 }  // Normal case
          238  +**    az = { "c", "a", "b", 0 }           // g.bSchemaPK==1
          239  +**
          240  +**    CREATE TABLE t2(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(b));
          241  +**    *pnPKey = 1;
          242  +**    az = { "b", "a", "c", 0 }
          243  +**
          244  +**    CREATE TABLE t3(x,y,z,PRIMARY KEY(y,z));
          245  +**    *pnPKey = 1                         // Normal case
          246  +**    az = { "rowid", "x", "y", "z", 0 }  // Normal case
          247  +**    *pnPKey = 2                         // g.bSchemaPK==1
          248  +**    az = { "y", "x", "z", 0 }           // g.bSchemaPK==1
          249  +**
          250  +**    CREATE TABLE t4(x,y,z,PRIMARY KEY(y,z)) WITHOUT ROWID;
          251  +**    *pnPKey = 2
          252  +**    az = { "y", "z", "x", 0 }
          253  +**
          254  +**    CREATE TABLE t5(rowid,_rowid_,oid);
          255  +**    az = 0     // The rowid is not accessible
          256  +*/
          257  +static char **columnNames(const char *zDb, const char *zTab, int *pnPKey){
          258  +  char **az = 0;           /* List of column names to be returned */
          259  +  int naz = 0;             /* Number of entries in az[] */
          260  +  sqlite3_stmt *pStmt;     /* SQL statement being run */
          261  +  char *zPkIdxName = 0;    /* Name of the PRIMARY KEY index */
          262  +  int truePk = 0;          /* PRAGMA table_info indentifies the PK to use */
          263  +  int nPK = 0;             /* Number of PRIMARY KEY columns */
          264  +  int i, j;                /* Loop counters */
          265  +
          266  +  if( g.bSchemaPK==0 ){
          267  +    /* Normal case:  Figure out what the true primary key is for the table.
          268  +    **   *  For WITHOUT ROWID tables, the true primary key is the same as
          269  +    **      the schema PRIMARY KEY, which is guaranteed to be present.
          270  +    **   *  For rowid tables with an INTEGER PRIMARY KEY, the true primary
          271  +    **      key is the INTEGER PRIMARY KEY.
          272  +    **   *  For all other rowid tables, the rowid is the true primary key.
          273  +    */
          274  +    pStmt = db_prepare("PRAGMA %s.index_list=%Q", zDb, zTab);
          275  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
          276  +      if( sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,3),"pk")==0 ){
          277  +        zPkIdxName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
          278  +        break;
          279  +      }
          280  +    }
          281  +    sqlite3_finalize(pStmt);
          282  +    if( zPkIdxName ){
          283  +      int nKey = 0;
          284  +      int nCol = 0;
          285  +      truePk = 0;
          286  +      pStmt = db_prepare("PRAGMA %s.index_xinfo=%Q", zDb, zPkIdxName);
          287  +      while( SQLITE_ROW==sqlite3_step(pStmt) ){
          288  +        nCol++;
          289  +        if( sqlite3_column_int(pStmt,5) ){ nKey++; continue; }
          290  +        if( sqlite3_column_int(pStmt,1)>=0 ) truePk = 1;
          291  +      }
          292  +      if( nCol==nKey ) truePk = 1;
          293  +      if( truePk ){
          294  +        nPK = nKey;
          295  +      }else{
          296  +        nPK = 1;
          297  +      }
          298  +      sqlite3_finalize(pStmt);
          299  +      sqlite3_free(zPkIdxName);
          300  +    }else{
          301  +      truePk = 1;
          302  +      nPK = 1;
          303  +    }
          304  +    pStmt = db_prepare("PRAGMA %s.table_info=%Q", zDb, zTab);
          305  +  }else{
          306  +    /* The g.bSchemaPK==1 case:  Use whatever primary key is declared
          307  +    ** in the schema.  The "rowid" will still be used as the primary key
          308  +    ** if the table definition does not contain a PRIMARY KEY.
          309  +    */
          310  +    nPK = 0;
          311  +    pStmt = db_prepare("PRAGMA %s.table_info=%Q", zDb, zTab);
          312  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
          313  +      if( sqlite3_column_int(pStmt,5)>0 ) nPK++;
          314  +    }
          315  +    sqlite3_reset(pStmt);
          316  +    if( nPK==0 ) nPK = 1;
          317  +    truePk = 1;
          318  +  }
          319  +  *pnPKey = nPK;
          320  +  naz = nPK;
          321  +  az = sqlite3_malloc( sizeof(char*)*(nPK+1) );
          322  +  if( az==0 ) runtimeError("out of memory");
          323  +  memset(az, 0, sizeof(char*)*(nPK+1));
          324  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
          325  +    int iPKey;
          326  +    if( truePk && (iPKey = sqlite3_column_int(pStmt,5))>0 ){
          327  +      az[iPKey-1] = safeId((char*)sqlite3_column_text(pStmt,1));
          328  +    }else{
          329  +      az = sqlite3_realloc(az, sizeof(char*)*(naz+2) );
          330  +      if( az==0 ) runtimeError("out of memory");
          331  +      az[naz++] = safeId((char*)sqlite3_column_text(pStmt,1));
          332  +    }
          333  +  }
          334  +  sqlite3_finalize(pStmt);
          335  +  if( az ) az[naz] = 0;
          336  +  if( az[0]==0 ){
          337  +    const char *azRowid[] = { "rowid", "_rowid_", "oid" };
          338  +    for(i=0; i<sizeof(azRowid)/sizeof(azRowid[0]); i++){
          339  +      for(j=1; j<naz; j++){
          340  +        if( sqlite3_stricmp(az[j], azRowid[i])==0 ) break;
          341  +      }
          342  +      if( j>=naz ){
          343  +        az[0] = sqlite3_mprintf("%s", azRowid[i]);
          344  +        break;
          345  +      }
          346  +    }
          347  +    if( az[0]==0 ){
          348  +      for(i=1; i<naz; i++) sqlite3_free(az[i]);
          349  +      sqlite3_free(az);
          350  +      az = 0;
          351  +    }
          352  +  }
          353  +  return az;
          354  +}
          355  +
          356  +/*
          357  +** Print the sqlite3_value X as an SQL literal.
          358  +*/
          359  +static void printQuoted(FILE *out, sqlite3_value *X){
          360  +  switch( sqlite3_value_type(X) ){
          361  +    case SQLITE_FLOAT: {
          362  +      double r1;
          363  +      char zBuf[50];
          364  +      r1 = sqlite3_value_double(X);
          365  +      sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
          366  +      fprintf(out, "%s", zBuf);
          367  +      break;
          368  +    }
          369  +    case SQLITE_INTEGER: {
          370  +      fprintf(out, "%lld", sqlite3_value_int64(X));
          371  +      break;
          372  +    }
          373  +    case SQLITE_BLOB: {
          374  +      const unsigned char *zBlob = sqlite3_value_blob(X);
          375  +      int nBlob = sqlite3_value_bytes(X);
          376  +      if( zBlob ){
          377  +        int i;
          378  +        fprintf(out, "x'");
          379  +        for(i=0; i<nBlob; i++){
          380  +          fprintf(out, "%02x", zBlob[i]);
          381  +        }
          382  +        fprintf(out, "'");
          383  +      }else{
          384  +        fprintf(out, "NULL");
          385  +      }
          386  +      break;
          387  +    }
          388  +    case SQLITE_TEXT: {
          389  +      const unsigned char *zArg = sqlite3_value_text(X);
          390  +      int i, j;
          391  +
          392  +      if( zArg==0 ){
          393  +        fprintf(out, "NULL");
          394  +      }else{
          395  +        fprintf(out, "'");
          396  +        for(i=j=0; zArg[i]; i++){
          397  +          if( zArg[i]=='\'' ){
          398  +            fprintf(out, "%.*s'", i-j+1, &zArg[j]);
          399  +            j = i+1;
          400  +          }
          401  +        }
          402  +        fprintf(out, "%s'", &zArg[j]);
          403  +      }
          404  +      break;
          405  +    }
          406  +    case SQLITE_NULL: {
          407  +      fprintf(out, "NULL");
          408  +      break;
          409  +    }
          410  +  }
          411  +}
          412  +
          413  +/*
          414  +** Output SQL that will recreate the aux.zTab table.
          415  +*/
          416  +static void dump_table(const char *zTab, FILE *out){
          417  +  char *zId = safeId(zTab); /* Name of the table */
          418  +  char **az = 0;            /* List of columns */
          419  +  int nPk;                  /* Number of true primary key columns */
          420  +  int nCol;                 /* Number of data columns */
          421  +  int i;                    /* Loop counter */
          422  +  sqlite3_stmt *pStmt;      /* SQL statement */
          423  +  const char *zSep;         /* Separator string */
          424  +  Str ins;                  /* Beginning of the INSERT statement */
          425  +
          426  +  pStmt = db_prepare("SELECT sql FROM aux.sqlite_master WHERE name=%Q", zTab);
          427  +  if( SQLITE_ROW==sqlite3_step(pStmt) ){
          428  +    fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0));
          429  +  }
          430  +  sqlite3_finalize(pStmt);
          431  +  if( !g.bSchemaOnly ){
          432  +    az = columnNames("aux", zTab, &nPk);
          433  +    strInit(&ins);
          434  +    if( az==0 ){
          435  +      pStmt = db_prepare("SELECT * FROM aux.%s", zId);
          436  +      strPrintf(&ins,"INSERT INTO %s VALUES", zId);
          437  +    }else{
          438  +      Str sql;
          439  +      strInit(&sql);
          440  +      zSep =  "SELECT";
          441  +      for(i=0; az[i]; i++){
          442  +        strPrintf(&sql, "%s %s", zSep, az[i]);
          443  +        zSep = ",";
          444  +      }
          445  +      strPrintf(&sql," FROM aux.%s", zId);
          446  +      zSep = " ORDER BY";
          447  +      for(i=1; i<=nPk; i++){
          448  +        strPrintf(&sql, "%s %d", zSep, i);
          449  +        zSep = ",";
          450  +      }
          451  +      pStmt = db_prepare("%s", sql.z);
          452  +      strFree(&sql);
          453  +      strPrintf(&ins, "INSERT INTO %s", zId);
          454  +      zSep = "(";
          455  +      for(i=0; az[i]; i++){
          456  +        strPrintf(&ins, "%s%s", zSep, az[i]);
          457  +        zSep = ",";
          458  +      }
          459  +      strPrintf(&ins,") VALUES");
          460  +      namelistFree(az);
          461  +    }
          462  +    nCol = sqlite3_column_count(pStmt);
          463  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
          464  +      fprintf(out, "%s",ins.z);
          465  +      zSep = "(";
          466  +      for(i=0; i<nCol; i++){
          467  +        fprintf(out, "%s",zSep);
          468  +        printQuoted(out, sqlite3_column_value(pStmt,i));
          469  +        zSep = ",";
          470  +      }
          471  +      fprintf(out, ");\n");
          472  +    }
          473  +    sqlite3_finalize(pStmt);
          474  +    strFree(&ins);
          475  +  } /* endif !g.bSchemaOnly */
          476  +  pStmt = db_prepare("SELECT sql FROM aux.sqlite_master"
          477  +                     " WHERE type='index' AND tbl_name=%Q AND sql IS NOT NULL",
          478  +                     zTab);
          479  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
          480  +    fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0));
          481  +  }
          482  +  sqlite3_finalize(pStmt);
          483  +}
          484  +
          485  +
          486  +/*
          487  +** Compute all differences for a single table.
          488  +*/
          489  +static void diff_one_table(const char *zTab, FILE *out){
          490  +  char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */
          491  +  char **az = 0;            /* Columns in main */
          492  +  char **az2 = 0;           /* Columns in aux */
          493  +  int nPk;                  /* Primary key columns in main */
          494  +  int nPk2;                 /* Primary key columns in aux */
          495  +  int n;                    /* Number of columns in main */
          496  +  int n2;                   /* Number of columns in aux */
          497  +  int nQ;                   /* Number of output columns in the diff query */
          498  +  int i;                    /* Loop counter */
          499  +  const char *zSep;         /* Separator string */
          500  +  Str sql;                  /* Comparison query */
          501  +  sqlite3_stmt *pStmt;      /* Query statement to do the diff */
          502  +
          503  +  strInit(&sql);
          504  +  if( g.fDebug==DEBUG_COLUMN_NAMES ){
          505  +    /* Simply run columnNames() on all tables of the origin
          506  +    ** database and show the results.  This is used for testing
          507  +    ** and debugging of the columnNames() function.
          508  +    */
          509  +    az = columnNames("aux",zTab, &nPk);
          510  +    if( az==0 ){
          511  +      printf("Rowid not accessible for %s\n", zId);
          512  +    }else{
          513  +      printf("%s:", zId);
          514  +      for(i=0; az[i]; i++){
          515  +        printf(" %s", az[i]);
          516  +        if( i+1==nPk ) printf(" *");
          517  +      }
          518  +      printf("\n");
          519  +    }
          520  +    goto end_diff_one_table;
          521  +  }
          522  +    
          523  +
          524  +  if( sqlite3_table_column_metadata(g.db,"aux",zTab,0,0,0,0,0,0) ){
          525  +    if( !sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
          526  +      /* Table missing from second database. */
          527  +      fprintf(out, "DROP TABLE %s;\n", zId);
          528  +    }
          529  +    goto end_diff_one_table;
          530  +  }
          531  +
          532  +  if( sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
          533  +    /* Table missing from source */
          534  +    dump_table(zTab, out);
          535  +    goto end_diff_one_table;
          536  +  }
          537  +
          538  +  az = columnNames("main", zTab, &nPk);
          539  +  az2 = columnNames("aux", zTab, &nPk2);
          540  +  if( az && az2 ){
          541  +    for(n=0; az[n]; n++){
          542  +      if( sqlite3_stricmp(az[n],az2[n])!=0 ) break;
          543  +    }
          544  +  }
          545  +  if( az==0
          546  +   || az2==0
          547  +   || nPk!=nPk2
          548  +   || az[n]
          549  +  ){
          550  +    /* Schema mismatch */
          551  +    fprintf(out, "DROP TABLE %s;\n", zId);
          552  +    dump_table(zTab, out);
          553  +    goto end_diff_one_table;
          554  +  }
          555  +
          556  +  /* Build the comparison query */
          557  +  for(n2=n; az[n2]; n2++){}
          558  +  nQ = nPk2+1+2*(n2-nPk2);
          559  +  if( n2>nPk2 ){
          560  +    zSep = "SELECT ";
          561  +    for(i=0; i<nPk; i++){
          562  +      strPrintf(&sql, "%sB.%s", zSep, az[i]);
          563  +      zSep = ", ";
          564  +    }
          565  +    strPrintf(&sql, ", 1%s -- changed row\n", nPk==n ? "" : ",");
          566  +    while( az[i] ){
          567  +      strPrintf(&sql, "       A.%s IS NOT B.%s, B.%s%s\n",
          568  +                az[i], az[i], az[i], i==n2-1 ? "" : ",");
          569  +      i++;
          570  +    }
          571  +    strPrintf(&sql, "  FROM main.%s A, aux.%s B\n", zId, zId);
          572  +    zSep = " WHERE";
          573  +    for(i=0; i<nPk; i++){
          574  +      strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
          575  +      zSep = " AND";
          576  +    }
          577  +    zSep = "\n   AND (";
          578  +    while( az[i] ){
          579  +      strPrintf(&sql, "%sA.%s IS NOT B.%s%s\n",
          580  +                zSep, az[i], az[i], i==n2-1 ? ")" : "");
          581  +      zSep = "        OR ";
          582  +      i++;
          583  +    }
          584  +    strPrintf(&sql, " UNION ALL\n");
          585  +  }
          586  +  zSep = "SELECT ";
          587  +  for(i=0; i<nPk; i++){
          588  +    strPrintf(&sql, "%sA.%s", zSep, az[i]);
          589  +    zSep = ", ";
          590  +  }
          591  +  strPrintf(&sql, ", 2%s -- deleted row\n", nPk==n ? "" : ",");
          592  +  while( az[i] ){
          593  +    strPrintf(&sql, "       NULL, NULL%s\n", i==n2-1 ? "" : ",");
          594  +    i++;
          595  +  }
          596  +  strPrintf(&sql, "  FROM main.%s A\n", zId);
          597  +  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
          598  +  zSep =          "                   WHERE";
          599  +  for(i=0; i<nPk; i++){
          600  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
          601  +    zSep = " AND";
          602  +  }
          603  +  strPrintf(&sql, ")\n");
          604  +  zSep = " UNION ALL\nSELECT ";
          605  +  for(i=0; i<nPk; i++){
          606  +    strPrintf(&sql, "%sB.%s", zSep, az[i]);
          607  +    zSep = ", ";
          608  +  }
          609  +  strPrintf(&sql, ", 3%s -- inserted row\n", nPk==n ? "" : ",");
          610  +  while( az2[i] ){
          611  +    strPrintf(&sql, "       1, B.%s%s\n", az[i], i==n2-1 ? "" : ",");
          612  +    i++;
          613  +  }
          614  +  strPrintf(&sql, "  FROM aux.%s B\n", zId);
          615  +  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
          616  +  zSep =          "                   WHERE";
          617  +  for(i=0; i<nPk; i++){
          618  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
          619  +    zSep = " AND";
          620  +  }
          621  +  strPrintf(&sql, ")\n ORDER BY");
          622  +  zSep = " ";
          623  +  for(i=1; i<=nPk; i++){
          624  +    strPrintf(&sql, "%s%d", zSep, i);
          625  +    zSep = ", ";
          626  +  }
          627  +  strPrintf(&sql, ";\n");
          628  +
          629  +  if( g.fDebug & DEBUG_DIFF_SQL ){ 
          630  +    printf("SQL for %s:\n%s\n", zId, sql.z);
          631  +    goto end_diff_one_table;
          632  +  }
          633  +
          634  +  /* Drop indexes that are missing in the destination */
          635  +  pStmt = db_prepare(
          636  +    "SELECT name FROM main.sqlite_master"
          637  +    " WHERE type='index' AND tbl_name=%Q"
          638  +    "   AND sql IS NOT NULL"
          639  +    "   AND sql NOT IN (SELECT sql FROM aux.sqlite_master"
          640  +    "                    WHERE type='index' AND tbl_name=%Q"
          641  +    "                      AND sql IS NOT NULL)",
          642  +    zTab, zTab);
          643  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
          644  +    char *z = safeId((const char*)sqlite3_column_text(pStmt,0));
          645  +    fprintf(out, "DROP INDEX %s;\n", z);
          646  +    sqlite3_free(z);
          647  +  }
          648  +  sqlite3_finalize(pStmt);
          649  +
          650  +  /* Run the query and output differences */
          651  +  if( !g.bSchemaOnly ){
          652  +    pStmt = db_prepare(sql.z);
          653  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
          654  +      int iType = sqlite3_column_int(pStmt, nPk);
          655  +      if( iType==1 || iType==2 ){
          656  +        if( iType==1 ){       /* Change the content of a row */
          657  +          fprintf(out, "UPDATE %s", zId);
          658  +          zSep = " SET";
          659  +          for(i=nPk+1; i<nQ; i+=2){
          660  +            if( sqlite3_column_int(pStmt,i)==0 ) continue;
          661  +            fprintf(out, "%s %s=", zSep, az2[(i+nPk-1)/2]);
          662  +            zSep = ",";
          663  +            printQuoted(out, sqlite3_column_value(pStmt,i+1));
          664  +          }
          665  +        }else{                /* Delete a row */
          666  +          fprintf(out, "DELETE FROM %s", zId);
          667  +        }
          668  +        zSep = " WHERE";
          669  +        for(i=0; i<nPk; i++){
          670  +          fprintf(out, "%s %s=", zSep, az2[i]);
          671  +          printQuoted(out, sqlite3_column_value(pStmt,i));
          672  +          zSep = ",";
          673  +        }
          674  +        fprintf(out, ";\n");
          675  +      }else{                  /* Insert a row */
          676  +        fprintf(out, "INSERT INTO %s(%s", zId, az2[0]);
          677  +        for(i=1; az2[i]; i++) fprintf(out, ",%s", az2[i]);
          678  +        fprintf(out, ") VALUES");
          679  +        zSep = "(";
          680  +        for(i=0; i<nPk2; i++){
          681  +          fprintf(out, "%s", zSep);
          682  +          zSep = ",";
          683  +          printQuoted(out, sqlite3_column_value(pStmt,i));
          684  +        }
          685  +        for(i=nPk2+2; i<nQ; i+=2){
          686  +          fprintf(out, ",");
          687  +          printQuoted(out, sqlite3_column_value(pStmt,i));
          688  +        }
          689  +        fprintf(out, ");\n");
          690  +      }
          691  +    }
          692  +    sqlite3_finalize(pStmt);
          693  +  } /* endif !g.bSchemaOnly */
          694  +
          695  +  /* Create indexes that are missing in the source */
          696  +  pStmt = db_prepare(
          697  +    "SELECT sql FROM aux.sqlite_master"
          698  +    " WHERE type='index' AND tbl_name=%Q"
          699  +    "   AND sql IS NOT NULL"
          700  +    "   AND sql NOT IN (SELECT sql FROM main.sqlite_master"
          701  +    "                    WHERE type='index' AND tbl_name=%Q"
          702  +    "                      AND sql IS NOT NULL)",
          703  +    zTab, zTab);
          704  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
          705  +    fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0));
          706  +  }
          707  +  sqlite3_finalize(pStmt);
          708  +
          709  +end_diff_one_table:
          710  +  strFree(&sql);
          711  +  sqlite3_free(zId);
          712  +  namelistFree(az);
          713  +  namelistFree(az2);
          714  +  return;
          715  +}
          716  +
          717  +/*
          718  +** Display a summary of differences between two versions of the same
          719  +** table table.
          720  +**
          721  +**   *  Number of rows changed
          722  +**   *  Number of rows added
          723  +**   *  Number of rows deleted
          724  +**   *  Number of identical rows
          725  +*/
          726  +static void summarize_one_table(const char *zTab, FILE *out){
          727  +  char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */
          728  +  char **az = 0;            /* Columns in main */
          729  +  char **az2 = 0;           /* Columns in aux */
          730  +  int nPk;                  /* Primary key columns in main */
          731  +  int nPk2;                 /* Primary key columns in aux */
          732  +  int n;                    /* Number of columns in main */
          733  +  int n2;                   /* Number of columns in aux */
          734  +  int i;                    /* Loop counter */
          735  +  const char *zSep;         /* Separator string */
          736  +  Str sql;                  /* Comparison query */
          737  +  sqlite3_stmt *pStmt;      /* Query statement to do the diff */
          738  +  sqlite3_int64 nUpdate;    /* Number of updated rows */
          739  +  sqlite3_int64 nUnchanged; /* Number of unmodified rows */
          740  +  sqlite3_int64 nDelete;    /* Number of deleted rows */
          741  +  sqlite3_int64 nInsert;    /* Number of inserted rows */
          742  +
          743  +  strInit(&sql);
          744  +  if( sqlite3_table_column_metadata(g.db,"aux",zTab,0,0,0,0,0,0) ){
          745  +    if( !sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
          746  +      /* Table missing from second database. */
          747  +      fprintf(out, "%s: missing from second database\n", zTab);
          748  +    }
          749  +    goto end_summarize_one_table;
          750  +  }
          751  +
          752  +  if( sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
          753  +    /* Table missing from source */
          754  +    fprintf(out, "%s: missing from first database\n", zTab);
          755  +    goto end_summarize_one_table;
          756  +  }
          757  +
          758  +  az = columnNames("main", zTab, &nPk);
          759  +  az2 = columnNames("aux", zTab, &nPk2);
          760  +  if( az && az2 ){
          761  +    for(n=0; az[n]; n++){
          762  +      if( sqlite3_stricmp(az[n],az2[n])!=0 ) break;
          763  +    }
          764  +  }
          765  +  if( az==0
          766  +   || az2==0
          767  +   || nPk!=nPk2
          768  +   || az[n]
          769  +  ){
          770  +    /* Schema mismatch */
          771  +    fprintf(out, "%s: incompatible schema\n", zTab);
          772  +    goto end_summarize_one_table;
          773  +  }
          774  +
          775  +  /* Build the comparison query */
          776  +  for(n2=n; az[n2]; n2++){}
          777  +  strPrintf(&sql, "SELECT 1, count(*)");
          778  +  if( n2==nPk2 ){
          779  +    strPrintf(&sql, ", 0\n");
          780  +  }else{
          781  +    zSep = ", sum(";
          782  +    for(i=nPk; az[i]; i++){
          783  +      strPrintf(&sql, "%sA.%s IS NOT B.%s", zSep, az[i], az[i]);
          784  +      zSep = " OR ";
          785  +    }
          786  +    strPrintf(&sql, ")\n");
          787  +  }
          788  +  strPrintf(&sql, "  FROM main.%s A, aux.%s B\n", zId, zId);
          789  +  zSep = " WHERE";
          790  +  for(i=0; i<nPk; i++){
          791  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
          792  +    zSep = " AND";
          793  +  }
          794  +  strPrintf(&sql, " UNION ALL\n");
          795  +  strPrintf(&sql, "SELECT 2, count(*), 0\n");
          796  +  strPrintf(&sql, "  FROM main.%s A\n", zId);
          797  +  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B ", zId);
          798  +  zSep = "WHERE";
          799  +  for(i=0; i<nPk; i++){
          800  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
          801  +    zSep = " AND";
          802  +  }
          803  +  strPrintf(&sql, ")\n");
          804  +  strPrintf(&sql, " UNION ALL\n");
          805  +  strPrintf(&sql, "SELECT 3, count(*), 0\n");
          806  +  strPrintf(&sql, "  FROM aux.%s B\n", zId);
          807  +  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A ", zId);
          808  +  zSep = "WHERE";
          809  +  for(i=0; i<nPk; i++){
          810  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
          811  +    zSep = " AND";
          812  +  }
          813  +  strPrintf(&sql, ")\n ORDER BY 1;\n");
          814  +
          815  +  if( (g.fDebug & DEBUG_DIFF_SQL)!=0 ){ 
          816  +    printf("SQL for %s:\n%s\n", zId, sql.z);
          817  +    goto end_summarize_one_table;
          818  +  }
          819  +
          820  +  /* Run the query and output difference summary */
          821  +  pStmt = db_prepare(sql.z);
          822  +  nUpdate = 0;
          823  +  nInsert = 0;
          824  +  nDelete = 0;
          825  +  nUnchanged = 0;
          826  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
          827  +    switch( sqlite3_column_int(pStmt,0) ){
          828  +      case 1:
          829  +        nUpdate = sqlite3_column_int64(pStmt,2);
          830  +        nUnchanged = sqlite3_column_int64(pStmt,1) - nUpdate;
          831  +        break;
          832  +      case 2:
          833  +        nDelete = sqlite3_column_int64(pStmt,1);
          834  +        break;
          835  +      case 3:
          836  +        nInsert = sqlite3_column_int64(pStmt,1);
          837  +        break;
          838  +    }
          839  +  }
          840  +  sqlite3_finalize(pStmt);
          841  +  fprintf(out, "%s: %lld changes, %lld inserts, %lld deletes, %lld unchanged\n",
          842  +          zTab, nUpdate, nInsert, nDelete, nUnchanged);
          843  +
          844  +end_summarize_one_table:
          845  +  strFree(&sql);
          846  +  sqlite3_free(zId);
          847  +  namelistFree(az);
          848  +  namelistFree(az2);
          849  +  return;
          850  +}
          851  +
          852  +/*
          853  +** Write a 64-bit signed integer as a varint onto out
          854  +*/
          855  +static void putsVarint(FILE *out, sqlite3_uint64 v){
          856  +  int i, n;
          857  +  unsigned char p[12];
          858  +  if( v & (((sqlite3_uint64)0xff000000)<<32) ){
          859  +    p[8] = (unsigned char)v;
          860  +    v >>= 8;
          861  +    for(i=7; i>=0; i--){
          862  +      p[i] = (unsigned char)((v & 0x7f) | 0x80);
          863  +      v >>= 7;
          864  +    }
          865  +    fwrite(p, 8, 1, out);
          866  +  }else{
          867  +    n = 9;
          868  +    do{
          869  +      p[n--] = (unsigned char)((v & 0x7f) | 0x80);
          870  +      v >>= 7;
          871  +    }while( v!=0 );
          872  +    p[9] &= 0x7f;
          873  +    fwrite(p+n+1, 9-n, 1, out);
          874  +  }
          875  +}
          876  +
          877  +/*
          878  +** Write an SQLite value onto out.
          879  +*/
          880  +static void putValue(FILE *out, sqlite3_value *pVal){
          881  +  int iDType = sqlite3_value_type(pVal);
          882  +  sqlite3_int64 iX;
          883  +  double rX;
          884  +  sqlite3_uint64 uX;
          885  +  int j;
          886  +
          887  +  putc(iDType, out);
          888  +  switch( iDType ){
          889  +    case SQLITE_INTEGER:
          890  +      iX = sqlite3_value_int64(pVal);
          891  +      memcpy(&uX, &iX, 8);
          892  +      for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
          893  +      break;
          894  +    case SQLITE_FLOAT:
          895  +      rX = sqlite3_value_int64(pVal);
          896  +      memcpy(&uX, &rX, 8);
          897  +      for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
          898  +      break;
          899  +    case SQLITE_TEXT:
          900  +      iX = sqlite3_value_bytes(pVal);
          901  +      putsVarint(out, (sqlite3_uint64)iX);
          902  +      fwrite(sqlite3_value_text(pVal),1,iX,out);
          903  +      break;
          904  +    case SQLITE_BLOB:
          905  +      iX = sqlite3_value_bytes(pVal);
          906  +      putsVarint(out, (sqlite3_uint64)iX);
          907  +      fwrite(sqlite3_value_blob(pVal),1,iX,out);
          908  +      break;
          909  +    case SQLITE_NULL:
          910  +      break;
          911  +  }
          912  +}
          913  +
          914  +/*
          915  +** Generate a CHANGESET for all differences from main.zTab to aux.zTab.
          916  +*/
          917  +static void changeset_one_table(const char *zTab, FILE *out){
          918  +  sqlite3_stmt *pStmt;          /* SQL statment */
          919  +  char *zId = safeId(zTab);     /* Escaped name of the table */
          920  +  char **azCol = 0;             /* List of escaped column names */
          921  +  int nCol = 0;                 /* Number of columns */
          922  +  int *aiFlg = 0;               /* 0 if column is not part of PK */
          923  +  int *aiPk = 0;                /* Column numbers for each PK column */
          924  +  int nPk = 0;                  /* Number of PRIMARY KEY columns */
          925  +  Str sql;                      /* SQL for the diff query */
          926  +  int i, k;                     /* Loop counters */
          927  +  const char *zSep;             /* List separator */
          928  +
          929  +  pStmt = db_prepare(
          930  +      "SELECT A.sql=B.sql FROM main.sqlite_master A, aux.sqlite_master B"
          931  +      " WHERE A.name=%Q AND B.name=%Q", zTab, zTab
          932  +  );
          933  +  if( SQLITE_ROW==sqlite3_step(pStmt) ){
          934  +    if( sqlite3_column_int(pStmt,0)==0 ){
          935  +      runtimeError("schema changes for table %s", safeId(zTab));
          936  +    }
          937  +  }else{
          938  +    runtimeError("table %s missing from one or both databases", safeId(zTab));
          939  +  }
          940  +  sqlite3_finalize(pStmt);
          941  +  pStmt = db_prepare("PRAGMA main.table_info=%Q", zTab);
          942  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
          943  +    nCol++;
          944  +    azCol = sqlite3_realloc(azCol, sizeof(char*)*nCol);
          945  +    if( azCol==0 ) runtimeError("out of memory");
          946  +    aiFlg = sqlite3_realloc(aiFlg, sizeof(int)*nCol);
          947  +    if( aiFlg==0 ) runtimeError("out of memory");
          948  +    azCol[nCol-1] = safeId((const char*)sqlite3_column_text(pStmt,1));
          949  +    aiFlg[nCol-1] = i = sqlite3_column_int(pStmt,5);
          950  +    if( i>0 ){
          951  +      if( i>nPk ){
          952  +        nPk = i;
          953  +        aiPk = sqlite3_realloc(aiPk, sizeof(int)*nPk);
          954  +        if( aiPk==0 ) runtimeError("out of memory");
          955  +      }
          956  +      aiPk[i-1] = nCol-1;
          957  +    }
          958  +  }
          959  +  sqlite3_finalize(pStmt);
          960  +  if( nPk==0 ) goto end_changeset_one_table; 
          961  +  strInit(&sql);
          962  +  if( nCol>nPk ){
          963  +    strPrintf(&sql, "SELECT %d", SQLITE_UPDATE);
          964  +    for(i=0; i<nCol; i++){
          965  +      if( aiFlg[i] ){
          966  +        strPrintf(&sql, ",\n       A.%s", azCol[i]);
          967  +      }else{
          968  +        strPrintf(&sql, ",\n       A.%s IS NOT B.%s, A.%s, B.%s",
          969  +                  azCol[i], azCol[i], azCol[i], azCol[i]);
          970  +      }
          971  +    }
          972  +    strPrintf(&sql,"\n  FROM main.%s A, aux.%s B\n", zId, zId);
          973  +    zSep = " WHERE";
          974  +    for(i=0; i<nPk; i++){
          975  +      strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
          976  +      zSep = " AND";
          977  +    }
          978  +    zSep = "\n   AND (";
          979  +    for(i=0; i<nCol; i++){
          980  +      if( aiFlg[i] ) continue;
          981  +      strPrintf(&sql, "%sA.%s IS NOT B.%s", zSep, azCol[i], azCol[i]);
          982  +      zSep = " OR\n        ";
          983  +    }
          984  +    strPrintf(&sql,")\n UNION ALL\n");
          985  +  }
          986  +  strPrintf(&sql, "SELECT %d", SQLITE_DELETE);
          987  +  for(i=0; i<nCol; i++){
          988  +    if( aiFlg[i] ){
          989  +      strPrintf(&sql, ",\n       A.%s", azCol[i]);
          990  +    }else{
          991  +      strPrintf(&sql, ",\n       1, A.%s, NULL", azCol[i]);
          992  +    }
          993  +  }
          994  +  strPrintf(&sql, "\n  FROM main.%s A\n", zId);
          995  +  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
          996  +  zSep =          "                   WHERE";
          997  +  for(i=0; i<nPk; i++){
          998  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
          999  +    zSep = " AND";
         1000  +  }
         1001  +  strPrintf(&sql, ")\n UNION ALL\n");
         1002  +  strPrintf(&sql, "SELECT %d", SQLITE_INSERT);
         1003  +  for(i=0; i<nCol; i++){
         1004  +    if( aiFlg[i] ){
         1005  +      strPrintf(&sql, ",\n       B.%s", azCol[i]);
         1006  +    }else{
         1007  +      strPrintf(&sql, ",\n       1, NULL, B.%s", azCol[i]);
         1008  +    }
         1009  +  }
         1010  +  strPrintf(&sql, "\n  FROM aux.%s B\n", zId);
         1011  +  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
         1012  +  zSep =          "                   WHERE";
         1013  +  for(i=0; i<nPk; i++){
         1014  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
         1015  +    zSep = " AND";
         1016  +  }
         1017  +  strPrintf(&sql, ")\n");
         1018  +  strPrintf(&sql, " ORDER BY");
         1019  +  zSep = " ";
         1020  +  for(i=0; i<nPk; i++){
         1021  +    strPrintf(&sql, "%s %d", zSep, aiPk[i]+2);
         1022  +    zSep = ",";
         1023  +  }
         1024  +  strPrintf(&sql, ";\n");
         1025  +
         1026  +  if( g.fDebug & DEBUG_DIFF_SQL ){ 
         1027  +    printf("SQL for %s:\n%s\n", zId, sql.z);
         1028  +    goto end_changeset_one_table;
         1029  +  }
         1030  +
         1031  +  putc('T', out);
         1032  +  putsVarint(out, (sqlite3_uint64)nCol);
         1033  +  for(i=0; i<nCol; i++) putc(aiFlg[i]!=0, out);
         1034  +  fwrite(zTab, 1, strlen(zTab), out);
         1035  +  putc(0, out);
         1036  +
         1037  +  pStmt = db_prepare("%s", sql.z);
         1038  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
         1039  +    int iType = sqlite3_column_int(pStmt,0);
         1040  +    putc(iType, out);
         1041  +    putc(0, out);
         1042  +    switch( sqlite3_column_int(pStmt,0) ){
         1043  +      case SQLITE_UPDATE: {
         1044  +        for(k=1, i=0; i<nCol; i++){
         1045  +          if( aiFlg[i] ){
         1046  +            putValue(out, sqlite3_column_value(pStmt,k));
         1047  +            k++;
         1048  +          }else if( sqlite3_column_int(pStmt,k) ){
         1049  +            putValue(out, sqlite3_column_value(pStmt,k+1));
         1050  +            k += 3;
         1051  +          }else{
         1052  +            putc(0, out);
         1053  +            k += 3;
         1054  +          }
         1055  +        }
         1056  +        for(k=1, i=0; i<nCol; i++){
         1057  +          if( aiFlg[i] ){
         1058  +            putc(0, out);
         1059  +            k++;
         1060  +          }else if( sqlite3_column_int(pStmt,k) ){
         1061  +            putValue(out, sqlite3_column_value(pStmt,k+2));
         1062  +            k += 3;
         1063  +          }else{
         1064  +            putc(0, out);
         1065  +            k += 3;
         1066  +          }
         1067  +        }
         1068  +        break;
         1069  +      }
         1070  +      case SQLITE_INSERT: {
         1071  +        for(k=1, i=0; i<nCol; i++){
         1072  +          if( aiFlg[i] ){
         1073  +            putValue(out, sqlite3_column_value(pStmt,k));
         1074  +            k++;
         1075  +          }else{
         1076  +            putValue(out, sqlite3_column_value(pStmt,k+2));
         1077  +            k += 3;
         1078  +          }
         1079  +        }
         1080  +        break;
         1081  +      }
         1082  +      case SQLITE_DELETE: {
         1083  +        for(k=1, i=0; i<nCol; i++){
         1084  +          if( aiFlg[i] ){
         1085  +            putValue(out, sqlite3_column_value(pStmt,k));
         1086  +            k++;
         1087  +          }else{
         1088  +            putValue(out, sqlite3_column_value(pStmt,k+1));
         1089  +            k += 3;
         1090  +          }
         1091  +        }
         1092  +        break;
         1093  +      }
         1094  +    }
         1095  +  }
         1096  +  sqlite3_finalize(pStmt);
         1097  +  
         1098  +end_changeset_one_table:
         1099  +  while( nCol>0 ) sqlite3_free(azCol[--nCol]);
         1100  +  sqlite3_free(azCol);
         1101  +  sqlite3_free(aiPk);
         1102  +  sqlite3_free(zId);
         1103  +}
         1104  +
         1105  +/*
         1106  +** Print sketchy documentation for this utility program
         1107  +*/
         1108  +static void showHelp(void){
         1109  +  printf("Usage: %s [options] DB1 DB2\n", g.zArgv0);
         1110  +  printf(
         1111  +"Output SQL text that would transform DB1 into DB2.\n"
         1112  +"Options:\n"
         1113  +"  --changeset FILE      Write a CHANGESET into FILE\n"
         1114  +"  --primarykey          Use schema-defined PRIMARY KEYs\n"
         1115  +"  --schema              Show only differences in the schema\n"
         1116  +"  --summary             Show only a summary of the differences\n"
         1117  +"  --table TAB           Show only differences in table TAB\n"
         1118  +  );
         1119  +}
         1120  +
         1121  +int main(int argc, char **argv){
         1122  +  const char *zDb1 = 0;
         1123  +  const char *zDb2 = 0;
         1124  +  int i;
         1125  +  int rc;
         1126  +  char *zErrMsg = 0;
         1127  +  char *zSql;
         1128  +  sqlite3_stmt *pStmt;
         1129  +  char *zTab = 0;
         1130  +  FILE *out = stdout;
         1131  +  void (*xDiff)(const char*,FILE*) = diff_one_table;
         1132  +
         1133  +  g.zArgv0 = argv[0];
         1134  +  for(i=1; i<argc; i++){
         1135  +    const char *z = argv[i];
         1136  +    if( z[0]=='-' ){
         1137  +      z++;
         1138  +      if( z[0]=='-' ) z++;
         1139  +      if( strcmp(z,"changeset")==0 ){
         1140  +        out = fopen(argv[++i], "wb");
         1141  +        if( out==0 ) cmdlineError("cannot open: %s", argv[i]);
         1142  +        xDiff = changeset_one_table;
         1143  +      }else
         1144  +      if( strcmp(z,"debug")==0 ){
         1145  +        g.fDebug = strtol(argv[++i], 0, 0);
         1146  +      }else
         1147  +      if( strcmp(z,"help")==0 ){
         1148  +        showHelp();
         1149  +        return 0;
         1150  +      }else
         1151  +      if( strcmp(z,"primarykey")==0 ){
         1152  +        g.bSchemaPK = 1;
         1153  +      }else
         1154  +      if( strcmp(z,"schema")==0 ){
         1155  +        g.bSchemaOnly = 1;
         1156  +      }else
         1157  +      if( strcmp(z,"summary")==0 ){
         1158  +        xDiff = summarize_one_table;
         1159  +      }else
         1160  +      if( strcmp(z,"table")==0 ){
         1161  +        zTab = argv[++i];
         1162  +      }else
         1163  +      {
         1164  +        cmdlineError("unknown option: %s", argv[i]);
         1165  +      }
         1166  +    }else if( zDb1==0 ){
         1167  +      zDb1 = argv[i];
         1168  +    }else if( zDb2==0 ){
         1169  +      zDb2 = argv[i];
         1170  +    }else{
         1171  +      cmdlineError("unknown argument: %s", argv[i]);
         1172  +    }
         1173  +  }
         1174  +  if( zDb2==0 ){
         1175  +    cmdlineError("two database arguments required");
         1176  +  }
         1177  +  rc = sqlite3_open(zDb1, &g.db);
         1178  +  if( rc ){
         1179  +    cmdlineError("cannot open database file \"%s\"", zDb1);
         1180  +  }
         1181  +  rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_master", 0, 0, &zErrMsg);
         1182  +  if( rc || zErrMsg ){
         1183  +    cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1);
         1184  +  }
         1185  +  zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2);
         1186  +  rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
         1187  +  if( rc || zErrMsg ){
         1188  +    cmdlineError("cannot attach database \"%s\"", zDb2);
         1189  +  }
         1190  +  rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg);
         1191  +  if( rc || zErrMsg ){
         1192  +    cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb2);
         1193  +  }
         1194  +
         1195  +  if( zTab ){
         1196  +    xDiff(zTab, out);
         1197  +  }else{
         1198  +    /* Handle tables one by one */
         1199  +    pStmt = db_prepare(
         1200  +      "SELECT name FROM main.sqlite_master\n"
         1201  +      " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
         1202  +      " UNION\n"
         1203  +      "SELECT name FROM aux.sqlite_master\n"
         1204  +      " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
         1205  +      " ORDER BY name"
         1206  +    );
         1207  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
         1208  +      xDiff((const char*)sqlite3_column_text(pStmt,0), out);
         1209  +    }
         1210  +    sqlite3_finalize(pStmt);
         1211  +  }
         1212  +
         1213  +  /* TBD: Handle trigger differences */
         1214  +  /* TBD: Handle view differences */
         1215  +  sqlite3_close(g.db);
         1216  +  return 0;
         1217  +}