/ Check-in [e709d3b4]
Login

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

Overview
Comment:Merge all recent trunk enhances into the apple-osx branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: e709d3b4e7b91331f58ed8319f98790cfb190cb1
User & Date: drh 2015-04-13 23:11:45
Context
2015-04-15
13:51
Merge recent trunk fixes and enhancements into the apple-osx branch. check-in: bfc7142a user: drh tags: apple-osx
2015-04-13
23:11
Merge all recent trunk enhances into the apple-osx branch. check-in: e709d3b4 user: drh tags: apple-osx
23:05
Add #ifdef magic for HAVE_GETHOSTUUID so that the build will hopefully now work on more verions of MacOS with SQLITE_ENABLE_LOCKING_STYLE turned on. check-in: 211411d0 user: drh tags: trunk
2015-04-08
12:47
Merge in all version 3.8.9 changes from trunk. check-in: 30121870 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to Makefile.in.

   545    545   		-version-info "8:6:8"
   546    546   
   547    547   sqlite3$(TEXE):	$(TOP)/src/shell.c libsqlite3.la sqlite3.h
   548    548   	$(LTLINK) $(READLINE_FLAGS) \
   549    549   		-o $@ $(TOP)/src/shell.c libsqlite3.la \
   550    550   		$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
   551    551   
          552  +sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
          553  +	$(LTLINK) -o $@ $(TOP)/tool/sqldiff.c	sqlite3.c $(TLIBS)
          554  +
   552    555   mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
   553    556   	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   554    557   		$(TLIBS) -rpath "$(libdir)"
   555    558   
   556    559   MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
   557    560   MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
   558    561   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.

  1015   1015   */
  1016   1016   static int fts3ContentColumns(
  1017   1017     sqlite3 *db,                    /* Database handle */
  1018   1018     const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
  1019   1019     const char *zTbl,               /* Name of content table */
  1020   1020     const char ***pazCol,           /* OUT: Malloc'd array of column names */
  1021   1021     int *pnCol,                     /* OUT: Size of array *pazCol */
  1022         -  int *pnStr                      /* OUT: Bytes of string content */
         1022  +  int *pnStr,                     /* OUT: Bytes of string content */
         1023  +  char **pzErr                    /* OUT: error message */
  1023   1024   ){
  1024   1025     int rc = SQLITE_OK;             /* Return code */
  1025   1026     char *zSql;                     /* "SELECT *" statement on zTbl */  
  1026   1027     sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
  1027   1028   
  1028   1029     zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
  1029   1030     if( !zSql ){
  1030   1031       rc = SQLITE_NOMEM;
  1031   1032     }else{
  1032   1033       rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
         1034  +    if( rc!=SQLITE_OK ){
         1035  +      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
         1036  +    }
  1033   1037     }
  1034   1038     sqlite3_free(zSql);
  1035   1039   
  1036   1040     if( rc==SQLITE_OK ){
  1037   1041       const char **azCol;           /* Output array */
  1038   1042       int nStr = 0;                 /* Size of all column names (incl. 0x00) */
  1039   1043       int nCol;                     /* Number of table columns */
................................................................................
  1277   1281       sqlite3_free(zCompress); 
  1278   1282       sqlite3_free(zUncompress); 
  1279   1283       zCompress = 0;
  1280   1284       zUncompress = 0;
  1281   1285       if( nCol==0 ){
  1282   1286         sqlite3_free((void*)aCol); 
  1283   1287         aCol = 0;
  1284         -      rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
         1288  +      rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
  1285   1289   
  1286   1290         /* If a languageid= option was specified, remove the language id
  1287   1291         ** column from the aCol[] array. */ 
  1288   1292         if( rc==SQLITE_OK && zLanguageid ){
  1289   1293           int j;
  1290   1294           for(j=0; j<nCol; j++){
  1291   1295             if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){

Changes to main.mk.

   386    386   	$(AR) libsqlite3.a $(LIBOBJ)
   387    387   	$(RANLIB) libsqlite3.a
   388    388   
   389    389   sqlite3$(EXE):	$(TOP)/src/shell.c libsqlite3.a sqlite3.h
   390    390   	$(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE)                  \
   391    391   		$(TOP)/src/shell.c                                  \
   392    392   		libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
          393  +
          394  +sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
          395  +	$(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \
          396  +		$(TOP)/tool/sqldiff.c	sqlite3.c $(TLIBS) $(THREADLIB)
   393    397   
   394    398   mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
   395    399   	$(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   396    400   		$(TLIBS) $(THREADLIB)
   397    401   
   398    402   MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
   399    403   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.

  6758   6758     }else{
  6759   6759       assert( bBulk==0 || bBulk==1 );
  6760   6760       if( iParentIdx==0 ){                 
  6761   6761         nxDiv = 0;
  6762   6762       }else if( iParentIdx==i ){
  6763   6763         nxDiv = i-2+bBulk;
  6764   6764       }else{
  6765         -      assert( bBulk==0 );
  6766   6765         nxDiv = iParentIdx-1;
  6767   6766       }
  6768   6767       i = 2-bBulk;
  6769   6768     }
  6770   6769     nOld = i+1;
  6771   6770     if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
  6772   6771       pRight = &pParent->aData[pParent->hdrOffset+8];

Changes to src/build.c.

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

    87     87   
    88     88   #if SQLITE_ENABLE_LOCKING_STYLE
    89     89   # include <sys/ioctl.h>
    90     90   # include <uuid/uuid.h>
    91     91   # include <sys/file.h>
    92     92   # include <sys/param.h>
    93     93   #endif /* SQLITE_ENABLE_LOCKING_STYLE */
           94  +
           95  +#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
           96  +                           (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
           97  +#  if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
           98  +       && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
           99  +#    define HAVE_GETHOSTUUID 1
          100  +#  else
          101  +#    warning "gethostuuid() is disabled."
          102  +#  endif
          103  +#endif
          104  +
    94    105   
    95    106   #if OS_VXWORKS
    96    107   # include <sys/ioctl.h>
    97    108   # include <semaphore.h>
    98    109   # include <limits.h>
    99    110   #endif /* OS_VXWORKS */
   100    111   
................................................................................
  7823   7834   #ifdef SQLITE_TEST
  7824   7835   /* simulate multiple hosts by creating unique hostid file paths */
  7825   7836   int sqlite3_hostid_num = 0;
  7826   7837   #endif
  7827   7838   
  7828   7839   #define PROXY_HOSTIDLEN    16  /* conch file host id length */
  7829   7840   
         7841  +#ifdef HAVE_GETHOSTUUID
  7830   7842   /* Not always defined in the headers as it ought to be */
  7831   7843   extern int gethostuuid(uuid_t id, const struct timespec *wait);
         7844  +#endif
  7832   7845   
  7833   7846   /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
  7834   7847   ** bytes of writable memory.
  7835   7848   */
  7836   7849   static int proxyGetHostID(unsigned char *pHostID, int *pError){
  7837   7850     assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
  7838   7851     memset(pHostID, 0, PROXY_HOSTIDLEN);
  7839         -# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
  7840         -                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
         7852  +#ifdef HAVE_GETHOSTUUID
  7841   7853     {
  7842   7854       struct timespec timeout = {1, 0}; /* 1 sec timeout */
  7843   7855       if( gethostuuid(pHostID, &timeout) ){
  7844   7856         int err = errno;
  7845   7857         if( pError ){
  7846   7858           *pError = err;
  7847   7859         }

Changes to src/select.c.

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

  1232   1232   #define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
  1233   1233   #define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
  1234   1234   #define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
  1235   1235   #define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
  1236   1236   #define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
  1237   1237   #define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
  1238   1238   #define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
         1239  +#define SQLITE_Vacuum         0x08000000  /* Currently in a VACUUM */
  1239   1240   
  1240   1241   
  1241   1242   /*
  1242   1243   ** Bits of the sqlite3.dbOptFlags field that are used by the
  1243   1244   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
  1244   1245   ** selectively disable various optimizations.
  1245   1246   */

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   3895       sqlite3DbFree(db, pFree);
  3876         -  }
  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   6161       res = 0;
  6154   6162       apArg = p->apArg;
  6155   6163       for(i = 0; i<nArg; i++){
  6156   6164         apArg[i] = &pArgc[i+1];
  6157   6165       }
  6158         -
  6159   6166       rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
  6160   6167       sqlite3VtabImportErrmsg(p, pVtab);
  6161   6168       if( rc==SQLITE_OK ){
  6162   6169         res = pModule->xEof(pVtabCursor);
  6163   6170       }
         6171  +  pCur->nullRow = 0;
  6164   6172       VdbeBranchTaken(res!=0,2);
  6165         -    if( res ){
  6166         -      pc = pOp->p2 - 1;
  6167         -    }
  6168         -  }
  6169         -  pCur->nullRow = 0;
  6170         -
         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.

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

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

Changes to src/where.c.

  4777   4777         }
  4778   4778         assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
  4779   4779                           ** changes "x IN (?)" into "x=?". */
  4780   4780   
  4781   4781       }else if( eOp & (WO_EQ) ){
  4782   4782         pNew->wsFlags |= WHERE_COLUMN_EQ;
  4783   4783         if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
  4784         -        if( iCol>=0 && !IsUniqueIndex(pProbe) ){
         4784  +        if( iCol>=0 && pProbe->uniqNotNull==0 ){
  4785   4785             pNew->wsFlags |= WHERE_UNQ_WANTED;
  4786   4786           }else{
  4787   4787             pNew->wsFlags |= WHERE_ONEROW;
  4788   4788           }
  4789   4789         }
  4790   4790       }else if( eOp & WO_ISNULL ){
  4791   4791         pNew->wsFlags |= WHERE_COLUMN_NULL;
................................................................................
  6237   6237         }
  6238   6238       }else{
  6239   6239         pWInfo->nOBSat = pFrom->isOrdered;
  6240   6240         if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
  6241   6241         pWInfo->revMask = pFrom->revLoop;
  6242   6242       }
  6243   6243       if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
  6244         -        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr
         6244  +        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
  6245   6245       ){
  6246   6246         Bitmask revMask = 0;
  6247   6247         int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, 
  6248   6248             pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
  6249   6249         );
  6250   6250         assert( pWInfo->sorted==0 );
  6251   6251         if( nOrder==pWInfo->pOrderBy->nExpr ){

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/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/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/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(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  +      printf("%s", zBuf);
          367  +      break;
          368  +    }
          369  +    case SQLITE_INTEGER: {
          370  +      printf("%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  +        printf("x'");
          379  +        for(i=0; i<nBlob; i++){
          380  +          printf("%02x", zBlob[i]);
          381  +        }
          382  +        printf("'");
          383  +      }else{
          384  +        printf("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  +        printf("NULL");
          394  +      }else{
          395  +        printf("'");
          396  +        for(i=j=0; zArg[i]; i++){
          397  +          if( zArg[i]=='\'' ){
          398  +            printf("%.*s'", i-j+1, &zArg[j]);
          399  +            j = i+1;
          400  +          }
          401  +        }
          402  +        printf("%s'", &zArg[j]);
          403  +      }
          404  +      break;
          405  +    }
          406  +    case SQLITE_NULL: {
          407  +      printf("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){
          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  +    printf("%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  +      printf("%s",ins.z);
          465  +      zSep = "(";
          466  +      for(i=0; i<nCol; i++){
          467  +        printf("%s",zSep);
          468  +        printQuoted(sqlite3_column_value(pStmt,i));
          469  +        zSep = ",";
          470  +      }
          471  +      printf(");\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  +    printf("%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){
          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  +      printf("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);
          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  +    printf("DROP TABLE %s;\n", zId);
          552  +    dump_table(zTab);
          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  +    printf("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  +          printf("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  +            printf("%s %s=", zSep, az2[(i+nPk-1)/2]);
          662  +            zSep = ",";
          663  +            printQuoted(sqlite3_column_value(pStmt,i+1));
          664  +          }
          665  +        }else{                /* Delete a row */
          666  +          printf("DELETE FROM %s", zId);
          667  +        }
          668  +        zSep = " WHERE";
          669  +        for(i=0; i<nPk; i++){
          670  +          printf("%s %s=", zSep, az2[i]);
          671  +          printQuoted(sqlite3_column_value(pStmt,i));
          672  +          zSep = ",";
          673  +        }
          674  +        printf(";\n");
          675  +      }else{                  /* Insert a row */
          676  +        printf("INSERT INTO %s(%s", zId, az2[0]);
          677  +        for(i=1; az2[i]; i++) printf(",%s", az2[i]);
          678  +        printf(") VALUES");
          679  +        zSep = "(";
          680  +        for(i=0; i<nPk2; i++){
          681  +          printf("%s", zSep);
          682  +          zSep = ",";
          683  +          printQuoted(sqlite3_column_value(pStmt,i));
          684  +        }
          685  +        for(i=nPk2+2; i<nQ; i+=2){
          686  +          printf(",");
          687  +          printQuoted(sqlite3_column_value(pStmt,i));
          688  +        }
          689  +        printf(");\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  +    printf("%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  +** Write a 64-bit signed integer as a varint onto out
          719  +*/
          720  +static void putsVarint(FILE *out, sqlite3_uint64 v){
          721  +  int i, n;
          722  +  unsigned char p[12];
          723  +  if( v & (((sqlite3_uint64)0xff000000)<<32) ){
          724  +    p[8] = (unsigned char)v;
          725  +    v >>= 8;
          726  +    for(i=7; i>=0; i--){
          727  +      p[i] = (unsigned char)((v & 0x7f) | 0x80);
          728  +      v >>= 7;
          729  +    }
          730  +    fwrite(p, 8, 1, out);
          731  +  }else{
          732  +    n = 9;
          733  +    do{
          734  +      p[n--] = (unsigned char)((v & 0x7f) | 0x80);
          735  +      v >>= 7;
          736  +    }while( v!=0 );
          737  +    p[9] &= 0x7f;
          738  +    fwrite(p+n+1, 9-n, 1, out);
          739  +  }
          740  +}
          741  +
          742  +/*
          743  +** Write an SQLite value onto out.
          744  +*/
          745  +static void putValue(FILE *out, sqlite3_value *pVal){
          746  +  int iDType = sqlite3_value_type(pVal);
          747  +  sqlite3_int64 iX;
          748  +  double rX;
          749  +  sqlite3_uint64 uX;
          750  +  int j;
          751  +
          752  +  putc(iDType, out);
          753  +  switch( iDType ){
          754  +    case SQLITE_INTEGER:
          755  +      iX = sqlite3_value_int64(pVal);
          756  +      memcpy(&uX, &iX, 8);
          757  +      for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
          758  +      break;
          759  +    case SQLITE_FLOAT:
          760  +      rX = sqlite3_value_int64(pVal);
          761  +      memcpy(&uX, &rX, 8);
          762  +      for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
          763  +      break;
          764  +    case SQLITE_TEXT:
          765  +      iX = sqlite3_value_bytes(pVal);
          766  +      putsVarint(out, (sqlite3_uint64)iX);
          767  +      fwrite(sqlite3_value_text(pVal),1,iX,out);
          768  +      break;
          769  +    case SQLITE_BLOB:
          770  +      iX = sqlite3_value_bytes(pVal);
          771  +      putsVarint(out, (sqlite3_uint64)iX);
          772  +      fwrite(sqlite3_value_blob(pVal),1,iX,out);
          773  +      break;
          774  +    case SQLITE_NULL:
          775  +      break;
          776  +  }
          777  +}
          778  +
          779  +/*
          780  +** Generate a CHANGESET for all differences from main.zTab to aux.zTab.
          781  +*/
          782  +static void changeset_one_table(const char *zTab, FILE *out){
          783  +  sqlite3_stmt *pStmt;          /* SQL statment */
          784  +  char *zId = safeId(zTab);     /* Escaped name of the table */
          785  +  char **azCol = 0;             /* List of escaped column names */
          786  +  int nCol = 0;                 /* Number of columns */
          787  +  int *aiFlg = 0;               /* 0 if column is not part of PK */
          788  +  int *aiPk = 0;                /* Column numbers for each PK column */
          789  +  int nPk = 0;                  /* Number of PRIMARY KEY columns */
          790  +  Str sql;                      /* SQL for the diff query */
          791  +  int i, k;                     /* Loop counters */
          792  +  const char *zSep;             /* List separator */
          793  +
          794  +  pStmt = db_prepare(
          795  +      "SELECT A.sql=B.sql FROM main.sqlite_master A, aux.sqlite_master B"
          796  +      " WHERE A.name=%Q AND B.name=%Q", zTab, zTab
          797  +  );
          798  +  if( SQLITE_ROW==sqlite3_step(pStmt) ){
          799  +    if( sqlite3_column_int(pStmt,0)==0 ){
          800  +      runtimeError("schema changes for table %s", safeId(zTab));
          801  +    }
          802  +  }else{
          803  +    runtimeError("table %s missing from one or both databases", safeId(zTab));
          804  +  }
          805  +  sqlite3_finalize(pStmt);
          806  +  pStmt = db_prepare("PRAGMA main.table_info=%Q", zTab);
          807  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
          808  +    nCol++;
          809  +    azCol = sqlite3_realloc(azCol, sizeof(char*)*nCol);
          810  +    if( azCol==0 ) runtimeError("out of memory");
          811  +    aiFlg = sqlite3_realloc(aiFlg, sizeof(int)*nCol);
          812  +    if( aiFlg==0 ) runtimeError("out of memory");
          813  +    azCol[nCol-1] = safeId((const char*)sqlite3_column_text(pStmt,1));
          814  +    aiFlg[nCol-1] = i = sqlite3_column_int(pStmt,5);
          815  +    if( i>0 ){
          816  +      if( i>nPk ){
          817  +        nPk = i;
          818  +        aiPk = sqlite3_realloc(aiPk, sizeof(int)*nPk);
          819  +        if( aiPk==0 ) runtimeError("out of memory");
          820  +      }
          821  +      aiPk[i-1] = nCol-1;
          822  +    }
          823  +  }
          824  +  sqlite3_finalize(pStmt);
          825  +  if( nPk==0 ) goto end_changeset_one_table; 
          826  +  strInit(&sql);
          827  +  if( nCol>nPk ){
          828  +    strPrintf(&sql, "SELECT %d", SQLITE_UPDATE);
          829  +    for(i=0; i<nCol; i++){
          830  +      if( aiFlg[i] ){
          831  +        strPrintf(&sql, ",\n       A.%s", azCol[i]);
          832  +      }else{
          833  +        strPrintf(&sql, ",\n       A.%s IS NOT B.%s, A.%s, B.%s",
          834  +                  azCol[i], azCol[i], azCol[i], azCol[i]);
          835  +      }
          836  +    }
          837  +    strPrintf(&sql,"\n  FROM main.%s A, aux.%s B\n", zId, zId);
          838  +    zSep = " WHERE";
          839  +    for(i=0; i<nPk; i++){
          840  +      strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
          841  +      zSep = " AND";
          842  +    }
          843  +    zSep = "\n   AND (";
          844  +    for(i=0; i<nCol; i++){
          845  +      if( aiFlg[i] ) continue;
          846  +      strPrintf(&sql, "%sA.%s IS NOT B.%s", zSep, azCol[i], azCol[i]);
          847  +      zSep = " OR\n        ";
          848  +    }
          849  +    strPrintf(&sql,")\n UNION ALL\n");
          850  +  }
          851  +  strPrintf(&sql, "SELECT %d", SQLITE_DELETE);
          852  +  for(i=0; i<nCol; i++){
          853  +    if( aiFlg[i] ){
          854  +      strPrintf(&sql, ",\n       A.%s", azCol[i]);
          855  +    }else{
          856  +      strPrintf(&sql, ",\n       1, A.%s, NULL", azCol[i]);
          857  +    }
          858  +  }
          859  +  strPrintf(&sql, "\n  FROM main.%s A\n", zId);
          860  +  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
          861  +  zSep =          "                   WHERE";
          862  +  for(i=0; i<nPk; i++){
          863  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
          864  +    zSep = " AND";
          865  +  }
          866  +  strPrintf(&sql, ")\n UNION ALL\n");
          867  +  strPrintf(&sql, "SELECT %d", SQLITE_INSERT);
          868  +  for(i=0; i<nCol; i++){
          869  +    if( aiFlg[i] ){
          870  +      strPrintf(&sql, ",\n       B.%s", azCol[i]);
          871  +    }else{
          872  +      strPrintf(&sql, ",\n       1, NULL, B.%s", azCol[i]);
          873  +    }
          874  +  }
          875  +  strPrintf(&sql, "\n  FROM aux.%s B\n", zId);
          876  +  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
          877  +  zSep =          "                   WHERE";
          878  +  for(i=0; i<nPk; i++){
          879  +    strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
          880  +    zSep = " AND";
          881  +  }
          882  +  strPrintf(&sql, ")\n");
          883  +  strPrintf(&sql, " ORDER BY");
          884  +  zSep = " ";
          885  +  for(i=0; i<nPk; i++){
          886  +    strPrintf(&sql, "%s %d", zSep, aiPk[i]+2);
          887  +    zSep = ",";
          888  +  }
          889  +  strPrintf(&sql, ";\n");
          890  +
          891  +  if( g.fDebug & DEBUG_DIFF_SQL ){ 
          892  +    printf("SQL for %s:\n%s\n", zId, sql.z);
          893  +    goto end_changeset_one_table;
          894  +  }
          895  +
          896  +  putc('T', out);
          897  +  putsVarint(out, (sqlite3_uint64)nCol);
          898  +  for(i=0; i<nCol; i++) putc(aiFlg[i]!=0, out);
          899  +  fwrite(zTab, 1, strlen(zTab), out);
          900  +  putc(0, out);
          901  +
          902  +  pStmt = db_prepare("%s", sql.z);
          903  +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
          904  +    int iType = sqlite3_column_int(pStmt,0);
          905  +    putc(iType, out);
          906  +    putc(0, out);
          907  +    switch( sqlite3_column_int(pStmt,0) ){
          908  +      case SQLITE_UPDATE: {
          909  +        for(k=1, i=0; i<nCol; i++){
          910  +          if( aiFlg[i] ){
          911  +            putValue(out, sqlite3_column_value(pStmt,k));
          912  +            k++;
          913  +          }else if( sqlite3_column_int(pStmt,k) ){
          914  +            putValue(out, sqlite3_column_value(pStmt,k+1));
          915  +            k += 3;
          916  +          }else{
          917  +            putc(0, out);
          918  +            k += 3;
          919  +          }
          920  +        }
          921  +        for(k=1, i=0; i<nCol; i++){
          922  +          if( aiFlg[i] ){
          923  +            putc(0, out);
          924  +            k++;
          925  +          }else if( sqlite3_column_int(pStmt,k) ){
          926  +            putValue(out, sqlite3_column_value(pStmt,k+2));
          927  +            k += 3;
          928  +          }else{
          929  +            putc(0, out);
          930  +            k += 3;
          931  +          }
          932  +        }
          933  +        break;
          934  +      }
          935  +      case SQLITE_INSERT: {
          936  +        for(k=1, i=0; i<nCol; i++){
          937  +          if( aiFlg[i] ){
          938  +            putValue(out, sqlite3_column_value(pStmt,k));
          939  +            k++;
          940  +          }else{
          941  +            putValue(out, sqlite3_column_value(pStmt,k+2));
          942  +            k += 3;
          943  +          }
          944  +        }
          945  +        break;
          946  +      }
          947  +      case SQLITE_DELETE: {
          948  +        for(k=1, i=0; i<nCol; i++){
          949  +          if( aiFlg[i] ){
          950  +            putValue(out, sqlite3_column_value(pStmt,k));
          951  +            k++;
          952  +          }else{
          953  +            putValue(out, sqlite3_column_value(pStmt,k+1));
          954  +            k += 3;
          955  +          }
          956  +        }
          957  +        break;
          958  +      }
          959  +    }
          960  +  }
          961  +  sqlite3_finalize(pStmt);
          962  +  
          963  +end_changeset_one_table:
          964  +  while( nCol>0 ) sqlite3_free(azCol[--nCol]);
          965  +  sqlite3_free(azCol);
          966  +  sqlite3_free(aiPk);
          967  +  sqlite3_free(zId);
          968  +}
          969  +
          970  +/*
          971  +** Print sketchy documentation for this utility program
          972  +*/
          973  +static void showHelp(void){
          974  +  printf("Usage: %s [options] DB1 DB2\n", g.zArgv0);
          975  +  printf(
          976  +"Output SQL text that would transform DB1 into DB2.\n"
          977  +"Options:\n"
          978  +"  --changeset FILE      Write a CHANGESET into FILE\n"
          979  +"  --primarykey          Use schema-defined PRIMARY KEYs\n"
          980  +"  --schema              Show only differences in the schema\n"
          981  +"  --table TAB           Show only differences in table TAB\n"
          982  +  );
          983  +}
          984  +
          985  +int main(int argc, char **argv){
          986  +  const char *zDb1 = 0;
          987  +  const char *zDb2 = 0;
          988  +  int i;
          989  +  int rc;
          990  +  char *zErrMsg = 0;
          991  +  char *zSql;
          992  +  sqlite3_stmt *pStmt;
          993  +  char *zTab = 0;
          994  +  FILE *out = 0;
          995  +
          996  +  g.zArgv0 = argv[0];
          997  +  for(i=1; i<argc; i++){
          998  +    const char *z = argv[i];
          999  +    if( z[0]=='-' ){
         1000  +      z++;
         1001  +      if( z[0]=='-' ) z++;
         1002  +      if( strcmp(z,"changeset")==0 ){
         1003  +        out = fopen(argv[++i], "wb");
         1004  +        if( out==0 ) cmdlineError("cannot open: %s", argv[i]);
         1005  +      }else
         1006  +      if( strcmp(z,"debug")==0 ){
         1007  +        g.fDebug = strtol(argv[++i], 0, 0);
         1008  +      }else
         1009  +      if( strcmp(z,"help")==0 ){
         1010  +        showHelp();
         1011  +        return 0;
         1012  +      }else
         1013  +      if( strcmp(z,"primarykey")==0 ){
         1014  +        g.bSchemaPK = 1;
         1015  +      }else
         1016  +      if( strcmp(z,"schema")==0 ){
         1017  +        g.bSchemaOnly = 1;
         1018  +      }else
         1019  +      if( strcmp(z,"table")==0 ){
         1020  +        zTab = argv[++i];
         1021  +      }else
         1022  +      {
         1023  +        cmdlineError("unknown option: %s", argv[i]);
         1024  +      }
         1025  +    }else if( zDb1==0 ){
         1026  +      zDb1 = argv[i];
         1027  +    }else if( zDb2==0 ){
         1028  +      zDb2 = argv[i];
         1029  +    }else{
         1030  +      cmdlineError("unknown argument: %s", argv[i]);
         1031  +    }
         1032  +  }
         1033  +  if( zDb2==0 ){
         1034  +    cmdlineError("two database arguments required");
         1035  +  }
         1036  +  rc = sqlite3_open(zDb1, &g.db);
         1037  +  if( rc ){
         1038  +    cmdlineError("cannot open database file \"%s\"", zDb1);
         1039  +  }
         1040  +  rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_master", 0, 0, &zErrMsg);
         1041  +  if( rc || zErrMsg ){
         1042  +    cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1);
         1043  +  }
         1044  +  zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2);
         1045  +  rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
         1046  +  if( rc || zErrMsg ){
         1047  +    cmdlineError("cannot attach database \"%s\"", zDb2);
         1048  +  }
         1049  +  rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg);
         1050  +  if( rc || zErrMsg ){
         1051  +    cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb2);
         1052  +  }
         1053  +
         1054  +  if( zTab ){
         1055  +    if( out ){
         1056  +      changeset_one_table(zTab, out);
         1057  +    }else{
         1058  +      diff_one_table(zTab);
         1059  +    }
         1060  +  }else{
         1061  +    /* Handle tables one by one */
         1062  +    pStmt = db_prepare(
         1063  +      "SELECT name FROM main.sqlite_master\n"
         1064  +      " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
         1065  +      " UNION\n"
         1066  +      "SELECT name FROM aux.sqlite_master\n"
         1067  +      " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
         1068  +      " ORDER BY name"
         1069  +    );
         1070  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
         1071  +      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
         1072  +      if( out ){
         1073  +        changeset_one_table(zTab, out);
         1074  +      }else{
         1075  +        diff_one_table(zTab);
         1076  +      }
         1077  +    }
         1078  +    sqlite3_finalize(pStmt);
         1079  +  }
         1080  +
         1081  +  /* TBD: Handle trigger differences */
         1082  +  /* TBD: Handle view differences */
         1083  +  sqlite3_close(g.db);
         1084  +  return 0;
         1085  +}