/ Check-in [5addd123]
Login

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

Overview
Comment:Merge in the WITHOUT ROWID changes. A few tests are failing now. They will be fixed in a follow-on check-in.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 5addd1234ded59ce60fb633b76ac87d483377edd
User & Date: drh 2013-11-11 22:55:26
Context
2013-11-11
23:02
Fix two test cases (due to changes in the formatting of constraint errors) so that all tests now pass. check-in: 129e2b69 user: drh tags: sessions
22:55
Merge in the WITHOUT ROWID changes. A few tests are failing now. They will be fixed in a follow-on check-in. check-in: 5addd123 user: drh tags: sessions
19:56
Fix typos in compile and run-time tests of the sqlite library version number in rtree.c. check-in: f58d5701 user: dan tags: trunk
2013-10-28
22:47
Merge all trunk changes since 3.8.1 into the sessions branch. check-in: aa72ea8a user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   941    941   	echo "static const char *zMainloop = " >> $@
   942    942   	$(NAWK) -f $(TOP)/tool/tostr.awk $(TOP)/tool/spaceanal.tcl >> $@
   943    943   	echo "; return zMainloop; }" >> $@
   944    944   
   945    945   sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
   946    946   	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)
   947    947   
          948  +showdb$(TEXE):	$(TOP)/tool/showdb.c sqlite3.c
          949  +	$(LTLINK) -o $@ $(TOP)/tool/showdb.c sqlite3.c $(TLIBS)
          950  +
          951  +wordcount$(TEXE):	$(TOP)/test/wordcount.c sqlite3.c
          952  +	$(LTLINK) -o $@ $(TOP)/test/wordcount.c sqlite3.c $(TLIBS)
          953  +
   948    954   # Standard install and cleanup targets
   949    955   #
   950    956   lib_install:	libsqlite3.la
   951    957   	$(INSTALL) -d $(DESTDIR)$(libdir)
   952    958   	$(LTINSTALL) libsqlite3.la $(DESTDIR)$(libdir)
   953    959   	
   954    960   install:	sqlite3$(BEXE) lib_install sqlite3.h sqlite3.pc ${HAVE_TCL:1=tcl_install}

Changes to Makefile.msc.

  1350   1350   	echo static const char *zMainloop = >> $@
  1351   1351   	$(NAWK) -f $(TOP)\tool\tostr.awk $(TOP)\tool\spaceanal.tcl >> $@
  1352   1352   	echo ; return zMainloop; } >> $@
  1353   1353   
  1354   1354   sqlite3_analyzer.exe:	sqlite3_analyzer.c $(LIBRESOBJS)
  1355   1355   	$(LTLINK) -DBUILD_sqlite -DTCLSH=2 -I$(TCLINCDIR) sqlite3_analyzer.c \
  1356   1356   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
         1357  +
         1358  +showdb.exe:	$(TOP)/tool/showdb.c sqlite3.c
         1359  +	$(LTLINK) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o $@ \
         1360  +		$(TOP)/tool/showdb.c sqlite3.c
         1361  +
         1362  +wordcount.exe:	$(TOP)/test/wordcount.c sqlite3.c
         1363  +	$(LTLINK) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o $@ \
         1364  +		$(TOP)/test/wordcount.c sqlite3.c
  1357   1365   
  1358   1366   clean:
  1359   1367   	del /Q *.lo *.ilk *.lib *.obj *.pdb sqlite3.exe libsqlite3.lib
  1360   1368   	del /Q *.cod *.da *.bb *.bbg gmon.out
  1361   1369   	del /Q sqlite3.h opcodes.c opcodes.h
  1362   1370   	del /Q lemon.exe lempar.c parse.*
  1363   1371   	del /Q mkkeywordhash.exe keywordhash.h

Changes to VERSION.

     1         -3.8.1
            1  +3.8.2

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

Changes to ext/fts3/fts3_expr.c.

   151    151         }
   152    152       }
   153    153     }
   154    154     *ppCsr = pCsr;
   155    155     return rc;
   156    156   }
   157    157   
          158  +/*
          159  +** Function getNextNode(), which is called by fts3ExprParse(), may itself
          160  +** call fts3ExprParse(). So this forward declaration is required.
          161  +*/
          162  +static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
   158    163   
   159    164   /*
   160    165   ** Extract the next token from buffer z (length n) using the tokenizer
   161    166   ** and other information (column names etc.) in pParse. Create an Fts3Expr
   162    167   ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
   163    168   ** single token and set *ppExpr to point to it. If the end of the buffer is
   164    169   ** reached before a token is found, set *ppExpr to zero. It is the
................................................................................
   185    190     rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
   186    191     if( rc==SQLITE_OK ){
   187    192       const char *zToken;
   188    193       int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
   189    194       int nByte;                               /* total space to allocate */
   190    195   
   191    196       rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
   192         -    if( rc==SQLITE_OK ){
          197  +
          198  +    if( (rc==SQLITE_OK || rc==SQLITE_DONE) && sqlite3_fts3_enable_parentheses ){
          199  +      int i;
          200  +      if( rc==SQLITE_DONE ) iStart = n;
          201  +      for(i=0; i<iStart; i++){
          202  +        if( z[i]=='(' ){
          203  +          pParse->nNest++;
          204  +          rc = fts3ExprParse(pParse, &z[i+1], n-i-1, &pRet, &nConsumed);
          205  +          if( rc==SQLITE_OK && !pRet ){
          206  +            rc = SQLITE_DONE;
          207  +          }
          208  +          nConsumed = (int)(i + 1 + nConsumed);
          209  +          break;
          210  +        }
          211  +
          212  +        if( z[i]==')' ){
          213  +          rc = SQLITE_DONE;
          214  +          pParse->nNest--;
          215  +          nConsumed = i+1;
          216  +          break;
          217  +        }
          218  +      }
          219  +    }
          220  +
          221  +    if( nConsumed==0 && rc==SQLITE_OK ){
   193    222         nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
   194    223         pRet = (Fts3Expr *)fts3MallocZero(nByte);
   195    224         if( !pRet ){
   196    225           rc = SQLITE_NOMEM;
   197    226         }else{
   198    227           pRet->eType = FTSQUERY_PHRASE;
   199    228           pRet->pPhrase = (Fts3Phrase *)&pRet[1];
................................................................................
   365    394     }
   366    395     sqlite3_free(zTemp);
   367    396     sqlite3_free(p);
   368    397     *ppExpr = 0;
   369    398     return SQLITE_NOMEM;
   370    399   }
   371    400   
   372         -/*
   373         -** Function getNextNode(), which is called by fts3ExprParse(), may itself
   374         -** call fts3ExprParse(). So this forward declaration is required.
   375         -*/
   376         -static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
   377         -
   378    401   /*
   379    402   ** The output variable *ppExpr is populated with an allocated Fts3Expr 
   380    403   ** structure, or set to 0 if the end of the input buffer is reached.
   381    404   **
   382    405   ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
   383    406   ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
   384    407   ** If SQLITE_ERROR is returned, pContext is populated with an error message.
................................................................................
   467    490   
   468    491         /* Turns out that wasn't a keyword after all. This happens if the
   469    492         ** user has supplied a token such as "ORacle". Continue.
   470    493         */
   471    494       }
   472    495     }
   473    496   
   474         -  /* Check for an open bracket. */
   475         -  if( sqlite3_fts3_enable_parentheses ){
   476         -    if( *zInput=='(' ){
   477         -      int nConsumed;
   478         -      pParse->nNest++;
   479         -      rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
   480         -      if( rc==SQLITE_OK && !*ppExpr ){
   481         -        rc = SQLITE_DONE;
   482         -      }
   483         -      *pnConsumed = (int)((zInput - z) + 1 + nConsumed);
   484         -      return rc;
   485         -    }
   486         -  
   487         -    /* Check for a close bracket. */
   488         -    if( *zInput==')' ){
   489         -      pParse->nNest--;
   490         -      *pnConsumed = (int)((zInput - z) + 1);
   491         -      return SQLITE_DONE;
   492         -    }
   493         -  }
   494         -
   495    497     /* See if we are dealing with a quoted phrase. If this is the case, then
   496    498     ** search for the closing quote and pass the whole string to getNextString()
   497    499     ** for processing. This is easy to do, as fts3 has no syntax for escaping
   498    500     ** a quote character embedded in a string.
   499    501     */
   500    502     if( *zInput=='"' ){
   501    503       for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);

Changes to ext/rtree/rtree.c.

   133    133   
   134    134   /* Size of hash table Rtree.aHash. This hash table is not expected to
   135    135   ** ever contain very many entries, so a fixed number of buckets is 
   136    136   ** used.
   137    137   */
   138    138   #define HASHSIZE 128
   139    139   
          140  +/* The xBestIndex method of this virtual table requires an estimate of
          141  +** the number of rows in the virtual table to calculate the costs of
          142  +** various strategies. If possible, this estimate is loaded from the
          143  +** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
          144  +** Otherwise, if no sqlite_stat1 entry is available, use 
          145  +** RTREE_DEFAULT_ROWEST.
          146  +*/
          147  +#define RTREE_DEFAULT_ROWEST 1048576
          148  +#define RTREE_MIN_ROWEST         100
          149  +
   140    150   /* 
   141    151   ** An rtree virtual-table object.
   142    152   */
   143    153   struct Rtree {
   144    154     sqlite3_vtab base;
   145    155     sqlite3 *db;                /* Host database connection */
   146    156     int iNodeSize;              /* Size in bytes of each node in the node table */
................................................................................
   147    157     int nDim;                   /* Number of dimensions */
   148    158     int nBytesPerCell;          /* Bytes consumed per cell */
   149    159     int iDepth;                 /* Current depth of the r-tree structure */
   150    160     char *zDb;                  /* Name of database containing r-tree table */
   151    161     char *zName;                /* Name of r-tree table */ 
   152    162     RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
   153    163     int nBusy;                  /* Current number of users of this structure */
          164  +  i64 nRowEst;                /* Estimated number of rows in this table */
   154    165   
   155    166     /* List of nodes removed during a CondenseTree operation. List is
   156    167     ** linked together via the pointer normally used for hash chains -
   157    168     ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
   158    169     ** headed by the node (leaf nodes have RtreeNode.iNode==0).
   159    170     */
   160    171     RtreeNode *pDeleted;
................................................................................
  1338   1349         assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
  1339   1350       }
  1340   1351     }
  1341   1352   
  1342   1353     rtreeRelease(pRtree);
  1343   1354     return rc;
  1344   1355   }
         1356  +
         1357  +/*
         1358  +** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
         1359  +** extension is currently being used by a version of SQLite too old to
         1360  +** support estimatedRows. In that case this function is a no-op.
         1361  +*/
         1362  +static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
         1363  +#if SQLITE_VERSION_NUMBER>=3008002
         1364  +  if( sqlite3_libversion_number()>=3008002 ){
         1365  +    pIdxInfo->estimatedRows = nRow;
         1366  +  }
         1367  +#endif
         1368  +}
  1345   1369   
  1346   1370   /*
  1347   1371   ** Rtree virtual table module xBestIndex method. There are three
  1348   1372   ** table scan strategies to choose from (in order from most to 
  1349   1373   ** least desirable):
  1350   1374   **
  1351   1375   **   idxNum     idxStr        Strategy
................................................................................
  1374   1398   **   ----------------------
  1375   1399   **
  1376   1400   ** The second of each pair of bytes identifies the coordinate column
  1377   1401   ** to which the constraint applies. The leftmost coordinate column
  1378   1402   ** is 'a', the second from the left 'b' etc.
  1379   1403   */
  1380   1404   static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
         1405  +  Rtree *pRtree = (Rtree*)tab;
  1381   1406     int rc = SQLITE_OK;
  1382   1407     int ii;
         1408  +  i64 nRow;                       /* Estimated rows returned by this scan */
  1383   1409   
  1384   1410     int iIdx = 0;
  1385   1411     char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
  1386   1412     memset(zIdxStr, 0, sizeof(zIdxStr));
  1387         -  UNUSED_PARAMETER(tab);
  1388   1413   
  1389   1414     assert( pIdxInfo->idxStr==0 );
  1390   1415     for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
  1391   1416       struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
  1392   1417   
  1393   1418       if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
  1394   1419         /* We have an equality constraint on the rowid. Use strategy 1. */
................................................................................
  1400   1425         pIdxInfo->idxNum = 1;
  1401   1426         pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
  1402   1427         pIdxInfo->aConstraintUsage[jj].omit = 1;
  1403   1428   
  1404   1429         /* This strategy involves a two rowid lookups on an B-Tree structures
  1405   1430         ** and then a linear search of an R-Tree node. This should be 
  1406   1431         ** considered almost as quick as a direct rowid lookup (for which 
  1407         -      ** sqlite uses an internal cost of 0.0).
         1432  +      ** sqlite uses an internal cost of 0.0). It is expected to return
         1433  +      ** a single row.
  1408   1434         */ 
  1409         -      pIdxInfo->estimatedCost = 10.0;
         1435  +      pIdxInfo->estimatedCost = 30.0;
         1436  +      setEstimatedRows(pIdxInfo, 1);
  1410   1437         return SQLITE_OK;
  1411   1438       }
  1412   1439   
  1413   1440       if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
  1414   1441         u8 op;
  1415   1442         switch( p->op ){
  1416   1443           case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
................................................................................
  1431   1458     }
  1432   1459   
  1433   1460     pIdxInfo->idxNum = 2;
  1434   1461     pIdxInfo->needToFreeIdxStr = 1;
  1435   1462     if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
  1436   1463       return SQLITE_NOMEM;
  1437   1464     }
  1438         -  assert( iIdx>=0 );
  1439         -  pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
         1465  +
         1466  +  nRow = pRtree->nRowEst / (iIdx + 1);
         1467  +  pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
         1468  +  setEstimatedRows(pIdxInfo, nRow);
         1469  +
  1440   1470     return rc;
  1441   1471   }
  1442   1472   
  1443   1473   /*
  1444   1474   ** Return the N-dimensional volumn of the cell stored in *p.
  1445   1475   */
  1446   1476   static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
................................................................................
  2906   2936     );
  2907   2937     if( zSql ){
  2908   2938       rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
  2909   2939       sqlite3_free(zSql);
  2910   2940     }
  2911   2941     return rc;
  2912   2942   }
         2943  +
         2944  +/*
         2945  +** This function populates the pRtree->nRowEst variable with an estimate
         2946  +** of the number of rows in the virtual table. If possible, this is based
         2947  +** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
         2948  +*/
         2949  +static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
         2950  +  const char *zSql = "SELECT stat FROM sqlite_stat1 WHERE tbl= ? || '_rowid'";
         2951  +  sqlite3_stmt *p;
         2952  +  int rc;
         2953  +  i64 nRow = 0;
         2954  +
         2955  +  rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
         2956  +  if( rc==SQLITE_OK ){
         2957  +    sqlite3_bind_text(p, 1, pRtree->zName, -1, SQLITE_STATIC);
         2958  +    if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
         2959  +    rc = sqlite3_finalize(p);
         2960  +  }else if( rc!=SQLITE_NOMEM ){
         2961  +    rc = SQLITE_OK;
         2962  +  }
         2963  +
         2964  +  if( rc==SQLITE_OK ){
         2965  +    if( nRow==0 ){
         2966  +      pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
         2967  +    }else{
         2968  +      pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
         2969  +    }
         2970  +  }
         2971  +
         2972  +  return rc;
         2973  +}
  2913   2974   
  2914   2975   static sqlite3_module rtreeModule = {
  2915   2976     0,                          /* iVersion */
  2916   2977     rtreeCreate,                /* xCreate - create a table */
  2917   2978     rtreeConnect,               /* xConnect - connect to an existing table */
  2918   2979     rtreeBestIndex,             /* xBestIndex - Determine search strategy */
  2919   2980     rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
................................................................................
  2992   3053     appStmt[3] = &pRtree->pReadRowid;
  2993   3054     appStmt[4] = &pRtree->pWriteRowid;
  2994   3055     appStmt[5] = &pRtree->pDeleteRowid;
  2995   3056     appStmt[6] = &pRtree->pReadParent;
  2996   3057     appStmt[7] = &pRtree->pWriteParent;
  2997   3058     appStmt[8] = &pRtree->pDeleteParent;
  2998   3059   
         3060  +  rc = rtreeQueryStat1(db, pRtree);
  2999   3061     for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
  3000   3062       char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
  3001   3063       if( zSql ){
  3002   3064         rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
  3003   3065       }else{
  3004   3066         rc = SQLITE_NOMEM;
  3005   3067       }

Changes to ext/rtree/rtree6.test.

    92     92     0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?)}
    93     93   }
    94     94   
    95     95   do_eqp_test rtree6.2.4 {
    96     96     SELECT * FROM t1,t2 WHERE v=10 and x1<10 and x2>10
    97     97   } {
    98     98     0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:CaEb} 
    99         -  0 1 1 {SCAN TABLE t2}
           99  +  0 1 1 {SEARCH TABLE t2 USING AUTOMATIC COVERING INDEX (v=?)}
   100    100   }
   101    101   
   102    102   do_eqp_test rtree6.2.5 {
   103    103     SELECT * FROM t1,t2 WHERE k=ii AND x1<v
   104    104   } {
   105    105     0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:} 
   106    106     0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?)}

Added ext/rtree/rtreeC.test.

            1  +# 2011 March 2
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# Make sure the rtreenode() testing function can handle entries with
           12  +# 64-bit rowids.
           13  +# 
           14  +
           15  +if {![info exists testdir]} {
           16  +  set testdir [file join [file dirname [info script]] .. .. test]
           17  +} 
           18  +source $testdir/tester.tcl
           19  +ifcapable !rtree { finish_test ; return }
           20  +set testprefix rtreeC
           21  +
           22  +do_execsql_test 1.0 {
           23  +  CREATE VIRTUAL TABLE r_tree USING rtree(id, min_x, max_x, min_y, max_y);
           24  +  CREATE TABLE t(x, y);
           25  +}
           26  +
           27  +do_eqp_test 1.1 {
           28  +  SELECT * FROM r_tree, t 
           29  +  WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND t.x<=max_y
           30  +} {
           31  +  0 0 1 {SCAN TABLE t}
           32  +  0 1 0 {SCAN TABLE r_tree VIRTUAL TABLE INDEX 2:DdBcDbBa}
           33  +}
           34  +
           35  +do_eqp_test 1.2 {
           36  +  SELECT * FROM t, r_tree
           37  +  WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND t.x<=max_y
           38  +} {
           39  +  0 0 0 {SCAN TABLE t}
           40  +  0 1 1 {SCAN TABLE r_tree VIRTUAL TABLE INDEX 2:DdBcDbBa}
           41  +}
           42  +
           43  +do_eqp_test 1.3 {
           44  +  SELECT * FROM t, r_tree
           45  +  WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND ?<=max_y
           46  +} {
           47  +  0 0 0 {SCAN TABLE t}
           48  +  0 1 1 {SCAN TABLE r_tree VIRTUAL TABLE INDEX 2:DdBcDbBa}
           49  +}
           50  +
           51  +do_eqp_test 1.5 {
           52  +  SELECT * FROM t, r_tree
           53  +} {
           54  +  0 0 1 {SCAN TABLE r_tree VIRTUAL TABLE INDEX 2:}
           55  +  0 1 0 {SCAN TABLE t} 
           56  +}
           57  +
           58  +do_execsql_test 2.0 {
           59  +  INSERT INTO t VALUES(0, 0);
           60  +  INSERT INTO t VALUES(0, 1);
           61  +  INSERT INTO t VALUES(0, 2);
           62  +  INSERT INTO t VALUES(0, 3);
           63  +  INSERT INTO t VALUES(0, 4);
           64  +  INSERT INTO t VALUES(0, 5);
           65  +  INSERT INTO t VALUES(0, 6);
           66  +  INSERT INTO t VALUES(0, 7);
           67  +  INSERT INTO t VALUES(0, 8);
           68  +  INSERT INTO t VALUES(0, 9);
           69  +
           70  +  INSERT INTO t SELECT x+1, y FROM t;
           71  +  INSERT INTO t SELECT x+2, y FROM t;
           72  +  INSERT INTO t SELECT x+4, y FROM t;
           73  +  INSERT INTO r_tree SELECT NULL, x-1, x+1, y-1, y+1 FROM t;
           74  +  ANALYZE;
           75  +}
           76  +
           77  +db close
           78  +sqlite3 db test.db
           79  +
           80  +do_eqp_test 2.1 {
           81  +  SELECT * FROM r_tree, t 
           82  +  WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND t.x<=max_y
           83  +} {
           84  +  0 0 1 {SCAN TABLE t}
           85  +  0 1 0 {SCAN TABLE r_tree VIRTUAL TABLE INDEX 2:DdBcDbBa}
           86  +}
           87  +
           88  +do_eqp_test 2.2 {
           89  +  SELECT * FROM t, r_tree
           90  +  WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND t.x<=max_y
           91  +} {
           92  +  0 0 0 {SCAN TABLE t}
           93  +  0 1 1 {SCAN TABLE r_tree VIRTUAL TABLE INDEX 2:DdBcDbBa}
           94  +}
           95  +
           96  +do_eqp_test 2.3 {
           97  +  SELECT * FROM t, r_tree
           98  +  WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND ?<=max_y
           99  +} {
          100  +  0 0 0 {SCAN TABLE t}
          101  +  0 1 1 {SCAN TABLE r_tree VIRTUAL TABLE INDEX 2:DdBcDbBa}
          102  +}
          103  +
          104  +do_eqp_test 2.5 {
          105  +  SELECT * FROM t, r_tree
          106  +} {
          107  +  0 0 1 {SCAN TABLE r_tree VIRTUAL TABLE INDEX 2:}
          108  +  0 1 0 {SCAN TABLE t} 
          109  +}
          110  +
          111  +finish_test
          112  +

Changes to magic.txt.

    23     23   >68  belong  =0x0f055113  Fossil global configuration - 
    24     24   >68  belong  =0x0f055111  Fossil repository -
    25     25   >68  belong  =0x42654462  Bentley Systems BeSQLite Database -
    26     26   >68  belong  =0x42654c6e  Bentley Systems Localization File -
    27     27   >60  belong  =0x5f4d544e  Monotone source repository -
    28     28   >68  belong  =0x47504b47  OGC GeoPackage file -
    29     29   >68  belong  =0x47503130  OGC GeoPackage version 1.0 file -
           30  +>68  belong  =0x45737269  Esri Spatially-Enabled Database -
    30     31   >0   string  =SQLite      SQLite3 database

Changes to main.mk.

   627    627   TEST_EXTENSION = $(SHPREFIX)testloadext.$(SO)
   628    628   $(TEST_EXTENSION): $(TOP)/src/test_loadext.c
   629    629   	$(MKSHLIB) $(TOP)/src/test_loadext.c -o $(TEST_EXTENSION)
   630    630   
   631    631   extensiontest: testfixture$(EXE) $(TEST_EXTENSION)
   632    632   	./testfixture$(EXE) $(TOP)/test/loadext.test
   633    633   
   634         -showdb:	$(TOP)/tool/showdb.c sqlite3.c
   635         -	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showdb \
          634  +showdb$(EXE):	$(TOP)/tool/showdb.c sqlite3.c
          635  +	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showdb$(EXE) \
   636    636   		$(TOP)/tool/showdb.c sqlite3.c
          637  +
          638  +wordcount$(EXE):	$(TOP)/test/wordcount.c sqlite3.c
          639  +	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o wordcount$(EXE) \
          640  +		$(TOP)/test/wordcount.c sqlite3.c
   637    641   
   638    642   # This target will fail if the SQLite amalgamation contains any exported
   639    643   # symbols that do not begin with "sqlite3_". It is run as part of the
   640    644   # releasetest.tcl script.
   641    645   #
   642    646   checksymbols: sqlite3.o
   643    647   	nm -g --defined-only sqlite3.o | grep -v " sqlite3_" ; test $$? -ne 0

Changes to mkopcodec.awk.

     8      8   # opcodes used by the VDBE.  These strings are used when disassembling a
     9      9   # VDBE program during tracing or as a result of the EXPLAIN keyword.
    10     10   #
    11     11   BEGIN {
    12     12     print "/* Automatically generated.  Do not edit */"
    13     13     print "/* See the mkopcodec.awk script for details. */"
    14     14     printf "#if !defined(SQLITE_OMIT_EXPLAIN)"
    15         -  printf    " || !defined(NDEBUG)"
    16     15     printf    " || defined(VDBE_PROFILE)"
    17     16     print     " || defined(SQLITE_DEBUG)"
           17  +  print "#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)"
           18  +  print "# define OpHelp(X) \"\\0\" X"
           19  +  print "#else"
           20  +  print "# define OpHelp(X)"
           21  +  print "#endif"
    18     22     print "const char *sqlite3OpcodeName(int i){"
    19     23     print " static const char *const azName[] = { \"?\","
    20     24     mx = 0
    21     25   }
    22         -/define OP_/ {
           26  +/^.define OP_/ {
    23     27     sub("OP_","",$2)
    24     28     i = $3+0
    25     29     label[i] = $2
    26     30     if( mx<i ) mx = i
           31  +  for(j=5; j<NF; j++) if( $j=="synopsis:" ) break
           32  +  if( j<NF ){
           33  +    j++
           34  +    x = $j
           35  +    for(j=j+1; j<NF; j++) x = x " " $j
           36  +    synopsis[i] = x
           37  +  }else{
           38  +    synopsis[i] = ""
           39  +  }
    27     40   }
    28     41   END {
    29     42     for(i=1; i<=mx; i++){
    30         -    printf "     /* %3d */ \"%s\",\n", i, label[i]
           43  +    printf "     /* %3d */ %-18s OpHelp(\"%s\"),\n", i, \
           44  +        "\"" label[i] "\"", synopsis[i]
    31     45     }
    32     46     print "  };"
    33     47     print "  return azName[i];"
    34     48     print "}"
    35     49     print "#endif"
    36     50   }

Changes to mkopcodeh.awk.

    33     33   #
    34     34   
    35     35   
    36     36   # Remember the TK_ values from the parse.h file
    37     37   /^#define TK_/ {
    38     38     tk[$2] = 0+$3    # tk[x] holds the numeric value for TK symbol X
    39     39   }
           40  +
           41  +# Find "/* Opcode: " lines in the vdbe.c file.  Each one introduces
           42  +# a new opcode.  Remember which parameters are used.
           43  +/^.. Opcode: / {
           44  +  currentOp = "OP_" $3
           45  +  m = 0
           46  +  for(i=4; i<=NF; i++){
           47  +    x = $i
           48  +    if( x=="P1" ) m += 1
           49  +    if( x=="P2" ) m += 2
           50  +    if( x=="P3" ) m += 4
           51  +    if( x=="P4" ) m += 8
           52  +    if( x=="P5" ) m += 16
           53  +  }
           54  +  paramused[currentOp] = m
           55  +}
           56  +
           57  +# Find "** Synopsis: " lines that follow Opcode:
           58  +/^.. Synopsis: / {
           59  +  if( currentOp ){
           60  +    x = $3
           61  +    for(i=4; i<=NF; i++){
           62  +      x = x " " $i
           63  +    }
           64  +    synopsis[currentOp] = x
           65  +  }
           66  +}
    40     67   
    41     68   # Scan for "case OP_aaaa:" lines in the vdbe.c file
    42     69   /^case OP_/ {
    43     70     name = $2
    44     71     sub(/:/,"",name)
    45     72     sub("\r","",name)
    46     73     op[name] = -1       # op[x] holds the numeric value for OP symbol x
................................................................................
   132    159       }
   133    160     }
   134    161     max = cnt
   135    162     for(i=1; i<=max; i++){
   136    163       if( !used[i] ){
   137    164         def[i] = "OP_NotUsed_" i 
   138    165       }
   139         -    printf "#define %-25s %15d", def[i], i
          166  +    printf "#define %-16s %3d", def[i], i
          167  +    com = ""
   140    168       if( sameas[i] ){
   141         -      printf "   /* same as %-12s*/", sameas[i]
   142         -    } 
          169  +      com = "same as " sameas[i]
          170  +    }
          171  +    x = synopsis[def[i]]
          172  +    if( x ){
          173  +      if( com=="" ){
          174  +        com = "synopsis: " x
          175  +      } else {
          176  +        com = com ", synopsis: " x
          177  +      }
          178  +    }
          179  +    if( com!="" ){
          180  +      printf " /* %-42s */", com
          181  +    }
   143    182       printf "\n"
   144    183     }
   145    184   
   146    185     # Generate the bitvectors:
   147    186     #
   148    187     #  bit 0:     jump
   149    188     #  bit 1:     pushes a result onto stack
................................................................................
   176    215     print "#define OPFLG_INITIALIZER {\\"
   177    216     for(i=0; i<=max; i++){
   178    217       if( i%8==0 ) printf("/* %3d */",i)
   179    218       printf " 0x%02x,", bv[i]
   180    219       if( i%8==7 ) printf("\\\n");
   181    220     }
   182    221     print "}"
          222  +  if( 0 ){
          223  +    print "\n/* Bitmask to indicate which fields (P1..P5) of each opcode are"
          224  +    print "** actually used.\n*/"
          225  +    print "#define OP_PARAM_USED_INITIALIZER {\\"
          226  +    for(i=0; i<=max; i++){
          227  +      if( i%8==0 ) printf("/* %3d */",i)
          228  +      printf " 0x%02x,", paramused[def[i]]
          229  +      if( i%8==7 ) printf("\\\n");
          230  +    }
          231  +    print "}"
          232  +  }
   183    233   }

Changes to src/analyze.c.

   240    240         }
   241    241       }
   242    242     }
   243    243   
   244    244     /* Open the sqlite_stat[134] tables for writing. */
   245    245     for(i=0; aTable[i].zCols; i++){
   246    246       assert( i<ArraySize(aTable) );
   247         -    sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
   248         -    sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
          247  +    sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
   249    248       sqlite3VdbeChangeP5(v, aCreateTbl[i]);
   250    249     }
   251    250   }
   252    251   
   253    252   /*
   254    253   ** Recommended number of samples for sqlite_stat4
   255    254   */
................................................................................
   265    264   typedef struct Stat4Accum Stat4Accum;
   266    265   typedef struct Stat4Sample Stat4Sample;
   267    266   struct Stat4Sample {
   268    267     tRowcnt *anEq;                  /* sqlite_stat4.nEq */
   269    268     tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
   270    269   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   271    270     tRowcnt *anLt;                  /* sqlite_stat4.nLt */
   272         -  i64 iRowid;                     /* Rowid in main table of the key */
          271  +  union {
          272  +    i64 iRowid;                     /* Rowid in main table of the key */
          273  +    u8 *aRowid;                     /* Key for WITHOUT ROWID tables */
          274  +  } u;
          275  +  u32 nRowid;                     /* Sizeof aRowid[] */
   273    276     u8 isPSample;                   /* True if a periodic sample */
   274    277     int iCol;                       /* If !isPSample, the reason for inclusion */
   275    278     u32 iHash;                      /* Tiebreaker hash */
   276    279   #endif
   277    280   };                                                    
   278    281   struct Stat4Accum {
   279    282     tRowcnt nRow;             /* Number of rows in the entire table */
   280    283     tRowcnt nPSample;         /* How often to do a periodic sample */
   281    284     int nCol;                 /* Number of columns in index + rowid */
   282    285     int mxSample;             /* Maximum number of samples to accumulate */
   283    286     Stat4Sample current;      /* Current row as a Stat4Sample */
   284    287     u32 iPrn;                 /* Pseudo-random number used for sampling */
   285         -  Stat4Sample *aBest;       /* Array of (nCol-1) best samples */
          288  +  Stat4Sample *aBest;       /* Array of nCol best samples */
   286    289     int iMin;                 /* Index in a[] of entry with minimum score */
   287    290     int nSample;              /* Current number of samples */
   288    291     int iGet;                 /* Index of current sample accessed by stat_get() */
   289    292     Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
          293  +  sqlite3 *db;              /* Database connection, for malloc() */
   290    294   };
          295  +
          296  +/* Reclaim memory used by a Stat4Sample
          297  +*/
          298  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          299  +static void sampleClear(sqlite3 *db, Stat4Sample *p){
          300  +  assert( db!=0 );
          301  +  if( p->nRowid ){
          302  +    sqlite3DbFree(db, p->u.aRowid);
          303  +    p->nRowid = 0;
          304  +  }
          305  +}
          306  +#endif
          307  +
          308  +/* Initialize the BLOB value of a ROWID
          309  +*/
          310  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          311  +static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
          312  +  assert( db!=0 );
          313  +  if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
          314  +  p->u.aRowid = sqlite3DbMallocRaw(db, n);
          315  +  if( p->u.aRowid ){
          316  +    p->nRowid = n;
          317  +    memcpy(p->u.aRowid, pData, n);
          318  +  }else{
          319  +    p->nRowid = 0;
          320  +  }
          321  +}
          322  +#endif
          323  +
          324  +/* Initialize the INTEGER value of a ROWID.
          325  +*/
          326  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          327  +static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
          328  +  assert( db!=0 );
          329  +  if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
          330  +  p->nRowid = 0;
          331  +  p->u.iRowid = iRowid;
          332  +}
          333  +#endif
          334  +
          335  +
          336  +/*
          337  +** Copy the contents of object (*pFrom) into (*pTo).
          338  +*/
          339  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          340  +static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
          341  +  pTo->isPSample = pFrom->isPSample;
          342  +  pTo->iCol = pFrom->iCol;
          343  +  pTo->iHash = pFrom->iHash;
          344  +  memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
          345  +  memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
          346  +  memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
          347  +  if( pFrom->nRowid ){
          348  +    sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
          349  +  }else{
          350  +    sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
          351  +  }
          352  +}
          353  +#endif
          354  +
          355  +/*
          356  +** Reclaim all memory of a Stat4Accum structure.
          357  +*/
          358  +static void stat4Destructor(void *pOld){
          359  +  Stat4Accum *p = (Stat4Accum*)pOld;
          360  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          361  +  int i;
          362  +  for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
          363  +  for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
          364  +  sampleClear(p->db, &p->current);
          365  +#endif
          366  +  sqlite3DbFree(p->db, p);
          367  +}
   291    368   
   292    369   /*
   293    370   ** Implementation of the stat_init(N,C) SQL function. The two parameters
   294    371   ** are the number of rows in the table or index (C) and the number of columns
   295    372   ** in the index (N).  The second argument (C) is only used for STAT3 and STAT4.
   296    373   **
   297    374   ** This routine allocates the Stat4Accum object in heap memory. The return 
................................................................................
   303    380     int argc,
   304    381     sqlite3_value **argv
   305    382   ){
   306    383     Stat4Accum *p;
   307    384     int nCol;                       /* Number of columns in index being sampled */
   308    385     int nColUp;                     /* nCol rounded up for alignment */
   309    386     int n;                          /* Bytes of space to allocate */
          387  +  sqlite3 *db;                    /* Database connection */
   310    388   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   311    389     int mxSample = SQLITE_STAT4_SAMPLES;
   312    390   #endif
   313    391   
   314    392     /* Decode the three function arguments */
   315    393     UNUSED_PARAMETER(argc);
   316    394     nCol = sqlite3_value_int(argv[0]);
................................................................................
   319    397   
   320    398     /* Allocate the space required for the Stat4Accum object */
   321    399     n = sizeof(*p) 
   322    400       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anEq */
   323    401       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anDLt */
   324    402   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   325    403       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anLt */
   326         -    + sizeof(Stat4Sample)*(nCol+mxSample)   /* Stat4Accum.aBest[], a[] */
          404  +    + sizeof(Stat4Sample)*(nCol+mxSample)     /* Stat4Accum.aBest[], a[] */
   327    405       + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
   328    406   #endif
   329    407     ;
   330         -  p = sqlite3MallocZero(n);
          408  +  db = sqlite3_context_db_handle(context);
          409  +  p = sqlite3DbMallocZero(db, n);
   331    410     if( p==0 ){
   332    411       sqlite3_result_error_nomem(context);
   333    412       return;
   334    413     }
   335    414   
          415  +  p->db = db;
   336    416     p->nRow = 0;
   337    417     p->nCol = nCol;
   338    418     p->current.anDLt = (tRowcnt*)&p[1];
   339    419     p->current.anEq = &p->current.anDLt[nColUp];
   340    420   
   341    421   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   342    422     {
................................................................................
   363    443       for(i=0; i<nCol; i++){
   364    444         p->aBest[i].iCol = i;
   365    445       }
   366    446     }
   367    447   #endif
   368    448   
   369    449     /* Return a pointer to the allocated object to the caller */
   370         -  sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
          450  +  sqlite3_result_blob(context, p, sizeof(p), stat4Destructor);
   371    451   }
   372    452   static const FuncDef statInitFuncdef = {
   373    453     1+IsStat34,      /* nArg */
   374    454     SQLITE_UTF8,     /* funcFlags */
   375    455     0,               /* pUserData */
   376    456     0,               /* pNext */
   377    457     statInit,        /* xFunc */
................................................................................
   437    517     }
   438    518     return 0;
   439    519   #else
   440    520     return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
   441    521   #endif
   442    522   }
   443    523   
   444         -/*
   445         -** Copy the contents of object (*pFrom) into (*pTo).
   446         -*/
   447         -static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
   448         -  pTo->iRowid = pFrom->iRowid;
   449         -  pTo->isPSample = pFrom->isPSample;
   450         -  pTo->iCol = pFrom->iCol;
   451         -  pTo->iHash = pFrom->iHash;
   452         -  memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
   453         -  memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
   454         -  memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
   455         -}
   456         -
   457    524   /*
   458    525   ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
   459    526   ** remove the least desirable sample from p->a[] to make room.
   460    527   */
   461    528   static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
   462         -  Stat4Sample *pSample;
          529  +  Stat4Sample *pSample = 0;
   463    530     int i;
   464    531   
   465    532     assert( IsStat4 || nEqZero==0 );
   466    533   
   467    534   #ifdef SQLITE_ENABLE_STAT4
   468    535     if( pNew->isPSample==0 ){
   469    536       Stat4Sample *pUpgrade = 0;
................................................................................
   495    562   
   496    563     /* If necessary, remove sample iMin to make room for the new sample. */
   497    564     if( p->nSample>=p->mxSample ){
   498    565       Stat4Sample *pMin = &p->a[p->iMin];
   499    566       tRowcnt *anEq = pMin->anEq;
   500    567       tRowcnt *anLt = pMin->anLt;
   501    568       tRowcnt *anDLt = pMin->anDLt;
          569  +    sampleClear(p->db, pMin);
   502    570       memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
   503    571       pSample = &p->a[p->nSample-1];
          572  +    pSample->nRowid = 0;
   504    573       pSample->anEq = anEq;
   505    574       pSample->anDLt = anDLt;
   506    575       pSample->anLt = anLt;
   507    576       p->nSample = p->mxSample-1;
   508    577     }
   509    578   
   510    579     /* The "rows less-than" for the rowid column must be greater than that
................................................................................
   593    662   #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
   594    663     UNUSED_PARAMETER( p );
   595    664     UNUSED_PARAMETER( iChng );
   596    665   #endif
   597    666   }
   598    667   
   599    668   /*
   600         -** Implementation of the stat_push SQL function:  stat_push(P,R,C)
          669  +** Implementation of the stat_push SQL function:  stat_push(P,C,R)
   601    670   ** Arguments:
   602    671   **
   603    672   **    P     Pointer to the Stat4Accum object created by stat_init()
   604    673   **    C     Index of left-most column to differ from previous row
   605         -**    R     Rowid for the current row
          674  +**    R     Rowid for the current row.  Might be a key record for
          675  +**          WITHOUT ROWID tables.
   606    676   **
   607    677   ** The SQL function always returns NULL.
   608    678   **
   609         -** The R parameter is only used for STAT3 and STAT4.
          679  +** The R parameter is only used for STAT3 and STAT4
   610    680   */
   611    681   static void statPush(
   612    682     sqlite3_context *context,
   613    683     int argc,
   614    684     sqlite3_value **argv
   615    685   ){
   616    686     int i;
................................................................................
   642    712         p->current.anLt[i] += p->current.anEq[i];
   643    713   #endif
   644    714         p->current.anEq[i] = 1;
   645    715       }
   646    716     }
   647    717     p->nRow++;
   648    718   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   649         -  p->current.iRowid = sqlite3_value_int64(argv[2]);
          719  +  if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
          720  +    sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
          721  +  }else{
          722  +    sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
          723  +                                       sqlite3_value_blob(argv[2]));
          724  +  }
   650    725     p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
   651    726   #endif
   652    727   
   653    728   #ifdef SQLITE_ENABLE_STAT4
   654    729     {
   655    730       tRowcnt nLt = p->current.anLt[p->nCol-1];
   656    731   
................................................................................
   766    841   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   767    842     else if( eCall==STAT_GET_ROWID ){
   768    843       if( p->iGet<0 ){
   769    844         samplePushPrevious(p, 0);
   770    845         p->iGet = 0;
   771    846       }
   772    847       if( p->iGet<p->nSample ){
   773         -      sqlite3_result_int64(context, p->a[p->iGet].iRowid);
          848  +      Stat4Sample *pS = p->a + p->iGet;
          849  +      if( pS->nRowid==0 ){
          850  +        sqlite3_result_int64(context, pS->u.iRowid);
          851  +      }else{
          852  +        sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
          853  +                            SQLITE_TRANSIENT);
          854  +      }
   774    855       }
   775    856     }else{
   776    857       tRowcnt *aCnt = 0;
   777    858   
   778    859       assert( p->iGet<p->nSample );
   779    860       switch( eCall ){
   780    861         case STAT_GET_NEQ:  aCnt = p->a[p->iGet].anEq; break;
................................................................................
   903    984     iIdxCur = iTab++;
   904    985     pParse->nTab = MAX(pParse->nTab, iTab);
   905    986     sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
   906    987     sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
   907    988   
   908    989     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   909    990       int nCol;                     /* Number of columns indexed by pIdx */
   910         -    KeyInfo *pKey;                /* KeyInfo structure for pIdx */
   911    991       int *aGotoChng;               /* Array of jump instruction addresses */
   912    992       int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
   913    993       int addrGotoChng0;            /* Address of "Goto addr_chng_0" */
   914    994       int addrNextRow;              /* Address of "next_row:" */
          995  +    const char *zIdxName;         /* Name of the index */
   915    996   
   916    997       if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
   917    998       if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
   918    999       VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
   919         -    nCol = pIdx->nColumn;
         1000  +    nCol = pIdx->nKeyCol;
   920   1001       aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
   921   1002       if( aGotoChng==0 ) continue;
   922         -    pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   923   1003   
   924   1004       /* Populate the register containing the index name. */
   925         -    sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
         1005  +    if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
         1006  +      zIdxName = pTab->zName;
         1007  +    }else{
         1008  +      zIdxName = pIdx->zName;
         1009  +    }
         1010  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
   926   1011   
   927   1012       /*
   928   1013       ** Pseudo-code for loop that calls stat_push():
   929   1014       **
   930   1015       **   Rewind csr
   931   1016       **   if eof(csr) goto end_of_scan;
   932   1017       **   regChng = 0
................................................................................
   961   1046       ** when building a record to insert into the sample column of 
   962   1047       ** the sqlite_stat4 table.  */
   963   1048       pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
   964   1049   
   965   1050       /* Open a read-only cursor on the index being analyzed. */
   966   1051       assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
   967   1052       sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
   968         -    sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF); 
         1053  +    sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
   969   1054       VdbeComment((v, "%s", pIdx->zName));
   970   1055   
   971   1056       /* Invoke the stat_init() function. The arguments are:
   972   1057       ** 
   973   1058       **    (1) the number of columns in the index including the rowid,
   974   1059       **    (2) the number of rows in the index,
   975   1060       **
................................................................................
  1035   1120       **   regRowid = idx(rowid)            // STAT34 only
  1036   1121       **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
  1037   1122       **   Next csr
  1038   1123       **   if !eof(csr) goto next_row;
  1039   1124       */
  1040   1125       sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
  1041   1126   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1042         -    sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
  1043   1127       assert( regRowid==(regStat4+2) );
         1128  +    if( HasRowid(pTab) ){
         1129  +      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
         1130  +    }else{
         1131  +      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
         1132  +      int j, k, regKey;
         1133  +      regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
         1134  +      for(j=0; j<pPk->nKeyCol; j++){
         1135  +        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
         1136  +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
         1137  +        VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
         1138  +      }
         1139  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
         1140  +      sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
         1141  +    }
  1044   1142   #endif
  1045   1143       assert( regChng==(regStat4+1) );
  1046   1144       sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
  1047   1145       sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
  1048   1146       sqlite3VdbeChangeP5(v, 2+IsStat34);
  1049   1147       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
  1050   1148   
................................................................................
  1062   1160         int regLt = regStat1+1;
  1063   1161         int regDLt = regStat1+2;
  1064   1162         int regSample = regStat1+3;
  1065   1163         int regCol = regStat1+4;
  1066   1164         int regSampleRowid = regCol + nCol;
  1067   1165         int addrNext;
  1068   1166         int addrIsNull;
         1167  +      u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
  1069   1168   
  1070   1169         pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
  1071   1170   
  1072   1171         addrNext = sqlite3VdbeCurrentAddr(v);
  1073   1172         callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
  1074   1173         addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
  1075   1174         callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
  1076   1175         callStatGet(v, regStat4, STAT_GET_NLT, regLt);
  1077   1176         callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
  1078         -      sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, addrNext, regSampleRowid);
         1177  +      sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
  1079   1178   #ifdef SQLITE_ENABLE_STAT3
  1080   1179         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, 
  1081   1180                                         pIdx->aiColumn[0], regSample);
  1082   1181   #else
  1083   1182         for(i=0; i<nCol; i++){
  1084         -        int iCol = pIdx->aiColumn[i];
         1183  +        i16 iCol = pIdx->aiColumn[i];
  1085   1184           sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
  1086   1185         }
  1087   1186         sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
  1088   1187   #endif
  1089   1188         sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
  1090   1189         sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
  1091   1190         sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
................................................................................
  1333   1432       pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
  1334   1433     }else{
  1335   1434       pIndex = 0;
  1336   1435     }
  1337   1436     z = argv[2];
  1338   1437   
  1339   1438     if( pIndex ){
  1340         -    decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst, pIndex);
         1439  +    decodeIntArray((char*)z, pIndex->nKeyCol+1, pIndex->aiRowEst, pIndex);
  1341   1440       if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
  1342   1441     }else{
  1343   1442       Index fakeIdx;
  1344   1443       fakeIdx.szIdxRow = pTable->szTabRow;
  1345   1444       decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
  1346   1445       pTable->szTabRow = fakeIdx.szIdxRow;
  1347   1446     }
................................................................................
  1379   1478   ** stored in pIdx->aSample[]. 
  1380   1479   */
  1381   1480   static void initAvgEq(Index *pIdx){
  1382   1481     if( pIdx ){
  1383   1482       IndexSample *aSample = pIdx->aSample;
  1384   1483       IndexSample *pFinal = &aSample[pIdx->nSample-1];
  1385   1484       int iCol;
  1386         -    for(iCol=0; iCol<pIdx->nColumn; iCol++){
         1485  +    for(iCol=0; iCol<pIdx->nKeyCol; iCol++){
  1387   1486         int i;                    /* Used to iterate through samples */
  1388   1487         tRowcnt sumEq = 0;        /* Sum of the nEq values */
  1389   1488         tRowcnt nSum = 0;         /* Number of terms contributing to sumEq */
  1390   1489         tRowcnt avgEq = 0;
  1391   1490         tRowcnt nDLt = pFinal->anDLt[iCol];
  1392   1491   
  1393   1492         /* Set nSum to the number of distinct (iCol+1) field prefixes that
................................................................................
  1406   1505         }
  1407   1506         if( avgEq==0 ) avgEq = 1;
  1408   1507         pIdx->aAvgEq[iCol] = avgEq;
  1409   1508         if( pIdx->nSampleCol==1 ) break;
  1410   1509       }
  1411   1510     }
  1412   1511   }
         1512  +
         1513  +/*
         1514  +** Look up an index by name.  Or, if the name of a WITHOUT ROWID table
         1515  +** is supplied instead, find the PRIMARY KEY index for that table.
         1516  +*/
         1517  +static Index *findIndexOrPrimaryKey(
         1518  +  sqlite3 *db,
         1519  +  const char *zName,
         1520  +  const char *zDb
         1521  +){
         1522  +  Index *pIdx = sqlite3FindIndex(db, zName, zDb);
         1523  +  if( pIdx==0 ){
         1524  +    Table *pTab = sqlite3FindTable(db, zName, zDb);
         1525  +    if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
         1526  +  }
         1527  +  return pIdx;
         1528  +}
  1413   1529   
  1414   1530   /*
  1415   1531   ** Load the content from either the sqlite_stat4 or sqlite_stat3 table 
  1416   1532   ** into the relevant Index.aSample[] arrays.
  1417   1533   **
  1418   1534   ** Arguments zSql1 and zSql2 must point to SQL statements that return
  1419   1535   ** data equivalent to the following (statements are different for stat3,
................................................................................
  1456   1572       int nByte;      /* Bytes of space required */
  1457   1573       int i;          /* Bytes of space required */
  1458   1574       tRowcnt *pSpace;
  1459   1575   
  1460   1576       zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1461   1577       if( zIndex==0 ) continue;
  1462   1578       nSample = sqlite3_column_int(pStmt, 1);
  1463         -    pIdx = sqlite3FindIndex(db, zIndex, zDb);
         1579  +    pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
  1464   1580       assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
  1465   1581       /* Index.nSample is non-zero at this point if data has already been
  1466   1582       ** loaded from the stat4 table. In this case ignore stat3 data.  */
  1467   1583       if( pIdx==0 || pIdx->nSample ) continue;
  1468   1584       if( bStat3==0 ){
  1469         -      nIdxCol = pIdx->nColumn+1;
  1470         -      nAvgCol = pIdx->nColumn;
         1585  +      nIdxCol = pIdx->nKeyCol+1;
         1586  +      nAvgCol = pIdx->nKeyCol;
  1471   1587       }
  1472   1588       pIdx->nSampleCol = nIdxCol;
  1473   1589       nByte = sizeof(IndexSample) * nSample;
  1474   1590       nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
  1475   1591       nByte += nAvgCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
  1476   1592   
  1477   1593       pIdx->aSample = sqlite3DbMallocZero(db, nByte);
................................................................................
  1502   1618     while( sqlite3_step(pStmt)==SQLITE_ROW ){
  1503   1619       char *zIndex;                 /* Index name */
  1504   1620       Index *pIdx;                  /* Pointer to the index object */
  1505   1621       int nCol = 1;                 /* Number of columns in index */
  1506   1622   
  1507   1623       zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1508   1624       if( zIndex==0 ) continue;
  1509         -    pIdx = sqlite3FindIndex(db, zIndex, zDb);
         1625  +    pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
  1510   1626       if( pIdx==0 ) continue;
  1511   1627       /* This next condition is true if data has already been loaded from 
  1512   1628       ** the sqlite_stat4 table. In this case ignore stat3 data.  */
  1513   1629       nCol = pIdx->nSampleCol;
  1514   1630       if( bStat3 && nCol>1 ) continue;
  1515   1631       if( pIdx!=pPrevIdx ){
  1516   1632         initAvgEq(pPrevIdx);

Changes to src/btree.c.

   680    680     const void *pKey,   /* Packed key if the btree is an index */
   681    681     i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
   682    682     int bias,           /* Bias search to the high end */
   683    683     int *pRes           /* Write search results here */
   684    684   ){
   685    685     int rc;                    /* Status code */
   686    686     UnpackedRecord *pIdxKey;   /* Unpacked index key */
   687         -  char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
          687  +  char aSpace[200];          /* Temp space for pIdxKey - to avoid a malloc */
   688    688     char *pFree = 0;
   689    689   
   690    690     if( pKey ){
   691    691       assert( nKey==(i64)(int)nKey );
   692    692       pIdxKey = sqlite3VdbeAllocUnpackedRecord(
   693    693           pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
   694    694       );

Changes to src/build.c.

   378    378   /*
   379    379   ** Reclaim the memory used by an index
   380    380   */
   381    381   static void freeIndex(sqlite3 *db, Index *p){
   382    382   #ifndef SQLITE_OMIT_ANALYZE
   383    383     sqlite3DeleteIndexSamples(db, p);
   384    384   #endif
          385  +  if( db==0 || db->pnBytesFreed==0 ) sqlite3KeyInfoUnref(p->pKeyInfo);
   385    386     sqlite3ExprDelete(db, p->pPartIdxWhere);
   386    387     sqlite3DbFree(db, p->zColAff);
          388  +  if( p->isResized ) sqlite3DbFree(db, p->azColl);
   387    389     sqlite3DbFree(db, p);
   388    390   }
   389    391   
   390    392   /*
   391    393   ** For the index called zIdxName which is found in the database iDb,
   392    394   ** unlike that index from its Table then remove the index from
   393    395   ** the index hash table and free all memory structures associated
................................................................................
   637    639   /*
   638    640   ** Open the sqlite_master table stored in database number iDb for
   639    641   ** writing. The table is opened using cursor 0.
   640    642   */
   641    643   void sqlite3OpenMasterTable(Parse *p, int iDb){
   642    644     Vdbe *v = sqlite3GetVdbe(p);
   643    645     sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
   644         -  sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
   645         -  sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
          646  +  sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
   646    647     if( p->nTab==0 ){
   647    648       p->nTab = 1;
   648    649     }
   649    650   }
   650    651   
   651    652   /*
   652    653   ** Parameter zName points to a nul-terminated buffer containing the name
................................................................................
   742    743             && (pParse->db->flags & SQLITE_WriteSchema)==0
   743    744             && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
   744    745       sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
   745    746       return SQLITE_ERROR;
   746    747     }
   747    748     return SQLITE_OK;
   748    749   }
          750  +
          751  +/*
          752  +** Return the PRIMARY KEY index of a table
          753  +*/
          754  +Index *sqlite3PrimaryKeyIndex(Table *pTab){
          755  +  Index *p;
          756  +  for(p=pTab->pIndex; p && p->autoIndex!=2; p=p->pNext){}
          757  +  return p;
          758  +}
          759  +
          760  +/*
          761  +** Return the column of index pIdx that corresponds to table
          762  +** column iCol.  Return -1 if not found.
          763  +*/
          764  +i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
          765  +  int i;
          766  +  for(i=0; i<pIdx->nColumn; i++){
          767  +    if( iCol==pIdx->aiColumn[i] ) return i;
          768  +  }
          769  +  return -1;
          770  +}
   749    771   
   750    772   /*
   751    773   ** Begin constructing a new table representation in memory.  This is
   752    774   ** the first of several action routines that get called in response
   753    775   ** to a CREATE TABLE statement.  In particular, this routine is called
   754    776   ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
   755    777   ** flag is true if the table should be stored in the auxiliary database
................................................................................
   942    964       */
   943    965   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
   944    966       if( isView || isVirtual ){
   945    967         sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
   946    968       }else
   947    969   #endif
   948    970       {
   949         -      sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
          971  +      pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
   950    972       }
   951    973       sqlite3OpenMasterTable(pParse, iDb);
   952    974       sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
   953    975       sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
   954    976       sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
   955    977       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   956    978       sqlite3VdbeAddOp0(v, OP_Close);
................................................................................
  1210   1232     int onError,      /* What to do with a uniqueness conflict */
  1211   1233     int autoInc,      /* True if the AUTOINCREMENT keyword is present */
  1212   1234     int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
  1213   1235   ){
  1214   1236     Table *pTab = pParse->pNewTable;
  1215   1237     char *zType = 0;
  1216   1238     int iCol = -1, i;
         1239  +  int nTerm;
  1217   1240     if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
  1218   1241     if( pTab->tabFlags & TF_HasPrimaryKey ){
  1219   1242       sqlite3ErrorMsg(pParse, 
  1220   1243         "table \"%s\" has more than one primary key", pTab->zName);
  1221   1244       goto primary_key_exit;
  1222   1245     }
  1223   1246     pTab->tabFlags |= TF_HasPrimaryKey;
  1224   1247     if( pList==0 ){
  1225   1248       iCol = pTab->nCol - 1;
  1226   1249       pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
         1250  +    zType = pTab->aCol[iCol].zType;
         1251  +    nTerm = 1;
  1227   1252     }else{
  1228         -    for(i=0; i<pList->nExpr; i++){
         1253  +    nTerm = pList->nExpr;
         1254  +    for(i=0; i<nTerm; i++){
  1229   1255         for(iCol=0; iCol<pTab->nCol; iCol++){
  1230   1256           if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
         1257  +          pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
         1258  +          zType = pTab->aCol[iCol].zType;
  1231   1259             break;
  1232   1260           }
  1233   1261         }
  1234         -      if( iCol<pTab->nCol ){
  1235         -        pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
  1236         -      }
  1237   1262       }
  1238         -    if( pList->nExpr>1 ) iCol = -1;
  1239   1263     }
  1240         -  if( iCol>=0 && iCol<pTab->nCol ){
  1241         -    zType = pTab->aCol[iCol].zType;
  1242         -  }
  1243         -  if( zType && sqlite3StrICmp(zType, "INTEGER")==0
  1244         -        && sortOrder==SQLITE_SO_ASC ){
         1264  +  if( nTerm==1
         1265  +   && zType && sqlite3StrICmp(zType, "INTEGER")==0
         1266  +   && sortOrder==SQLITE_SO_ASC
         1267  +  ){
  1245   1268       pTab->iPKey = iCol;
  1246   1269       pTab->keyConf = (u8)onError;
  1247   1270       assert( autoInc==0 || autoInc==1 );
  1248   1271       pTab->tabFlags |= autoInc*TF_Autoincrement;
         1272  +    if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
  1249   1273     }else if( autoInc ){
  1250   1274   #ifndef SQLITE_OMIT_AUTOINCREMENT
  1251   1275       sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
  1252   1276          "INTEGER PRIMARY KEY");
  1253   1277   #endif
  1254   1278     }else{
         1279  +    Vdbe *v = pParse->pVdbe;
  1255   1280       Index *p;
         1281  +    if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
  1256   1282       p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
  1257   1283                              0, sortOrder, 0);
  1258   1284       if( p ){
  1259   1285         p->autoIndex = 2;
         1286  +      if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
  1260   1287       }
  1261   1288       pList = 0;
  1262   1289     }
  1263   1290   
  1264   1291   primary_key_exit:
  1265   1292     sqlite3ExprListDelete(pParse->db, pList);
  1266   1293     return;
................................................................................
  1309   1336       p->aCol[i].zColl = zColl;
  1310   1337     
  1311   1338       /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
  1312   1339       ** then an index may have been created on this column before the
  1313   1340       ** collation type was added. Correct this if it is the case.
  1314   1341       */
  1315   1342       for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
  1316         -      assert( pIdx->nColumn==1 );
         1343  +      assert( pIdx->nKeyCol==1 );
  1317   1344         if( pIdx->aiColumn[0]==i ){
  1318   1345           pIdx->azColl[0] = p->aCol[i].zColl;
  1319   1346         }
  1320   1347       }
  1321   1348     }else{
  1322   1349       sqlite3DbFree(db, zColl);
  1323   1350     }
................................................................................
  1500   1527       memcpy(&zStmt[k], zType, len);
  1501   1528       k += len;
  1502   1529       assert( k<=n );
  1503   1530     }
  1504   1531     sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
  1505   1532     return zStmt;
  1506   1533   }
         1534  +
         1535  +/*
         1536  +** Resize an Index object to hold N columns total.  Return SQLITE_OK
         1537  +** on success and SQLITE_NOMEM on an OOM error.
         1538  +*/
         1539  +static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
         1540  +  char *zExtra;
         1541  +  int nByte;
         1542  +  if( pIdx->nColumn>=N ) return SQLITE_OK;
         1543  +  assert( pIdx->isResized==0 );
         1544  +  nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
         1545  +  zExtra = sqlite3DbMallocZero(db, nByte);
         1546  +  if( zExtra==0 ) return SQLITE_NOMEM;
         1547  +  memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
         1548  +  pIdx->azColl = (char**)zExtra;
         1549  +  zExtra += sizeof(char*)*N;
         1550  +  memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
         1551  +  pIdx->aiColumn = (i16*)zExtra;
         1552  +  zExtra += sizeof(i16)*N;
         1553  +  memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
         1554  +  pIdx->aSortOrder = (u8*)zExtra;
         1555  +  pIdx->nColumn = N;
         1556  +  pIdx->isResized = 1;
         1557  +  return SQLITE_OK;
         1558  +}
  1507   1559   
  1508   1560   /*
  1509   1561   ** Estimate the total row width for a table.
  1510   1562   */
  1511   1563   static void estimateTableWidth(Table *pTab){
  1512   1564     unsigned wTable = 0;
  1513   1565     const Column *pTabCol;
................................................................................
  1519   1571     pTab->szTabRow = sqlite3LogEst(wTable*4);
  1520   1572   }
  1521   1573   
  1522   1574   /*
  1523   1575   ** Estimate the average size of a row for an index.
  1524   1576   */
  1525   1577   static void estimateIndexWidth(Index *pIdx){
  1526         -  unsigned wIndex = 1;
         1578  +  unsigned wIndex = 0;
  1527   1579     int i;
  1528   1580     const Column *aCol = pIdx->pTable->aCol;
  1529   1581     for(i=0; i<pIdx->nColumn; i++){
  1530         -    assert( pIdx->aiColumn[i]>=0 && pIdx->aiColumn[i]<pIdx->pTable->nCol );
  1531         -    wIndex += aCol[pIdx->aiColumn[i]].szEst;
         1582  +    i16 x = pIdx->aiColumn[i];
         1583  +    assert( x<pIdx->pTable->nCol );
         1584  +    wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
  1532   1585     }
  1533   1586     pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
  1534   1587   }
         1588  +
         1589  +/* Return true if value x is found any of the first nCol entries of aiCol[]
         1590  +*/
         1591  +static int hasColumn(const i16 *aiCol, int nCol, int x){
         1592  +  while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
         1593  +  return 0;
         1594  +}
         1595  +
         1596  +/*
         1597  +** This routine runs at the end of parsing a CREATE TABLE statement that
         1598  +** has a WITHOUT ROWID clause.  The job of this routine is to convert both
         1599  +** internal schema data structures and the generated VDBE code so that they
         1600  +** are appropriate for a WITHOUT ROWID table instead of a rowid table.
         1601  +** Changes include:
         1602  +**
         1603  +**     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
         1604  +**          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
         1605  +**          data storage is a covering index btree.
         1606  +**     (2)  Bypass the creation of the sqlite_master table entry
         1607  +**          for the PRIMARY KEY as the the primary key index is now
         1608  +**          identified by the sqlite_master table entry of the table itself.
         1609  +**     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
         1610  +**          schema to the rootpage from the main table.
         1611  +**     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
         1612  +**     (5)  Add all table columns to the PRIMARY KEY Index object
         1613  +**          so that the PRIMARY KEY is a covering index.  The surplus
         1614  +**          columns are part of KeyInfo.nXField and are not used for
         1615  +**          sorting or lookup or uniqueness checks.
         1616  +**     (6)  Replace the rowid tail on all automatically generated UNIQUE
         1617  +**          indices with the PRIMARY KEY columns.
         1618  +*/
         1619  +static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
         1620  +  Index *pIdx;
         1621  +  Index *pPk;
         1622  +  int nPk;
         1623  +  int i, j;
         1624  +  sqlite3 *db = pParse->db;
         1625  +  Vdbe *v = pParse->pVdbe;
         1626  +
         1627  +  /* Convert the OP_CreateTable opcode that would normally create the
         1628  +  ** root-page for the table into a OP_CreateIndex opcode.  The index
         1629  +  ** created will become the PRIMARY KEY index.
         1630  +  */
         1631  +  if( pParse->addrCrTab ){
         1632  +    assert( v );
         1633  +    sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
         1634  +  }
         1635  +
         1636  +  /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
         1637  +  ** table entry.
         1638  +  */
         1639  +  if( pParse->addrSkipPK ){
         1640  +    assert( v );
         1641  +    sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto;
         1642  +  }
         1643  +
         1644  +  /* Locate the PRIMARY KEY index.  Or, if this table was originally
         1645  +  ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. 
         1646  +  */
         1647  +  if( pTab->iPKey>=0 ){
         1648  +    ExprList *pList;
         1649  +    pList = sqlite3ExprListAppend(pParse, 0, 0);
         1650  +    if( pList==0 ) return;
         1651  +    pList->a[0].zName = sqlite3DbStrDup(pParse->db,
         1652  +                                        pTab->aCol[pTab->iPKey].zName);
         1653  +    pList->a[0].sortOrder = pParse->iPkSortOrder;
         1654  +    assert( pParse->pNewTable==pTab );
         1655  +    pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
         1656  +    if( pPk==0 ) return;
         1657  +    pPk->autoIndex = 2;
         1658  +    pTab->iPKey = -1;
         1659  +  }else{
         1660  +    pPk = sqlite3PrimaryKeyIndex(pTab);
         1661  +  }
         1662  +  pPk->isCovering = 1;
         1663  +  assert( pPk!=0 );
         1664  +  nPk = pPk->nKeyCol;
         1665  +
         1666  +  /* Make sure every column of the PRIMARY KEY is NOT NULL */
         1667  +  for(i=0; i<nPk; i++){
         1668  +    pTab->aCol[pPk->aiColumn[i]].notNull = 1;
         1669  +  }
         1670  +  pPk->uniqNotNull = 1;
         1671  +
         1672  +  /* The root page of the PRIMARY KEY is the table root page */
         1673  +  pPk->tnum = pTab->tnum;
         1674  +
         1675  +  /* Update the in-memory representation of all UNIQUE indices by converting
         1676  +  ** the final rowid column into one or more columns of the PRIMARY KEY.
         1677  +  */
         1678  +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
         1679  +    int n;
         1680  +    if( pIdx->autoIndex==2 ) continue;
         1681  +    for(i=n=0; i<nPk; i++){
         1682  +      if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
         1683  +    }
         1684  +    if( n==0 ){
         1685  +      /* This index is a superset of the primary key */
         1686  +      pIdx->nColumn = pIdx->nKeyCol;
         1687  +      continue;
         1688  +    }
         1689  +    if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
         1690  +    for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
         1691  +      if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
         1692  +        pIdx->aiColumn[j] = pPk->aiColumn[i];
         1693  +        pIdx->azColl[j] = pPk->azColl[i];
         1694  +        j++;
         1695  +      }
         1696  +    }
         1697  +    assert( pIdx->nColumn>=pIdx->nKeyCol+n );
         1698  +    assert( pIdx->nColumn>=j );
         1699  +  }
         1700  +
         1701  +  /* Add all table columns to the PRIMARY KEY index
         1702  +  */
         1703  +  if( nPk<pTab->nCol ){
         1704  +    if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
         1705  +    for(i=0, j=nPk; i<pTab->nCol; i++){
         1706  +      if( !hasColumn(pPk->aiColumn, j, i) ){
         1707  +        assert( j<pPk->nColumn );
         1708  +        pPk->aiColumn[j] = i;
         1709  +        pPk->azColl[j] = "BINARY";
         1710  +        j++;
         1711  +      }
         1712  +    }
         1713  +    assert( pPk->nColumn==j );
         1714  +    assert( pTab->nCol==j );
         1715  +  }else{
         1716  +    pPk->nColumn = pTab->nCol;
         1717  +  }
         1718  +}
  1535   1719   
  1536   1720   /*
  1537   1721   ** This routine is called to report the final ")" that terminates
  1538   1722   ** a CREATE TABLE statement.
  1539   1723   **
  1540   1724   ** The table structure that other action routines have been building
  1541   1725   ** is added to the internal hash tables, assuming no errors have
................................................................................
  1552   1736   ** was called to create a table generated from a 
  1553   1737   ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
  1554   1738   ** the new table will match the result set of the SELECT.
  1555   1739   */
  1556   1740   void sqlite3EndTable(
  1557   1741     Parse *pParse,          /* Parse context */
  1558   1742     Token *pCons,           /* The ',' token after the last column defn. */
  1559         -  Token *pEnd,            /* The final ')' token in the CREATE TABLE */
         1743  +  Token *pEnd,            /* The ')' before options in the CREATE TABLE */
         1744  +  u8 tabOpts,             /* Extra table options. Usually 0. */
  1560   1745     Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
  1561   1746   ){
  1562   1747     Table *p;                 /* The new table */
  1563   1748     sqlite3 *db = pParse->db; /* The database connection */
  1564   1749     int iDb;                  /* Database in which the table lives */
  1565   1750     Index *pIdx;              /* An implied index of the table */
  1566   1751   
................................................................................
  1567   1752     if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
  1568   1753       return;
  1569   1754     }
  1570   1755     p = pParse->pNewTable;
  1571   1756     if( p==0 ) return;
  1572   1757   
  1573   1758     assert( !db->init.busy || !pSelect );
         1759  +
         1760  +  /* If the db->init.busy is 1 it means we are reading the SQL off the
         1761  +  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
         1762  +  ** So do not write to the disk again.  Extract the root page number
         1763  +  ** for the table from the db->init.newTnum field.  (The page number
         1764  +  ** should have been put there by the sqliteOpenCb routine.)
         1765  +  */
         1766  +  if( db->init.busy ){
         1767  +    p->tnum = db->init.newTnum;
         1768  +  }
         1769  +
         1770  +  /* Special processing for WITHOUT ROWID Tables */
         1771  +  if( tabOpts & TF_WithoutRowid ){
         1772  +    if( (p->tabFlags & TF_Autoincrement) ){
         1773  +      sqlite3ErrorMsg(pParse,
         1774  +          "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
         1775  +      return;
         1776  +    }
         1777  +    if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
         1778  +      sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
         1779  +    }else{
         1780  +      p->tabFlags |= TF_WithoutRowid;
         1781  +      convertToWithoutRowidTable(pParse, p);
         1782  +    }
         1783  +  }
  1574   1784   
  1575   1785     iDb = sqlite3SchemaToIndex(db, p->pSchema);
  1576   1786   
  1577   1787   #ifndef SQLITE_OMIT_CHECK
  1578   1788     /* Resolve names in all CHECK constraint expressions.
  1579   1789     */
  1580   1790     if( p->pCheck ){
................................................................................
  1584   1794   
  1585   1795     /* Estimate the average row size for the table and for all implied indices */
  1586   1796     estimateTableWidth(p);
  1587   1797     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
  1588   1798       estimateIndexWidth(pIdx);
  1589   1799     }
  1590   1800   
  1591         -  /* If the db->init.busy is 1 it means we are reading the SQL off the
  1592         -  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
  1593         -  ** So do not write to the disk again.  Extract the root page number
  1594         -  ** for the table from the db->init.newTnum field.  (The page number
  1595         -  ** should have been put there by the sqliteOpenCb routine.)
  1596         -  */
  1597         -  if( db->init.busy ){
  1598         -    p->tnum = db->init.newTnum;
  1599         -  }
  1600         -
  1601   1801     /* If not initializing, then create a record for the new table
  1602   1802     ** in the SQLITE_MASTER table of the database.
  1603   1803     **
  1604   1804     ** If this is a TEMPORARY table, write the entry into the auxiliary
  1605   1805     ** file instead of into the main database file.
  1606   1806     */
  1607   1807     if( !db->init.busy ){
................................................................................
  1667   1867         }
  1668   1868       }
  1669   1869   
  1670   1870       /* Compute the complete text of the CREATE statement */
  1671   1871       if( pSelect ){
  1672   1872         zStmt = createTableStmt(db, p);
  1673   1873       }else{
  1674         -      n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
         1874  +      Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
         1875  +      n = (int)(pEnd2->z - pParse->sNameToken.z);
         1876  +      if( pEnd2->z[0]!=';' ) n += pEnd2->n;
  1675   1877         zStmt = sqlite3MPrintf(db, 
  1676   1878             "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
  1677   1879         );
  1678   1880       }
  1679   1881   
  1680   1882       /* A slot for the record has already been allocated in the 
  1681   1883       ** SQLITE_MASTER table.  We just need to update that slot with all
................................................................................
  1815   2017     n = (int)(sEnd.z - pBegin->z);
  1816   2018     z = pBegin->z;
  1817   2019     while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
  1818   2020     sEnd.z = &z[n-1];
  1819   2021     sEnd.n = 1;
  1820   2022   
  1821   2023     /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
  1822         -  sqlite3EndTable(pParse, 0, &sEnd, 0);
         2024  +  sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
  1823   2025     return;
  1824   2026   }
  1825   2027   #endif /* SQLITE_OMIT_VIEW */
  1826   2028   
  1827   2029   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
  1828   2030   /*
  1829   2031   ** The Table structure pTable is really a VIEW.  Fill in the names of
................................................................................
  2270   2472   }
  2271   2473   
  2272   2474   /*
  2273   2475   ** This routine is called to create a new foreign key on the table
  2274   2476   ** currently under construction.  pFromCol determines which columns
  2275   2477   ** in the current table point to the foreign key.  If pFromCol==0 then
  2276   2478   ** connect the key to the last column inserted.  pTo is the name of
  2277         -** the table referred to.  pToCol is a list of tables in the other
  2278         -** pTo table that the foreign key points to.  flags contains all
         2479  +** the table referred to (a.k.a the "parent" table).  pToCol is a list
         2480  +** of tables in the parent pTo table.  flags contains all
  2279   2481   ** information about the conflict resolution algorithms specified
  2280   2482   ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
  2281   2483   **
  2282   2484   ** An FKey structure is created and added to the table currently
  2283   2485   ** under construction in the pParse->pNewTable field.
  2284   2486   **
  2285   2487   ** The foreign key is set for IMMEDIATE processing.  A subsequent call
................................................................................
  2454   2656   
  2455   2657     v = sqlite3GetVdbe(pParse);
  2456   2658     if( v==0 ) return;
  2457   2659     if( memRootPage>=0 ){
  2458   2660       tnum = memRootPage;
  2459   2661     }else{
  2460   2662       tnum = pIndex->tnum;
  2461         -    sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
  2462   2663     }
  2463         -  pKey = sqlite3IndexKeyinfo(pParse, pIndex);
  2464         -  sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2465         -                    (char *)pKey, P4_KEYINFO_HANDOFF);
  2466         -  sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
         2664  +  pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
  2467   2665   
  2468   2666     /* Open the sorter cursor if we are to use one. */
  2469   2667     iSorter = pParse->nTab++;
  2470         -  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
         2668  +  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)
         2669  +                    sqlite3KeyInfoRef(pKey), P4_KEYINFO);
  2471   2670   
  2472   2671     /* Open the table. Loop through all rows of the table, inserting index
  2473   2672     ** records into the sorter. */
  2474   2673     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2475   2674     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
  2476   2675     regRecord = sqlite3GetTempReg(pParse);
  2477   2676   
  2478         -  sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1, &iPartIdxLabel);
         2677  +  sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 0, &iPartIdxLabel);
  2479   2678     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
  2480   2679     sqlite3VdbeResolveLabel(v, iPartIdxLabel);
  2481   2680     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
  2482   2681     sqlite3VdbeJumpHere(v, addr1);
         2682  +  if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
         2683  +  sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
         2684  +                    (char *)pKey, P4_KEYINFO);
         2685  +  sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
         2686  +
  2483   2687     addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
  2484         -  if( pIndex->onError!=OE_None ){
         2688  +  assert( pKey!=0 || db->mallocFailed || pParse->nErr );
         2689  +  if( pIndex->onError!=OE_None && pKey!=0 ){
  2485   2690       int j2 = sqlite3VdbeCurrentAddr(v) + 3;
  2486   2691       sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
  2487   2692       addr2 = sqlite3VdbeCurrentAddr(v);
  2488         -    sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
  2489         -    sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_UNIQUE,
  2490         -        OE_Abort, "indexed columns are not unique", P4_STATIC
  2491         -    );
         2693  +    sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
         2694  +                         pKey->nField - pIndex->nKeyCol);
         2695  +    sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
  2492   2696     }else{
  2493   2697       addr2 = sqlite3VdbeCurrentAddr(v);
  2494   2698     }
  2495   2699     sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
  2496   2700     sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
  2497   2701     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  2498   2702     sqlite3ReleaseTempReg(pParse, regRecord);
................................................................................
  2499   2703     sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
  2500   2704     sqlite3VdbeJumpHere(v, addr1);
  2501   2705   
  2502   2706     sqlite3VdbeAddOp1(v, OP_Close, iTab);
  2503   2707     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
  2504   2708     sqlite3VdbeAddOp1(v, OP_Close, iSorter);
  2505   2709   }
         2710  +
         2711  +/*
         2712  +** Allocate heap space to hold an Index object with nCol columns.
         2713  +**
         2714  +** Increase the allocation size to provide an extra nExtra bytes
         2715  +** of 8-byte aligned space after the Index object and return a
         2716  +** pointer to this extra space in *ppExtra.
         2717  +*/
         2718  +Index *sqlite3AllocateIndexObject(
         2719  +  sqlite3 *db,         /* Database connection */
         2720  +  i16 nCol,            /* Total number of columns in the index */
         2721  +  int nExtra,          /* Number of bytes of extra space to alloc */
         2722  +  char **ppExtra       /* Pointer to the "extra" space */
         2723  +){
         2724  +  Index *p;            /* Allocated index object */
         2725  +  int nByte;           /* Bytes of space for Index object + arrays */
         2726  +
         2727  +  nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
         2728  +          ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
         2729  +          ROUND8(sizeof(tRowcnt)*(nCol+1) +    /* Index.aiRowEst   */
         2730  +                 sizeof(i16)*nCol +            /* Index.aiColumn   */
         2731  +                 sizeof(u8)*nCol);             /* Index.aSortOrder */
         2732  +  p = sqlite3DbMallocZero(db, nByte + nExtra);
         2733  +  if( p ){
         2734  +    char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
         2735  +    p->azColl = (char**)pExtra;      pExtra += ROUND8(sizeof(char*)*nCol);
         2736  +    p->aiRowEst = (tRowcnt*)pExtra;  pExtra += sizeof(tRowcnt)*(nCol+1);
         2737  +    p->aiColumn = (i16*)pExtra;      pExtra += sizeof(i16)*nCol;
         2738  +    p->aSortOrder = (u8*)pExtra;
         2739  +    p->nColumn = nCol;
         2740  +    p->nKeyCol = nCol - 1;
         2741  +    *ppExtra = ((char*)p) + nByte;
         2742  +  }
         2743  +  return p;
         2744  +}
  2506   2745   
  2507   2746   /*
  2508   2747   ** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
  2509   2748   ** and pTblList is the name of the table that is to be indexed.  Both will 
  2510   2749   ** be NULL for a primary key or an index that is created to satisfy a
  2511   2750   ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
  2512   2751   ** as the table to be indexed.  pParse->pNewTable is a table that is
................................................................................
  2534   2773   ){
  2535   2774     Index *pRet = 0;     /* Pointer to return */
  2536   2775     Table *pTab = 0;     /* Table to be indexed */
  2537   2776     Index *pIndex = 0;   /* The index to be created */
  2538   2777     char *zName = 0;     /* Name of the index */
  2539   2778     int nName;           /* Number of characters in zName */
  2540   2779     int i, j;
  2541         -  Token nullId;        /* Fake token for an empty ID list */
  2542   2780     DbFixer sFix;        /* For assigning database names to pTable */
  2543   2781     int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
  2544   2782     sqlite3 *db = pParse->db;
  2545   2783     Db *pDb;             /* The specific table containing the indexed database */
  2546   2784     int iDb;             /* Index of the database that is being written */
  2547   2785     Token *pName = 0;    /* Unqualified name of the index to create */
  2548   2786     struct ExprList_item *pListItem; /* For looping over pList */
  2549   2787     const Column *pTabCol;           /* A column in the table */
  2550         -  int nCol;                        /* Number of columns */
  2551   2788     int nExtra = 0;                  /* Space allocated for zExtra[] */
         2789  +  int nExtraCol;                   /* Number of extra columns needed */
  2552   2790     char *zExtra;                    /* Extra space after the Index object */
         2791  +  Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
  2553   2792   
  2554   2793     assert( pParse->nErr==0 );      /* Never called with prior errors */
  2555   2794     if( db->mallocFailed || IN_DECLARE_VTAB ){
  2556   2795       goto exit_create_index;
  2557   2796     }
  2558   2797     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  2559   2798       goto exit_create_index;
................................................................................
  2597   2836       if( pTab==0 ) goto exit_create_index;
  2598   2837       if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
  2599   2838         sqlite3ErrorMsg(pParse, 
  2600   2839              "cannot create a TEMP index on non-TEMP table \"%s\"",
  2601   2840              pTab->zName);
  2602   2841         goto exit_create_index;
  2603   2842       }
         2843  +    if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
  2604   2844     }else{
  2605   2845       assert( pName==0 );
  2606   2846       assert( pStart==0 );
  2607   2847       pTab = pParse->pNewTable;
  2608   2848       if( !pTab ) goto exit_create_index;
  2609   2849       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  2610   2850     }
................................................................................
  2692   2932   #endif
  2693   2933   
  2694   2934     /* If pList==0, it means this routine was called to make a primary
  2695   2935     ** key out of the last column added to the table under construction.
  2696   2936     ** So create a fake list to simulate this.
  2697   2937     */
  2698   2938     if( pList==0 ){
  2699         -    nullId.z = pTab->aCol[pTab->nCol-1].zName;
  2700         -    nullId.n = sqlite3Strlen30((char*)nullId.z);
  2701   2939       pList = sqlite3ExprListAppend(pParse, 0, 0);
  2702   2940       if( pList==0 ) goto exit_create_index;
  2703         -    sqlite3ExprListSetName(pParse, pList, &nullId, 0);
         2941  +    pList->a[0].zName = sqlite3DbStrDup(pParse->db,
         2942  +                                        pTab->aCol[pTab->nCol-1].zName);
  2704   2943       pList->a[0].sortOrder = (u8)sortOrder;
  2705   2944     }
  2706   2945   
  2707   2946     /* Figure out how many bytes of space are required to store explicitly
  2708   2947     ** specified collation sequence names.
  2709   2948     */
  2710   2949     for(i=0; i<pList->nExpr; i++){
................................................................................
  2715   2954       }
  2716   2955     }
  2717   2956   
  2718   2957     /* 
  2719   2958     ** Allocate the index structure. 
  2720   2959     */
  2721   2960     nName = sqlite3Strlen30(zName);
  2722         -  nCol = pList->nExpr;
  2723         -  pIndex = sqlite3DbMallocZero(db, 
  2724         -      ROUND8(sizeof(Index)) +              /* Index structure  */
  2725         -      ROUND8(sizeof(tRowcnt)*(nCol+1)) +   /* Index.aiRowEst   */
  2726         -      sizeof(char *)*nCol +                /* Index.azColl     */
  2727         -      sizeof(int)*nCol +                   /* Index.aiColumn   */
  2728         -      sizeof(u8)*nCol +                    /* Index.aSortOrder */
  2729         -      nName + 1 +                          /* Index.zName      */
  2730         -      nExtra                               /* Collation sequence names */
  2731         -  );
         2961  +  nExtraCol = pPk ? pPk->nKeyCol : 1;
         2962  +  pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
         2963  +                                      nName + nExtra + 1, &zExtra);
  2732   2964     if( db->mallocFailed ){
  2733   2965       goto exit_create_index;
  2734   2966     }
  2735         -  zExtra = (char*)pIndex;
  2736         -  pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
  2737         -  pIndex->azColl = (char**)
  2738         -     ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
  2739   2967     assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
  2740   2968     assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
  2741         -  pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
  2742         -  pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
  2743         -  pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
  2744         -  zExtra = (char *)(&pIndex->zName[nName+1]);
         2969  +  pIndex->zName = zExtra;
         2970  +  zExtra += nName + 1;
  2745   2971     memcpy(pIndex->zName, zName, nName+1);
  2746   2972     pIndex->pTable = pTab;
  2747         -  pIndex->nColumn = pList->nExpr;
  2748   2973     pIndex->onError = (u8)onError;
  2749         -  pIndex->uniqNotNull = onError==OE_Abort;
         2974  +  pIndex->uniqNotNull = onError!=OE_None;
  2750   2975     pIndex->autoIndex = (u8)(pName==0);
  2751   2976     pIndex->pSchema = db->aDb[iDb].pSchema;
         2977  +  pIndex->nKeyCol = pList->nExpr;
  2752   2978     if( pPIWhere ){
  2753   2979       sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
  2754   2980       pIndex->pPartIdxWhere = pPIWhere;
  2755   2981       pPIWhere = 0;
  2756   2982     }
  2757   2983     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  2758   2984   
................................................................................
  2784   3010       }
  2785   3011       if( j>=pTab->nCol ){
  2786   3012         sqlite3ErrorMsg(pParse, "table %s has no column named %s",
  2787   3013           pTab->zName, zColName);
  2788   3014         pParse->checkSchema = 1;
  2789   3015         goto exit_create_index;
  2790   3016       }
  2791         -    pIndex->aiColumn[i] = j;
         3017  +    assert( pTab->nCol<=0x7fff && j<=0x7fff );
         3018  +    pIndex->aiColumn[i] = (i16)j;
  2792   3019       if( pListItem->pExpr ){
  2793   3020         int nColl;
  2794   3021         assert( pListItem->pExpr->op==TK_COLLATE );
  2795   3022         zColl = pListItem->pExpr->u.zToken;
  2796   3023         nColl = sqlite3Strlen30(zColl) + 1;
  2797   3024         assert( nExtra>=nColl );
  2798   3025         memcpy(zExtra, zColl, nColl);
................................................................................
  2807   3034         goto exit_create_index;
  2808   3035       }
  2809   3036       pIndex->azColl[i] = zColl;
  2810   3037       requestedSortOrder = pListItem->sortOrder & sortOrderMask;
  2811   3038       pIndex->aSortOrder[i] = (u8)requestedSortOrder;
  2812   3039       if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
  2813   3040     }
         3041  +  if( pPk ){
         3042  +    for(j=0; j<pPk->nKeyCol; j++){
         3043  +      int x = pPk->aiColumn[j];
         3044  +      if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
         3045  +        pIndex->nColumn--; 
         3046  +      }else{
         3047  +        pIndex->aiColumn[i] = x;
         3048  +        pIndex->azColl[i] = pPk->azColl[j];
         3049  +        pIndex->aSortOrder[i] = pPk->aSortOrder[j];
         3050  +        i++;
         3051  +      }
         3052  +    }
         3053  +    assert( i==pIndex->nColumn );
         3054  +  }else{
         3055  +    pIndex->aiColumn[i] = -1;
         3056  +    pIndex->azColl[i] = "BINARY";
         3057  +  }
  2814   3058     sqlite3DefaultRowEst(pIndex);
  2815   3059     if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
  2816   3060   
  2817   3061     if( pTab==pParse->pNewTable ){
  2818   3062       /* This routine has been called to create an automatic index as a
  2819   3063       ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
  2820   3064       ** a PRIMARY KEY or UNIQUE clause following the column definitions.
................................................................................
  2839   3083       Index *pIdx;
  2840   3084       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  2841   3085         int k;
  2842   3086         assert( pIdx->onError!=OE_None );
  2843   3087         assert( pIdx->autoIndex );
  2844   3088         assert( pIndex->onError!=OE_None );
  2845   3089   
  2846         -      if( pIdx->nColumn!=pIndex->nColumn ) continue;
  2847         -      for(k=0; k<pIdx->nColumn; k++){
         3090  +      if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
         3091  +      for(k=0; k<pIdx->nKeyCol; k++){
  2848   3092           const char *z1;
  2849   3093           const char *z2;
  2850   3094           if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
  2851   3095           z1 = pIdx->azColl[k];
  2852   3096           z2 = pIndex->azColl[k];
  2853   3097           if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
  2854   3098         }
  2855         -      if( k==pIdx->nColumn ){
         3099  +      if( k==pIdx->nKeyCol ){
  2856   3100           if( pIdx->onError!=pIndex->onError ){
  2857   3101             /* This constraint creates the same index as a previous
  2858   3102             ** constraint specified somewhere in the CREATE TABLE statement.
  2859   3103             ** However the ON CONFLICT clauses are different. If both this 
  2860   3104             ** constraint and the previous equivalent constraint have explicit
  2861   3105             ** ON CONFLICT clauses this is an error. Otherwise, use the
  2862   3106             ** explicitly specified behavior for the index.
................................................................................
  2890   3134       }
  2891   3135       db->flags |= SQLITE_InternChanges;
  2892   3136       if( pTblName!=0 ){
  2893   3137         pIndex->tnum = db->init.newTnum;
  2894   3138       }
  2895   3139     }
  2896   3140   
  2897         -  /* If the db->init.busy is 0 then create the index on disk.  This
  2898         -  ** involves writing the index into the master table and filling in the
  2899         -  ** index with the current table contents.
  2900         -  **
  2901         -  ** The db->init.busy is 0 when the user first enters a CREATE INDEX 
  2902         -  ** command.  db->init.busy is 1 when a database is opened and 
  2903         -  ** CREATE INDEX statements are read out of the master table.  In
  2904         -  ** the latter case the index already exists on disk, which is why
  2905         -  ** we don't want to recreate it.
         3141  +  /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
         3142  +  ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
         3143  +  ** emit code to allocate the index rootpage on disk and make an entry for
         3144  +  ** the index in the sqlite_master table and populate the index with
         3145  +  ** content.  But, do not do this if we are simply reading the sqlite_master
         3146  +  ** table to parse the schema, or if this index is the PRIMARY KEY index
         3147  +  ** of a WITHOUT ROWID table.
  2906   3148     **
  2907         -  ** If pTblName==0 it means this index is generated as a primary key
  2908         -  ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
         3149  +  ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
         3150  +  ** or UNIQUE index in a CREATE TABLE statement.  Since the table
  2909   3151     ** has just been created, it contains no data and the index initialization
  2910   3152     ** step can be skipped.
  2911   3153     */
  2912         -  else if( pParse->nErr==0 ){
         3154  +  else if( pParse->nErr==0 && (HasRowid(pTab) || pTblName!=0) ){
  2913   3155       Vdbe *v;
  2914   3156       char *zStmt;
  2915   3157       int iMem = ++pParse->nMem;
  2916   3158   
  2917   3159       v = sqlite3GetVdbe(pParse);
  2918   3160       if( v==0 ) goto exit_create_index;
  2919   3161   
................................................................................
  3017   3259     tRowcnt *a = pIdx->aiRowEst;
  3018   3260     int i;
  3019   3261     tRowcnt n;
  3020   3262     assert( a!=0 );
  3021   3263     a[0] = pIdx->pTable->nRowEst;
  3022   3264     if( a[0]<10 ) a[0] = 10;
  3023   3265     n = 10;
  3024         -  for(i=1; i<=pIdx->nColumn; i++){
         3266  +  for(i=1; i<=pIdx->nKeyCol; i++){
  3025   3267       a[i] = n;
  3026   3268       if( n>5 ) n--;
  3027   3269     }
  3028   3270     if( pIdx->onError!=OE_None ){
  3029         -    a[pIdx->nColumn] = 1;
         3271  +    a[pIdx->nKeyCol] = 1;
  3030   3272     }
  3031   3273   }
  3032   3274   
  3033   3275   /*
  3034   3276   ** This routine will drop an existing named index.  This routine
  3035   3277   ** implements the DROP INDEX statement.
  3036   3278   */
................................................................................
  3710   3952   ** and/or current transaction is rolled back.
  3711   3953   */
  3712   3954   void sqlite3HaltConstraint(
  3713   3955     Parse *pParse,    /* Parsing context */
  3714   3956     int errCode,      /* extended error code */
  3715   3957     int onError,      /* Constraint type */
  3716   3958     char *p4,         /* Error message */
  3717         -  int p4type        /* P4_STATIC or P4_TRANSIENT */
         3959  +  i8 p4type,        /* P4_STATIC or P4_TRANSIENT */
         3960  +  u8 p5Errmsg       /* P5_ErrMsg type */
  3718   3961   ){
  3719   3962     Vdbe *v = sqlite3GetVdbe(pParse);
  3720   3963     assert( (errCode&0xff)==SQLITE_CONSTRAINT );
  3721   3964     if( onError==OE_Abort ){
  3722   3965       sqlite3MayAbort(pParse);
  3723   3966     }
  3724   3967     sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
         3968  +  if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg);
         3969  +}
         3970  +
         3971  +/*
         3972  +** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
         3973  +*/
         3974  +void sqlite3UniqueConstraint(
         3975  +  Parse *pParse,    /* Parsing context */
         3976  +  int onError,      /* Constraint type */
         3977  +  Index *pIdx       /* The index that triggers the constraint */
         3978  +){
         3979  +  char *zErr;
         3980  +  int j;
         3981  +  StrAccum errMsg;
         3982  +  Table *pTab = pIdx->pTable;
         3983  +
         3984  +  sqlite3StrAccumInit(&errMsg, 0, 0, 200);
         3985  +  errMsg.db = pParse->db;
         3986  +  for(j=0; j<pIdx->nKeyCol; j++){
         3987  +    char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
         3988  +    if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
         3989  +    sqlite3StrAccumAppend(&errMsg, pTab->zName, -1);
         3990  +    sqlite3StrAccumAppend(&errMsg, ".", 1);
         3991  +    sqlite3StrAccumAppend(&errMsg, zCol, -1);
         3992  +  }
         3993  +  zErr = sqlite3StrAccumFinish(&errMsg);
         3994  +  sqlite3HaltConstraint(pParse, 
         3995  +    (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
         3996  +    onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
         3997  +}
         3998  +
         3999  +
         4000  +/*
         4001  +** Code an OP_Halt due to non-unique rowid.
         4002  +*/
         4003  +void sqlite3RowidConstraint(
         4004  +  Parse *pParse,    /* Parsing context */
         4005  +  int onError,      /* Conflict resolution algorithm */
         4006  +  Table *pTab       /* The table with the non-unique rowid */ 
         4007  +){
         4008  +  char *zMsg;
         4009  +  int rc;
         4010  +  if( pTab->iPKey>=0 ){
         4011  +    zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
         4012  +                          pTab->aCol[pTab->iPKey].zName);
         4013  +    rc = SQLITE_CONSTRAINT_PRIMARYKEY;
         4014  +  }else{
         4015  +    zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
         4016  +    rc = SQLITE_CONSTRAINT_ROWID;
         4017  +  }
         4018  +  sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
         4019  +                        P5_ConstraintUnique);
  3725   4020   }
  3726   4021   
  3727   4022   /*
  3728   4023   ** Check to see if pIndex uses the collating sequence pColl.  Return
  3729   4024   ** true if it does and false if it does not.
  3730   4025   */
  3731   4026   #ifndef SQLITE_OMIT_REINDEX
  3732   4027   static int collationMatch(const char *zColl, Index *pIndex){
  3733   4028     int i;
  3734   4029     assert( zColl!=0 );
  3735   4030     for(i=0; i<pIndex->nColumn; i++){
  3736   4031       const char *z = pIndex->azColl[i];
  3737         -    assert( z!=0 );
  3738         -    if( 0==sqlite3StrICmp(z, zColl) ){
         4032  +    assert( z!=0 || pIndex->aiColumn[i]<0 );
         4033  +    if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
  3739   4034         return 1;
  3740   4035       }
  3741   4036     }
  3742   4037     return 0;
  3743   4038   }
  3744   4039   #endif
  3745   4040   
................................................................................
  3850   4145       return;
  3851   4146     }
  3852   4147     sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
  3853   4148   }
  3854   4149   #endif
  3855   4150   
  3856   4151   /*
  3857         -** Return a dynamicly allocated KeyInfo structure that can be used
  3858         -** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
         4152  +** Return a KeyInfo structure that is appropriate for the given Index.
         4153  +**
         4154  +** The KeyInfo structure for an index is cached in the Index object.
         4155  +** So there might be multiple references to the returned pointer.  The
         4156  +** caller should not try to modify the KeyInfo object.
  3859   4157   **
  3860         -** If successful, a pointer to the new structure is returned. In this case
  3861         -** the caller is responsible for calling sqlite3DbFree(db, ) on the returned 
  3862         -** pointer. If an error occurs (out of memory or missing collation 
  3863         -** sequence), NULL is returned and the state of pParse updated to reflect
  3864         -** the error.
         4158  +** The caller should invoke sqlite3KeyInfoUnref() on the returned object
         4159  +** when it has finished using it.
  3865   4160   */
  3866         -KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
  3867         -  int i;
  3868         -  int nCol = pIdx->nColumn;
  3869         -  KeyInfo *pKey;
  3870         -
  3871         -  pKey = sqlite3KeyInfoAlloc(pParse->db, nCol);
  3872         -  if( pKey ){
  3873         -    for(i=0; i<nCol; i++){
  3874         -      char *zColl = pIdx->azColl[i];
  3875         -      assert( zColl );
  3876         -      pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
  3877         -      pKey->aSortOrder[i] = pIdx->aSortOrder[i];
         4161  +KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
         4162  +  if( pParse->nErr ) return 0;
         4163  +#ifndef SQLITE_OMIT_SHARED_CACHE
         4164  +  if( pIdx->pKeyInfo && pIdx->pKeyInfo->db!=pParse->db ){
         4165  +    sqlite3KeyInfoUnref(pIdx->pKeyInfo);
         4166  +    pIdx->pKeyInfo = 0;
         4167  +  }
         4168  +#endif
         4169  +  if( pIdx->pKeyInfo==0 ){
         4170  +    int i;
         4171  +    int nCol = pIdx->nColumn;
         4172  +    int nKey = pIdx->nKeyCol;
         4173  +    KeyInfo *pKey;
         4174  +    if( pIdx->uniqNotNull ){
         4175  +      pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
         4176  +    }else{
         4177  +      pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
         4178  +    }
         4179  +    if( pKey ){
         4180  +      assert( sqlite3KeyInfoIsWriteable(pKey) );
         4181  +      for(i=0; i<nCol; i++){
         4182  +        char *zColl = pIdx->azColl[i];
         4183  +        if( NEVER(zColl==0) ) zColl = "BINARY";
         4184  +        pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
         4185  +        pKey->aSortOrder[i] = pIdx->aSortOrder[i];
         4186  +      }
         4187  +      if( pParse->nErr ){
         4188  +        sqlite3KeyInfoUnref(pKey);
         4189  +      }else{
         4190  +        pIdx->pKeyInfo = pKey;
         4191  +      }
  3878   4192       }
  3879   4193     }
  3880         -
  3881         -  if( pParse->nErr ){
  3882         -    sqlite3DbFree(pParse->db, pKey);
  3883         -    pKey = 0;
  3884         -  }
  3885         -  return pKey;
         4194  +  return sqlite3KeyInfoRef(pIdx->pKeyInfo);
  3886   4195   }

Changes to src/delete.c.

   130    130   Expr *sqlite3LimitWhere(
   131    131     Parse *pParse,               /* The parser context */
   132    132     SrcList *pSrc,               /* the FROM clause -- which tables to scan */
   133    133     Expr *pWhere,                /* The WHERE clause.  May be null */
   134    134     ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
   135    135     Expr *pLimit,                /* The LIMIT clause.  May be null */
   136    136     Expr *pOffset,               /* The OFFSET clause.  May be null */
   137         -  char *zStmtType              /* Either DELETE or UPDATE.  For error messages. */
          137  +  char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
   138    138   ){
   139    139     Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
   140    140     Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
   141    141     Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
   142    142     ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
   143    143     SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
   144    144     Select *pSelect = NULL;      /* Complete SELECT tree */
................................................................................
   205    205   limit_where_cleanup_2:
   206    206     sqlite3ExprDelete(pParse->db, pWhere);
   207    207     sqlite3ExprListDelete(pParse->db, pOrderBy);
   208    208     sqlite3ExprDelete(pParse->db, pLimit);
   209    209     sqlite3ExprDelete(pParse->db, pOffset);
   210    210     return 0;
   211    211   }
   212         -#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
          212  +#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
          213  +       /*      && !defined(SQLITE_OMIT_SUBQUERY) */
   213    214   
   214    215   /*
   215    216   ** Generate code for a DELETE FROM statement.
   216    217   **
   217    218   **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
   218    219   **                 \________/       \________________/
   219    220   **                  pTabList              pWhere
................................................................................
   226    227     Vdbe *v;               /* The virtual database engine */
   227    228     Table *pTab;           /* The table from which records will be deleted */
   228    229     const char *zDb;       /* Name of database holding pTab */
   229    230     int end, addr = 0;     /* A couple addresses of generated code */
   230    231     int i;                 /* Loop counter */
   231    232     WhereInfo *pWInfo;     /* Information about the WHERE clause */
   232    233     Index *pIdx;           /* For looping over indices of the table */
   233         -  int iCur;              /* VDBE Cursor number for pTab */
          234  +  int iTabCur;           /* Cursor number for the table */
          235  +  int iDataCur;          /* VDBE cursor for the canonical data source */
          236  +  int iIdxCur;           /* Cursor number of the first index */
   234    237     sqlite3 *db;           /* Main database structure */
   235    238     AuthContext sContext;  /* Authorization context */
   236    239     NameContext sNC;       /* Name context to resolve expressions in */
   237    240     int iDb;               /* Database number */
   238    241     int memCnt = -1;       /* Memory cell used for change counting */
   239    242     int rcauth;            /* Value returned by authorization callback */
   240    243   
................................................................................
   291    294       goto delete_from_cleanup;
   292    295     }
   293    296     assert(!isView || pTrigger);
   294    297   
   295    298     /* Assign  cursor number to the table and all its indices.
   296    299     */
   297    300     assert( pTabList->nSrc==1 );
   298         -  iCur = pTabList->a[0].iCursor = pParse->nTab++;
          301  +  iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
   299    302     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   300    303       pParse->nTab++;
   301    304     }
   302    305   
   303    306     /* Start the view context
   304    307     */
   305    308     if( isView ){
................................................................................
   316    319     sqlite3BeginWriteOperation(pParse, 1, iDb);
   317    320   
   318    321     /* If we are trying to delete from a view, realize that view into
   319    322     ** a ephemeral table.
   320    323     */
   321    324   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
   322    325     if( isView ){
   323         -    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
          326  +    sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
          327  +    iDataCur = iIdxCur = iTabCur;
   324    328     }
   325    329   #endif
   326    330   
   327    331     /* Resolve the column names in the WHERE clause.
   328    332     */
   329    333     memset(&sNC, 0, sizeof(sNC));
   330    334     sNC.pParse = pParse;
................................................................................
   354    358   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
   355    359      && db->xPreUpdateCallback==0
   356    360   #endif
   357    361      && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
   358    362     ){
   359    363       assert( !isView );
   360    364       sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
   361         -    sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
   362         -                      pTab->zName, P4_STATIC);
          365  +    if( HasRowid(pTab) ){
          366  +      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
          367  +                        pTab->zName, P4_STATIC);
          368  +    }
   363    369       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   364    370         assert( pIdx->pSchema==pTab->pSchema );
   365    371         sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
   366    372       }
   367    373     }else
   368    374   #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
   369         -  /* The usual case: There is a WHERE clause so we have to scan through
   370         -  ** the table and pick which records to delete.
   371         -  */
   372         -  {
          375  +  if( !HasRowid(pTab) ){
          376  +    /* There is a WHERE clause on a WITHOUT ROWID table.  
          377  +    */
          378  +    Index *pPk;      /* The PRIMARY KEY index on the table */
          379  +    int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
          380  +    int iEph;        /* Ephemeral table holding all primary key values */
          381  +    int iKey;        /* Key value inserting into iEph */
          382  +    i16 nPk;         /* Number of components of the PRIMARY KEY */
          383  +
          384  +    pPk = sqlite3PrimaryKeyIndex(pTab);
          385  +    assert( pPk!=0 );
          386  +    nPk = pPk->nKeyCol;
          387  +    iPk = pParse->nMem+1;
          388  +    pParse->nMem += nPk;
          389  +    iKey = ++pParse->nMem;
          390  +    iEph = pParse->nTab++;
          391  +
          392  +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
          393  +    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
          394  +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 0, 0);
          395  +    if( pWInfo==0 ) goto delete_from_cleanup;
          396  +    for(i=0; i<nPk; i++){
          397  +      sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, pPk->aiColumn[i],iPk+i);
          398  +    }
          399  +    sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
          400  +                      sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
          401  +    sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, iKey);
          402  +    if( db->flags & SQLITE_CountRows ){
          403  +      sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
          404  +    }
          405  +    sqlite3WhereEnd(pWInfo);
          406  +
          407  +    /* Open cursors for all indices of the table.
          408  +    */
          409  +    sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite,
          410  +                               iTabCur, &iDataCur, &iIdxCur);
          411  +
          412  +    /* Loop over the primary keys to be deleted. */
          413  +    addr = sqlite3VdbeAddOp1(v, OP_Rewind, iEph);
          414  +    sqlite3VdbeAddOp2(v, OP_RowKey, iEph, iPk);
          415  +
          416  +    /* Delete the row */
          417  +    sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
          418  +                             iPk, 0, 1, OE_Default, 0);
          419  +
          420  +    /* End of the delete loop */
          421  +    sqlite3VdbeAddOp2(v, OP_Next, iEph, addr+1);
          422  +    sqlite3VdbeJumpHere(v, addr);
          423  +
          424  +    /* Close the cursors open on the table and its indexes. */
          425  +    assert( iDataCur>=iIdxCur );
          426  +    for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
          427  +      sqlite3VdbeAddOp1(v, OP_Close, iIdxCur+i);
          428  +    }
          429  +  }else{
          430  +    /* There is a WHERE clause on a rowid table.  Run a loop that extracts
          431  +    ** all rowids to be deleted into a RowSet.
          432  +    */
   373    433       int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
   374    434       int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
   375    435       int regRowid;                   /* Actual register containing rowids */
   376    436   
   377    437       /* Collect rowids of every row to be deleted.
   378    438       */
   379    439       sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
   380    440       pWInfo = sqlite3WhereBegin(
   381    441           pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
   382    442       );
   383    443       if( pWInfo==0 ) goto delete_from_cleanup;
   384         -    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0);
          444  +    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iRowid, 0);
   385    445       sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
   386    446       if( db->flags & SQLITE_CountRows ){
   387    447         sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
   388    448       }
   389    449       sqlite3WhereEnd(pWInfo);
   390    450   
   391    451       /* Delete every item whose key was written to the list during the
................................................................................
   394    454       end = sqlite3VdbeMakeLabel(v);
   395    455   
   396    456       /* Unless this is a view, open cursors for the table we are 
   397    457       ** deleting from and all its indices. If this is a view, then the
   398    458       ** only effect this statement has is to fire the INSTEAD OF 
   399    459       ** triggers.  */
   400    460       if( !isView ){
   401         -      sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
          461  +      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur,
          462  +                                 &iDataCur, &iIdxCur);
          463  +      assert( iDataCur==iTabCur );
          464  +      assert( iIdxCur==iDataCur+1 );
   402    465       }
   403    466   
   404    467       addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
   405    468   
   406    469       /* Delete the row */
   407    470   #ifndef SQLITE_OMIT_VIRTUALTABLE
   408    471       if( IsVirtual(pTab) ){
................................................................................
   411    474         sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, (char*)pVTab, P4_VTAB);
   412    475         sqlite3VdbeChangeP5(v, OE_Abort);
   413    476         sqlite3MayAbort(pParse);
   414    477       }else
   415    478   #endif
   416    479       {
   417    480         int count = (pParse->nested==0);    /* True to count changes */
   418         -      sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
          481  +      sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
          482  +                               iRowid, 1, count, OE_Default, 0);
   419    483       }
   420    484   
   421    485       /* End of the delete loop */
   422    486       sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
   423    487       sqlite3VdbeResolveLabel(v, end);
   424    488   
   425    489       /* Close the cursors open on the table and its indexes. */
   426    490       if( !isView && !IsVirtual(pTab) ){
   427         -      for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   428         -        sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
          491  +      sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
          492  +      for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
          493  +        sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
   429    494         }
   430         -      sqlite3VdbeAddOp1(v, OP_Close, iCur);
   431    495       }
   432    496     }
   433    497   
   434    498     /* Update the sqlite_sequence table by storing the content of the
   435    499     ** maximum rowid counter values recorded while inserting into
   436    500     ** autoincrement tables.
   437    501     */
................................................................................
   463    527   #endif
   464    528   #ifdef pTrigger
   465    529    #undef pTrigger
   466    530   #endif
   467    531   
   468    532   /*
   469    533   ** This routine generates VDBE code that causes a single row of a
   470         -** single table to be deleted.
          534  +** single table to be deleted.  Both the original table entry and
          535  +** all indices are removed.
   471    536   **
   472         -** The VDBE must be in a particular state when this routine is called.
   473         -** These are the requirements:
          537  +** Preconditions:
   474    538   **
   475         -**   1.  A read/write cursor pointing to pTab, the table containing the row
   476         -**       to be deleted, must be opened as cursor number $iCur.
          539  +**   1.  iDataCur is an open cursor on the btree that is the canonical data
          540  +**       store for the table.  (This will be either the table itself,
          541  +**       in the case of a rowid table, or the PRIMARY KEY index in the case
          542  +**       of a WITHOUT ROWID table.)
   477    543   **
   478    544   **   2.  Read/write cursors for all indices of pTab must be open as
   479         -**       cursor number base+i for the i-th index.
          545  +**       cursor number iIdxCur+i for the i-th index.
   480    546   **
   481         -**   3.  The record number of the row to be deleted must be stored in
   482         -**       memory cell iRowid.
   483         -**
   484         -** This routine generates code to remove both the table record and all 
   485         -** index entries that point to that record.
          547  +**   3.  The primary key for the row to be deleted must be stored in a
          548  +**       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
          549  +**       that a search record formed from OP_MakeRecord is contained in the
          550  +**       single memory location iPk.
   486    551   */
   487    552   void sqlite3GenerateRowDelete(
   488    553     Parse *pParse,     /* Parsing context */
   489    554     Table *pTab,       /* Table containing the row to be deleted */
   490         -  int iCur,          /* Cursor number for the table */
   491         -  int iRowid,        /* Memory cell that contains the rowid to delete */
   492         -  int count,         /* If non-zero, increment the row change counter */
   493    555     Trigger *pTrigger, /* List of triggers to (potentially) fire */
   494         -  int onconf         /* Default ON CONFLICT policy for triggers */
          556  +  int iDataCur,      /* Cursor from which column data is extracted */
          557  +  int iIdxCur,       /* First index cursor */
          558  +  int iPk,           /* First memory cell containing the PRIMARY KEY */
          559  +  i16 nPk,           /* Number of PRIMARY KEY memory cells */
          560  +  u8 count,          /* If non-zero, increment the row change counter */
          561  +  u8 onconf,         /* Default ON CONFLICT policy for triggers */
          562  +  u8 bNoSeek         /* iDataCur is already pointing to the row to delete */
   495    563   ){
   496    564     Vdbe *v = pParse->pVdbe;        /* Vdbe */
   497    565     int iOld = 0;                   /* First register in OLD.* array */
   498    566     int iLabel;                     /* Label resolved to end of generated code */
          567  +  u8 opSeek;                      /* Seek opcode */
   499    568   
   500    569     /* Vdbe is guaranteed to have been allocated by this stage. */
   501    570     assert( v );
          571  +  VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
          572  +                         iDataCur, iIdxCur, iPk, (int)nPk));
   502    573   
   503    574     /* Seek cursor iCur to the row to delete. If this row no longer exists 
   504    575     ** (this can happen if a trigger program has already deleted it), do
   505    576     ** not attempt to delete it or fire any DELETE triggers.  */
   506    577     iLabel = sqlite3VdbeMakeLabel(v);
   507         -  sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
          578  +  opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
          579  +  if( !bNoSeek ) sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
   508    580    
   509    581     /* If there are any triggers to fire, allocate a range of registers to
   510    582     ** use for the old.* references in the triggers.  */
   511    583     if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
   512    584       u32 mask;                     /* Mask of OLD.* columns in use */
   513    585       int iCol;                     /* Iterator used while populating OLD.* */
   514    586   
................................................................................
   519    591       );
   520    592       mask |= sqlite3FkOldmask(pParse, pTab);
   521    593       iOld = pParse->nMem+1;
   522    594       pParse->nMem += (1 + pTab->nCol);
   523    595   
   524    596       /* Populate the OLD.* pseudo-table register array. These values will be 
   525    597       ** used by any BEFORE and AFTER triggers that exist.  */
   526         -    sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
          598  +    sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
   527    599       for(iCol=0; iCol<pTab->nCol; iCol++){
   528    600         if( mask==0xffffffff || mask&(1<<iCol) ){
   529         -        sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
          601  +        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
   530    602         }
   531    603       }
   532    604   
   533    605       /* Invoke BEFORE DELETE trigger programs. */
   534    606       sqlite3CodeRowTrigger(pParse, pTrigger, 
   535    607           TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
   536    608       );
   537    609   
   538    610       /* Seek the cursor to the row to be deleted again. It may be that
   539    611       ** the BEFORE triggers coded above have already removed the row
   540    612       ** being deleted. Do not attempt to delete the row a second time, and 
   541    613       ** do not fire AFTER triggers.  */
   542         -    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
          614  +    sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
   543    615   
   544    616       /* Do FK processing. This call checks that any FK constraints that
   545    617       ** refer to this table (i.e. constraints attached to other tables) 
   546    618       ** are not violated by deleting this row.  */
   547    619       sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
   548    620     }
   549    621   
................................................................................
   554    626     ** If variable 'count' is non-zero, then this OP_Delete instruction should
   555    627     ** invoke the update-hook. The pre-update-hook, on the other hand should
   556    628     ** be invoked unless table pTab is a system table. The difference is that
   557    629     ** the update-hook is not invoked for rows removed by REPLACE, but the 
   558    630     ** pre-update-hook is.
   559    631     */ 
   560    632     if( pTab->pSelect==0 ){
   561         -    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
   562         -    sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
          633  +    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
          634  +    sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
   563    635       sqlite3VdbeChangeP4(v, -1, (char*)pTab, P4_TABLE);
   564    636     }
   565    637   
   566    638     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
   567    639     ** handle rows (possibly in other tables) that refer via a foreign key
   568    640     ** to the row just deleted. */ 
   569    641     sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
................................................................................
   573    645         TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
   574    646     );
   575    647   
   576    648     /* Jump here if the row had already been deleted before any BEFORE
   577    649     ** trigger programs were invoked. Or if a trigger program throws a 
   578    650     ** RAISE(IGNORE) exception.  */
   579    651     sqlite3VdbeResolveLabel(v, iLabel);
          652  +  VdbeModuleComment((v, "END: GenRowDel()"));
   580    653   }
   581    654   
   582    655   /*
   583    656   ** This routine generates VDBE code that causes the deletion of all
   584         -** index entries associated with a single row of a single table.
          657  +** index entries associated with a single row of a single table, pTab
   585    658   **
   586         -** The VDBE must be in a particular state when this routine is called.
   587         -** These are the requirements:
          659  +** Preconditions:
   588    660   **
   589         -**   1.  A read/write cursor pointing to pTab, the table containing the row
   590         -**       to be deleted, must be opened as cursor number "iCur".
          661  +**   1.  A read/write cursor "iDataCur" must be open on the canonical storage
          662  +**       btree for the table pTab.  (This will be either the table itself
          663  +**       for rowid tables or to the primary key index for WITHOUT ROWID
          664  +**       tables.)
   591    665   **
   592    666   **   2.  Read/write cursors for all indices of pTab must be open as
   593         -**       cursor number iCur+i for the i-th index.
          667  +**       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
          668  +**       index is the 0-th index.)
   594    669   **
   595         -**   3.  The "iCur" cursor must be pointing to the row that is to be
   596         -**       deleted.
          670  +**   3.  The "iDataCur" cursor must be already be positioned on the row
          671  +**       that is to be deleted.
   597    672   */
   598    673   void sqlite3GenerateRowIndexDelete(
   599    674     Parse *pParse,     /* Parsing and code generating context */
   600    675     Table *pTab,       /* Table containing the row to be deleted */
   601         -  int iCur,          /* Cursor number for the table */
          676  +  int iDataCur,      /* Cursor of table holding data. */
          677  +  int iIdxCur,       /* First index cursor */
   602    678     int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
   603    679   ){
   604         -  int i;
   605         -  Index *pIdx;
   606         -  int r1;
   607         -  int iPartIdxLabel;
   608         -  Vdbe *v = pParse->pVdbe;
          680  +  int i;             /* Index loop counter */
          681  +  int r1;            /* Register holding an index key */
          682  +  int iPartIdxLabel; /* Jump destination for skipping partial index entries */
          683  +  Index *pIdx;       /* Current index */
          684  +  Vdbe *v;           /* The prepared statement under construction */
          685  +  Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
   609    686   
   610         -  for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   611         -    if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
   612         -    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0, &iPartIdxLabel);
   613         -    sqlite3VdbeAddOp3(v, OP_IdxDelete, iCur+i, r1, pIdx->nColumn+1);
          687  +  v = pParse->pVdbe;
          688  +  pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
          689  +  for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
          690  +    assert( iIdxCur+i!=iDataCur || pPk==pIdx );
          691  +    if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
          692  +    if( pIdx==pPk ) continue;
          693  +    VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
          694  +    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1, &iPartIdxLabel);
          695  +    sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
          696  +                      pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
   614    697       sqlite3VdbeResolveLabel(v, iPartIdxLabel);
   615    698     }
   616    699   }
   617    700   
   618    701   /*
   619         -** Generate code that will assemble an index key and put it in register
          702  +** Generate code that will assemble an index key and stores it in register
   620    703   ** regOut.  The key with be for index pIdx which is an index on pTab.
   621    704   ** iCur is the index of a cursor open on the pTab table and pointing to
   622         -** the entry that needs indexing.
          705  +** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
          706  +** iCur must be the cursor of the PRIMARY KEY index.
   623    707   **
   624    708   ** Return a register number which is the first in a block of
   625    709   ** registers that holds the elements of the index key.  The
   626    710   ** block of registers has already been deallocated by the time
   627    711   ** this routine returns.
   628    712   **
   629    713   ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
................................................................................
   632    716   ** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
   633    717   ** will be set to zero which is an empty label that is ignored by
   634    718   ** sqlite3VdbeResolveLabel().
   635    719   */
   636    720   int sqlite3GenerateIndexKey(
   637    721     Parse *pParse,       /* Parsing context */
   638    722     Index *pIdx,         /* The index for which to generate a key */
   639         -  int iCur,            /* Cursor number for the pIdx->pTable table */
   640         -  int regOut,          /* Write the new index key to this register */
   641         -  int doMakeRec,       /* Run the OP_MakeRecord instruction if true */
          723  +  int iDataCur,        /* Cursor number from which to take column data */
          724  +  int regOut,          /* Put the new key into this register if not 0 */
          725  +  int prefixOnly,      /* Compute only a unique prefix of the key */
   642    726     int *piPartIdxLabel  /* OUT: Jump to this label to skip partial index */
   643    727   ){
   644    728     Vdbe *v = pParse->pVdbe;
   645    729     int j;
   646    730     Table *pTab = pIdx->pTable;
   647    731     int regBase;
   648    732     int nCol;
          733  +  Index *pPk;
   649    734   
   650    735     if( piPartIdxLabel ){
   651    736       if( pIdx->pPartIdxWhere ){
   652    737         *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
   653         -      pParse->iPartIdxTab = iCur;
          738  +      pParse->iPartIdxTab = iDataCur;
   654    739         sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
   655    740                            SQLITE_JUMPIFNULL);
   656    741       }else{
   657    742         *piPartIdxLabel = 0;
   658    743       }
   659    744     }
   660         -  nCol = pIdx->nColumn;
   661         -  regBase = sqlite3GetTempRange(pParse, nCol+1);
   662         -  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
          745  +  nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
          746  +  regBase = sqlite3GetTempRange(pParse, nCol);
          747  +  pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
   663    748     for(j=0; j<nCol; j++){
   664         -    int idx = pIdx->aiColumn[j];
   665         -    if( idx==pTab->iPKey ){
   666         -      sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
          749  +    i16 idx = pIdx->aiColumn[j];
          750  +    if( pPk ) idx = sqlite3ColumnOfIndex(pPk, idx);
          751  +    if( idx<0 || idx==pTab->iPKey ){
          752  +      sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regBase+j);
   667    753       }else{
   668         -      sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
   669         -      sqlite3ColumnDefault(v, pTab, idx, -1);
          754  +      sqlite3VdbeAddOp3(v, OP_Column, iDataCur, idx, regBase+j);
          755  +      sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[j], -1);
   670    756       }
   671    757     }
   672         -  if( doMakeRec ){
          758  +  if( regOut ){
   673    759       const char *zAff;
   674    760       if( pTab->pSelect
   675    761        || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
   676    762       ){
   677    763         zAff = 0;
   678    764       }else{
   679    765         zAff = sqlite3IndexAffinityStr(v, pIdx);
   680    766       }
   681         -    sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
          767  +    sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
   682    768       sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
   683    769     }
   684         -  sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
          770  +  sqlite3ReleaseTempRange(pParse, regBase, nCol);
   685    771     return regBase;
   686    772   }

Changes to src/expr.c.

  1528   1528     ** ephemeral table.
  1529   1529     */
  1530   1530     p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
  1531   1531     if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
  1532   1532       sqlite3 *db = pParse->db;              /* Database connection */
  1533   1533       Table *pTab;                           /* Table <table>. */
  1534   1534       Expr *pExpr;                           /* Expression <column> */
  1535         -    int iCol;                              /* Index of column <column> */
  1536         -    int iDb;                               /* Database idx for pTab */
         1535  +    i16 iCol;                              /* Index of column <column> */
         1536  +    i16 iDb;                               /* Database idx for pTab */
  1537   1537   
  1538   1538       assert( p );                        /* Because of isCandidateForInOpt(p) */
  1539   1539       assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
  1540   1540       assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
  1541   1541       assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
  1542   1542       pTab = p->pSrc->a[0].pTab;
  1543   1543       pExpr = p->pEList->a[0].pExpr;
  1544         -    iCol = pExpr->iColumn;
         1544  +    iCol = (i16)pExpr->iColumn;
  1545   1545      
  1546   1546       /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
  1547   1547       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  1548   1548       sqlite3CodeVerifySchema(pParse, iDb);
  1549   1549       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  1550   1550   
  1551   1551       /* This function is only called from two places. In both cases the vdbe
................................................................................
  1575   1575         ** it is not, it is not possible to use any index.
  1576   1576         */
  1577   1577         int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
  1578   1578   
  1579   1579         for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
  1580   1580           if( (pIdx->aiColumn[0]==iCol)
  1581   1581            && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
  1582         -         && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
         1582  +         && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
  1583   1583           ){
  1584         -          int iAddr;
  1585         -          char *pKey;
  1586         -  
  1587         -          pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
  1588         -          iAddr = sqlite3CodeOnce(pParse);
  1589         -  
  1590         -          sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
  1591         -                               pKey,P4_KEYINFO_HANDOFF);
         1584  +          int iAddr = sqlite3CodeOnce(pParse);
         1585  +          sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
         1586  +          sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1592   1587             VdbeComment((v, "%s", pIdx->zName));
  1593   1588             assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
  1594   1589             eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
  1595   1590   
  1596   1591             sqlite3VdbeJumpHere(v, iAddr);
  1597   1592             if( prNotFound && !pTab->aCol[iCol].notNull ){
  1598   1593               *prNotFound = ++pParse->nMem;
................................................................................
  1724   1719         ** if either column has NUMERIC or INTEGER affinity. If neither
  1725   1720         ** 'x' nor the SELECT... statement are columns, then numeric affinity
  1726   1721         ** is used.
  1727   1722         */
  1728   1723         pExpr->iTable = pParse->nTab++;
  1729   1724         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
  1730   1725         if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  1731         -      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1);
         1726  +      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
  1732   1727   
  1733   1728         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1734   1729           /* Case 1:     expr IN (SELECT ...)
  1735   1730           **
  1736   1731           ** Generate code to write the results of the select into the temporary
  1737   1732           ** table allocated and opened above.
  1738   1733           */
................................................................................
  1742   1737           assert( !isRowid );
  1743   1738           sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
  1744   1739           dest.affSdst = (u8)affinity;
  1745   1740           assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
  1746   1741           pExpr->x.pSelect->iLimit = 0;
  1747   1742           testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
  1748   1743           if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
  1749         -          sqlite3DbFree(pParse->db, pKeyInfo);
         1744  +          sqlite3KeyInfoUnref(pKeyInfo);
  1750   1745             return 0;
  1751   1746           }
  1752   1747           pEList = pExpr->x.pSelect->pEList;
  1753   1748           assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
  1754   1749           assert( pEList!=0 );
  1755   1750           assert( pEList->nExpr>0 );
         1751  +        assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  1756   1752           pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
  1757   1753                                                            pEList->a[0].pExpr);
  1758   1754         }else if( ALWAYS(pExpr->x.pList!=0) ){
  1759   1755           /* Case 2:     expr IN (exprlist)
  1760   1756           **
  1761   1757           ** For each expression, build an index key from the evaluation and
  1762   1758           ** store it in the temporary table. If <expr> is a column, then use
................................................................................
  1768   1764           struct ExprList_item *pItem;
  1769   1765           int r1, r2, r3;
  1770   1766   
  1771   1767           if( !affinity ){
  1772   1768             affinity = SQLITE_AFF_NONE;
  1773   1769           }
  1774   1770           if( pKeyInfo ){
         1771  +          assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  1775   1772             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  1776   1773           }
  1777   1774   
  1778   1775           /* Loop through each expression in <exprlist>. */
  1779   1776           r1 = sqlite3GetTempReg(pParse);
  1780   1777           r2 = sqlite3GetTempReg(pParse);
  1781   1778           sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
................................................................................
  1809   1806               }
  1810   1807             }
  1811   1808           }
  1812   1809           sqlite3ReleaseTempReg(pParse, r1);
  1813   1810           sqlite3ReleaseTempReg(pParse, r2);
  1814   1811         }
  1815   1812         if( pKeyInfo ){
  1816         -        sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO_HANDOFF);
         1813  +        sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
  1817   1814         }
  1818   1815         break;
  1819   1816       }
  1820   1817   
  1821   1818       case TK_EXISTS:
  1822   1819       case TK_SELECT:
  1823   1820       default: {
................................................................................
  2210   2207   
  2211   2208   /*
  2212   2209   ** Generate code to extract the value of the iCol-th column of a table.
  2213   2210   */
  2214   2211   void sqlite3ExprCodeGetColumnOfTable(
  2215   2212     Vdbe *v,        /* The VDBE under construction */
  2216   2213     Table *pTab,    /* The table containing the value */
  2217         -  int iTabCur,    /* The cursor for this table */
         2214  +  int iTabCur,    /* The table cursor.  Or the PK cursor for WITHOUT ROWID */
  2218   2215     int iCol,       /* Index of the column to extract */
  2219         -  int regOut      /* Extract the valud into this register */
         2216  +  int regOut      /* Extract the value into this register */
  2220   2217   ){
  2221   2218     if( iCol<0 || iCol==pTab->iPKey ){
  2222   2219       sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
  2223   2220     }else{
  2224   2221       int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
  2225         -    sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
         2222  +    int x = iCol;
         2223  +    if( !HasRowid(pTab) ){
         2224  +      x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
         2225  +    }
         2226  +    sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
  2226   2227     }
  2227   2228     if( iCol>=0 ){
  2228   2229       sqlite3ColumnDefault(v, pTab, iCol, regOut);
  2229   2230     }
  2230   2231   }
  2231   2232   
  2232   2233   /*
................................................................................
  2964   2965         }
  2965   2966         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2966   2967         if( pExpr->affinity==OE_Ignore ){
  2967   2968           sqlite3VdbeAddOp4(
  2968   2969               v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
  2969   2970         }else{
  2970   2971           sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
  2971         -                              pExpr->affinity, pExpr->u.zToken, 0);
         2972  +                              pExpr->affinity, pExpr->u.zToken, 0, 0);
  2972   2973         }
  2973   2974   
  2974   2975         break;
  2975   2976       }
  2976   2977   #endif
  2977   2978     }
  2978   2979     sqlite3ReleaseTempReg(pParse, regFree1);

Changes to src/fkey.c.

   221    221       assert( nCol>1 );
   222    222       aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
   223    223       if( !aiCol ) return 1;
   224    224       *paiCol = aiCol;
   225    225     }
   226    226   
   227    227     for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
   228         -    if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ 
          228  +    if( pIdx->nKeyCol==nCol && pIdx->onError!=OE_None ){ 
   229    229         /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
   230    230         ** of columns. If each indexed column corresponds to a foreign key
   231    231         ** column of pFKey, then this index is a winner.  */
   232    232   
   233    233         if( zKey==0 ){
   234    234           /* If zKey is NULL, then this foreign key is implicitly mapped to 
   235    235           ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
................................................................................
   244    244         }else{
   245    245           /* If zKey is non-NULL, then this foreign key was declared to
   246    246           ** map to an explicit list of columns in table pParent. Check if this
   247    247           ** index matches those columns. Also, check that the index uses
   248    248           ** the default collation sequences for each column. */
   249    249           int i, j;
   250    250           for(i=0; i<nCol; i++){
   251         -          int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
          251  +          i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
   252    252             char *zDfltColl;                  /* Def. collation for column */
   253    253             char *zIdxCol;                    /* Name of indexed column */
   254    254   
   255    255             /* If the index uses a collation sequence that is different from
   256    256             ** the default collation sequence for the column, this index is
   257    257             ** unusable. Bail out early in this case.  */
   258    258             zDfltColl = pParent->aCol[iCol].zColl;
................................................................................
   375    375         sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
   376    376         sqlite3VdbeJumpHere(v, iMustBeInt);
   377    377         sqlite3ReleaseTempReg(pParse, regTemp);
   378    378       }else{
   379    379         int nCol = pFKey->nCol;
   380    380         int regTemp = sqlite3GetTempRange(pParse, nCol);
   381    381         int regRec = sqlite3GetTempReg(pParse);
   382         -      KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   383    382     
   384    383         sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
   385         -      sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
          384  +      sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
   386    385         for(i=0; i<nCol; i++){
   387    386           sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
   388    387         }
   389    388     
   390    389         /* If the parent table is the same as the child table, and we are about
   391    390         ** to increment the constraint-counter (i.e. this is an INSERT operation),
   392    391         ** then check if the row being inserted matches itself. If so, do not
................................................................................
   428    427     ){
   429    428       /* Special case: If this is an INSERT statement that will insert exactly
   430    429       ** one row into the table, raise a constraint immediately instead of
   431    430       ** incrementing a counter. This is necessary as the VM code is being
   432    431       ** generated for will not open a statement transaction.  */
   433    432       assert( nIncr==1 );
   434    433       sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
   435         -        OE_Abort, "foreign key constraint failed", P4_STATIC
   436         -    );
          434  +        OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
   437    435     }else{
   438    436       if( nIncr>0 && pFKey->isDeferred==0 ){
   439    437         sqlite3ParseToplevel(pParse)->mayAbort = 1;
   440    438       }
   441    439       sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
   442    440     }
   443    441   
   444    442     sqlite3VdbeResolveLabel(v, iOk);
   445    443     sqlite3VdbeAddOp1(v, OP_Close, iCur);
   446    444   }
   447    445   
          446  +
          447  +/*
          448  +** Return an Expr object that refers to a memory register corresponding
          449  +** to column iCol of table pTab.
          450  +**
          451  +** regBase is the first of an array of register that contains the data
          452  +** for pTab.  regBase itself holds the rowid.  regBase+1 holds the first
          453  +** column.  regBase+2 holds the second column, and so forth.
          454  +*/
          455  +static Expr *exprTableRegister(
          456  +  Parse *pParse,     /* Parsing and code generating context */
          457  +  Table *pTab,       /* The table whose content is at r[regBase]... */
          458  +  int regBase,       /* Contents of table pTab */
          459  +  i16 iCol           /* Which column of pTab is desired */
          460  +){
          461  +  Expr *pExpr;
          462  +  Column *pCol;
          463  +  const char *zColl;
          464  +  sqlite3 *db = pParse->db;
          465  +
          466  +  pExpr = sqlite3Expr(db, TK_REGISTER, 0);
          467  +  if( pExpr ){
          468  +    if( iCol>=0 && iCol!=pTab->iPKey ){
          469  +      pCol = &pTab->aCol[iCol];
          470  +      pExpr->iTable = regBase + iCol + 1;
          471  +      pExpr->affinity = pCol->affinity;
          472  +      zColl = pCol->zColl;
          473  +      if( zColl==0 ) zColl = db->pDfltColl->zName;
          474  +      pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
          475  +    }else{
          476  +      pExpr->iTable = regBase;
          477  +      pExpr->affinity = SQLITE_AFF_INTEGER;
          478  +    }
          479  +  }
          480  +  return pExpr;
          481  +}
          482  +
          483  +/*
          484  +** Return an Expr object that refers to column iCol of table pTab which
          485  +** has cursor iCur.
          486  +*/
          487  +static Expr *exprTableColumn(
          488  +  sqlite3 *db,      /* The database connection */
          489  +  Table *pTab,      /* The table whose column is desired */
          490  +  int iCursor,      /* The open cursor on the table */
          491  +  i16 iCol          /* The column that is wanted */
          492  +){
          493  +  Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
          494  +  if( pExpr ){
          495  +    pExpr->pTab = pTab;
          496  +    pExpr->iTable = iCursor;
          497  +    pExpr->iColumn = iCol;
          498  +  }
          499  +  return pExpr;
          500  +}
          501  +
   448    502   /*
   449    503   ** This function is called to generate code executed when a row is deleted
   450    504   ** from the parent table of foreign key constraint pFKey and, if pFKey is 
   451    505   ** deferred, when a row is inserted into the same table. When generating
   452    506   ** code for an SQL UPDATE operation, this function may be called twice -
   453    507   ** once to "delete" the old row and once to "insert" the new row.
   454    508   **
................................................................................
   456    510   ** table that correspond to the parent table row being deleted or inserted.
   457    511   ** For each child row found, one of the following actions is taken:
   458    512   **
   459    513   **   Operation | FK type   | Action taken
   460    514   **   --------------------------------------------------------------------------
   461    515   **   DELETE      immediate   Increment the "immediate constraint counter".
   462    516   **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
   463         -**                           throw a "foreign key constraint failed" exception.
          517  +**                           throw a "FOREIGN KEY constraint failed" exception.
   464    518   **
   465    519   **   INSERT      immediate   Decrement the "immediate constraint counter".
   466    520   **
   467    521   **   DELETE      deferred    Increment the "deferred constraint counter".
   468    522   **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
   469         -**                           throw a "foreign key constraint failed" exception.
          523  +**                           throw a "FOREIGN KEY constraint failed" exception.
   470    524   **
   471    525   **   INSERT      deferred    Decrement the "deferred constraint counter".
   472    526   **
   473    527   ** These operations are identified in the comment at the top of this file 
   474    528   ** (fkey.c) as "I.2" and "D.2".
   475    529   */
   476    530   static void fkScanChildren(
   477    531     Parse *pParse,                  /* Parse context */
   478         -  SrcList *pSrc,                  /* SrcList containing the table to scan */
   479         -  Table *pTab,
   480         -  Index *pIdx,                    /* Foreign key index */
   481         -  FKey *pFKey,                    /* Foreign key relationship */
          532  +  SrcList *pSrc,                  /* The child table to be scanned */
          533  +  Table *pTab,                    /* The parent table */
          534  +  Index *pIdx,                    /* Index on parent covering the foreign key */
          535  +  FKey *pFKey,                    /* The foreign key linking pSrc to pTab */
   482    536     int *aiCol,                     /* Map from pIdx cols to child table cols */
   483         -  int regData,                    /* Referenced table data starts here */
          537  +  int regData,                    /* Parent row data starts here */
   484    538     int nIncr                       /* Amount to increment deferred counter by */
   485    539   ){
   486    540     sqlite3 *db = pParse->db;       /* Database handle */
   487    541     int i;                          /* Iterator variable */
   488    542     Expr *pWhere = 0;               /* WHERE clause to scan with */
   489    543     NameContext sNameContext;       /* Context used to resolve WHERE clause */
   490    544     WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
   491    545     int iFkIfZero = 0;              /* Address of OP_FkIfZero */
   492    546     Vdbe *v = sqlite3GetVdbe(pParse);
   493    547   
   494         -  assert( !pIdx || pIdx->pTable==pTab );
          548  +  assert( pIdx==0 || pIdx->pTable==pTab );
          549  +  assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
          550  +  assert( pIdx!=0 || pFKey->nCol==1 );
   495    551   
   496    552     if( nIncr<0 ){
   497    553       iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
   498    554     }
   499    555   
   500    556     /* Create an Expr object representing an SQL expression like:
   501    557     **
................................................................................
   505    561     ** the parent key columns. The affinity of the parent key column should
   506    562     ** be applied to each child key value before the comparison takes place.
   507    563     */
   508    564     for(i=0; i<pFKey->nCol; i++){
   509    565       Expr *pLeft;                  /* Value from parent table row */
   510    566       Expr *pRight;                 /* Column ref to child table */
   511    567       Expr *pEq;                    /* Expression (pLeft = pRight) */
   512         -    int iCol;                     /* Index of column in child table */ 
          568  +    i16 iCol;                     /* Index of column in child table */ 
   513    569       const char *zCol;             /* Name of column in child table */
   514    570   
   515         -    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
   516         -    if( pLeft ){
   517         -      /* Set the collation sequence and affinity of the LHS of each TK_EQ
   518         -      ** expression to the parent key column defaults.  */
   519         -      if( pIdx ){
   520         -        Column *pCol;
   521         -        const char *zColl;
   522         -        iCol = pIdx->aiColumn[i];
   523         -        pCol = &pTab->aCol[iCol];
   524         -        if( pTab->iPKey==iCol ) iCol = -1;
   525         -        pLeft->iTable = regData+iCol+1;
   526         -        pLeft->affinity = pCol->affinity;
   527         -        zColl = pCol->zColl;
   528         -        if( zColl==0 ) zColl = db->pDfltColl->zName;
   529         -        pLeft = sqlite3ExprAddCollateString(pParse, pLeft, zColl);
   530         -      }else{
   531         -        pLeft->iTable = regData;
   532         -        pLeft->affinity = SQLITE_AFF_INTEGER;
   533         -      }
   534         -    }
          571  +    iCol = pIdx ? pIdx->aiColumn[i] : -1;
          572  +    pLeft = exprTableRegister(pParse, pTab, regData, iCol);
   535    573       iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
   536    574       assert( iCol>=0 );
   537    575       zCol = pFKey->pFrom->aCol[iCol].zName;
   538    576       pRight = sqlite3Expr(db, TK_ID, zCol);
   539    577       pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
   540    578       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
   541    579     }
   542    580   
   543         -  /* If the child table is the same as the parent table, and this scan
   544         -  ** is taking place as part of a DELETE operation (operation D.2), omit the
   545         -  ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE 
   546         -  ** clause, where $rowid is the rowid of the row being deleted.  */
          581  +  /* If the child table is the same as the parent table, then add terms
          582  +  ** to the WHERE clause that prevent this entry from being scanned.
          583  +  ** The added WHERE clause terms are like this:
          584  +  **
          585  +  **     $current_rowid!=rowid
          586  +  **     NOT( $current_a==a AND $current_b==b AND ... )
          587  +  **
          588  +  ** The first form is used for rowid tables.  The second form is used
          589  +  ** for WITHOUT ROWID tables.  In the second form, the primary key is
          590  +  ** (a,b,...)
          591  +  */
   547    592     if( pTab==pFKey->pFrom && nIncr>0 ){
   548         -    Expr *pEq;                    /* Expression (pLeft = pRight) */
          593  +    Expr *pNe;                    /* Expression (pLeft != pRight) */
   549    594       Expr *pLeft;                  /* Value from parent table row */
   550    595       Expr *pRight;                 /* Column ref to child table */
   551         -    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
   552         -    pRight = sqlite3Expr(db, TK_COLUMN, 0);
   553         -    if( pLeft && pRight ){
   554         -      pLeft->iTable = regData;
   555         -      pLeft->affinity = SQLITE_AFF_INTEGER;
   556         -      pRight->iTable = pSrc->a[0].iCursor;
   557         -      pRight->iColumn = -1;
          596  +    if( HasRowid(pTab) ){
          597  +      pLeft = exprTableRegister(pParse, pTab, regData, -1);
          598  +      pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
          599  +      pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
          600  +    }else{
          601  +      Expr *pEq, *pAll = 0;
          602  +      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
          603  +      for(i=0; i<pPk->nKeyCol; i++){
          604  +        i16 iCol = pIdx->aiColumn[i];
          605  +        pLeft = exprTableRegister(pParse, pTab, regData, iCol);
          606  +        pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
          607  +        pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
          608  +        pAll = sqlite3ExprAnd(db, pAll, pEq);
          609  +      }
          610  +      pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
   558    611       }
   559         -    pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
   560         -    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
          612  +    pWhere = sqlite3ExprAnd(db, pWhere, pNe);
   561    613     }
   562    614   
   563    615     /* Resolve the references in the WHERE clause. */
   564    616     memset(&sNameContext, 0, sizeof(NameContext));
   565    617     sNameContext.pSrcList = pSrc;
   566    618     sNameContext.pParse = pParse;
   567    619     sqlite3ResolveExprNames(&sNameContext, pWhere);
................................................................................
   583    635     sqlite3ExprDelete(db, pWhere);
   584    636     if( iFkIfZero ){
   585    637       sqlite3VdbeJumpHere(v, iFkIfZero);
   586    638     }
   587    639   }
   588    640   
   589    641   /*
   590         -** This function returns a pointer to the head of a linked list of FK
   591         -** constraints for which table pTab is the parent table. For example,
          642  +** This function returns a linked list of FKey objects (connected by
          643  +** FKey.pNextTo) holding all children of table pTab.  For example,
   592    644   ** given the following schema:
   593    645   **
   594    646   **   CREATE TABLE t1(a PRIMARY KEY);
   595    647   **   CREATE TABLE t2(b REFERENCES t1(a);
   596    648   **
   597    649   ** Calling this function with table "t1" as an argument returns a pointer
   598    650   ** to the FKey structure representing the foreign key constraint on table
................................................................................
   675    727       ** If the SQLITE_DeferFKs flag is set, then this is not required, as
   676    728       ** the statement transaction will not be rolled back even if FK
   677    729       ** constraints are violated.
   678    730       */
   679    731       if( (db->flags & SQLITE_DeferFKs)==0 ){
   680    732         sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
   681    733         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
   682         -          OE_Abort, "foreign key constraint failed", P4_STATIC
   683         -      );
          734  +          OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
   684    735       }
   685    736   
   686    737       if( iSkip ){
   687    738         sqlite3VdbeResolveLabel(v, iSkip);
   688    739       }
   689    740     }
   690    741   }
................................................................................
   886    937         ** be found, adding the child row has violated the FK constraint. */ 
   887    938         fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
   888    939       }
   889    940   
   890    941       sqlite3DbFree(db, aiFree);
   891    942     }
   892    943   
   893         -  /* Loop through all the foreign key constraints that refer to this table */
          944  +  /* Loop through all the foreign key constraints that refer to this table.
          945  +  ** (the "child" constraints) */
   894    946     for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
   895    947       Index *pIdx = 0;              /* Foreign key index for pFKey */
   896    948       SrcList *pSrc;
   897    949       int *aiCol = 0;
   898    950   
   899    951       if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
   900    952         continue;
................................................................................
   911    963   
   912    964       if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
   913    965         if( !isIgnoreErrors || db->mallocFailed ) return;
   914    966         continue;
   915    967       }
   916    968       assert( aiCol || pFKey->nCol==1 );
   917    969   
   918         -    /* Create a SrcList structure containing a single table (the table 
   919         -    ** the foreign key that refers to this table is attached to). This
   920         -    ** is required for the sqlite3WhereXXX() interface.  */
          970  +    /* Create a SrcList structure containing the child table.  We need the
          971  +    ** child table as a SrcList for sqlite3WhereBegin() */
   921    972       pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
   922    973       if( pSrc ){
   923    974         struct SrcList_item *pItem = pSrc->a;
   924    975         pItem->pTab = pFKey->pFrom;
   925    976         pItem->zName = pFKey->pFrom->zName;
   926    977         pItem->pTab->nRef++;
   927    978         pItem->iCursor = pParse->nTab++;
................................................................................
   962   1013       for(p=pTab->pFKey; p; p=p->pNextFrom){
   963   1014         for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
   964   1015       }
   965   1016       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
   966   1017         Index *pIdx = 0;
   967   1018         sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
   968   1019         if( pIdx ){
   969         -        for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
         1020  +        for(i=0; i<pIdx->nKeyCol; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
   970   1021         }
   971   1022       }
   972   1023     }
   973   1024     return mask;
   974   1025   }
   975   1026   
   976   1027   
................................................................................
  1154   1205   
  1155   1206       if( action==OE_Restrict ){
  1156   1207         Token tFrom;
  1157   1208         Expr *pRaise; 
  1158   1209   
  1159   1210         tFrom.z = zFrom;
  1160   1211         tFrom.n = nFrom;
  1161         -      pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
         1212  +      pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
  1162   1213         if( pRaise ){
  1163   1214           pRaise->affinity = OE_Abort;
  1164   1215         }
  1165   1216         pSelect = sqlite3SelectNew(pParse, 
  1166   1217             sqlite3ExprListAppend(pParse, 0, pRaise),
  1167   1218             sqlite3SrcListAppend(db, 0, &tFrom, 0),
  1168   1219             pWhere,

Changes to src/func.c.

   134    134     assert( argc==1 );
   135    135     UNUSED_PARAMETER(argc);
   136    136     switch( sqlite3_value_type(argv[0]) ){
   137    137       case SQLITE_INTEGER: {
   138    138         i64 iVal = sqlite3_value_int64(argv[0]);
   139    139         if( iVal<0 ){
   140    140           if( (iVal<<1)==0 ){
   141         -          /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
   142         -          ** abs(X) throws an integer overflow error since there is no
          141  +          /* IMP: R-31676-45509 If X is the integer -9223372036854775808
          142  +          ** then abs(X) throws an integer overflow error since there is no
   143    143             ** equivalent positive 64-bit two complement value. */
   144    144             sqlite3_result_error(context, "integer overflow", -1);
   145    145             return;
   146    146           }
   147    147           iVal = -iVal;
   148    148         } 
   149    149         sqlite3_result_int64(context, iVal);

Changes to src/insert.c.

    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle INSERT statements in SQLite.
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   
    17     17   /*
    18         -** Generate code that will open a table for reading.
           18  +** Generate code that will 
           19  +**
           20  +**   (1) acquire a lock for table pTab then
           21  +**   (2) open pTab as cursor iCur.
           22  +**
           23  +** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
           24  +** for that table that is actually opened.
    19     25   */
    20     26   void sqlite3OpenTable(
    21         -  Parse *p,       /* Generate code into this VDBE */
           27  +  Parse *pParse,  /* Generate code into this VDBE */
    22     28     int iCur,       /* The cursor number of the table */
    23     29     int iDb,        /* The database index in sqlite3.aDb[] */
    24     30     Table *pTab,    /* The table to be opened */
    25     31     int opcode      /* OP_OpenRead or OP_OpenWrite */
    26     32   ){
    27     33     Vdbe *v;
    28     34     assert( !IsVirtual(pTab) );
    29         -  v = sqlite3GetVdbe(p);
           35  +  v = sqlite3GetVdbe(pParse);
    30     36     assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
    31         -  sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
    32         -  sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
    33         -  sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
    34         -  VdbeComment((v, "%s", pTab->zName));
           37  +  sqlite3TableLock(pParse, iDb, pTab->tnum, 
           38  +                   (opcode==OP_OpenWrite)?1:0, pTab->zName);
           39  +  if( HasRowid(pTab) ){
           40  +    sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
           41  +    VdbeComment((v, "%s", pTab->zName));
           42  +  }else{
           43  +    Index *pPk = sqlite3PrimaryKeyIndex(pTab);
           44  +    assert( pPk!=0 );
           45  +    assert( pPk->tnum=pTab->tnum );
           46  +    sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
           47  +    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
           48  +    VdbeComment((v, "%s", pTab->zName));
           49  +  }
    35     50   }
    36     51   
    37     52   /*
    38     53   ** Return a pointer to the column affinity string associated with index
    39     54   ** pIdx. A column affinity string has one character for each column in 
    40     55   ** the table, according to the affinity of the column:
    41     56   **
................................................................................
    63     78       ** The column affinity string will eventually be deleted by
    64     79       ** sqliteDeleteIndex() when the Index structure itself is cleaned
    65     80       ** up.
    66     81       */
    67     82       int n;
    68     83       Table *pTab = pIdx->pTable;
    69     84       sqlite3 *db = sqlite3VdbeDb(v);
    70         -    pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
           85  +    pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
    71     86       if( !pIdx->zColAff ){
    72     87         db->mallocFailed = 1;
    73     88         return 0;
    74     89       }
    75     90       for(n=0; n<pIdx->nColumn; n++){
    76         -      pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
           91  +      i16 x = pIdx->aiColumn[n];
           92  +      pIdx->zColAff[n] = x<0 ? SQLITE_AFF_INTEGER : pTab->aCol[x].affinity;
    77     93       }
    78         -    pIdx->zColAff[n++] = SQLITE_AFF_INTEGER;
    79     94       pIdx->zColAff[n] = 0;
    80     95     }
    81     96    
    82     97     return pIdx->zColAff;
    83     98   }
    84     99   
    85    100   /*
................................................................................
   417    432     Table *pDest,         /* The table we are inserting into */
   418    433     Select *pSelect,      /* A SELECT statement to use as the data source */
   419    434     int onError,          /* How to handle constraint errors */
   420    435     int iDbDest           /* The database of pDest */
   421    436   );
   422    437   
   423    438   /*
   424         -** This routine is call to handle SQL of the following forms:
          439  +** This routine is called to handle SQL of the following forms:
   425    440   **
   426    441   **    insert into TABLE (IDLIST) values(EXPRLIST)
   427    442   **    insert into TABLE (IDLIST) select
   428    443   **
   429    444   ** The IDLIST following the table name is always optional.  If omitted,
   430    445   ** then a list of all columns for the table is substituted.  The IDLIST
   431    446   ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
................................................................................
   432    447   **
   433    448   ** The pList parameter holds EXPRLIST in the first form of the INSERT
   434    449   ** statement above, and pSelect is NULL.  For the second form, pList is
   435    450   ** NULL and pSelect is a pointer to the select statement used to generate
   436    451   ** data for the insert.
   437    452   **
   438    453   ** The code generated follows one of four templates.  For a simple
   439         -** select with data coming from a VALUES clause, the code executes
          454  +** insert with data coming from a VALUES clause, the code executes
   440    455   ** once straight down through.  Pseudo-code follows (we call this
   441    456   ** the "1st template"):
   442    457   **
   443    458   **         open write cursor to <table> and its indices
   444         -**         puts VALUES clause expressions onto the stack
          459  +**         put VALUES clause expressions into registers
   445    460   **         write the resulting record into <table>
   446    461   **         cleanup
   447    462   **
   448    463   ** The three remaining templates assume the statement is of the form
   449    464   **
   450    465   **   INSERT INTO <table> SELECT ...
   451    466   **
................................................................................
   535    550     char *zTab;           /* Name of the table into which we are inserting */
   536    551     const char *zDb;      /* Name of the database holding this table */
   537    552     int i, j, idx;        /* Loop counters */
   538    553     Vdbe *v;              /* Generate code into this virtual machine */
   539    554     Index *pIdx;          /* For looping over indices of the table */
   540    555     int nColumn;          /* Number of columns in the data */
   541    556     int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
   542         -  int baseCur = 0;      /* VDBE Cursor number for pTab */
   543         -  int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
          557  +  int iDataCur = 0;     /* VDBE cursor that is the main data repository */
          558  +  int iIdxCur = 0;      /* First index cursor */
          559  +  int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
   544    560     int endOfLoop;        /* Label for the end of the insertion loop */
   545    561     int useTempTable = 0; /* Store SELECT results in intermediate table */
   546    562     int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
   547    563     int addrInsTop = 0;   /* Jump to label "D" */
   548    564     int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
   549    565     int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
   550    566     SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
   551    567     int iDb;              /* Index of database holding TABLE */
   552    568     Db *pDb;              /* The database containing table being inserted into */
   553    569     int appendFlag = 0;   /* True if the insert is likely to be an append */
          570  +  int withoutRowid;     /* 0 for normal table.  1 for WITHOUT ROWID table */
   554    571   
   555    572     /* Register allocations */
   556    573     int regFromSelect = 0;/* Base register for data coming from SELECT */
   557    574     int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
   558    575     int regRowCount = 0;  /* Memory cell used for the row counter */
   559    576     int regIns;           /* Block of regs holding rowid+data being inserted */
   560    577     int regRowid;         /* registers holding insert rowid */
................................................................................
   586    603     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   587    604     assert( iDb<db->nDb );
   588    605     pDb = &db->aDb[iDb];
   589    606     zDb = pDb->zName;
   590    607     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
   591    608       goto insert_cleanup;
   592    609     }
          610  +  withoutRowid = !HasRowid(pTab);
   593    611   
   594    612     /* Figure out if we have any triggers and if the table being
   595    613     ** inserted into is a view
   596    614     */
   597    615   #ifndef SQLITE_OMIT_TRIGGER
   598    616     pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
   599    617     isView = pTab->pSelect!=0;
................................................................................
   605    623   #ifdef SQLITE_OMIT_VIEW
   606    624   # undef isView
   607    625   # define isView 0
   608    626   #endif
   609    627     assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
   610    628   
   611    629     /* If pTab is really a view, make sure it has been initialized.
   612         -  ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 
   613         -  ** module table).
          630  +  ** ViewGetColumnNames() is a no-op if pTab is not a view.
   614    631     */
   615    632     if( sqlite3ViewGetColumnNames(pParse, pTab) ){
   616    633       goto insert_cleanup;
   617    634     }
   618    635   
   619         -  /* Ensure that:
   620         -  *  (a) the table is not read-only, 
   621         -  *  (b) that if it is a view then ON INSERT triggers exist
          636  +  /* Cannot insert into a read-only table.
   622    637     */
   623    638     if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
   624    639       goto insert_cleanup;
   625    640     }
   626    641   
   627    642     /* Allocate a VDBE
   628    643     */
................................................................................
   655    670   
   656    671     /* Figure out how many columns of data are supplied.  If the data
   657    672     ** is coming from a SELECT statement, then generate a co-routine that
   658    673     ** produces a single row of the SELECT on each invocation.  The
   659    674     ** co-routine is the common header to the 3rd and 4th templates.
   660    675     */
   661    676     if( pSelect ){
   662         -    /* Data is coming from a SELECT.  Generate a co-routine to run that
   663         -    ** SELECT. */
          677  +    /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
   664    678       int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
   665    679       if( rc ) goto insert_cleanup;
   666    680   
   667    681       regEof = dest.iSDParm + 1;
   668    682       regFromSelect = dest.iSdst;
   669    683       assert( pSelect->pEList );
   670    684       nColumn = pSelect->pEList->nExpr;
   671    685       assert( dest.nSdst==nColumn );
   672    686   
   673    687       /* Set useTempTable to TRUE if the result of the SELECT statement
   674    688       ** should be written into a temporary table (template 4).  Set to
   675         -    ** FALSE if each* row of the SELECT can be written directly into
          689  +    ** FALSE if each output row of the SELECT can be written directly into
   676    690       ** the destination table (template 3).
   677    691       **
   678    692       ** A temp table must be used if the table being updated is also one
   679    693       ** of the tables being read by the SELECT statement.  Also use a 
   680    694       ** temp table in the case of row triggers.
   681    695       */
   682    696       if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
................................................................................
   751    765     }
   752    766   
   753    767     /* If the INSERT statement included an IDLIST term, then make sure
   754    768     ** all elements of the IDLIST really are columns of the table and 
   755    769     ** remember the column indices.
   756    770     **
   757    771     ** If the table has an INTEGER PRIMARY KEY column and that column
   758         -  ** is named in the IDLIST, then record in the keyColumn variable
   759         -  ** the index into IDLIST of the primary key column.  keyColumn is
          772  +  ** is named in the IDLIST, then record in the ipkColumn variable
          773  +  ** the index into IDLIST of the primary key column.  ipkColumn is
   760    774     ** the index of the primary key as it appears in IDLIST, not as
   761         -  ** is appears in the original table.  (The index of the primary
   762         -  ** key in the original table is pTab->iPKey.)
          775  +  ** is appears in the original table.  (The index of the INTEGER
          776  +  ** PRIMARY KEY in the original table is pTab->iPKey.)
   763    777     */
   764    778     if( pColumn ){
   765    779       for(i=0; i<pColumn->nId; i++){
   766    780         pColumn->a[i].idx = -1;
   767    781       }
   768    782       for(i=0; i<pColumn->nId; i++){
   769    783         for(j=0; j<pTab->nCol; j++){
   770    784           if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
   771    785             pColumn->a[i].idx = j;
   772    786             if( j==pTab->iPKey ){
   773         -            keyColumn = i;
          787  +            ipkColumn = i;  assert( !withoutRowid );
   774    788             }
   775    789             break;
   776    790           }
   777    791         }
   778    792         if( j>=pTab->nCol ){
   779         -        if( sqlite3IsRowid(pColumn->a[i].zName) ){
   780         -          keyColumn = i;
          793  +        if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
          794  +          ipkColumn = i;
   781    795           }else{
   782    796             sqlite3ErrorMsg(pParse, "table %S has no column named %s",
   783    797                 pTabList, 0, pColumn->a[i].zName);
   784    798             pParse->checkSchema = 1;
   785    799             goto insert_cleanup;
   786    800           }
   787    801         }
   788    802       }
   789    803     }
   790    804   
   791    805     /* If there is no IDLIST term but the table has an integer primary
   792         -  ** key, the set the keyColumn variable to the primary key column index
   793         -  ** in the original table definition.
          806  +  ** key, the set the ipkColumn variable to the integer primary key 
          807  +  ** column index in the original table definition.
   794    808     */
   795    809     if( pColumn==0 && nColumn>0 ){
   796         -    keyColumn = pTab->iPKey;
          810  +    ipkColumn = pTab->iPKey;
   797    811     }
   798    812       
   799    813     /* Initialize the count of rows to be inserted
   800    814     */
   801    815     if( db->flags & SQLITE_CountRows ){
   802    816       regRowCount = ++pParse->nMem;
   803    817       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
   804    818     }
   805    819   
   806    820     /* If this is not a view, open the table and and all indices */
   807    821     if( !isView ){
   808    822       int nIdx;
   809         -
   810         -    baseCur = pParse->nTab;
   811         -    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
          823  +    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1,
          824  +                                      &iDataCur, &iIdxCur);
   812    825       aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
   813    826       if( aRegIdx==0 ){
   814    827         goto insert_cleanup;
   815    828       }
   816    829       for(i=0; i<nIdx; i++){
   817    830         aRegIdx[i] = ++pParse->nMem;
   818    831       }
................................................................................
   864    877   
   865    878       /* build the NEW.* reference row.  Note that if there is an INTEGER
   866    879       ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
   867    880       ** translated into a unique ID for the row.  But on a BEFORE trigger,
   868    881       ** we do not know what the unique ID will be (because the insert has
   869    882       ** not happened yet) so we substitute a rowid of -1
   870    883       */
   871         -    if( keyColumn<0 ){
          884  +    if( ipkColumn<0 ){
   872    885         sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
   873    886       }else{
   874    887         int j1;
          888  +      assert( !withoutRowid );
   875    889         if( useTempTable ){
   876         -        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
          890  +        sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
   877    891         }else{
   878    892           assert( pSelect==0 );  /* Otherwise useTempTable is true */
   879         -        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
          893  +        sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
   880    894         }
   881    895         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
   882    896         sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
   883    897         sqlite3VdbeJumpHere(v, j1);
   884    898         sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
   885    899       }
   886    900   
................................................................................
   922    936       /* Fire BEFORE or INSTEAD OF triggers */
   923    937       sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
   924    938           pTab, regCols-pTab->nCol-1, onError, endOfLoop);
   925    939   
   926    940       sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
   927    941     }
   928    942   
   929         -  /* Push the record number for the new entry onto the stack.  The
   930         -  ** record number is a randomly generate integer created by NewRowid
   931         -  ** except when the table has an INTEGER PRIMARY KEY column, in which
   932         -  ** case the record number is the same as that column. 
          943  +  /* Compute the content of the next row to insert into a range of
          944  +  ** registers beginning at regIns.
   933    945     */
   934    946     if( !isView ){
   935    947       if( IsVirtual(pTab) ){
   936    948         /* The row that the VUpdate opcode will delete: none */
   937    949         sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
   938    950       }
   939         -    if( keyColumn>=0 ){
          951  +    if( ipkColumn>=0 ){
   940    952         if( useTempTable ){
   941         -        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
          953  +        sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
   942    954         }else if( pSelect ){
   943         -        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
          955  +        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+ipkColumn, regRowid);
   944    956         }else{
   945    957           VdbeOp *pOp;
   946         -        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
          958  +        sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
   947    959           pOp = sqlite3VdbeGetOp(v, -1);
   948    960           if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
   949    961             appendFlag = 1;
   950    962             pOp->opcode = OP_NewRowid;
   951         -          pOp->p1 = baseCur;
          963  +          pOp->p1 = iDataCur;
   952    964             pOp->p2 = regRowid;
   953    965             pOp->p3 = regAutoinc;
   954    966           }
   955    967         }
   956    968         /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
   957    969         ** to generate a unique primary key value.
   958    970         */
   959    971         if( !appendFlag ){
   960    972           int j1;
   961    973           if( !IsVirtual(pTab) ){
   962    974             j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
   963         -          sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
          975  +          sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
   964    976             sqlite3VdbeJumpHere(v, j1);
   965    977           }else{
   966    978             j1 = sqlite3VdbeCurrentAddr(v);
   967    979             sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
   968    980           }
   969    981           sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
   970    982         }
   971         -    }else if( IsVirtual(pTab) ){
          983  +    }else if( IsVirtual(pTab) || withoutRowid ){
   972    984         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
   973    985       }else{
   974         -      sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
          986  +      sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
   975    987         appendFlag = 1;
   976    988       }
   977    989       autoIncStep(pParse, regAutoinc, regRowid);
   978    990   
   979         -    /* Push onto the stack, data for all columns of the new entry, beginning
          991  +    /* Compute data for all columns of the new entry, beginning
   980    992       ** with the first column.
   981    993       */
   982    994       nHidden = 0;
   983    995       for(i=0; i<pTab->nCol; i++){
   984    996         int iRegStore = regRowid+1+i;
   985    997         if( i==pTab->iPKey ){
   986    998           /* The value of the INTEGER PRIMARY KEY column is always a NULL.
   987         -        ** Whenever this column is read, the record number will be substituted
   988         -        ** in its place.  So will fill this column with a NULL to avoid
          999  +        ** Whenever this column is read, the rowid will be substituted
         1000  +        ** in its place.  Hence, fill this column with a NULL to avoid
   989   1001           ** taking up data space with information that will never be used. */
   990   1002           sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
   991   1003           continue;
   992   1004         }
   993   1005         if( pColumn==0 ){
   994   1006           if( IsHiddenColumn(&pTab->aCol[i]) ){
   995   1007             assert( IsVirtual(pTab) );
................................................................................
  1024   1036         sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
  1025   1037         sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
  1026   1038         sqlite3MayAbort(pParse);
  1027   1039       }else
  1028   1040   #endif
  1029   1041       {
  1030   1042         int isReplace;    /* Set to true if constraints may cause a replace */
  1031         -      sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
  1032         -          keyColumn>=0, 0, onError, endOfLoop, &isReplace
         1043  +      sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
         1044  +          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
  1033   1045         );
  1034   1046         sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
  1035         -      sqlite3CompleteInsertion(
  1036         -          pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
  1037         -      );
         1047  +      sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
         1048  +                               regIns, aRegIdx, 0, appendFlag, isReplace==0);
  1038   1049       }
  1039   1050     }
  1040   1051   
  1041   1052     /* Update the count of rows that are inserted
  1042   1053     */
  1043   1054     if( (db->flags & SQLITE_CountRows)!=0 ){
  1044   1055       sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
................................................................................
  1061   1072     }else if( pSelect ){
  1062   1073       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
  1063   1074       sqlite3VdbeJumpHere(v, addrInsTop);
  1064   1075     }
  1065   1076   
  1066   1077     if( !IsVirtual(pTab) && !isView ){
  1067   1078       /* Close all tables opened */
  1068         -    sqlite3VdbeAddOp1(v, OP_Close, baseCur);
  1069         -    for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
  1070         -      sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
         1079  +    if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
         1080  +    for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
         1081  +      sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur);
  1071   1082       }
  1072   1083     }
  1073   1084   
  1074   1085   insert_end:
  1075   1086     /* Update the sqlite_sequence table by storing the content of the
  1076   1087     ** maximum rowid counter values recorded while inserting into
  1077   1088     ** autoincrement tables.
................................................................................
  1108   1119   #ifdef pTrigger
  1109   1120    #undef pTrigger
  1110   1121   #endif
  1111   1122   #ifdef tmask
  1112   1123    #undef tmask
  1113   1124   #endif
  1114   1125   
  1115         -
  1116   1126   /*
  1117         -** Generate code to do constraint checks prior to an INSERT or an UPDATE.
  1118         -**
  1119         -** The input is a range of consecutive registers as follows:
  1120         -**
  1121         -**    1.  The rowid of the row after the update.
  1122         -**
  1123         -**    2.  The data in the first column of the entry after the update.
  1124         -**
  1125         -**    i.  Data from middle columns...
  1126         -**
  1127         -**    N.  The data in the last column of the entry after the update.
  1128         -**
  1129         -** The regRowid parameter is the index of the register containing (1).
  1130         -**
  1131         -** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
  1132         -** the address of a register containing the rowid before the update takes
  1133         -** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
  1134         -** is false, indicating an INSERT statement, then a non-zero rowidChng 
  1135         -** indicates that the rowid was explicitly specified as part of the
  1136         -** INSERT statement. If rowidChng is false, it means that  the rowid is
  1137         -** computed automatically in an insert or that the rowid value is not 
  1138         -** modified by an update.
  1139         -**
  1140         -** The code generated by this routine store new index entries into
         1127  +** Generate code to do constraint checks prior to an INSERT or an UPDATE
         1128  +** on table pTab.
         1129  +**
         1130  +** The regNewData parameter is the first register in a range that contains
         1131  +** the data to be inserted or the data after the update.  There will be
         1132  +** pTab->nCol+1 registers in this range.  The first register (the one
         1133  +** that regNewData points to) will contain the new rowid, or NULL in the
         1134  +** case of a WITHOUT ROWID table.  The second register in the range will
         1135  +** contain the content of the first table column.  The third register will
         1136  +** contain the content of the second table column.  And so forth.
         1137  +**
         1138  +** The regOldData parameter is similar to regNewData except that it contains
         1139  +** the data prior to an UPDATE rather than afterwards.  regOldData is zero
         1140  +** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
         1141  +** checking regOldData for zero.
         1142  +**
         1143  +** For an UPDATE, the pkChng boolean is true if the true primary key (the
         1144  +** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
         1145  +** might be modified by the UPDATE.  If pkChng is false, then the key of
         1146  +** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
         1147  +**
         1148  +** For an INSERT, the pkChng boolean indicates whether or not the rowid
         1149  +** was explicitly specified as part of the INSERT statement.  If pkChng
         1150  +** is zero, it means that the either rowid is computed automatically or
         1151  +** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
         1152  +** pkChng will only be true if the INSERT statement provides an integer
         1153  +** value for either the rowid column or its INTEGER PRIMARY KEY alias.
         1154  +**
         1155  +** The code generated by this routine will store new index entries into
  1141   1156   ** registers identified by aRegIdx[].  No index entry is created for
  1142   1157   ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
  1143   1158   ** the same as the order of indices on the linked list of indices
  1144         -** attached to the table.
         1159  +** at pTab->pIndex.
         1160  +**
         1161  +** The caller must have already opened writeable cursors on the main
         1162  +** table and all applicable indices (that is to say, all indices for which
         1163  +** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
         1164  +** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
         1165  +** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
         1166  +** for the first index in the pTab->pIndex list.  Cursors for other indices
         1167  +** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
  1145   1168   **
  1146   1169   ** This routine also generates code to check constraints.  NOT NULL,
  1147   1170   ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
  1148   1171   ** then the appropriate action is performed.  There are five possible
  1149   1172   ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
  1150   1173   **
  1151   1174   **  Constraint type  Action       What Happens
  1152   1175   **  ---------------  ----------   ----------------------------------------
  1153   1176   **  any              ROLLBACK     The current transaction is rolled back and
  1154         -**                                sqlite3_exec() returns immediately with a
         1177  +**                                sqlite3_step() returns immediately with a
  1155   1178   **                                return code of SQLITE_CONSTRAINT.
  1156   1179   **
  1157   1180   **  any              ABORT        Back out changes from the current command
  1158   1181   **                                only (do not do a complete rollback) then
  1159         -**                                cause sqlite3_exec() to return immediately
         1182  +**                                cause sqlite3_step() to return immediately
  1160   1183   **                                with SQLITE_CONSTRAINT.
  1161   1184   **
  1162         -**  any              FAIL         Sqlite3_exec() returns immediately with a
         1185  +**  any              FAIL         Sqlite3_step() returns immediately with a
  1163   1186   **                                return code of SQLITE_CONSTRAINT.  The
  1164   1187   **                                transaction is not rolled back and any
  1165         -**                                prior changes are retained.
         1188  +**                                changes to prior rows are retained.
  1166   1189   **
  1167         -**  any              IGNORE       The record number and data is popped from
  1168         -**                                the stack and there is an immediate jump
  1169         -**                                to label ignoreDest.
         1190  +**  any              IGNORE       The attempt in insert or update the current
         1191  +**                                row is skipped, without throwing an error.
         1192  +**                                Processing continues with the next row.
         1193  +**                                (There is an immediate jump to ignoreDest.)
  1170   1194   **
  1171   1195   **  NOT NULL         REPLACE      The NULL value is replace by the default
  1172   1196   **                                value for that column.  If the default value
  1173   1197   **                                is NULL, the action is the same as ABORT.
  1174   1198   **
  1175   1199   **  UNIQUE           REPLACE      The other row that conflicts with the row
  1176   1200   **                                being inserted is removed.
................................................................................
  1177   1201   **
  1178   1202   **  CHECK            REPLACE      Illegal.  The results in an exception.
  1179   1203   **
  1180   1204   ** Which action to take is determined by the overrideError parameter.
  1181   1205   ** Or if overrideError==OE_Default, then the pParse->onError parameter
  1182   1206   ** is used.  Or if pParse->onError==OE_Default then the onError value
  1183   1207   ** for the constraint is used.
  1184         -**
  1185         -** The calling routine must open a read/write cursor for pTab with
  1186         -** cursor number "baseCur".  All indices of pTab must also have open
  1187         -** read/write cursors with cursor number baseCur+i for the i-th cursor.
  1188         -** Except, if there is no possibility of a REPLACE action then
  1189         -** cursors do not need to be open for indices where aRegIdx[i]==0.
  1190   1208   */
  1191   1209   void sqlite3GenerateConstraintChecks(
  1192         -  Parse *pParse,      /* The parser context */
  1193         -  Table *pTab,        /* the table into which we are inserting */
  1194         -  int baseCur,        /* Index of a read/write cursor pointing at pTab */
  1195         -  int regRowid,       /* Index of the range of input registers */
  1196         -  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
  1197         -  int rowidChng,      /* True if the rowid might collide with existing entry */
  1198         -  int isUpdate,       /* True for UPDATE, False for INSERT */
  1199         -  int overrideError,  /* Override onError to this if not OE_Default */
  1200         -  int ignoreDest,     /* Jump to this label on an OE_Ignore resolution */
  1201         -  int *pbMayReplace   /* OUT: Set to true if constraint may cause a replace */
         1210  +  Parse *pParse,       /* The parser context */
         1211  +  Table *pTab,         /* The table being inserted or updated */
         1212  +  int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
         1213  +  int iDataCur,        /* Canonical data cursor (main table or PK index) */
         1214  +  int iIdxCur,         /* First index cursor */
         1215  +  int regNewData,      /* First register in a range holding values to insert */
         1216  +  int regOldData,      /* Previous content.  0 for INSERTs */
         1217  +  u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
         1218  +  u8 overrideError,    /* Override onError to this if not OE_Default */
         1219  +  int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
         1220  +  int *pbMayReplace    /* OUT: Set to true if constraint may cause a replace */
  1202   1221   ){
  1203         -  int i;              /* loop counter */
  1204         -  Vdbe *v;            /* VDBE under constrution */
  1205         -  int nCol;           /* Number of columns */
  1206         -  int onError;        /* Conflict resolution strategy */
  1207         -  int j1;             /* Addresss of jump instruction */
  1208         -  int j2 = 0, j3;     /* Addresses of jump instructions */
  1209         -  int regData;        /* Register containing first data column */
  1210         -  int iCur;           /* Table cursor number */
         1222  +  Vdbe *v;             /* VDBE under constrution */
  1211   1223     Index *pIdx;         /* Pointer to one of the indices */
         1224  +  Index *pPk = 0;      /* The PRIMARY KEY index */
  1212   1225     sqlite3 *db;         /* Database connection */
         1226  +  int i;               /* loop counter */
         1227  +  int ix;              /* Index loop counter */
         1228  +  int nCol;            /* Number of columns */
         1229  +  int onError;         /* Conflict resolution strategy */
         1230  +  int j1;              /* Addresss of jump instruction */
  1213   1231     int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
  1214         -  int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
         1232  +  int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
         1233  +  int ipkTop = 0;      /* Top of the rowid change constraint check */
         1234  +  int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
         1235  +  u8 isUpdate;         /* True if this is an UPDATE operation */
  1215   1236   
         1237  +  isUpdate = regOldData!=0;
  1216   1238     db = pParse->db;
  1217   1239     v = sqlite3GetVdbe(pParse);
  1218   1240     assert( v!=0 );
  1219   1241     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
  1220   1242     nCol = pTab->nCol;
  1221         -  regData = regRowid + 1;
         1243  +  
         1244  +  /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
         1245  +  ** normal rowid tables.  nPkField is the number of key fields in the 
         1246  +  ** pPk index or 1 for a rowid table.  In other words, nPkField is the
         1247  +  ** number of fields in the true primary key of the table. */
         1248  +  if( HasRowid(pTab) ){
         1249  +    pPk = 0;
         1250  +    nPkField = 1;
         1251  +  }else{
         1252  +    pPk = sqlite3PrimaryKeyIndex(pTab);
         1253  +    nPkField = pPk->nKeyCol;
         1254  +  }
         1255  +
         1256  +  /* Record that this module has started */
         1257  +  VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
         1258  +                     iDataCur, iIdxCur, regNewData, regOldData, pkChng));
  1222   1259   
  1223   1260     /* Test all NOT NULL constraints.
  1224   1261     */
  1225   1262     for(i=0; i<nCol; i++){
  1226   1263       if( i==pTab->iPKey ){
  1227   1264         continue;
  1228   1265       }
................................................................................
  1237   1274         onError = OE_Abort;
  1238   1275       }
  1239   1276       assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
  1240   1277           || onError==OE_Ignore || onError==OE_Replace );
  1241   1278       switch( onError ){
  1242   1279         case OE_Abort:
  1243   1280           sqlite3MayAbort(pParse);
         1281  +        /* Fall through */
  1244   1282         case OE_Rollback:
  1245   1283         case OE_Fail: {
  1246         -        char *zMsg;
  1247         -        sqlite3VdbeAddOp3(v, OP_HaltIfNull,
  1248         -                          SQLITE_CONSTRAINT_NOTNULL, onError, regData+i);
  1249         -        zMsg = sqlite3MPrintf(db, "%s.%s may not be NULL",
  1250         -                              pTab->zName, pTab->aCol[i].zName);
  1251         -        sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
         1284  +        char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
         1285  +                                    pTab->aCol[i].zName);
         1286  +        sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
         1287  +                          regNewData+1+i, zMsg, P4_DYNAMIC);
         1288  +        sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
  1252   1289           break;
  1253   1290         }
  1254   1291         case OE_Ignore: {
  1255         -        sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
         1292  +        sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
  1256   1293           break;
  1257   1294         }
  1258   1295         default: {
  1259   1296           assert( onError==OE_Replace );
  1260         -        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
  1261         -        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
         1297  +        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
         1298  +        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
  1262   1299           sqlite3VdbeJumpHere(v, j1);
  1263   1300           break;
  1264   1301         }
  1265   1302       }
  1266   1303     }
  1267   1304   
  1268   1305     /* Test all CHECK constraints
  1269   1306     */
  1270   1307   #ifndef SQLITE_OMIT_CHECK
  1271   1308     if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
  1272   1309       ExprList *pCheck = pTab->pCheck;
  1273         -    pParse->ckBase = regData;
         1310  +    pParse->ckBase = regNewData+1;
  1274   1311       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
  1275   1312       for(i=0; i<pCheck->nExpr; i++){
  1276   1313         int allOk = sqlite3VdbeMakeLabel(v);
  1277   1314         sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
  1278   1315         if( onError==OE_Ignore ){
  1279   1316           sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1280   1317         }else{
  1281         -        char *zConsName = pCheck->a[i].zName;
         1318  +        char *zName = pCheck->a[i].zName;
         1319  +        if( zName==0 ) zName = pTab->zName;
  1282   1320           if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
  1283         -        if( zConsName ){
  1284         -          zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
  1285         -        }else{
  1286         -          zConsName = 0;
  1287         -        }
  1288   1321           sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
  1289         -                              onError, zConsName, P4_DYNAMIC);
         1322  +                              onError, zName, P4_TRANSIENT,
         1323  +                              P5_ConstraintCheck);
  1290   1324         }
  1291   1325         sqlite3VdbeResolveLabel(v, allOk);
  1292   1326       }
  1293   1327     }
  1294   1328   #endif /* !defined(SQLITE_OMIT_CHECK) */
  1295   1329   
  1296         -  /* If we have an INTEGER PRIMARY KEY, make sure the primary key
  1297         -  ** of the new record does not previously exist.  Except, if this
  1298         -  ** is an UPDATE and the primary key is not changing, that is OK.
         1330  +  /* If rowid is changing, make sure the new rowid does not previously
         1331  +  ** exist in the table.
  1299   1332     */
  1300         -  if( rowidChng ){
         1333  +  if( pkChng && pPk==0 ){
         1334  +    int addrRowidOk = sqlite3VdbeMakeLabel(v);
         1335  +
         1336  +    /* Figure out what action to take in case of a rowid collision */
  1301   1337       onError = pTab->keyConf;
  1302   1338       if( overrideError!=OE_Default ){
  1303   1339         onError = overrideError;
  1304   1340       }else if( onError==OE_Default ){
  1305   1341         onError = OE_Abort;
  1306   1342       }
  1307         -    
         1343  +
  1308   1344       if( isUpdate ){
  1309         -      j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
         1345  +      /* pkChng!=0 does not mean that the rowid has change, only that
         1346  +      ** it might have changed.  Skip the conflict logic below if the rowid
         1347  +      ** is unchanged. */
         1348  +      sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
  1310   1349       }
  1311         -    j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
         1350  +
         1351  +    /* If the response to a rowid conflict is REPLACE but the response
         1352  +    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
         1353  +    ** to defer the running of the rowid conflict checking until after
         1354  +    ** the UNIQUE constraints have run.
         1355  +    */
         1356  +    if( onError==OE_Replace && overrideError!=OE_Replace ){
         1357  +      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
         1358  +        if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
         1359  +          ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
         1360  +          break;
         1361  +        }
         1362  +      }
         1363  +    }
         1364  +
         1365  +    /* Check to see if the new rowid already exists in the table.  Skip
         1366  +    ** the following conflict logic if it does not. */
         1367  +    sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
         1368  +
         1369  +    /* Generate code that deals with a rowid collision */
  1312   1370       switch( onError ){
  1313   1371         default: {
  1314   1372           onError = OE_Abort;
  1315   1373           /* Fall thru into the next case */
  1316   1374         }
  1317   1375         case OE_Rollback:
  1318   1376         case OE_Abort:
  1319   1377         case OE_Fail: {
  1320         -        sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_PRIMARYKEY,
  1321         -           onError, "PRIMARY KEY must be unique", P4_STATIC);
         1378  +        sqlite3RowidConstraint(pParse, onError, pTab);
  1322   1379           break;
  1323   1380         }
  1324   1381         case OE_Replace: {
  1325   1382           /* If there are DELETE triggers on this table and the
  1326   1383           ** recursive-triggers flag is set, call GenerateRowDelete() to
  1327   1384           ** remove the conflicting row from the table. This will fire
  1328   1385           ** the triggers and remove both the table and index b-tree entries.
................................................................................
  1346   1403           */
  1347   1404           Trigger *pTrigger = 0;
  1348   1405           if( db->flags&SQLITE_RecTriggers ){
  1349   1406             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
  1350   1407           }
  1351   1408           if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
  1352   1409             sqlite3MultiWrite(pParse);
  1353         -          sqlite3GenerateRowDelete(
  1354         -              pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
  1355         -          );
         1410  +          sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
         1411  +                                   regNewData, 1, 0, OE_Replace, 1);
  1356   1412           }else{
  1357   1413   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  1358         -          /* This OP_Delete opcode fires the pre-update-hook only. It does
  1359         -          ** not modify the b-tree. It is more efficient to let the coming
  1360         -          ** OP_Insert replace the existing entry than it is to delete the
  1361         -          ** existing entry and then insert a new one. */
  1362         -          sqlite3VdbeAddOp2(v, OP_Delete, baseCur, OPFLAG_ISNOOP);
  1363         -          sqlite3VdbeChangeP4(v, -1, (char *)pTab, P4_TABLE);
         1414  +          if( HasRowid(pTab) ){
         1415  +            /* This OP_Delete opcode fires the pre-update-hook only. It does
         1416  +            ** not modify the b-tree. It is more efficient to let the coming
         1417  +            ** OP_Insert replace the existing entry than it is to delete the
         1418  +            ** existing entry and then insert a new one. */
         1419  +            sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
         1420  +            sqlite3VdbeChangeP4(v, -1, (char *)pTab, P4_TABLE);
         1421  +          }
  1364   1422   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
  1365   1423             if( pTab->pIndex ){
  1366   1424               sqlite3MultiWrite(pParse);
  1367         -            sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
         1425  +            sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
  1368   1426             }
  1369   1427           }
  1370   1428           seenReplace = 1;
  1371   1429           break;
  1372   1430         }
  1373   1431         case OE_Ignore: {
  1374         -        assert( seenReplace==0 );
         1432  +        /*assert( seenReplace==0 );*/
  1375   1433           sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1376   1434           break;
  1377   1435         }
  1378   1436       }
  1379         -    sqlite3VdbeJumpHere(v, j3);
  1380         -    if( isUpdate ){
  1381         -      sqlite3VdbeJumpHere(v, j2);
         1437  +    sqlite3VdbeResolveLabel(v, addrRowidOk);
         1438  +    if( ipkTop ){
         1439  +      ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
         1440  +      sqlite3VdbeJumpHere(v, ipkTop);
  1382   1441       }
  1383   1442     }
  1384   1443   
  1385   1444     /* Test all UNIQUE constraints by creating entries for each UNIQUE
  1386   1445     ** index and making sure that duplicate entries do not already exist.
  1387         -  ** Add the new records to the indices as we go.
         1446  +  ** Compute the revised record entries for indices as we go.
         1447  +  **
         1448  +  ** This loop also handles the case of the PRIMARY KEY index for a
         1449  +  ** WITHOUT ROWID table.
  1388   1450     */
  1389         -  for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
  1390         -    int regIdx;
  1391         -    int regR;
  1392         -    int addrSkipRow = 0;
         1451  +  for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
         1452  +    int regIdx;          /* Range of registers hold conent for pIdx */
         1453  +    int regR;            /* Range of registers holding conflicting PK */
         1454  +    int iThisCur;        /* Cursor for this UNIQUE index */
         1455  +    int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
  1393   1456   
  1394         -    if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
         1457  +    if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
         1458  +    iThisCur = iIdxCur+ix;
         1459  +    addrUniqueOk = sqlite3VdbeMakeLabel(v);
  1395   1460   
         1461  +    /* Skip partial indices for which the WHERE clause is not true */
  1396   1462       if( pIdx->pPartIdxWhere ){
  1397         -      sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[iCur]);
  1398         -      addrSkipRow = sqlite3VdbeMakeLabel(v);
  1399         -      pParse->ckBase = regData;
  1400         -      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrSkipRow,
         1463  +      sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
         1464  +      pParse->ckBase = regNewData+1;
         1465  +      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
  1401   1466                            SQLITE_JUMPIFNULL);
  1402   1467         pParse->ckBase = 0;
  1403   1468       }
  1404   1469   
  1405         -    /* Create a key for accessing the index entry */
  1406         -    regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
         1470  +    /* Create a record for this index entry as it should appear after
         1471  +    ** the insert or update.  Store that record in the aRegIdx[ix] register
         1472  +    */
         1473  +    regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
  1407   1474       for(i=0; i<pIdx->nColumn; i++){
  1408         -      int idx = pIdx->aiColumn[i];
  1409         -      if( idx==pTab->iPKey ){
  1410         -        sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
         1475  +      int iField = pIdx->aiColumn[i];
         1476  +      int x;
         1477  +      if( iField<0 || iField==pTab->iPKey ){
         1478  +        x = regNewData;
  1411   1479         }else{
  1412         -        sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
         1480  +        x = iField + regNewData + 1;
  1413   1481         }
         1482  +      sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
         1483  +      VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
  1414   1484       }
  1415         -    sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
  1416         -    sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
         1485  +    sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
  1417   1486       sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
  1418         -    sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
         1487  +    VdbeComment((v, "for %s", pIdx->zName));
         1488  +    sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
  1419   1489   
  1420         -    /* Find out what action to take in case there is an indexing conflict */
         1490  +    /* In an UPDATE operation, if this index is the PRIMARY KEY index 
         1491  +    ** of a WITHOUT ROWID table and there has been no change the
         1492  +    ** primary key, then no collision is possible.  The collision detection
         1493  +    ** logic below can all be skipped. */
         1494  +    if( isUpdate && pPk==pIdx && pkChng==0 ){
         1495  +      sqlite3VdbeResolveLabel(v, addrUniqueOk);
         1496  +      continue;
         1497  +    }
         1498  +
         1499  +    /* Find out what action to take in case there is a uniqueness conflict */
  1421   1500       onError = pIdx->onError;
  1422   1501       if( onError==OE_None ){ 
  1423         -      sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
  1424         -      sqlite3VdbeResolveLabel(v, addrSkipRow);
         1502  +      sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
         1503  +      sqlite3VdbeResolveLabel(v, addrUniqueOk);
  1425   1504         continue;  /* pIdx is not a UNIQUE index */
  1426   1505       }
  1427   1506       if( overrideError!=OE_Default ){
  1428   1507         onError = overrideError;
  1429   1508       }else if( onError==OE_Default ){
  1430   1509         onError = OE_Abort;
  1431   1510       }
  1432         -    if( seenReplace ){
  1433         -      if( onError==OE_Ignore ) onError = OE_Replace;
  1434         -      else if( onError==OE_Fail ) onError = OE_Abort;
  1435         -    }
  1436   1511       
  1437   1512       /* Check to see if the new index entry will be unique */
  1438         -    regR = sqlite3GetTempReg(pParse);
  1439         -    sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
  1440         -    j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
  1441         -                           regR, SQLITE_INT_TO_PTR(regIdx),
  1442         -                           P4_INT32);
  1443         -    sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
         1513  +    sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
         1514  +                         regIdx, pIdx->nKeyCol);
         1515  +
         1516  +    /* Generate code to handle collisions */
         1517  +    regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
         1518  +    if( HasRowid(pTab) ){
         1519  +      sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
         1520  +      /* Conflict only if the rowid of the existing index entry
         1521  +      ** is different from old-rowid */
         1522  +      if( isUpdate ){
         1523  +        sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
         1524  +      }
         1525  +    }else{
         1526  +      int x;
         1527  +      /* Extract the PRIMARY KEY from the end of the index entry and
         1528  +      ** store it in registers regR..regR+nPk-1 */
         1529  +      if( (isUpdate || onError==OE_Replace) && pIdx!=pPk ){
         1530  +        for(i=0; i<pPk->nKeyCol; i++){
         1531  +          x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
         1532  +          sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
         1533  +          VdbeComment((v, "%s.%s", pTab->zName,
         1534  +                       pTab->aCol[pPk->aiColumn[i]].zName));
         1535  +        }
         1536  +      }
         1537  +      if( isUpdate ){
         1538  +        /* If currently processing the PRIMARY KEY of a WITHOUT ROWID 
         1539  +        ** table, only conflict if the new PRIMARY KEY values are actually
         1540  +        ** different from the old.
         1541  +        **
         1542  +        ** For a UNIQUE index, only conflict if the PRIMARY KEY values
         1543  +        ** of the matched index row are different from the original PRIMARY
         1544  +        ** KEY values of this row before the update.  */
         1545  +        int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
         1546  +        int op = OP_Ne;
         1547  +        int regCmp = (pIdx->autoIndex==2 ? regIdx : regR);
         1548  +
         1549  +        for(i=0; i<pPk->nKeyCol; i++){
         1550  +          char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
         1551  +          x = pPk->aiColumn[i];
         1552  +          if( i==(pPk->nKeyCol-1) ){
         1553  +            addrJump = addrUniqueOk;
         1554  +            op = OP_Eq;
         1555  +          }
         1556  +          sqlite3VdbeAddOp4(v, op, 
         1557  +              regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
         1558  +          );
         1559  +        }
         1560  +      }
         1561  +    }
  1444   1562   
  1445   1563       /* Generate code that executes if the new index entry is not unique */
  1446   1564       assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
  1447   1565           || onError==OE_Ignore || onError==OE_Replace );
  1448   1566       switch( onError ){
  1449   1567         case OE_Rollback:
  1450   1568         case OE_Abort:
  1451   1569         case OE_Fail: {
  1452         -        int j;
  1453         -        StrAccum errMsg;
  1454         -        const char *zSep;
  1455         -        char *zErr;
  1456         -
  1457         -        sqlite3StrAccumInit(&errMsg, 0, 0, 200);
  1458         -        errMsg.db = db;
  1459         -        zSep = pIdx->nColumn>1 ? "columns " : "column ";
  1460         -        for(j=0; j<pIdx->nColumn; j++){
  1461         -          char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
  1462         -          sqlite3StrAccumAppend(&errMsg, zSep, -1);
  1463         -          zSep = ", ";
  1464         -          sqlite3StrAccumAppend(&errMsg, zCol, -1);
  1465         -        }
  1466         -        sqlite3StrAccumAppend(&errMsg,
  1467         -            pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
  1468         -        zErr = sqlite3StrAccumFinish(&errMsg);
  1469         -        sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_UNIQUE,
  1470         -                              onError, zErr, 0);
  1471         -        sqlite3DbFree(errMsg.db, zErr);
         1570  +        sqlite3UniqueConstraint(pParse, onError, pIdx);
  1472   1571           break;
  1473   1572         }
  1474   1573         case OE_Ignore: {
  1475         -        assert( seenReplace==0 );
  1476   1574           sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1477   1575           break;
  1478   1576         }
  1479   1577         default: {
  1480   1578           Trigger *pTrigger = 0;
  1481   1579           assert( onError==OE_Replace );
  1482   1580           sqlite3MultiWrite(pParse);
  1483   1581           if( db->flags&SQLITE_RecTriggers ){
  1484   1582             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
  1485   1583           }
  1486         -        sqlite3GenerateRowDelete(
  1487         -            pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
  1488         -        );
         1584  +        sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
         1585  +                                 regR, nPkField, 0, OE_Replace, pIdx==pPk);
  1489   1586           seenReplace = 1;
  1490   1587           break;
  1491   1588         }
  1492   1589       }
  1493         -    sqlite3VdbeJumpHere(v, j3);
  1494         -    sqlite3VdbeResolveLabel(v, addrSkipRow);
  1495         -    sqlite3ReleaseTempReg(pParse, regR);
         1590  +    sqlite3VdbeResolveLabel(v, addrUniqueOk);
         1591  +    sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
         1592  +    if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
         1593  +  }
         1594  +  if( ipkTop ){
         1595  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, ipkTop+1);
         1596  +    sqlite3VdbeJumpHere(v, ipkBottom);
  1496   1597     }
  1497   1598     
  1498         -  if( pbMayReplace ){
  1499         -    *pbMayReplace = seenReplace;
  1500         -  }
         1599  +  *pbMayReplace = seenReplace;
         1600  +  VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
  1501   1601   }
  1502   1602   
  1503   1603   /*
  1504   1604   ** This routine generates code to finish the INSERT or UPDATE operation
  1505   1605   ** that was started by a prior call to sqlite3GenerateConstraintChecks.
  1506         -** A consecutive range of registers starting at regRowid contains the
         1606  +** A consecutive range of registers starting at regNewData contains the
  1507   1607   ** rowid and the content to be inserted.
  1508   1608   **
  1509   1609   ** The arguments to this routine should be the same as the first six
  1510   1610   ** arguments to sqlite3GenerateConstraintChecks.
  1511   1611   */
  1512   1612   void sqlite3CompleteInsertion(
  1513   1613     Parse *pParse,      /* The parser context */
  1514   1614     Table *pTab,        /* the table into which we are inserting */
  1515         -  int baseCur,        /* Index of a read/write cursor pointing at pTab */
  1516         -  int regRowid,       /* Range of content */
         1615  +  int iDataCur,       /* Cursor of the canonical data source */
         1616  +  int iIdxCur,        /* First index cursor */
         1617  +  int regNewData,     /* Range of content */
  1517   1618     int *aRegIdx,       /* Register used by each index.  0 for unused indices */
  1518   1619     int isUpdate,       /* True for UPDATE, False for INSERT */
  1519   1620     int appendBias,     /* True if this is likely to be an append */
  1520   1621     int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
  1521   1622   ){
  1522         -  int i;
  1523         -  Vdbe *v;
  1524         -  Index *pIdx;
  1525         -  u8 pik_flags;
  1526         -  int regData;
  1527         -  int regRec;
         1623  +  Vdbe *v;            /* Prepared statements under construction */
         1624  +  Index *pIdx;        /* An index being inserted or updated */
         1625  +  u8 pik_flags;       /* flag values passed to the btree insert */
         1626  +  int regData;        /* Content registers (after the rowid) */
         1627  +  int regRec;         /* Register holding assemblied record for the table */
         1628  +  int i;              /* Loop counter */
  1528   1629   
  1529   1630     v = sqlite3GetVdbe(pParse);
  1530   1631     assert( v!=0 );
  1531   1632     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
  1532   1633     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1533   1634       if( aRegIdx[i]==0 ) continue;
  1534   1635       if( pIdx->pPartIdxWhere ){
  1535   1636         sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
  1536   1637       }
  1537         -    sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
  1538         -    if( useSeekResult ){
  1539         -      sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
         1638  +    sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
         1639  +    pik_flags = 0;
         1640  +    if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
         1641  +    if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
         1642  +      assert( pParse->nested==0 );
         1643  +      pik_flags |= OPFLAG_NCHANGE;
  1540   1644       }
         1645  +    if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
  1541   1646     }
  1542         -  regData = regRowid + 1;
         1647  +  if( !HasRowid(pTab) ) return;
         1648  +  regData = regNewData + 1;
  1543   1649     regRec = sqlite3GetTempReg(pParse);
  1544   1650     sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
  1545   1651     sqlite3TableAffinityStr(v, pTab);
  1546   1652     sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
  1547   1653     if( pParse->nested ){
  1548   1654       pik_flags = 0;
  1549   1655     }else{
................................................................................
  1552   1658     }
  1553   1659     if( appendBias ){
  1554   1660       pik_flags |= OPFLAG_APPEND;
  1555   1661     }
  1556   1662     if( useSeekResult ){
  1557   1663       pik_flags |= OPFLAG_USESEEKRESULT;
  1558   1664     }
  1559         -  sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
         1665  +  sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
  1560   1666     if( !pParse->nested ){
  1561   1667       sqlite3VdbeChangeP4(v, -1, (char *)pTab, P4_TABLE);
  1562   1668     }
  1563   1669     sqlite3VdbeChangeP5(v, pik_flags);
  1564   1670   }
  1565   1671   
  1566   1672   /*
  1567         -** Generate code that will open cursors for a table and for all
  1568         -** indices of that table.  The "baseCur" parameter is the cursor number used
  1569         -** for the table.  Indices are opened on subsequent cursors.
         1673  +** Allocate cursors for the pTab table and all its indices and generate
         1674  +** code to open and initialized those cursors.
         1675  +**
         1676  +** The cursor for the object that contains the complete data (normally
         1677  +** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
         1678  +** ROWID table) is returned in *piDataCur.  The first index cursor is
         1679  +** returned in *piIdxCur.  The number of indices is returned.
         1680  +**
         1681  +** Use iBase as the first cursor (either the *piDataCur for rowid tables
         1682  +** or the first index for WITHOUT ROWID tables) if it is non-negative.
         1683  +** If iBase is negative, then allocate the next available cursor.
  1570   1684   **
  1571         -** Return the number of indices on the table.
         1685  +** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
         1686  +** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
         1687  +** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
         1688  +** pTab->pIndex list.
  1572   1689   */
  1573   1690   int sqlite3OpenTableAndIndices(
  1574   1691     Parse *pParse,   /* Parsing context */
  1575   1692     Table *pTab,     /* Table to be opened */
  1576         -  int baseCur,     /* Cursor number assigned to the table */
  1577         -  int op           /* OP_OpenRead or OP_OpenWrite */
         1693  +  int op,          /* OP_OpenRead or OP_OpenWrite */
         1694  +  int iBase,       /* Use this for the table cursor, if there is one */
         1695  +  int *piDataCur,  /* Write the database source cursor number here */
         1696  +  int *piIdxCur    /* Write the first index cursor number here */
  1578   1697   ){
  1579   1698     int i;
  1580   1699     int iDb;
  1581   1700     Index *pIdx;
  1582   1701     Vdbe *v;
  1583   1702   
  1584         -  if( IsVirtual(pTab) ) return 0;
         1703  +  assert( op==OP_OpenRead || op==OP_OpenWrite );
         1704  +  if( IsVirtual(pTab) ){
         1705  +    *piDataCur = 0;
         1706  +    *piIdxCur = 1;
         1707  +    return 0;
         1708  +  }
  1585   1709     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  1586   1710     v = sqlite3GetVdbe(pParse);
  1587   1711     assert( v!=0 );
  1588         -  sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
  1589         -  for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1590         -    KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
         1712  +  if( iBase<0 ) iBase = pParse->nTab;
         1713  +  if( HasRowid(pTab) ){
         1714  +    *piDataCur = iBase++;
         1715  +    sqlite3OpenTable(pParse, *piDataCur, iDb, pTab, op);
         1716  +  }else{
         1717  +    sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
         1718  +  }
         1719  +  *piIdxCur = iBase;
         1720  +  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
         1721  +    int iIdxCur = iBase++;
  1591   1722       assert( pIdx->pSchema==pTab->pSchema );
  1592         -    sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
  1593         -                      (char*)pKey, P4_KEYINFO_HANDOFF);
         1723  +    if( pIdx->autoIndex==2 && !HasRowid(pTab) ) *piDataCur = iIdxCur;
         1724  +    sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
         1725  +    sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1594   1726       VdbeComment((v, "%s", pIdx->zName));
  1595   1727     }
  1596         -  if( pParse->nTab<baseCur+i ){
  1597         -    pParse->nTab = baseCur+i;
  1598         -  }
  1599         -  return i-1;
         1728  +  if( iBase>pParse->nTab ) pParse->nTab = iBase;
         1729  +  return i;
  1600   1730   }
  1601   1731   
  1602   1732   
  1603   1733   #ifdef SQLITE_TEST
  1604   1734   /*
  1605   1735   ** The following global variable is incremented whenever the
  1606   1736   ** transfer optimization is used.  This is used for testing
................................................................................
  1637   1767   **    *   The same collating sequence on each column
  1638   1768   **    *   The index has the exact same WHERE clause
  1639   1769   */
  1640   1770   static int xferCompatibleIndex(Index *pDest, Index *pSrc){
  1641   1771     int i;
  1642   1772     assert( pDest && pSrc );
  1643   1773     assert( pDest->pTable!=pSrc->pTable );
  1644         -  if( pDest->nColumn!=pSrc->nColumn ){
         1774  +  if( pDest->nKeyCol!=pSrc->nKeyCol ){
  1645   1775       return 0;   /* Different number of columns */
  1646   1776     }
  1647   1777     if( pDest->onError!=pSrc->onError ){
  1648   1778       return 0;   /* Different conflict resolution strategies */
  1649   1779     }
  1650         -  for(i=0; i<pSrc->nColumn; i++){
         1780  +  for(i=0; i<pSrc->nKeyCol; i++){
  1651   1781       if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
  1652   1782         return 0;   /* Different columns indexed */
  1653   1783       }
  1654   1784       if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
  1655   1785         return 0;   /* Different sort orders */
  1656   1786       }
  1657   1787       if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
................................................................................
  1702   1832     Table *pSrc;                     /* The table in the FROM clause of SELECT */
  1703   1833     Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
  1704   1834     struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
  1705   1835     int i;                           /* Loop counter */
  1706   1836     int iDbSrc;                      /* The database of pSrc */
  1707   1837     int iSrc, iDest;                 /* Cursors from source and destination */
  1708   1838     int addr1, addr2;                /* Loop addresses */
  1709         -  int emptyDestTest;               /* Address of test for empty pDest */
  1710         -  int emptySrcTest;                /* Address of test for empty pSrc */
         1839  +  int emptyDestTest = 0;           /* Address of test for empty pDest */
         1840  +  int emptySrcTest = 0;            /* Address of test for empty pSrc */
  1711   1841     Vdbe *v;                         /* The VDBE we are building */
  1712         -  KeyInfo *pKey;                   /* Key information for an index */
  1713   1842     int regAutoinc;                  /* Memory register used by AUTOINC */
  1714   1843     int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
  1715   1844     int regData, regRowid;           /* Registers holding data and rowid */
  1716   1845   
  1717   1846     if( pSelect==0 ){
  1718   1847       return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
  1719   1848     }
................................................................................
  1775   1904     pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
  1776   1905     if( pSrc==0 ){
  1777   1906       return 0;   /* FROM clause does not contain a real table */
  1778   1907     }
  1779   1908     if( pSrc==pDest ){
  1780   1909       return 0;   /* tab1 and tab2 may not be the same table */
  1781   1910     }
         1911  +  if( HasRowid(pDest)!=HasRowid(pSrc) ){
         1912  +    return 0;   /* source and destination must both be WITHOUT ROWID or not */
         1913  +  }
  1782   1914   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1783   1915     if( pSrc->tabFlags & TF_Virtual ){
  1784   1916       return 0;   /* tab2 must not be a virtual table */
  1785   1917     }
  1786   1918   #endif
  1787   1919     if( pSrc->pSelect ){
  1788   1920       return 0;   /* tab2 may not be a view */
................................................................................
  1845   1977   #endif
  1846   1978     iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
  1847   1979     v = sqlite3GetVdbe(pParse);
  1848   1980     sqlite3CodeVerifySchema(pParse, iDbSrc);
  1849   1981     iSrc = pParse->nTab++;
  1850   1982     iDest = pParse->nTab++;
  1851   1983     regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
         1984  +  regData = sqlite3GetTempReg(pParse);
         1985  +  regRowid = sqlite3GetTempReg(pParse);
  1852   1986     sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
         1987  +  assert( HasRowid(pDest) || destHasUniqueIdx );
  1853   1988     if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
  1854   1989      || destHasUniqueIdx                              /* (2) */
  1855   1990      || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
  1856   1991     ){
  1857   1992       /* In some circumstances, we are able to run the xfer optimization
  1858   1993       ** only if the destination table is initially empty.  This code makes
  1859   1994       ** that determination.  Conditions under which the destination must
................................................................................
  1867   2002       **     is unable to test uniqueness.)
  1868   2003       **
  1869   2004       ** (3) onError is something other than OE_Abort and OE_Rollback.
  1870   2005       */
  1871   2006       addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
  1872   2007       emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
  1873   2008       sqlite3VdbeJumpHere(v, addr1);
  1874         -  }else{
  1875         -    emptyDestTest = 0;
  1876   2009     }
  1877         -  sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
  1878         -  emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
  1879         -  regData = sqlite3GetTempReg(pParse);
  1880         -  regRowid = sqlite3GetTempReg(pParse);
  1881         -  if( pDest->iPKey>=0 ){
  1882         -    addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
  1883         -    addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
  1884         -    sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_PRIMARYKEY,
  1885         -        onError, "PRIMARY KEY must be unique", P4_STATIC);
  1886         -    sqlite3VdbeJumpHere(v, addr2);
  1887         -    autoIncStep(pParse, regAutoinc, regRowid);
  1888         -  }else if( pDest->pIndex==0 ){
  1889         -    addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
         2010  +  if( HasRowid(pSrc) ){
         2011  +    sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
         2012  +    emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
         2013  +    if( pDest->iPKey>=0 ){
         2014  +      addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
         2015  +      addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
         2016  +      sqlite3RowidConstraint(pParse, onError, pDest);
         2017  +      sqlite3VdbeJumpHere(v, addr2);
         2018  +      autoIncStep(pParse, regAutoinc, regRowid);
         2019  +    }else if( pDest->pIndex==0 ){
         2020  +      addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
         2021  +    }else{
         2022  +      addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
         2023  +      assert( (pDest->tabFlags & TF_Autoincrement)==0 );
         2024  +    }
         2025  +    sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
         2026  +    sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
         2027  +    sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
         2028  +    sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
         2029  +    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
         2030  +    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
         2031  +    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1890   2032     }else{
  1891         -    addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
  1892         -    assert( (pDest->tabFlags & TF_Autoincrement)==0 );
         2033  +    sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
         2034  +    sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
  1893   2035     }
  1894         -  sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
  1895         -  sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
  1896         -  sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
  1897         -  sqlite3VdbeChangeP4(v, -1, (char *)pDest, P4_TABLE);
  1898         -  sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
  1899   2036     for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
  1900   2037       for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
  1901   2038         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
  1902   2039       }
  1903   2040       assert( pSrcIdx );
  1904         -    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  1905         -    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1906         -    pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
  1907         -    sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
  1908         -                      (char*)pKey, P4_KEYINFO_HANDOFF);
         2041  +    sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
         2042  +    sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
  1909   2043       VdbeComment((v, "%s", pSrcIdx->zName));
  1910         -    pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
  1911         -    sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
  1912         -                      (char*)pKey, P4_KEYINFO_HANDOFF);
         2044  +    sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
         2045  +    sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
  1913   2046       sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
  1914   2047       VdbeComment((v, "%s", pDestIdx->zName));
  1915   2048       addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
  1916   2049       sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
  1917   2050       sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
  1918   2051       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
  1919   2052       sqlite3VdbeJumpHere(v, addr1);
         2053  +    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
         2054  +    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1920   2055     }
  1921   2056     sqlite3VdbeJumpHere(v, emptySrcTest);
  1922   2057     sqlite3ReleaseTempReg(pParse, regRowid);
  1923   2058     sqlite3ReleaseTempReg(pParse, regData);
  1924         -  sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  1925         -  sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1926   2059     if( emptyDestTest ){
  1927   2060       sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
  1928   2061       sqlite3VdbeJumpHere(v, emptyDestTest);
  1929   2062       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1930   2063       return 0;
  1931   2064     }else{
  1932   2065       return 1;
  1933   2066     }
  1934   2067   }
  1935   2068   #endif /* SQLITE_OMIT_XFER_OPT */

Changes to src/main.c.

  1131   1131                                   zName = "SQLITE_CONSTRAINT_PRIMARYKEY";   break;
  1132   1132         case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
  1133   1133         case SQLITE_CONSTRAINT_COMMITHOOK:
  1134   1134                                   zName = "SQLITE_CONSTRAINT_COMMITHOOK";   break;
  1135   1135         case SQLITE_CONSTRAINT_VTAB:    zName = "SQLITE_CONSTRAINT_VTAB";   break;
  1136   1136         case SQLITE_CONSTRAINT_FUNCTION:
  1137   1137                                   zName = "SQLITE_CONSTRAINT_FUNCTION";     break;
         1138  +      case SQLITE_CONSTRAINT_ROWID:   zName = "SQLITE_CONSTRAINT_ROWID";  break;
  1138   1139         case SQLITE_MISMATCH:           zName = "SQLITE_MISMATCH";          break;
  1139   1140         case SQLITE_MISUSE:             zName = "SQLITE_MISUSE";            break;
  1140   1141         case SQLITE_NOLFS:              zName = "SQLITE_NOLFS";             break;
  1141   1142         case SQLITE_AUTH:               zName = "SQLITE_AUTH";              break;
  1142   1143         case SQLITE_FORMAT:             zName = "SQLITE_FORMAT";            break;
  1143   1144         case SQLITE_RANGE:              zName = "SQLITE_RANGE";             break;
  1144   1145         case SQLITE_NOTADB:             zName = "SQLITE_NOTADB";            break;
................................................................................
  1966   1967   ** Return a string that describes the kind of error specified in the
  1967   1968   ** argument.  For now, this simply calls the internal sqlite3ErrStr()
  1968   1969   ** function.
  1969   1970   */
  1970   1971   const char *sqlite3_errstr(int rc){
  1971   1972     return sqlite3ErrStr(rc);
  1972   1973   }
         1974  +
         1975  +/*
         1976  +** Invalidate all cached KeyInfo objects for database connection "db"
         1977  +*/
         1978  +static void invalidateCachedKeyInfo(sqlite3 *db){
         1979  +  Db *pDb;                    /* A single database */
         1980  +  int iDb;                    /* The database index number */
         1981  +  HashElem *k;                /* For looping over tables in pDb */
         1982  +  Table *pTab;                /* A table in the database */
         1983  +  Index *pIdx;                /* Each index */
         1984  +
         1985  +  for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
         1986  +    if( pDb->pBt==0 ) continue;
         1987  +    sqlite3BtreeEnter(pDb->pBt);
         1988  +    for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
         1989  +      pTab = (Table*)sqliteHashData(k);
         1990  +      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
         1991  +        if( pIdx->pKeyInfo && pIdx->pKeyInfo->db==db ){
         1992  +          sqlite3KeyInfoUnref(pIdx->pKeyInfo);
         1993  +          pIdx->pKeyInfo = 0;
         1994  +        }
         1995  +      }
         1996  +    }
         1997  +    sqlite3BtreeLeave(pDb->pBt);
         1998  +  }
         1999  +}
  1973   2000   
  1974   2001   /*
  1975   2002   ** Create a new collating function for database "db".  The name is zName
  1976   2003   ** and the encoding is enc.
  1977   2004   */
  1978   2005   static int createCollation(
  1979   2006     sqlite3* db,
................................................................................
  2011   2038     if( pColl && pColl->xCmp ){
  2012   2039       if( db->nVdbeActive ){
  2013   2040         sqlite3Error(db, SQLITE_BUSY, 
  2014   2041           "unable to delete/modify collation sequence due to active statements");
  2015   2042         return SQLITE_BUSY;
  2016   2043       }
  2017   2044       sqlite3ExpirePreparedStatements(db);
         2045  +    invalidateCachedKeyInfo(db);
  2018   2046   
  2019   2047       /* If collation sequence pColl was created directly by a call to
  2020   2048       ** sqlite3_create_collation, and not generated by synthCollSeq(),
  2021   2049       ** then any copies made by synthCollSeq() need to be invalidated.
  2022   2050       ** Also, collation destructor - CollSeq.xDel() - function may need
  2023   2051       ** to be called.
  2024   2052       */ 

Changes to src/os_unix.c.

  1886   1886   ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
  1887   1887   ** must be either NO_LOCK or SHARED_LOCK.
  1888   1888   **
  1889   1889   ** If the locking level of the file descriptor is already at or below
  1890   1890   ** the requested locking level, this routine is a no-op.
  1891   1891   */
  1892   1892   static int unixUnlock(sqlite3_file *id, int eFileLock){
         1893  +#if SQLITE_MAX_MMAP_SIZE>0
  1893   1894     assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
         1895  +#endif
  1894   1896     return posixUnlock(id, eFileLock, 0);
  1895   1897   }
  1896   1898   
  1897   1899   #if SQLITE_MAX_MMAP_SIZE>0
  1898   1900   static int unixMapfile(unixFile *pFd, i64 nByte);
  1899   1901   static void unixUnmapfile(unixFile *pFd);
  1900   1902   #endif

Changes to src/os_win.c.

    55     55   ** Make sure at least one set of Win32 APIs is available.
    56     56   */
    57     57   #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
    58     58   #  error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
    59     59    must be defined."
    60     60   #endif
    61     61   
           62  +/*
           63  +** This constant should already be defined (in the "WinDef.h" SDK file).
           64  +*/
           65  +#ifndef MAX_PATH
           66  +#  define MAX_PATH                      (260)
           67  +#endif
           68  +
    62     69   /*
    63     70   ** Maximum pathname length (in chars) for Win32.  This should normally be
    64     71   ** MAX_PATH.
    65     72   */
    66     73   #ifndef SQLITE_WIN32_MAX_PATH_CHARS
    67     74   #  define SQLITE_WIN32_MAX_PATH_CHARS   (MAX_PATH)
    68     75   #endif
    69     76   
           77  +/*
           78  +** This constant should already be defined (in the "WinNT.h" SDK file).
           79  +*/
           80  +#ifndef UNICODE_STRING_MAX_CHARS
           81  +#  define UNICODE_STRING_MAX_CHARS      (32767)
           82  +#endif
           83  +
    70     84   /*
    71     85   ** Maximum pathname length (in chars) for WinNT.  This should normally be
    72         -** 32767.
           86  +** UNICODE_STRING_MAX_CHARS.
    73     87   */
    74     88   #ifndef SQLITE_WINNT_MAX_PATH_CHARS
    75         -#  define SQLITE_WINNT_MAX_PATH_CHARS   (32767)
           89  +#  define SQLITE_WINNT_MAX_PATH_CHARS   (UNICODE_STRING_MAX_CHARS)
    76     90   #endif
    77     91   
    78     92   /*
    79     93   ** Maximum pathname length (in bytes) for Win32.  The MAX_PATH macro is in
    80         -** characters, so we allocate 3 bytes per character assuming worst-case of
           94  +** characters, so we allocate 4 bytes per character assuming worst-case of
    81     95   ** 4-bytes-per-character for UTF8.
    82     96   */
    83     97   #ifndef SQLITE_WIN32_MAX_PATH_BYTES
    84     98   #  define SQLITE_WIN32_MAX_PATH_BYTES   (SQLITE_WIN32_MAX_PATH_CHARS*4)
    85     99   #endif
    86    100   
    87    101   /*
    88    102   ** Maximum pathname length (in bytes) for WinNT.  This should normally be
    89         -** 32767 * sizeof(WCHAR).
          103  +** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
    90    104   */
    91    105   #ifndef SQLITE_WINNT_MAX_PATH_BYTES
    92    106   #  define SQLITE_WINNT_MAX_PATH_BYTES   \
    93    107                               (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
    94    108   #endif
    95    109   
    96    110   /*
................................................................................
   113    127   ** [sometimes] not used by the code (e.g. via conditional compilation).
   114    128   */
   115    129   #ifndef UNUSED_VARIABLE_VALUE
   116    130   #  define UNUSED_VARIABLE_VALUE(x) (void)(x)
   117    131   #endif
   118    132   
   119    133   /*
   120         -** Returns the string that should be used as the directory separator.
          134  +** Returns the character that should be used as the directory separator.
   121    135   */
   122         -#ifndef winGetDirDep
   123         -#  ifdef __CYGWIN__
   124         -#    define winGetDirDep()              "/"
   125         -#  else
   126         -#    define winGetDirDep()              "\\"
   127         -#  endif
          136  +#ifndef winGetDirSep
          137  +#  define winGetDirSep()                '\\'
   128    138   #endif
   129    139   
   130    140   /*
   131    141   ** Do we need to manually define the Win32 file mapping APIs for use with WAL
   132    142   ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
   133    143   ** are not present in the header file)?
   134    144   */
................................................................................
   312    322   /*
   313    323   ** The winMemData structure stores information required by the Win32-specific
   314    324   ** sqlite3_mem_methods implementation.
   315    325   */
   316    326   typedef struct winMemData winMemData;
   317    327   struct winMemData {
   318    328   #ifndef NDEBUG
   319         -  u32 magic;    /* Magic number to detect structure corruption. */
          329  +  u32 magic1;   /* Magic number to detect structure corruption. */
   320    330   #endif
   321    331     HANDLE hHeap; /* The handle to our heap. */
   322    332     BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
          333  +#ifndef NDEBUG
          334  +  u32 magic2;   /* Magic number to detect structure corruption. */
          335  +#endif
   323    336   };
   324    337   
   325    338   #ifndef NDEBUG
   326         -#define WINMEM_MAGIC     0x42b2830b
          339  +#define WINMEM_MAGIC1     0x42b2830b
          340  +#define WINMEM_MAGIC2     0xbd4d7cf4
   327    341   #endif
   328    342   
   329    343   static struct winMemData win_mem_data = {
   330    344   #ifndef NDEBUG
   331         -  WINMEM_MAGIC,
          345  +  WINMEM_MAGIC1,
   332    346   #endif
   333    347     NULL, FALSE
          348  +#ifndef NDEBUG
          349  +  ,WINMEM_MAGIC2
          350  +#endif
   334    351   };
   335    352   
   336    353   #ifndef NDEBUG
   337         -#define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
          354  +#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
          355  +#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
          356  +#define winMemAssertMagic()  winMemAssertMagic1(); winMemAssertMagic2();
   338    357   #else
   339    358   #define winMemAssertMagic()
   340    359   #endif
   341    360   
   342         -#define winMemGetHeap() win_mem_data.hHeap
          361  +#define winMemGetDataPtr()  &win_mem_data
          362  +#define winMemGetHeap()     win_mem_data.hHeap
          363  +#define winMemGetOwned()    win_mem_data.bOwned
   343    364   
   344    365   static void *winMemMalloc(int nBytes);
   345    366   static void winMemFree(void *pPrior);
   346    367   static void *winMemRealloc(void *pPrior, int nBytes);
   347    368   static int winMemSize(void *p);
   348    369   static int winMemRoundup(int n);
   349    370   static int winMemInit(void *pAppData);
................................................................................
   732    753   #else
   733    754     { "HeapValidate",            (SYSCALL)0,                       0 },
   734    755   #endif
   735    756   
   736    757   #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
   737    758           LPCVOID))aSyscall[42].pCurrent)
   738    759   
          760  +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
          761  +  { "HeapCompact",             (SYSCALL)HeapCompact,             0 },
          762  +#else
          763  +  { "HeapCompact",             (SYSCALL)0,                       0 },
          764  +#endif
          765  +
          766  +#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
          767  +
   739    768   #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
   740    769     { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
   741    770   #else
   742    771     { "LoadLibraryA",            (SYSCALL)0,                       0 },
   743    772   #endif
   744    773   
   745         -#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[43].pCurrent)
          774  +#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
   746    775   
   747    776   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
   748    777           !defined(SQLITE_OMIT_LOAD_EXTENSION)
   749    778     { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
   750    779   #else
   751    780     { "LoadLibraryW",            (SYSCALL)0,                       0 },
   752    781   #endif
   753    782   
   754         -#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[44].pCurrent)
          783  +#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
   755    784   
   756    785   #if !SQLITE_OS_WINRT
   757    786     { "LocalFree",               (SYSCALL)LocalFree,               0 },
   758    787   #else
   759    788     { "LocalFree",               (SYSCALL)0,                       0 },
   760    789   #endif
   761    790   
   762         -#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[45].pCurrent)
          791  +#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
   763    792   
   764    793   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
   765    794     { "LockFile",                (SYSCALL)LockFile,                0 },
   766    795   #else
   767    796     { "LockFile",                (SYSCALL)0,                       0 },
   768    797   #endif
   769    798   
   770    799   #ifndef osLockFile
   771    800   #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   772         -        DWORD))aSyscall[46].pCurrent)
          801  +        DWORD))aSyscall[47].pCurrent)
   773    802   #endif
   774    803   
   775    804   #if !SQLITE_OS_WINCE
   776    805     { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
   777    806   #else
   778    807     { "LockFileEx",              (SYSCALL)0,                       0 },
   779    808   #endif
   780    809   
   781    810   #ifndef osLockFileEx
   782    811   #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
   783         -        LPOVERLAPPED))aSyscall[47].pCurrent)
          812  +        LPOVERLAPPED))aSyscall[48].pCurrent)
   784    813   #endif
   785    814   
   786    815   #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
   787    816     { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
   788    817   #else
   789    818     { "MapViewOfFile",           (SYSCALL)0,                       0 },
   790    819   #endif
   791    820   
   792    821   #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   793         -        SIZE_T))aSyscall[48].pCurrent)
          822  +        SIZE_T))aSyscall[49].pCurrent)
   794    823   
   795    824     { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
   796    825   
   797    826   #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
   798         -        int))aSyscall[49].pCurrent)
          827  +        int))aSyscall[50].pCurrent)
   799    828   
   800    829     { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
   801    830   
   802    831   #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
   803         -        LARGE_INTEGER*))aSyscall[50].pCurrent)
          832  +        LARGE_INTEGER*))aSyscall[51].pCurrent)
   804    833   
   805    834     { "ReadFile",                (SYSCALL)ReadFile,                0 },
   806    835   
   807    836   #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
   808         -        LPOVERLAPPED))aSyscall[51].pCurrent)
          837  +        LPOVERLAPPED))aSyscall[52].pCurrent)
   809    838   
   810    839     { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
   811    840   
   812         -#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[52].pCurrent)
          841  +#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
   813    842   
   814    843   #if !SQLITE_OS_WINRT
   815    844     { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
   816    845   #else
   817    846     { "SetFilePointer",          (SYSCALL)0,                       0 },
   818    847   #endif
   819    848   
   820    849   #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
   821         -        DWORD))aSyscall[53].pCurrent)
          850  +        DWORD))aSyscall[54].pCurrent)
   822    851   
   823    852   #if !SQLITE_OS_WINRT
   824    853     { "Sleep",                   (SYSCALL)Sleep,                   0 },
   825    854   #else
   826    855     { "Sleep",                   (SYSCALL)0,                       0 },
   827    856   #endif
   828    857   
   829         -#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[54].pCurrent)
          858  +#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
   830    859   
   831    860     { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
   832    861   
   833    862   #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
   834         -        LPFILETIME))aSyscall[55].pCurrent)
          863  +        LPFILETIME))aSyscall[56].pCurrent)
   835    864   
   836    865   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
   837    866     { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
   838    867   #else
   839    868     { "UnlockFile",              (SYSCALL)0,                       0 },
   840    869   #endif
   841    870   
   842    871   #ifndef osUnlockFile
   843    872   #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   844         -        DWORD))aSyscall[56].pCurrent)
          873  +        DWORD))aSyscall[57].pCurrent)
   845    874   #endif
   846    875   
   847    876   #if !SQLITE_OS_WINCE
   848    877     { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
   849    878   #else
   850    879     { "UnlockFileEx",            (SYSCALL)0,                       0 },
   851    880   #endif
   852    881   
   853    882   #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   854         -        LPOVERLAPPED))aSyscall[57].pCurrent)
          883  +        LPOVERLAPPED))aSyscall[58].pCurrent)
   855    884   
   856    885   #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
   857    886     { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
   858    887   #else
   859    888     { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
   860    889   #endif
   861    890   
   862         -#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[58].pCurrent)
          891  +#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
   863    892   
   864    893     { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
   865    894   
   866    895   #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
   867         -        LPCSTR,LPBOOL))aSyscall[59].pCurrent)
          896  +        LPCSTR,LPBOOL))aSyscall[60].pCurrent)
   868    897   
   869    898     { "WriteFile",               (SYSCALL)WriteFile,               0 },
   870    899   
   871    900   #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
   872         -        LPOVERLAPPED))aSyscall[60].pCurrent)
          901  +        LPOVERLAPPED))aSyscall[61].pCurrent)
   873    902   
   874    903   #if SQLITE_OS_WINRT
   875    904     { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
   876    905   #else
   877    906     { "CreateEventExW",          (SYSCALL)0,                       0 },
   878    907   #endif
   879    908   
   880    909   #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
   881         -        DWORD,DWORD))aSyscall[61].pCurrent)
          910  +        DWORD,DWORD))aSyscall[62].pCurrent)
   882    911   
   883    912   #if !SQLITE_OS_WINRT
   884    913     { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
   885    914   #else
   886    915     { "WaitForSingleObject",     (SYSCALL)0,                       0 },
   887    916   #endif
   888    917   
   889    918   #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
   890         -        DWORD))aSyscall[62].pCurrent)
          919  +        DWORD))aSyscall[63].pCurrent)
   891    920   
   892    921   #if SQLITE_OS_WINRT
   893    922     { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
   894    923   #else
   895    924     { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
   896    925   #endif
   897    926   
   898    927   #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
   899         -        BOOL))aSyscall[63].pCurrent)
          928  +        BOOL))aSyscall[64].pCurrent)
   900    929   
   901    930   #if SQLITE_OS_WINRT
   902    931     { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
   903    932   #else
   904    933     { "SetFilePointerEx",        (SYSCALL)0,                       0 },
   905    934   #endif
   906    935   
   907    936   #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
   908         -        PLARGE_INTEGER,DWORD))aSyscall[64].pCurrent)
          937  +        PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
   909    938   
   910    939   #if SQLITE_OS_WINRT
   911    940     { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
   912    941   #else
   913    942     { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
   914    943   #endif
   915    944   
   916    945   #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
   917         -        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[65].pCurrent)
          946  +        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
   918    947   
   919    948   #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
   920    949     { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
   921    950   #else
   922    951     { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
   923    952   #endif
   924    953   
   925    954   #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
   926         -        SIZE_T))aSyscall[66].pCurrent)
          955  +        SIZE_T))aSyscall[67].pCurrent)
   927    956   
   928    957   #if SQLITE_OS_WINRT
   929    958     { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
   930    959   #else
   931    960     { "CreateFile2",             (SYSCALL)0,                       0 },
   932    961   #endif
   933    962   
   934    963   #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
   935         -        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[67].pCurrent)
          964  +        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
   936    965   
   937    966   #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
   938    967     { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
   939    968   #else
   940    969     { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
   941    970   #endif
   942    971   
   943    972   #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
   944         -        DWORD))aSyscall[68].pCurrent)
          973  +        DWORD))aSyscall[69].pCurrent)
   945    974   
   946    975   #if SQLITE_OS_WINRT
   947    976     { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
   948    977   #else
   949    978     { "GetTickCount64",          (SYSCALL)0,                       0 },
   950    979   #endif
   951    980   
   952         -#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[69].pCurrent)
          981  +#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
   953    982   
   954    983   #if SQLITE_OS_WINRT
   955    984     { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
   956    985   #else
   957    986     { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
   958    987   #endif
   959    988   
   960    989   #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
   961         -        LPSYSTEM_INFO))aSyscall[70].pCurrent)
          990  +        LPSYSTEM_INFO))aSyscall[71].pCurrent)
   962    991   
   963    992   #if defined(SQLITE_WIN32_HAS_ANSI)
   964    993     { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
   965    994   #else
   966    995     { "OutputDebugStringA",      (SYSCALL)0,                       0 },
   967    996   #endif
   968    997   
   969         -#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[71].pCurrent)
          998  +#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
   970    999   
   971   1000   #if defined(SQLITE_WIN32_HAS_WIDE)
   972   1001     { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
   973   1002   #else
   974   1003     { "OutputDebugStringW",      (SYSCALL)0,                       0 },
   975   1004   #endif
   976   1005   
   977         -#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[72].pCurrent)
         1006  +#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
   978   1007   
   979   1008     { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
   980   1009   
   981         -#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[73].pCurrent)
         1010  +#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
   982   1011   
   983   1012   #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
   984   1013     { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
   985   1014   #else
   986   1015     { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
   987   1016   #endif
   988   1017   
   989   1018   #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
   990         -        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[74].pCurrent)
         1019  +        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
   991   1020   
   992   1021   }; /* End of the overrideable system calls */
   993   1022   
   994   1023   /*
   995   1024   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
   996   1025   ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
   997   1026   ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
................................................................................
  1070   1099     }
  1071   1100     for(i++; i<ArraySize(aSyscall); i++){
  1072   1101       if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
  1073   1102     }
  1074   1103     return 0;
  1075   1104   }
  1076   1105   
         1106  +#ifdef SQLITE_WIN32_MALLOC
         1107  +/*
         1108  +** If a Win32 native heap has been configured, this function will attempt to
         1109  +** compact it.  Upon success, SQLITE_OK will be returned.  Upon failure, one
         1110  +** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned.  The
         1111  +** "pnLargest" argument, if non-zero, will be used to return the size of the
         1112  +** largest committed free block in the heap, in bytes.
         1113  +*/
         1114  +int sqlite3_win32_compact_heap(LPUINT pnLargest){
         1115  +  int rc = SQLITE_OK;
         1116  +  UINT nLargest = 0;
         1117  +  HANDLE hHeap;
         1118  +
         1119  +  winMemAssertMagic();
         1120  +  hHeap = winMemGetHeap();
         1121  +  assert( hHeap!=0 );
         1122  +  assert( hHeap!=INVALID_HANDLE_VALUE );
         1123  +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
         1124  +  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
         1125  +#endif
         1126  +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
         1127  +  if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
         1128  +    DWORD lastErrno = osGetLastError();
         1129  +    if( lastErrno==NO_ERROR ){
         1130  +      sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
         1131  +                  (void*)hHeap);
         1132  +      rc = SQLITE_NOMEM;
         1133  +    }else{
         1134  +      sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
         1135  +                  osGetLastError(), (void*)hHeap);
         1136  +      rc = SQLITE_ERROR;
         1137  +    }
         1138  +  }
         1139  +#else
         1140  +  sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
         1141  +              (void*)hHeap);
         1142  +  rc = SQLITE_NOTFOUND;
         1143  +#endif
         1144  +  if( pnLargest ) *pnLargest = nLargest;
         1145  +  return rc;
         1146  +}
         1147  +
         1148  +/*
         1149  +** If a Win32 native heap has been configured, this function will attempt to
         1150  +** destroy and recreate it.  If the Win32 native heap is not isolated and/or
         1151  +** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
         1152  +** be returned and no changes will be made to the Win32 native heap.
         1153  +*/
         1154  +int sqlite3_win32_reset_heap(){
         1155  +  int rc;
         1156  +  MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
         1157  +  MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
         1158  +  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
         1159  +  MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
         1160  +  sqlite3_mutex_enter(pMaster);
         1161  +  sqlite3_mutex_enter(pMem);
         1162  +  winMemAssertMagic();
         1163  +  if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
         1164  +    /*
         1165  +    ** At this point, there should be no outstanding memory allocations on
         1166  +    ** the heap.  Also, since both the master and memsys locks are currently
         1167  +    ** being held by us, no other function (i.e. from another thread) should
         1168  +    ** be able to even access the heap.  Attempt to destroy and recreate our
         1169  +    ** isolated Win32 native heap now.
         1170  +    */
         1171  +    assert( winMemGetHeap()!=NULL );
         1172  +    assert( winMemGetOwned() );
         1173  +    assert( sqlite3_memory_used()==0 );
         1174  +    winMemShutdown(winMemGetDataPtr());
         1175  +    assert( winMemGetHeap()==NULL );
         1176  +    assert( !winMemGetOwned() );
         1177  +    assert( sqlite3_memory_used()==0 );
         1178  +    rc = winMemInit(winMemGetDataPtr());
         1179  +    assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
         1180  +    assert( rc!=SQLITE_OK || winMemGetOwned() );
         1181  +    assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
         1182  +  }else{
         1183  +    /*
         1184  +    ** The Win32 native heap cannot be modified because it may be in use.
         1185  +    */
         1186  +    rc = SQLITE_BUSY;
         1187  +  }
         1188  +  sqlite3_mutex_leave(pMem);
         1189  +  sqlite3_mutex_leave(pMaster);
         1190  +  return rc;
         1191  +}
         1192  +#endif /* SQLITE_WIN32_MALLOC */
         1193  +
  1077   1194   /*
  1078   1195   ** This function outputs the specified (ANSI) string to the Win32 debugger
  1079   1196   ** (if available).
  1080   1197   */
  1081   1198   
  1082   1199   void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
  1083   1200     char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
................................................................................
  1178   1295     void *p;
  1179   1296   
  1180   1297     winMemAssertMagic();
  1181   1298     hHeap = winMemGetHeap();
  1182   1299     assert( hHeap!=0 );
  1183   1300     assert( hHeap!=INVALID_HANDLE_VALUE );
  1184   1301   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
  1185         -  assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
         1302  +  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
  1186   1303   #endif
  1187   1304     assert( nBytes>=0 );
  1188   1305     p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
  1189   1306     if( !p ){
  1190   1307       sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
  1191   1308                   nBytes, osGetLastError(), (void*)hHeap);
  1192   1309     }
................................................................................
  1200   1317     HANDLE hHeap;
  1201   1318   
  1202   1319     winMemAssertMagic();
  1203   1320     hHeap = winMemGetHeap();
  1204   1321     assert( hHeap!=0 );
  1205   1322     assert( hHeap!=INVALID_HANDLE_VALUE );
  1206   1323   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
  1207         -  assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
         1324  +  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
  1208   1325   #endif
  1209   1326     if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
  1210   1327     if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
  1211   1328       sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
  1212   1329                   pPrior, osGetLastError(), (void*)hHeap);
  1213   1330     }
  1214   1331   }
................................................................................
  1221   1338     void *p;
  1222   1339   
  1223   1340     winMemAssertMagic();
  1224   1341     hHeap = winMemGetHeap();
  1225   1342     assert( hHeap!=0 );
  1226   1343     assert( hHeap!=INVALID_HANDLE_VALUE );
  1227   1344   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
  1228         -  assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
         1345  +  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
  1229   1346   #endif
  1230   1347     assert( nBytes>=0 );
  1231   1348     if( !pPrior ){
  1232   1349       p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
  1233   1350     }else{
  1234   1351       p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
  1235   1352     }
................................................................................
  1249   1366     SIZE_T n;
  1250   1367   
  1251   1368     winMemAssertMagic();
  1252   1369     hHeap = winMemGetHeap();
  1253   1370     assert( hHeap!=0 );
  1254   1371     assert( hHeap!=INVALID_HANDLE_VALUE );
  1255   1372   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
  1256         -  assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
         1373  +  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
  1257   1374   #endif
  1258   1375     if( !p ) return 0;
  1259   1376     n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
  1260   1377     if( n==(SIZE_T)-1 ){
  1261   1378       sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
  1262   1379                   p, osGetLastError(), (void*)hHeap);
  1263   1380       return 0;
................................................................................
  1275   1392   /*
  1276   1393   ** Initialize this module.
  1277   1394   */
  1278   1395   static int winMemInit(void *pAppData){
  1279   1396     winMemData *pWinMemData = (winMemData *)pAppData;
  1280   1397   
  1281   1398     if( !pWinMemData ) return SQLITE_ERROR;
  1282         -  assert( pWinMemData->magic==WINMEM_MAGIC );
         1399  +  assert( pWinMemData->magic1==WINMEM_MAGIC1 );
         1400  +  assert( pWinMemData->magic2==WINMEM_MAGIC2 );
  1283   1401   
  1284   1402   #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
  1285   1403     if( !pWinMemData->hHeap ){
  1286   1404       pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
  1287   1405                                         SQLITE_WIN32_HEAP_INIT_SIZE,
  1288   1406                                         SQLITE_WIN32_HEAP_MAX_SIZE);
  1289   1407       if( !pWinMemData->hHeap ){
................................................................................
  1317   1435   /*
  1318   1436   ** Deinitialize this module.
  1319   1437   */
  1320   1438   static void winMemShutdown(void *pAppData){
  1321   1439     winMemData *pWinMemData = (winMemData *)pAppData;
  1322   1440   
  1323   1441     if( !pWinMemData ) return;
         1442  +  assert( pWinMemData->magic1==WINMEM_MAGIC1 );
         1443  +  assert( pWinMemData->magic2==WINMEM_MAGIC2 );
         1444  +
  1324   1445     if( pWinMemData->hHeap ){
  1325   1446       assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
  1326   1447   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
  1327   1448       assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
  1328   1449   #endif
  1329   1450       if( pWinMemData->bOwned ){
  1330   1451         if( !osHeapDestroy(pWinMemData->hHeap) ){
................................................................................
  3986   4107   #endif
  3987   4108     /* caller will handle out of memory */
  3988   4109     return zConverted;
  3989   4110   }
  3990   4111   
  3991   4112   /*
  3992   4113   ** This function returns non-zero if the specified UTF-8 string buffer
  3993         -** ends with a directory separator character.
         4114  +** ends with a directory separator character or one was successfully
         4115  +** added to it.
  3994   4116   */
  3995         -static int winEndsInDirSep(char *zBuf){
         4117  +static int winMakeEndInDirSep(int nBuf, char *zBuf){
  3996   4118     if( zBuf ){
  3997   4119       int nLen = sqlite3Strlen30(zBuf);
  3998         -    return nLen>0 && winIsDirSep(zBuf[nLen-1]);
         4120  +    if( nLen>0 ){
         4121  +      if( winIsDirSep(zBuf[nLen-1]) ){
         4122  +        return 1;
         4123  +      }else if( nLen+1<nBuf ){
         4124  +        zBuf[nLen] = winGetDirSep();
         4125  +        zBuf[nLen+1] = '\0';
         4126  +        return 1;
         4127  +      }
         4128  +    }
  3999   4129     }
  4000   4130     return 0;
  4001   4131   }
  4002   4132   
  4003   4133   /*
  4004   4134   ** Create a temporary file name and store the resulting pointer into pzBuf.
  4005   4135   ** The pointer returned in pzBuf must be freed via sqlite3_free().
................................................................................
  4006   4136   */
  4007   4137   static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
  4008   4138     static char zChars[] =
  4009   4139       "abcdefghijklmnopqrstuvwxyz"
  4010   4140       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  4011   4141       "0123456789";
  4012   4142     size_t i, j;
  4013         -  int nBuf, nLen;
         4143  +  int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
         4144  +  int nMax, nBuf, nDir, nLen;
  4014   4145     char *zBuf;
  4015   4146   
  4016   4147     /* It's odd to simulate an io-error here, but really this is just
  4017   4148     ** using the io-error infrastructure to test that SQLite handles this
  4018   4149     ** function failing. 
  4019   4150     */
  4020   4151     SimulateIOError( return SQLITE_IOERR );
  4021   4152   
  4022   4153     /* Allocate a temporary buffer to store the fully qualified file
  4023   4154     ** name for the temporary file.  If this fails, we cannot continue.
  4024   4155     */
  4025         -  nBuf = pVfs->mxPathname;
  4026         -  zBuf = sqlite3MallocZero( nBuf+2 );
         4156  +  nMax = pVfs->mxPathname; nBuf = nMax + 2;
         4157  +  zBuf = sqlite3MallocZero( nBuf );
  4027   4158     if( !zBuf ){
  4028   4159       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4029   4160       return SQLITE_IOERR_NOMEM;
  4030   4161     }
  4031   4162   
  4032   4163     /* Figure out the effective temporary directory.  First, check if one
  4033   4164     ** has been explicitly set by the application; otherwise, use the one
  4034   4165     ** configured by the operating system.
  4035   4166     */
  4036         -  assert( nBuf>30 );
         4167  +  nDir = nMax - (nPre + 15);
         4168  +  assert( nDir>0 );
  4037   4169     if( sqlite3_temp_directory ){
  4038         -    sqlite3_snprintf(nBuf-30, zBuf, "%s%s", sqlite3_temp_directory,
  4039         -                     winEndsInDirSep(sqlite3_temp_directory) ? "" :
  4040         -                     winGetDirDep());
         4170  +    int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
         4171  +    if( nDirLen>0 ){
         4172  +      if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
         4173  +        nDirLen++;
         4174  +      }
         4175  +      if( nDirLen>nDir ){
         4176  +        sqlite3_free(zBuf);
         4177  +        OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
         4178  +        return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
         4179  +      }
         4180  +      sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
         4181  +    }
  4041   4182     }
  4042   4183   #if defined(__CYGWIN__)
  4043   4184     else{
  4044   4185       static const char *azDirs[] = {
  4045   4186          0, /* getenv("SQLITE_TMPDIR") */
  4046   4187          0, /* getenv("TMPDIR") */
  4047   4188          0, /* getenv("TMP") */
................................................................................
  4062   4203       if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
  4063   4204       if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
  4064   4205       for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
  4065   4206         void *zConverted;
  4066   4207         if( zDir==0 ) continue;
  4067   4208         /* If the path starts with a drive letter followed by the colon
  4068   4209         ** character, assume it is already a native Win32 path; otherwise,
  4069         -      ** it must be converted to a native Win32 path prior via the Cygwin
  4070         -      ** API prior to using it.
         4210  +      ** it must be converted to a native Win32 path via the Cygwin API
         4211  +      ** prior to using it.
  4071   4212         */
  4072   4213         if( winIsDriveLetterAndColon(zDir) ){
  4073   4214           zConverted = winConvertFromUtf8Filename(zDir);
  4074   4215           if( !zConverted ){
  4075   4216             sqlite3_free(zBuf);
  4076   4217             OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4077   4218             return SQLITE_IOERR_NOMEM;
  4078   4219           }
  4079   4220           if( winIsDir(zConverted) ){
  4080         -          sqlite3_snprintf(nBuf-30, zBuf, "%s", zDir);
         4221  +          sqlite3_snprintf(nMax, zBuf, "%s", zDir);
  4081   4222             sqlite3_free(zConverted);
  4082   4223             break;
  4083   4224           }
  4084   4225           sqlite3_free(zConverted);
  4085   4226         }else{
  4086         -        zConverted = sqlite3MallocZero( nBuf+1 );
         4227  +        zConverted = sqlite3MallocZero( nMax+1 );
  4087   4228           if( !zConverted ){
  4088   4229             sqlite3_free(zBuf);
  4089   4230             OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4090   4231             return SQLITE_IOERR_NOMEM;
  4091   4232           }
  4092   4233           if( cygwin_conv_path(
  4093   4234                   osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
  4094         -                zConverted, nBuf+1)<0 ){
         4235  +                zConverted, nMax+1)<0 ){
  4095   4236             sqlite3_free(zConverted);
  4096   4237             sqlite3_free(zBuf);
  4097   4238             OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
  4098   4239             return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
  4099         -                             "winGetTempname1", zDir);
         4240  +                             "winGetTempname2", zDir);
  4100   4241           }
  4101   4242           if( winIsDir(zConverted) ){
  4102   4243             /* At this point, we know the candidate directory exists and should
  4103   4244             ** be used.  However, we may need to convert the string containing
  4104   4245             ** its name into UTF-8 (i.e. if it is UTF-16 right now).
  4105   4246             */
  4106   4247             if( osIsNT() ){
................................................................................
  4107   4248               char *zUtf8 = winUnicodeToUtf8(zConverted);
  4108   4249               if( !zUtf8 ){
  4109   4250                 sqlite3_free(zConverted);
  4110   4251                 sqlite3_free(zBuf);
  4111   4252                 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4112   4253                 return SQLITE_IOERR_NOMEM;
  4113   4254               }
  4114         -            sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
         4255  +            sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
  4115   4256               sqlite3_free(zUtf8);
  4116   4257               sqlite3_free(zConverted);
  4117   4258               break;
  4118   4259             }else{
  4119         -            sqlite3_snprintf(nBuf-30, zBuf, "%s", zConverted);
         4260  +            sqlite3_snprintf(nMax, zBuf, "%s", zConverted);
  4120   4261               sqlite3_free(zConverted);
  4121   4262               break;
  4122   4263             }
  4123   4264           }
  4124   4265           sqlite3_free(zConverted);
  4125   4266         }
  4126   4267       }
  4127   4268     }
  4128   4269   #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
  4129   4270     else if( osIsNT() ){
  4130   4271       char *zMulti;
  4131         -    LPWSTR zWidePath = sqlite3MallocZero( nBuf*sizeof(WCHAR) );
         4272  +    LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
  4132   4273       if( !zWidePath ){
  4133   4274         sqlite3_free(zBuf);
  4134   4275         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4135   4276         return SQLITE_IOERR_NOMEM;
  4136   4277       }
  4137         -    if( osGetTempPathW(nBuf, zWidePath)==0 ){
         4278  +    if( osGetTempPathW(nMax, zWidePath)==0 ){
  4138   4279         sqlite3_free(zWidePath);
  4139   4280         sqlite3_free(zBuf);
  4140   4281         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
  4141   4282         return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
  4142         -                         "winGetTempname1", 0);
         4283  +                         "winGetTempname2", 0);
  4143   4284       }
  4144   4285       zMulti = winUnicodeToUtf8(zWidePath);
  4145   4286       if( zMulti ){
  4146         -      sqlite3_snprintf(nBuf-30, zBuf, "%s", zMulti);
         4287  +      sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
  4147   4288         sqlite3_free(zMulti);
  4148   4289         sqlite3_free(zWidePath);
  4149   4290       }else{
  4150   4291         sqlite3_free(zWidePath);
  4151   4292         sqlite3_free(zBuf);
  4152   4293         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4153   4294         return SQLITE_IOERR_NOMEM;
  4154   4295       }
  4155   4296     }
  4156   4297   #ifdef SQLITE_WIN32_HAS_ANSI
  4157   4298     else{
  4158   4299       char *zUtf8;
  4159         -    char *zMbcsPath = sqlite3MallocZero( nBuf );
         4300  +    char *zMbcsPath = sqlite3MallocZero( nMax );
  4160   4301       if( !zMbcsPath ){
  4161   4302         sqlite3_free(zBuf);
  4162   4303         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4163   4304         return SQLITE_IOERR_NOMEM;
  4164   4305       }
  4165         -    if( osGetTempPathA(nBuf, zMbcsPath)==0 ){
         4306  +    if( osGetTempPathA(nMax, zMbcsPath)==0 ){
  4166   4307         sqlite3_free(zBuf);
  4167   4308         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
  4168   4309         return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
  4169         -                         "winGetTempname2", 0);
         4310  +                         "winGetTempname3", 0);
  4170   4311       }
  4171   4312       zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
  4172   4313       if( zUtf8 ){
  4173         -      sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
         4314  +      sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
  4174   4315         sqlite3_free(zUtf8);
  4175   4316       }else{
  4176   4317         sqlite3_free(zBuf);
  4177   4318         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4178   4319         return SQLITE_IOERR_NOMEM;
  4179   4320       }
  4180   4321     }
  4181   4322   #endif /* SQLITE_WIN32_HAS_ANSI */
  4182   4323   #endif /* !SQLITE_OS_WINRT */
  4183   4324   
  4184         -  /* Check that the output buffer is large enough for the temporary file 
  4185         -  ** name. If it is not, return SQLITE_ERROR.
         4325  +  /*
         4326  +  ** Check to make sure the temporary directory ends with an appropriate
         4327  +  ** separator.  If it does not and there is not enough space left to add
         4328  +  ** one, fail.
         4329  +  */
         4330  +  if( !winMakeEndInDirSep(nDir+1, zBuf) ){
         4331  +    sqlite3_free(zBuf);
         4332  +    OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
         4333  +    return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
         4334  +  }
         4335  +
         4336  +  /*
         4337  +  ** Check that the output buffer is large enough for the temporary file 
         4338  +  ** name in the following format:
         4339  +  **
         4340  +  **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
         4341  +  **
         4342  +  ** If not, return SQLITE_ERROR.  The number 17 is used here in order to
         4343  +  ** account for the space used by the 15 character random suffix and the
         4344  +  ** two trailing NUL characters.  The final directory separator character
         4345  +  ** has already added if it was not already present.
  4186   4346     */
  4187   4347     nLen = sqlite3Strlen30(zBuf);
  4188         -
  4189         -  if( (nLen + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
         4348  +  if( (nLen + nPre + 17) > nBuf ){
  4190   4349       sqlite3_free(zBuf);
  4191   4350       OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
  4192         -    return winLogError(SQLITE_ERROR, 0, "winGetTempname3", 0);
         4351  +    return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
  4193   4352     }
  4194   4353   
  4195         -  sqlite3_snprintf(nBuf-18-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
         4354  +  sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
  4196   4355   
  4197   4356     j = sqlite3Strlen30(zBuf);
  4198   4357     sqlite3_randomness(15, &zBuf[j]);
  4199   4358     for(i=0; i<15; i++, j++){
  4200   4359       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  4201   4360     }
  4202   4361     zBuf[j] = 0;
................................................................................
  4243   4402     sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
  4244   4403     const char *zName,        /* Name of the file (UTF-8) */
  4245   4404     sqlite3_file *id,         /* Write the SQLite file handle here */
  4246   4405     int flags,                /* Open mode flags */
  4247   4406     int *pOutFlags            /* Status return flags */
  4248   4407   ){
  4249   4408     HANDLE h;
  4250         -  DWORD lastErrno;
         4409  +  DWORD lastErrno = 0;
  4251   4410     DWORD dwDesiredAccess;
  4252   4411     DWORD dwShareMode;
  4253   4412     DWORD dwCreationDisposition;
  4254   4413     DWORD dwFlagsAndAttributes = 0;
  4255   4414   #if SQLITE_OS_WINCE
  4256   4415     int isTemp = 0;
  4257   4416   #endif
................................................................................
  4534   4693     sqlite3_vfs *pVfs,          /* Not used on win32 */
  4535   4694     const char *zFilename,      /* Name of file to delete */
  4536   4695     int syncDir                 /* Not used on win32 */
  4537   4696   ){
  4538   4697     int cnt = 0;
  4539   4698     int rc;
  4540   4699     DWORD attr;
  4541         -  DWORD lastErrno;
         4700  +  DWORD lastErrno = 0;
  4542   4701     void *zConverted;
  4543   4702     UNUSED_PARAMETER(pVfs);
  4544   4703     UNUSED_PARAMETER(syncDir);
  4545   4704   
  4546   4705     SimulateIOError(return SQLITE_IOERR_DELETE);
  4547   4706     OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
  4548   4707   
................................................................................
  4642   4801     sqlite3_vfs *pVfs,         /* Not used on win32 */
  4643   4802     const char *zFilename,     /* Name of file to check */
  4644   4803     int flags,                 /* Type of test to make on this file */
  4645   4804     int *pResOut               /* OUT: Result */
  4646   4805   ){
  4647   4806     DWORD attr;
  4648   4807     int rc = 0;
  4649         -  DWORD lastErrno;
         4808  +  DWORD lastErrno = 0;
  4650   4809     void *zConverted;
  4651   4810     UNUSED_PARAMETER(pVfs);
  4652   4811   
  4653   4812     SimulateIOError( return SQLITE_IOERR_ACCESS; );
  4654   4813     OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
  4655   4814              zFilename, flags, pResOut));
  4656   4815   
................................................................................
  4788   4947       }
  4789   4948       if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
  4790   4949                            pVfs->mxPathname+1)<0 ){
  4791   4950         sqlite3_free(zOut);
  4792   4951         return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
  4793   4952                            "winFullPathname1", zRelative);
  4794   4953       }
  4795         -    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
  4796         -                     sqlite3_data_directory, winGetDirDep(), zOut);
         4954  +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
         4955  +                     sqlite3_data_directory, winGetDirSep(), zOut);
  4797   4956       sqlite3_free(zOut);
  4798   4957     }else{
  4799   4958       if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
  4800   4959         return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
  4801   4960                            "winFullPathname2", zRelative);
  4802   4961       }
  4803   4962     }
................................................................................
  4811   4970     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
  4812   4971       /*
  4813   4972       ** NOTE: We are dealing with a relative path name and the data
  4814   4973       **       directory has been set.  Therefore, use it as the basis
  4815   4974       **       for converting the relative path name to an absolute
  4816   4975       **       one by prepending the data directory and a backslash.
  4817   4976       */
  4818         -    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
  4819         -                     sqlite3_data_directory, winGetDirDep(), zRelative);
         4977  +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
         4978  +                     sqlite3_data_directory, winGetDirSep(), zRelative);
  4820   4979     }else{
  4821   4980       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
  4822   4981     }
  4823   4982     return SQLITE_OK;
  4824   4983   #endif
  4825   4984   
  4826   4985   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
................................................................................
  4844   5003     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
  4845   5004       /*
  4846   5005       ** NOTE: We are dealing with a relative path name and the data
  4847   5006       **       directory has been set.  Therefore, use it as the basis
  4848   5007       **       for converting the relative path name to an absolute
  4849   5008       **       one by prepending the data directory and a backslash.
  4850   5009       */
  4851         -    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
  4852         -                     sqlite3_data_directory, winGetDirDep(), zRelative);
         5010  +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
         5011  +                     sqlite3_data_directory, winGetDirSep(), zRelative);
  4853   5012       return SQLITE_OK;
  4854   5013     }
  4855   5014     zConverted = winConvertFromUtf8Filename(zRelative);
  4856   5015     if( zConverted==0 ){
  4857   5016       return SQLITE_IOERR_NOMEM;
  4858   5017     }
  4859   5018     if( osIsNT() ){
................................................................................
  5183   5342       winGetSystemCall,    /* xGetSystemCall */
  5184   5343       winNextSystemCall,   /* xNextSystemCall */
  5185   5344     };
  5186   5345   #endif
  5187   5346   
  5188   5347     /* Double-check that the aSyscall[] array has been constructed
  5189   5348     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  5190         -  assert( ArraySize(aSyscall)==75 );
         5349  +  assert( ArraySize(aSyscall)==76 );
  5191   5350   
  5192   5351     /* get memory map allocation granularity */
  5193   5352     memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
  5194   5353   #if SQLITE_OS_WINRT
  5195   5354     osGetNativeSystemInfo(&winSysInfo);
  5196   5355   #else
  5197   5356     osGetSystemInfo(&winSysInfo);

Changes to src/parse.y.

   159    159   ifnotexists(A) ::= .              {A = 0;}
   160    160   ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
   161    161   %type temp {int}
   162    162   %ifndef SQLITE_OMIT_TEMPDB
   163    163   temp(A) ::= TEMP.  {A = 1;}
   164    164   %endif  SQLITE_OMIT_TEMPDB
   165    165   temp(A) ::= .      {A = 0;}
   166         -create_table_args ::= LP columnlist conslist_opt(X) RP(Y). {
   167         -  sqlite3EndTable(pParse,&X,&Y,0);
          166  +create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
          167  +  sqlite3EndTable(pParse,&X,&E,F,0);
   168    168   }
   169    169   create_table_args ::= AS select(S). {
   170         -  sqlite3EndTable(pParse,0,0,S);
          170  +  sqlite3EndTable(pParse,0,0,0,S);
   171    171     sqlite3SelectDelete(pParse->db, S);
          172  +}
          173  +%type table_options {u8}
          174  +table_options(A) ::= .    {A = 0;}
          175  +table_options(A) ::= WITHOUT nm(X). {
          176  +  if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
          177  +    A = TF_WithoutRowid;
          178  +  }else{
          179  +    A = 0;
          180  +    sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
          181  +  }
   172    182   }
   173    183   columnlist ::= columnlist COMMA column.
   174    184   columnlist ::= column.
   175    185   
   176    186   // A "column" is a complete description of a single column in a
   177    187   // CREATE TABLE statement.  This includes the column name, its
   178    188   // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
................................................................................
   201    211   // This obviates the need for the "id" nonterminal.
   202    212   //
   203    213   %fallback ID
   204    214     ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
   205    215     CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
   206    216     IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
   207    217     QUERY KEY OF OFFSET PRAGMA RAISE RELEASE REPLACE RESTRICT ROW ROLLBACK
   208         -  SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL
          218  +  SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITHOUT
   209    219   %ifdef SQLITE_OMIT_COMPOUND_SELECT
   210    220     EXCEPT INTERSECT UNION
   211    221   %endif SQLITE_OMIT_COMPOUND_SELECT
   212    222     REINDEX RENAME CTIME_KW IF
   213    223     .
   214    224   %wildcard ANY.
   215    225   
................................................................................
   569    579   %type indexed_opt {Token}
   570    580   indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
   571    581   indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
   572    582   indexed_opt(A) ::= NOT INDEXED.      {A.z=0; A.n=1;}
   573    583   
   574    584   %type using_opt {IdList*}
   575    585   %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
   576         -using_opt(U) ::= USING LP inscollist(L) RP.  {U = L;}
          586  +using_opt(U) ::= USING LP idlist(L) RP.  {U = L;}
   577    587   using_opt(U) ::= .                        {U = 0;}
   578    588   
   579    589   
   580    590   %type orderby_opt {ExprList*}
   581    591   %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
   582    592   %type sortlist {ExprList*}
   583    593   %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
................................................................................
   736    746       A.pSelect = pRight;
   737    747     }
   738    748   }
   739    749   %endif SQLITE_OMIT_COMPOUND_SELECT
   740    750   
   741    751   %type inscollist_opt {IdList*}
   742    752   %destructor inscollist_opt {sqlite3IdListDelete(pParse->db, $$);}
   743         -%type inscollist {IdList*}
   744         -%destructor inscollist {sqlite3IdListDelete(pParse->db, $$);}
          753  +%type idlist {IdList*}
          754  +%destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
   745    755   
   746    756   inscollist_opt(A) ::= .                       {A = 0;}
   747         -inscollist_opt(A) ::= LP inscollist(X) RP.    {A = X;}
   748         -inscollist(A) ::= inscollist(X) COMMA nm(Y).
          757  +inscollist_opt(A) ::= LP idlist(X) RP.    {A = X;}
          758  +idlist(A) ::= idlist(X) COMMA nm(Y).
   749    759       {A = sqlite3IdListAppend(pParse->db,X,&Y);}
   750         -inscollist(A) ::= nm(Y).
          760  +idlist(A) ::= nm(Y).
   751    761       {A = sqlite3IdListAppend(pParse->db,0,&Y);}
   752    762   
   753    763   /////////////////////////// Expression Processing /////////////////////////////
   754    764   //
   755    765   
   756    766   %type expr {ExprSpan}
   757    767   %destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);}
................................................................................
  1223   1233   trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
  1224   1234   trigger_time(A) ::= .            { A = TK_BEFORE; }
  1225   1235   
  1226   1236   %type trigger_event {struct TrigEvent}
  1227   1237   %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
  1228   1238   trigger_event(A) ::= DELETE|INSERT(OP).       {A.a = @OP; A.b = 0;}
  1229   1239   trigger_event(A) ::= UPDATE(OP).              {A.a = @OP; A.b = 0;}
  1230         -trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
         1240  +trigger_event(A) ::= UPDATE OF idlist(X). {A.a = TK_UPDATE; A.b = X;}
  1231   1241   
  1232   1242   foreach_clause ::= .
  1233   1243   foreach_clause ::= FOR EACH ROW.
  1234   1244   
  1235   1245   %type when_clause {Expr*}
  1236   1246   %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
  1237   1247   when_clause(A) ::= .             { A = 0; }

Changes to src/pragma.c.

  1421   1421     case PragTyp_TABLE_INFO: if( zRight ){
  1422   1422       Table *pTab;
  1423   1423       pTab = sqlite3FindTable(db, zRight, zDb);
  1424   1424       if( pTab ){
  1425   1425         int i, k;
  1426   1426         int nHidden = 0;
  1427   1427         Column *pCol;
  1428         -      Index *pPk;
  1429         -      for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){}
         1428  +      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
  1430   1429         sqlite3VdbeSetNumCols(v, 6);
  1431   1430         pParse->nMem = 6;
  1432   1431         sqlite3CodeVerifySchema(pParse, iDb);
  1433   1432         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
  1434   1433         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
  1435   1434         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
  1436   1435         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
................................................................................
  1505   1504         pTab = pIdx->pTable;
  1506   1505         sqlite3VdbeSetNumCols(v, 3);
  1507   1506         pParse->nMem = 3;
  1508   1507         sqlite3CodeVerifySchema(pParse, iDb);
  1509   1508         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
  1510   1509         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
  1511   1510         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
  1512         -      for(i=0; i<pIdx->nColumn; i++){
  1513         -        int cnum = pIdx->aiColumn[i];
         1511  +      for(i=0; i<pIdx->nKeyCol; i++){
         1512  +        i16 cnum = pIdx->aiColumn[i];
  1514   1513           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
  1515   1514           sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
  1516   1515           assert( pTab->nCol>cnum );
  1517   1516           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
  1518   1517           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
  1519   1518         }
  1520   1519       }
................................................................................
  1677   1676           pIdx = 0;
  1678   1677           sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
  1679   1678           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
  1680   1679           if( x==0 ){
  1681   1680             if( pIdx==0 ){
  1682   1681               sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
  1683   1682             }else{
  1684         -            KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
  1685   1683               sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
  1686         -            sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
         1684  +            sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1687   1685             }
  1688   1686           }else{
  1689   1687             k = 0;
  1690   1688             break;
  1691   1689           }
  1692   1690         }
  1693   1691         assert( pParse->nErr>0 || pFK==0 );
................................................................................
  1843   1841         ** for all tables and indices in the database.
  1844   1842         */
  1845   1843         assert( sqlite3SchemaMutexHeld(db, i, 0) );
  1846   1844         pTbls = &db->aDb[i].pSchema->tblHash;
  1847   1845         for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  1848   1846           Table *pTab = sqliteHashData(x);
  1849   1847           Index *pIdx;
  1850         -        sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
  1851         -        cnt++;
         1848  +        if( HasRowid(pTab) ){
         1849  +          sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
         1850  +          VdbeComment((v, "%s", pTab->zName));
         1851  +          cnt++;
         1852  +        }
  1852   1853           for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1853   1854             sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
         1855  +          VdbeComment((v, "%s", pIdx->zName));
  1854   1856             cnt++;
  1855   1857           }
  1856   1858         }
  1857   1859   
  1858   1860         /* Make sure sufficient number of registers have been allocated */
  1859         -      pParse->nMem = MAX( pParse->nMem, cnt+7 );
         1861  +      pParse->nMem = MAX( pParse->nMem, cnt+8 );
  1860   1862   
  1861   1863         /* Do the b-tree integrity checks */
  1862   1864         sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
  1863   1865         sqlite3VdbeChangeP5(v, (u8)i);
  1864   1866         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
  1865   1867         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1866   1868            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
................................................................................
  1870   1872         sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
  1871   1873         sqlite3VdbeJumpHere(v, addr);
  1872   1874   
  1873   1875         /* Make sure all the indices are constructed correctly.
  1874   1876         */
  1875   1877         for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
  1876   1878           Table *pTab = sqliteHashData(x);
  1877         -        Index *pIdx;
         1879  +        Index *pIdx, *pPk;
  1878   1880           int loopTop;
         1881  +        int iDataCur, iIdxCur;
  1879   1882   
  1880   1883           if( pTab->pIndex==0 ) continue;
         1884  +        pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
  1881   1885           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
  1882   1886           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1883   1887           sqlite3VdbeJumpHere(v, addr);
  1884   1888           sqlite3ExprCacheClear(pParse);
  1885         -        sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
         1889  +        sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
         1890  +                                   1, &iDataCur, &iIdxCur);
         1891  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
  1886   1892           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1887         -          sqlite3VdbeAddOp2(v, OP_Integer, 0, 7+j); /* index entries counter */
         1893  +          sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
  1888   1894           }
  1889         -        pParse->nMem = MAX(pParse->nMem, 7+j);
  1890         -        loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0) + 1;
         1895  +        pParse->nMem = MAX(pParse->nMem, 8+j);
         1896  +        sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0);
         1897  +        loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
  1891   1898           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1892         -          int jmp2, jmp3;
         1899  +          int jmp2, jmp3, jmp4;
  1893   1900             int r1;
  1894         -          static const VdbeOpList idxErr[] = {
  1895         -            { OP_AddImm,      1, -1,  0},
  1896         -            { OP_String8,     0,  3,  0},    /* 1 */
  1897         -            { OP_Rowid,       1,  4,  0},
  1898         -            { OP_String8,     0,  5,  0},    /* 3 */
  1899         -            { OP_String8,     0,  6,  0},    /* 4 */
  1900         -            { OP_Concat,      4,  3,  3},
  1901         -            { OP_Concat,      5,  3,  3},
  1902         -            { OP_Concat,      6,  3,  3},
  1903         -            { OP_ResultRow,   3,  1,  0},
  1904         -            { OP_IfPos,       1,  0,  0},    /* 9 */
  1905         -            { OP_Halt,        0,  0,  0},
  1906         -          };
  1907         -          r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0, &jmp3);
  1908         -          sqlite3VdbeAddOp2(v, OP_AddImm, 7+j, 1);  /* increment entry count */
  1909         -          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
  1910         -          addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
  1911         -          sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
  1912         -          sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
  1913         -          sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
  1914         -          sqlite3VdbeJumpHere(v, addr+9);
         1901  +          if( pPk==pIdx ) continue;
         1902  +          r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3);
         1903  +          sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
         1904  +          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
         1905  +                                      pIdx->nColumn);
         1906  +          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
         1907  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
         1908  +          sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
         1909  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
         1910  +                            P4_STATIC);
         1911  +          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
         1912  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
         1913  +          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
         1914  +          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
         1915  +          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
         1916  +          sqlite3VdbeAddOp0(v, OP_Halt);
         1917  +          sqlite3VdbeJumpHere(v, jmp4);
  1915   1918             sqlite3VdbeJumpHere(v, jmp2);
  1916   1919             sqlite3VdbeResolveLabel(v, jmp3);
  1917   1920           }
  1918         -        sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop);
         1921  +        sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop);
  1919   1922           sqlite3VdbeJumpHere(v, loopTop-1);
  1920   1923   #ifndef SQLITE_OMIT_BTREECOUNT
  1921   1924           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
  1922   1925                        "wrong # of entries in index ", P4_STATIC);
  1923   1926           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
         1927  +          if( pPk==pIdx ) continue;
  1924   1928             addr = sqlite3VdbeCurrentAddr(v);
  1925   1929             sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
  1926   1930             sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1927         -          sqlite3VdbeAddOp2(v, OP_Count, j+2, 3);
  1928         -          sqlite3VdbeAddOp3(v, OP_Eq, 7+j, addr+8, 3);
         1931  +          sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
         1932  +          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3);
  1929   1933             sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
  1930   1934             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
  1931   1935             sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
  1932   1936             sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
  1933   1937           }
  1934   1938   #endif /* SQLITE_OMIT_BTREECOUNT */
  1935   1939         } 

Changes to src/resolve.c.

   222    222     int cntTab = 0;                   /* Number of matching table names */
   223    223     int nSubquery = 0;                /* How many levels of subquery */
   224    224     sqlite3 *db = pParse->db;         /* The database connection */
   225    225     struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
   226    226     struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
   227    227     NameContext *pTopNC = pNC;        /* First namecontext in the list */
   228    228     Schema *pSchema = 0;              /* Schema of the expression */
   229         -  int isTrigger = 0;
          229  +  int isTrigger = 0;                /* True if resolved to a trigger column */
          230  +  Table *pTab = 0;                  /* Table hold the row */
          231  +  Column *pCol;                     /* A column of pTab */
   230    232   
   231    233     assert( pNC );     /* the name context cannot be NULL. */
   232    234     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
   233    235     assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
   234    236   
   235    237     /* Initialize the node to no-match */
   236    238     pExpr->iTable = -1;
................................................................................
   263    265     /* Start at the inner-most context and move outward until a match is found */
   264    266     while( pNC && cnt==0 ){
   265    267       ExprList *pEList;
   266    268       SrcList *pSrcList = pNC->pSrcList;
   267    269   
   268    270       if( pSrcList ){
   269    271         for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
   270         -        Table *pTab;
   271         -        Column *pCol;
   272         -  
   273    272           pTab = pItem->pTab;
   274    273           assert( pTab!=0 && pTab->zName!=0 );
   275    274           assert( pTab->nCol>0 );
   276    275           if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
   277    276             int hit = 0;
   278    277             pEList = pItem->pSelect->pEList;
   279    278             for(j=0; j<pEList->nExpr; j++){
................................................................................
   325    324         }
   326    325       } /* if( pSrcList ) */
   327    326   
   328    327   #ifndef SQLITE_OMIT_TRIGGER
   329    328       /* If we have not already resolved the name, then maybe 
   330    329       ** it is a new.* or old.* trigger argument reference
   331    330       */
   332         -    if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
          331  +    if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
   333    332         int op = pParse->eTriggerOp;
   334         -      Table *pTab = 0;
   335    333         assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
   336    334         if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
   337    335           pExpr->iTable = 1;
   338    336           pTab = pParse->pTriggerTab;
   339    337         }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
   340    338           pExpr->iTable = 0;
   341    339           pTab = pParse->pTriggerTab;
   342    340         }
   343    341   
   344    342         if( pTab ){ 
   345    343           int iCol;
   346    344           pSchema = pTab->pSchema;
   347    345           cntTab++;
   348         -        for(iCol=0; iCol<pTab->nCol; iCol++){
   349         -          Column *pCol = &pTab->aCol[iCol];
          346  +        for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
   350    347             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
   351    348               if( iCol==pTab->iPKey ){
   352    349                 iCol = -1;
   353    350               }
   354    351               break;
   355    352             }
   356    353           }
   357         -        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
          354  +        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
   358    355             iCol = -1;        /* IMP: R-44911-55124 */
   359    356           }
   360    357           if( iCol<pTab->nCol ){
   361    358             cnt++;
   362    359             if( iCol<0 ){
   363    360               pExpr->affinity = SQLITE_AFF_INTEGER;
   364    361             }else if( pExpr->iTable==0 ){
................................................................................
   377    374         }
   378    375       }
   379    376   #endif /* !defined(SQLITE_OMIT_TRIGGER) */
   380    377   
   381    378       /*
   382    379       ** Perhaps the name is a reference to the ROWID
   383    380       */
   384         -    if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
          381  +    assert( pTab!=0 || cntTab==0 );
          382  +    if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
   385    383         cnt = 1;
   386    384         pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
   387    385         pExpr->affinity = SQLITE_AFF_INTEGER;
   388    386       }
   389    387   
   390    388       /*
   391    389       ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z

Changes to src/select.c.

   799    799     */
   800    800     if( pOrderBy==0 && p->iLimit ){
   801    801       sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
   802    802     }
   803    803   }
   804    804   
   805    805   /*
   806         -** Allocate a KeyInfo object sufficient for an index of N columns.
   807         -**
   808         -** Actually, always allocate one extra column for the rowid at the end
   809         -** of the index.  So the KeyInfo returned will have space sufficient for
   810         -** N+1 columns.
          806  +** Allocate a KeyInfo object sufficient for an index of N key columns and
          807  +** X extra columns.
   811    808   */
   812         -KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N){
   813         -  KeyInfo *p = sqlite3DbMallocZero(db, 
   814         -                   sizeof(KeyInfo) + (N+1)*(sizeof(CollSeq*)+1));
          809  +KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
          810  +  KeyInfo *p = sqlite3DbMallocZero(0, 
          811  +                   sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1));
   815    812     if( p ){
   816         -    p->aSortOrder = (u8*)&p->aColl[N+1];
          813  +    p->aSortOrder = (u8*)&p->aColl[N+X];
   817    814       p->nField = (u16)N;
          815  +    p->nXField = (u16)X;
   818    816       p->enc = ENC(db);
   819    817       p->db = db;
          818  +    p->nRef = 1;
          819  +  }else{
          820  +    db->mallocFailed = 1;
          821  +  }
          822  +  return p;
          823  +}
          824  +
          825  +/*
          826  +** Deallocate a KeyInfo object
          827  +*/
          828  +void sqlite3KeyInfoUnref(KeyInfo *p){
          829  +  if( p ){
          830  +    assert( p->nRef>0 );
          831  +    p->nRef--;
          832  +    if( p->nRef==0 ) sqlite3DbFree(0, p);
          833  +  }
          834  +}
          835  +
          836  +/*
          837  +** Make a new pointer to a KeyInfo object
          838  +*/
          839  +KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
          840  +  if( p ){
          841  +    assert( p->nRef>0 );
          842  +    p->nRef++;
   820    843     }
   821    844     return p;
   822    845   }
   823    846   
          847  +#ifdef SQLITE_DEBUG
          848  +/*
          849  +** Return TRUE if a KeyInfo object can be change.  The KeyInfo object
          850  +** can only be changed if this is just a single reference to the object.
          851  +**
          852  +** This routine is used only inside of assert() statements.
          853  +*/
          854  +int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
          855  +#endif /* SQLITE_DEBUG */
          856  +
   824    857   /*
   825    858   ** Given an expression list, generate a KeyInfo structure that records
   826    859   ** the collating sequence for each expression in that expression list.
   827    860   **
   828    861   ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
   829    862   ** KeyInfo structure is appropriate for initializing a virtual index to
   830    863   ** implement that clause.  If the ExprList is the result set of a SELECT
   831    864   ** then the KeyInfo structure is appropriate for initializing a virtual
   832    865   ** index to implement a DISTINCT test.
   833    866   **
   834    867   ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
   835    868   ** function is responsible for seeing that this structure is eventually
   836         -** freed.  Add the KeyInfo structure to the P4 field of an opcode using
   837         -** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
          869  +** freed.
   838    870   */
   839    871   static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
   840    872     int nExpr;
   841    873     KeyInfo *pInfo;
   842    874     struct ExprList_item *pItem;
   843    875     sqlite3 *db = pParse->db;
   844    876     int i;
   845    877   
   846    878     nExpr = pList->nExpr;
   847         -  pInfo = sqlite3KeyInfoAlloc(db, nExpr);
          879  +  pInfo = sqlite3KeyInfoAlloc(db, nExpr, 1);
   848    880     if( pInfo ){
          881  +    assert( sqlite3KeyInfoIsWriteable(pInfo) );
   849    882       for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
   850    883         CollSeq *pColl;
   851    884         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
   852    885         if( !pColl ) pColl = db->pDfltColl;
   853    886         pInfo->aColl[i] = pColl;
   854    887         pInfo->aSortOrder[i] = pItem->sortOrder;
   855    888       }
................................................................................
  1984   2017       KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
  1985   2018       Select *pLoop;                /* For looping through SELECT statements */
  1986   2019       CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
  1987   2020       int nCol;                     /* Number of columns in result set */
  1988   2021   
  1989   2022       assert( p->pRightmost==p );
  1990   2023       nCol = p->pEList->nExpr;
  1991         -    pKeyInfo = sqlite3KeyInfoAlloc(db, nCol);
         2024  +    pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
  1992   2025       if( !pKeyInfo ){
  1993   2026         rc = SQLITE_NOMEM;
  1994   2027         goto multi_select_end;
  1995   2028       }
  1996   2029       for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
  1997   2030         *apColl = multiSelectCollSeq(pParse, p, i);
  1998   2031         if( 0==*apColl ){
................................................................................
  2006   2039           if( addr<0 ){
  2007   2040             /* If [0] is unused then [1] is also unused.  So we can
  2008   2041             ** always safely abort as soon as the first unused slot is found */
  2009   2042             assert( pLoop->addrOpenEphm[1]<0 );
  2010   2043             break;
  2011   2044           }
  2012   2045           sqlite3VdbeChangeP2(v, addr, nCol);
  2013         -        sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
         2046  +        sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
         2047  +                            P4_KEYINFO);
  2014   2048           pLoop->addrOpenEphm[i] = -1;
  2015   2049         }
  2016   2050       }
  2017         -    sqlite3DbFree(db, pKeyInfo);
         2051  +    sqlite3KeyInfoUnref(pKeyInfo);
  2018   2052     }
  2019   2053   
  2020   2054   multi_select_end:
  2021   2055     pDest->iSdst = dest.iSdst;
  2022   2056     pDest->nSdst = dest.nSdst;
  2023   2057     sqlite3SelectDelete(db, pDelete);
  2024   2058     return rc;
................................................................................
  2049   2083     Parse *pParse,          /* Parsing context */
  2050   2084     Select *p,              /* The SELECT statement */
  2051   2085     SelectDest *pIn,        /* Coroutine supplying data */
  2052   2086     SelectDest *pDest,      /* Where to send the data */
  2053   2087     int regReturn,          /* The return address register */
  2054   2088     int regPrev,            /* Previous result register.  No uniqueness if 0 */
  2055   2089     KeyInfo *pKeyInfo,      /* For comparing with previous entry */
  2056         -  int p4type,             /* The p4 type for pKeyInfo */
  2057   2090     int iBreak              /* Jump here if we hit the LIMIT */
  2058   2091   ){
  2059   2092     Vdbe *v = pParse->pVdbe;
  2060   2093     int iContinue;
  2061   2094     int addr;
  2062   2095   
  2063   2096     addr = sqlite3VdbeCurrentAddr(v);
................................................................................
  2065   2098   
  2066   2099     /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
  2067   2100     */
  2068   2101     if( regPrev ){
  2069   2102       int j1, j2;
  2070   2103       j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
  2071   2104       j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
  2072         -                              (char*)pKeyInfo, p4type);
         2105  +                              (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
  2073   2106       sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
  2074   2107       sqlite3VdbeJumpHere(v, j1);
  2075   2108       sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
  2076   2109       sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
  2077   2110     }
  2078   2111     if( pParse->db->mallocFailed ) return 0;
  2079   2112   
................................................................................
  2363   2396     aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
  2364   2397     if( aPermute ){
  2365   2398       struct ExprList_item *pItem;
  2366   2399       for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
  2367   2400         assert( pItem->iOrderByCol>0  && pItem->iOrderByCol<=p->pEList->nExpr );
  2368   2401         aPermute[i] = pItem->iOrderByCol - 1;
  2369   2402       }
  2370         -    pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy);
         2403  +    pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1);
  2371   2404       if( pKeyMerge ){
  2372   2405         for(i=0; i<nOrderBy; i++){
  2373   2406           CollSeq *pColl;
  2374   2407           Expr *pTerm = pOrderBy->a[i].pExpr;
  2375   2408           if( pTerm->flags & EP_Collate ){
  2376   2409             pColl = sqlite3ExprCollSeq(pParse, pTerm);
  2377   2410           }else{
  2378   2411             pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
  2379   2412             if( pColl==0 ) pColl = db->pDfltColl;
  2380   2413             pOrderBy->a[i].pExpr =
  2381   2414                sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
  2382   2415           }
         2416  +        assert( sqlite3KeyInfoIsWriteable(pKeyMerge) );
  2383   2417           pKeyMerge->aColl[i] = pColl;
  2384   2418           pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
  2385   2419         }
  2386   2420       }
  2387   2421     }else{
  2388   2422       pKeyMerge = 0;
  2389   2423     }
................................................................................
  2401   2435       regPrev = 0;
  2402   2436     }else{
  2403   2437       int nExpr = p->pEList->nExpr;
  2404   2438       assert( nOrderBy>=nExpr || db->mallocFailed );
  2405   2439       regPrev = pParse->nMem+1;
  2406   2440       pParse->nMem += nExpr+1;
  2407   2441       sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
  2408         -    pKeyDup = sqlite3KeyInfoAlloc(db, nExpr);
         2442  +    pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
  2409   2443       if( pKeyDup ){
         2444  +      assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
  2410   2445         for(i=0; i<nExpr; i++){
  2411   2446           pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
  2412   2447           pKeyDup->aSortOrder[i] = 0;
  2413   2448         }
  2414   2449       }
  2415   2450     }
  2416   2451    
................................................................................
  2484   2519   
  2485   2520     /* Generate a subroutine that outputs the current row of the A
  2486   2521     ** select as the next output row of the compound select.
  2487   2522     */
  2488   2523     VdbeNoopComment((v, "Output routine for A"));
  2489   2524     addrOutA = generateOutputSubroutine(pParse,
  2490   2525                    p, &destA, pDest, regOutA,
  2491         -                 regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
         2526  +                 regPrev, pKeyDup, labelEnd);
  2492   2527     
  2493   2528     /* Generate a subroutine that outputs the current row of the B
  2494   2529     ** select as the next output row of the compound select.
  2495   2530     */
  2496   2531     if( op==TK_ALL || op==TK_UNION ){
  2497   2532       VdbeNoopComment((v, "Output routine for B"));
  2498   2533       addrOutB = generateOutputSubroutine(pParse,
  2499   2534                    p, &destB, pDest, regOutB,
  2500         -                 regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
         2535  +                 regPrev, pKeyDup, labelEnd);
  2501   2536     }
         2537  +  sqlite3KeyInfoUnref(pKeyDup);
  2502   2538   
  2503   2539     /* Generate a subroutine to run when the results from select A
  2504   2540     ** are exhausted and only data in select B remains.
  2505   2541     */
  2506   2542     VdbeNoopComment((v, "eof-A subroutine"));
  2507   2543     if( op==TK_EXCEPT || op==TK_INTERSECT ){
  2508   2544       addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
................................................................................
  2573   2609     sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
  2574   2610   
  2575   2611     /* Implement the main merge loop
  2576   2612     */
  2577   2613     sqlite3VdbeResolveLabel(v, labelCmpr);
  2578   2614     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
  2579   2615     sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
  2580         -                         (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
         2616  +                         (char*)pKeyMerge, P4_KEYINFO);
  2581   2617     sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
  2582   2618     sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
  2583   2619   
  2584   2620     /* Jump to the this point in order to terminate the query.
  2585   2621     */
  2586   2622     sqlite3VdbeResolveLabel(v, labelEnd);
  2587   2623   
................................................................................
  3799   3835         if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
  3800   3836           sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
  3801   3837              "argument");
  3802   3838           pFunc->iDistinct = -1;
  3803   3839         }else{
  3804   3840           KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
  3805   3841           sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
  3806         -                          (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
         3842  +                          (char*)pKeyInfo, P4_KEYINFO);
  3807   3843         }
  3808   3844       }
  3809   3845     }
  3810   3846   }
  3811   3847   
  3812   3848   /*
  3813   3849   ** Invoke the OP_AggFinalize opcode for every aggregate function
................................................................................
  4254   4290     if( pOrderBy ){
  4255   4291       KeyInfo *pKeyInfo;
  4256   4292       pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
  4257   4293       pOrderBy->iECursor = pParse->nTab++;
  4258   4294       p->addrOpenEphm[2] = addrSortIndex =
  4259   4295         sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
  4260   4296                              pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
  4261         -                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
         4297  +                           (char*)pKeyInfo, P4_KEYINFO);
  4262   4298     }else{
  4263   4299       addrSortIndex = -1;
  4264   4300     }
  4265   4301   
  4266   4302     /* If the output is destined for a temporary table, open that table.
  4267   4303     */
  4268   4304     if( pDest->eDest==SRT_EphemTab ){
................................................................................
  4282   4318     /* Open a virtual index to use for the distinct set.
  4283   4319     */
  4284   4320     if( p->selFlags & SF_Distinct ){
  4285   4321       sDistinct.tabTnct = pParse->nTab++;
  4286   4322       sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
  4287   4323                                   sDistinct.tabTnct, 0, 0,
  4288   4324                                   (char*)keyInfoFromExprList(pParse, p->pEList),
  4289         -                                P4_KEYINFO_HANDOFF);
         4325  +                                P4_KEYINFO);
  4290   4326       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  4291   4327       sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
  4292   4328     }else{
  4293   4329       sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
  4294   4330     }
  4295   4331   
  4296   4332     if( !isAgg && pGroupBy==0 ){
................................................................................
  4406   4442         ** that we do not need it after all, the OP_SorterOpen instruction
  4407   4443         ** will be converted into a Noop.  
  4408   4444         */
  4409   4445         sAggInfo.sortingIdx = pParse->nTab++;
  4410   4446         pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
  4411   4447         addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
  4412   4448             sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
  4413         -          0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
         4449  +          0, (char*)pKeyInfo, P4_KEYINFO);
  4414   4450   
  4415   4451         /* Initialize memory locations used by GROUP BY aggregate processing
  4416   4452         */
  4417   4453         iUseFlag = ++pParse->nMem;
  4418   4454         iAbortFlag = ++pParse->nMem;
  4419   4455         regOutputRow = ++pParse->nMem;
  4420   4456         addrOutputRow = sqlite3VdbeMakeLabel(v);
................................................................................
  4520   4556             if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
  4521   4557           }else{
  4522   4558             sAggInfo.directMode = 1;
  4523   4559             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
  4524   4560           }
  4525   4561         }
  4526   4562         sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
  4527         -                          (char*)pKeyInfo, P4_KEYINFO);
         4563  +                          (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
  4528   4564         j1 = sqlite3VdbeCurrentAddr(v);
  4529   4565         sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
  4530   4566   
  4531   4567         /* Generate code that runs whenever the GROUP BY changes.
  4532   4568         ** Changes in the GROUP BY are detected by the previous code
  4533   4569         ** block.  If there were no changes, this block is skipped.
  4534   4570         **
................................................................................
  4646   4682              && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
  4647   4683             ){
  4648   4684               pBest = pIdx;
  4649   4685             }
  4650   4686           }
  4651   4687           if( pBest ){
  4652   4688             iRoot = pBest->tnum;
  4653         -          pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
         4689  +          pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
  4654   4690           }
  4655   4691   
  4656   4692           /* Open a read-only cursor, execute the OP_Count, close the cursor. */
  4657         -        sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
         4693  +        sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
  4658   4694           if( pKeyInfo ){
  4659         -          sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
         4695  +          sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
  4660   4696           }
  4661   4697           sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
  4662   4698           sqlite3VdbeAddOp1(v, OP_Close, iCsr);
  4663   4699           explainSimpleCount(pParse, pTab, pBest);
  4664   4700         }else
  4665   4701   #endif /* SQLITE_OMIT_BTREECOUNT */
  4666   4702         {

Changes to src/shell.c.

    82     82   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
    83     83    * thus we always assume that we have a console. That can be
    84     84    * overridden with the -batch command line option.
    85     85    */
    86     86   #define isatty(x) 1
    87     87   #endif
    88     88   
    89         -/* True if the timer is enabled */
    90         -static int enableTimer = 0;
    91         -
    92     89   /* ctype macros that work with signed characters */
    93     90   #define IsSpace(X)  isspace((unsigned char)X)
    94     91   #define IsDigit(X)  isdigit((unsigned char)X)
    95     92   #define ToLower(X)  (char)tolower((unsigned char)X)
    96     93   
           94  +
           95  +/* True if the timer is enabled */
           96  +static int enableTimer = 0;
           97  +
           98  +/* Return the current wall-clock time */
           99  +static sqlite3_int64 timeOfDay(void){
          100  +  static sqlite3_vfs *clockVfs = 0;
          101  +  sqlite3_int64 t;
          102  +  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
          103  +  if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
          104  +    clockVfs->xCurrentTimeInt64(clockVfs, &t);
          105  +  }else{
          106  +    double r;
          107  +    clockVfs->xCurrentTime(clockVfs, &r);
          108  +    t = (sqlite3_int64)(r*86400000.0);
          109  +  }
          110  +  return t;
          111  +}
          112  +
    97    113   #if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL) \
    98    114    && !defined(__minux)
    99    115   #include <sys/time.h>
   100    116   #include <sys/resource.h>
   101    117   
   102    118   /* Saved resource information for the beginning of an operation */
   103         -static struct rusage sBegin;
          119  +static struct rusage sBegin;  /* CPU time at start */
          120  +static sqlite3_int64 iBegin;  /* Wall-clock time at start */
   104    121   
   105    122   /*
   106    123   ** Begin timing an operation
   107    124   */
   108    125   static void beginTimer(void){
   109    126     if( enableTimer ){
   110    127       getrusage(RUSAGE_SELF, &sBegin);
          128  +    iBegin = timeOfDay();
   111    129     }
   112    130   }
   113    131   
   114    132   /* Return the difference of two time_structs in seconds */
   115    133   static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
   116    134     return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + 
   117    135            (double)(pEnd->tv_sec - pStart->tv_sec);
................................................................................
   119    137   
   120    138   /*
   121    139   ** Print the timing results.
   122    140   */
   123    141   static void endTimer(void){
   124    142     if( enableTimer ){
   125    143       struct rusage sEnd;
          144  +    sqlite3_int64 iEnd = timeOfDay();
   126    145       getrusage(RUSAGE_SELF, &sEnd);
   127         -    printf("CPU Time: user %f sys %f\n",
          146  +    printf("Run Time: real %.3f user %f sys %f\n",
          147  +       (iEnd - iBegin)*0.001,
   128    148          timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
   129    149          timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
   130    150     }
   131    151   }
   132    152   
   133    153   #define BEGIN_TIMER beginTimer()
   134    154   #define END_TIMER endTimer()
................................................................................
   138    158   
   139    159   #include <windows.h>
   140    160   
   141    161   /* Saved resource information for the beginning of an operation */
   142    162   static HANDLE hProcess;
   143    163   static FILETIME ftKernelBegin;
   144    164   static FILETIME ftUserBegin;
          165  +static sqlite3_int64 ftWallBegin;
   145    166   typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME);
   146    167   static GETPROCTIMES getProcessTimesAddr = NULL;
   147    168   
   148    169   /*
   149    170   ** Check to see if we have timer support.  Return 1 if necessary
   150    171   ** support found (or found previously).
   151    172   */
................................................................................
   175    196   /*
   176    197   ** Begin timing an operation
   177    198   */
   178    199   static void beginTimer(void){
   179    200     if( enableTimer && getProcessTimesAddr ){
   180    201       FILETIME ftCreation, ftExit;
   181    202       getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin);
          203  +    ftWallBegin = timeOfDay();
   182    204     }
   183    205   }
   184    206   
   185    207   /* Return the difference of two FILETIME structs in seconds */
   186    208   static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
   187    209     sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
   188    210     sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
................................................................................
   191    213   
   192    214   /*
   193    215   ** Print the timing results.
   194    216   */
   195    217   static void endTimer(void){
   196    218     if( enableTimer && getProcessTimesAddr){
   197    219       FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
          220  +    sqlite3_int64 ftWallEnd = timeOfDay();
   198    221       getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd);
   199         -    printf("CPU Time: user %f sys %f\n",
          222  +    printf("Run Time: real %.3f user %f sys %f\n",
          223  +       (ftWallEnd - ftWallBegin)*0.001,
   200    224          timeDiff(&ftUserBegin, &ftUserEnd),
   201    225          timeDiff(&ftKernelBegin, &ftKernelEnd));
   202    226     }
   203    227   }
   204    228   
   205    229   #define BEGIN_TIMER beginTimer()
   206    230   #define END_TIMER endTimer()

Changes to src/sqlite.h.in.

   492    492   #define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
   493    493   #define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
   494    494   #define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
   495    495   #define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
   496    496   #define SQLITE_CONSTRAINT_TRIGGER      (SQLITE_CONSTRAINT | (7<<8))
   497    497   #define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
   498    498   #define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
          499  +#define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
   499    500   #define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
   500    501   #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
   501    502   #define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
   502    503   
   503    504   /*
   504    505   ** CAPI3REF: Flags For File Open Operations
   505    506   **
................................................................................
  1777   1778   ** codes are disabled by default for historical compatibility.
  1778   1779   */
  1779   1780   int sqlite3_extended_result_codes(sqlite3*, int onoff);
  1780   1781   
  1781   1782   /*
  1782   1783   ** CAPI3REF: Last Insert Rowid
  1783   1784   **
  1784         -** ^Each entry in an SQLite table has a unique 64-bit signed
         1785  +** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
         1786  +** has a unique 64-bit signed
  1785   1787   ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  1786   1788   ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  1787   1789   ** names are not also used by explicitly declared columns. ^If
  1788   1790   ** the table has a column of type [INTEGER PRIMARY KEY] then that column
  1789   1791   ** is another alias for the rowid.
  1790   1792   **
  1791         -** ^This routine returns the [rowid] of the most recent
  1792         -** successful [INSERT] into the database from the [database connection]
  1793         -** in the first argument.  ^As of SQLite version 3.7.7, this routines
  1794         -** records the last insert rowid of both ordinary tables and [virtual tables].
  1795         -** ^If no successful [INSERT]s
  1796         -** have ever occurred on that database connection, zero is returned.
         1793  +** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the 
         1794  +** most recent successful [INSERT] into a rowid table or [virtual table]
         1795  +** on database connection D.
         1796  +** ^Inserts into [WITHOUT ROWID] tables are not recorded.
         1797  +** ^If no successful [INSERT]s into rowid tables
         1798  +** have ever occurred on the database connection D, 
         1799  +** then sqlite3_last_insert_rowid(D) returns zero.
  1797   1800   **
  1798   1801   ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
  1799   1802   ** method, then this routine will return the [rowid] of the inserted
  1800   1803   ** row as long as the trigger or virtual table method is running.
  1801   1804   ** But once the trigger or virtual table method ends, the value returned 
  1802   1805   ** by this routine reverts to what it was before the trigger or virtual
  1803   1806   ** table method began.)^
................................................................................
  4806   4809   void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  4807   4810   
  4808   4811   /*
  4809   4812   ** CAPI3REF: Data Change Notification Callbacks
  4810   4813   **
  4811   4814   ** ^The sqlite3_update_hook() interface registers a callback function
  4812   4815   ** with the [database connection] identified by the first argument
  4813         -** to be invoked whenever a row is updated, inserted or deleted.
         4816  +** to be invoked whenever a row is updated, inserted or deleted in
         4817  +** a rowid table.
  4814   4818   ** ^Any callback set by a previous call to this function
  4815   4819   ** for the same database connection is overridden.
  4816   4820   **
  4817   4821   ** ^The second argument is a pointer to the function to invoke when a
  4818         -** row is updated, inserted or deleted.
         4822  +** row is updated, inserted or deleted in a rowid table.
  4819   4823   ** ^The first argument to the callback is a copy of the third argument
  4820   4824   ** to sqlite3_update_hook().
  4821   4825   ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
  4822   4826   ** or [SQLITE_UPDATE], depending on the operation that caused the callback
  4823   4827   ** to be invoked.
  4824   4828   ** ^The third and fourth arguments to the callback contain pointers to the
  4825   4829   ** database and table name containing the affected row.
  4826   4830   ** ^The final callback parameter is the [rowid] of the row.
  4827   4831   ** ^In the case of an update, this is the [rowid] after the update takes place.
  4828   4832   **
  4829   4833   ** ^(The update hook is not invoked when internal system tables are
  4830   4834   ** modified (i.e. sqlite_master and sqlite_sequence).)^
         4835  +** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
  4831   4836   **
  4832   4837   ** ^In the current implementation, the update hook
  4833   4838   ** is not invoked when duplication rows are deleted because of an
  4834   4839   ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
  4835   4840   ** invoked when rows are deleted using the [truncate optimization].
  4836   4841   ** The exceptions defined in this paragraph might change in a future
  4837   4842   ** release of SQLite.
................................................................................
  5281   5286   ** ^[sqlite3_free()] is used to free idxPtr if and only if
  5282   5287   ** needToFreeIdxPtr is true.
  5283   5288   **
  5284   5289   ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
  5285   5290   ** the correct order to satisfy the ORDER BY clause so that no separate
  5286   5291   ** sorting step is required.
  5287   5292   **
  5288         -** ^The estimatedCost value is an estimate of the cost of doing the
  5289         -** particular lookup.  A full scan of a table with N entries should have
  5290         -** a cost of N.  A binary search of a table of N entries should have a
  5291         -** cost of approximately log(N).
         5293  +** ^The estimatedCost value is an estimate of the cost of a particular
         5294  +** strategy. A cost of N indicates that the cost of the strategy is similar
         5295  +** to a linear scan of an SQLite table with N rows. A cost of log(N) 
         5296  +** indicates that the expense of the operation is similar to that of a
         5297  +** binary search on a unique indexed field of an SQLite table with N rows.
         5298  +**
         5299  +** ^The estimatedRows value is an estimate of the number of rows that
         5300  +** will be returned by the strategy.
         5301  +**
         5302  +** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
         5303  +** structure for SQLite version 3.8.2. If a virtual table extension is
         5304  +** used with an SQLite version earlier than 3.8.2, the results of attempting 
         5305  +** to read or write the estimatedRows field are undefined (but are likely 
         5306  +** to included crashing the application). The estimatedRows field should
         5307  +** therefore only be used if [sqlite3_libversion_number()] returns a
         5308  +** value greater than or equal to 3008002.
  5292   5309   */
  5293   5310   struct sqlite3_index_info {
  5294   5311     /* Inputs */
  5295   5312     int nConstraint;           /* Number of entries in aConstraint */
  5296   5313     struct sqlite3_index_constraint {
  5297   5314        int iColumn;              /* Column on left-hand side of constraint */
  5298   5315        unsigned char op;         /* Constraint operator */
................................................................................
  5309   5326       int argvIndex;           /* if >0, constraint is part of argv to xFilter */
  5310   5327       unsigned char omit;      /* Do not code a test for this constraint */
  5311   5328     } *aConstraintUsage;
  5312   5329     int idxNum;                /* Number used to identify the index */
  5313   5330     char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
  5314   5331     int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
  5315   5332     int orderByConsumed;       /* True if output is already ordered */
  5316         -  double estimatedCost;      /* Estimated cost of using this index */
         5333  +  double estimatedCost;           /* Estimated cost of using this index */
         5334  +  sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
  5317   5335   };
  5318   5336   
  5319   5337   /*
  5320   5338   ** CAPI3REF: Virtual Table Constraint Operator Codes
  5321   5339   **
  5322   5340   ** These macros defined the allowed values for the
  5323   5341   ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
................................................................................
  5513   5531   ** commit if the transaction continues to completion.)^
  5514   5532   **
  5515   5533   ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
  5516   5534   ** the opened blob.  ^The size of a blob may not be changed by this
  5517   5535   ** interface.  Use the [UPDATE] SQL command to change the size of a
  5518   5536   ** blob.
  5519   5537   **
         5538  +** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID]
         5539  +** table.  Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables.
         5540  +**
  5520   5541   ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  5521   5542   ** and the built-in [zeroblob] SQL function can be used, if desired,
  5522   5543   ** to create an empty, zero-filled blob in which to read or write using
  5523   5544   ** this interface.
  5524   5545   **
  5525   5546   ** To avoid a resource leak, every open [BLOB handle] should eventually
  5526   5547   ** be released by a call to [sqlite3_blob_close()].

Changes to src/sqliteInt.h.

   213    213   #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
   214    214   # define NDEBUG 1
   215    215   #endif
   216    216   #if defined(NDEBUG) && defined(SQLITE_DEBUG)
   217    217   # undef NDEBUG
   218    218   #endif
   219    219   
          220  +/*
          221  +** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
          222  +*/
          223  +#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
          224  +# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
          225  +#endif
          226  +
   220    227   /*
   221    228   ** The testcase() macro is used to aid in coverage testing.  When 
   222    229   ** doing coverage testing, the condition inside the argument to
   223    230   ** testcase() must be evaluated both true and false in order to
   224    231   ** get full branch coverage.  The testcase() macro is inserted
   225    232   ** to help ensure adequate test coverage in places where simple
   226    233   ** condition/decision coverage is inadequate.  For example, testcase()
................................................................................
  1415   1422   ** Allowed values for Tabe.tabFlags.
  1416   1423   */
  1417   1424   #define TF_Readonly        0x01    /* Read-only system table */
  1418   1425   #define TF_Ephemeral       0x02    /* An ephemeral table */
  1419   1426   #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
  1420   1427   #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
  1421   1428   #define TF_Virtual         0x10    /* Is a virtual table */
         1429  +#define TF_WithoutRowid    0x20    /* No rowid used. PRIMARY KEY is the key */
  1422   1430   
  1423   1431   
  1424   1432   /*
  1425   1433   ** Test to see whether or not a table is a virtual table.  This is
  1426   1434   ** done as a macro so that it will be optimized out when virtual
  1427   1435   ** table support is omitted from the build.
  1428   1436   */
................................................................................
  1430   1438   #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
  1431   1439   #  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
  1432   1440   #else
  1433   1441   #  define IsVirtual(X)      0
  1434   1442   #  define IsHiddenColumn(X) 0
  1435   1443   #endif
  1436   1444   
         1445  +/* Does the table have a rowid */
         1446  +#define HasRowid(X)     (((X)->tabFlags & TF_WithoutRowid)==0)
         1447  +
  1437   1448   /*
  1438   1449   ** Each foreign key constraint is an instance of the following structure.
  1439   1450   **
  1440   1451   ** A foreign key is associated with two tables.  The "from" table is
  1441   1452   ** the table that contains the REFERENCES clause that creates the foreign
  1442   1453   ** key.  The "to" table is the table that is named in the REFERENCES clause.
  1443   1454   ** Consider this example:
................................................................................
  1444   1455   **
  1445   1456   **     CREATE TABLE ex1(
  1446   1457   **       a INTEGER PRIMARY KEY,
  1447   1458   **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
  1448   1459   **     );
  1449   1460   **
  1450   1461   ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
         1462  +** Equivalent names:
         1463  +**
         1464  +**     from-table == child-table
         1465  +**       to-table == parent-table
  1451   1466   **
  1452   1467   ** Each REFERENCES clause generates an instance of the following structure
  1453   1468   ** which is attached to the from-table.  The to-table need not exist when
  1454   1469   ** the from-table is created.  The existence of the to-table is not checked.
         1470  +**
         1471  +** The list of all parents for child Table X is held at X.pFKey.
         1472  +**
         1473  +** A list of all children for a table named Z (which might not even exist)
         1474  +** is held in Schema.fkeyHash with a hash key of Z.
  1455   1475   */
  1456   1476   struct FKey {
  1457   1477     Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
  1458         -  FKey *pNextFrom;  /* Next foreign key in pFrom */
         1478  +  FKey *pNextFrom;  /* Next FKey with the same in pFrom. Next parent of pFrom */
  1459   1479     char *zTo;        /* Name of table that the key points to (aka: Parent) */
  1460         -  FKey *pNextTo;    /* Next foreign key on table named zTo */
  1461         -  FKey *pPrevTo;    /* Previous foreign key on table named zTo */
         1480  +  FKey *pNextTo;    /* Next with the same zTo. Next child of zTo. */
         1481  +  FKey *pPrevTo;    /* Previous with the same zTo */
  1462   1482     int nCol;         /* Number of columns in this key */
  1463   1483     /* EV: R-30323-21917 */
  1464         -  u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
  1465         -  u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
  1466         -  Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
  1467         -  struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
  1468         -    int iFrom;         /* Index of column in pFrom */
  1469         -    char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
  1470         -  } aCol[1];        /* One entry for each of nCol column s */
         1484  +  u8 isDeferred;       /* True if constraint checking is deferred till COMMIT */
         1485  +  u8 aAction[2];        /* ON DELETE and ON UPDATE actions, respectively */
         1486  +  Trigger *apTrigger[2];/* Triggers for aAction[] actions */
         1487  +  struct sColMap {      /* Mapping of columns in pFrom to columns in zTo */
         1488  +    int iFrom;            /* Index of column in pFrom */
         1489  +    char *zCol;           /* Name of column in zTo.  If NULL use PRIMARY KEY */
         1490  +  } aCol[1];            /* One entry for each of nCol columns */
  1471   1491   };
  1472   1492   
  1473   1493   /*
  1474   1494   ** SQLite supports many different ways to resolve a constraint
  1475   1495   ** error.  ROLLBACK processing means that a constraint violation
  1476   1496   ** causes the operation in process to fail and for the current transaction
  1477   1497   ** to be rolled back.  ABORT processing means the operation in process
................................................................................
  1516   1536   ** comparison of the two index keys.
  1517   1537   **
  1518   1538   ** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
  1519   1539   ** are nField slots for the columns of an index then one extra slot
  1520   1540   ** for the rowid at the end.
  1521   1541   */
  1522   1542   struct KeyInfo {
  1523         -  sqlite3 *db;        /* The database connection */
         1543  +  u32 nRef;           /* Number of references to this KeyInfo object */
  1524   1544     u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
  1525         -  u16 nField;         /* Maximum index for aColl[] and aSortOrder[] */
         1545  +  u16 nField;         /* Number of key columns in the index */
         1546  +  u16 nXField;        /* Number of columns beyond the key columns */
         1547  +  sqlite3 *db;        /* The database connection */
  1526   1548     u8 *aSortOrder;     /* Sort order for each column. */
  1527   1549     CollSeq *aColl[1];  /* Collating sequence for each term of the key */
  1528   1550   };
  1529   1551   
  1530   1552   /*
  1531   1553   ** An instance of the following structure holds information about a
  1532   1554   ** single index record that has already been parsed out into individual
................................................................................
  1541   1563   ** This structure holds a record that has already been disassembled
  1542   1564   ** into its constituent fields.
  1543   1565   */
  1544   1566   struct UnpackedRecord {
  1545   1567     KeyInfo *pKeyInfo;  /* Collation and sort-order information */
  1546   1568     u16 nField;         /* Number of entries in apMem[] */
  1547   1569     u8 flags;           /* Boolean settings.  UNPACKED_... below */
  1548         -  i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
  1549   1570     Mem *aMem;          /* Values */
  1550   1571   };
  1551   1572   
  1552   1573   /*
  1553   1574   ** Allowed values of UnpackedRecord.flags
  1554   1575   */
  1555   1576   #define UNPACKED_INCRKEY       0x01  /* Make this key an epsilon larger */
  1556   1577   #define UNPACKED_PREFIX_MATCH  0x02  /* A prefix match is considered OK */
  1557         -#define UNPACKED_PREFIX_SEARCH 0x04  /* Ignore final (rowid) field */
  1558   1578   
  1559   1579   /*
  1560   1580   ** Each SQL index is represented in memory by an
  1561   1581   ** instance of the following structure.
  1562   1582   **
  1563   1583   ** The columns of the table that are to be indexed are described
  1564   1584   ** by the aiColumn[] field of this structure.  For example, suppose
................................................................................
  1580   1600   ** it means this is not a unique index.  Otherwise it is a unique index
  1581   1601   ** and the value of Index.onError indicate the which conflict resolution 
  1582   1602   ** algorithm to employ whenever an attempt is made to insert a non-unique
  1583   1603   ** element.
  1584   1604   */
  1585   1605   struct Index {
  1586   1606     char *zName;             /* Name of this index */
  1587         -  int *aiColumn;           /* Which columns are used by this index.  1st is 0 */
         1607  +  i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
  1588   1608     tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
  1589   1609     Table *pTable;           /* The SQL table being indexed */
  1590   1610     char *zColAff;           /* String defining the affinity of each column */
  1591   1611     Index *pNext;            /* The next index associated with the same table */
  1592   1612     Schema *pSchema;         /* Schema containing this index */
  1593   1613     u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
  1594   1614     char **azColl;           /* Array of collation sequence names for index */
  1595   1615     Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
         1616  +  KeyInfo *pKeyInfo;       /* A KeyInfo object suitable for this index */
  1596   1617     int tnum;                /* DB Page containing root of this index */
  1597   1618     LogEst szIdxRow;         /* Estimated average row size in bytes */
  1598         -  u16 nColumn;             /* Number of columns in table used by this index */
         1619  +  u16 nKeyCol;             /* Number of columns forming the key */
         1620  +  u16 nColumn;             /* Number of columns stored in the index */
  1599   1621     u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  1600   1622     unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
  1601   1623     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
  1602   1624     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
         1625  +  unsigned isResized:1;    /* True if resizeIndexObject() has been called */
         1626  +  unsigned isCovering:1;   /* True if this is a covering index */
  1603   1627   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1604   1628     int nSample;             /* Number of elements in aSample[] */
  1605   1629     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
  1606   1630     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
  1607   1631     IndexSample *aSample;    /* Samples of the left-most key */
  1608   1632   #endif
  1609   1633   };
................................................................................
  2274   2298     TableLock *aTableLock; /* Required table locks for shared-cache mode */
  2275   2299   #endif
  2276   2300     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
  2277   2301   
  2278   2302     /* Information used while coding trigger programs. */
  2279   2303     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
  2280   2304     Table *pTriggerTab;  /* Table triggers are being coded for */
         2305  +  int addrCrTab;       /* Address of OP_CreateTable opcode on CREATE TABLE */
         2306  +  int addrSkipPK;      /* Address of instruction to skip PRIMARY KEY index */
  2281   2307     u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
  2282   2308     u32 oldmask;         /* Mask of old.* columns referenced */
  2283   2309     u32 newmask;         /* Mask of new.* columns referenced */
  2284   2310     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
  2285   2311     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
  2286   2312     u8 disableTriggers;  /* True to disable triggers */
  2287   2313   
  2288   2314     /* Above is constant between recursions.  Below is reset before and after
  2289   2315     ** each recursion */
  2290   2316   
  2291   2317     int nVar;                 /* Number of '?' variables seen in the SQL so far */
  2292   2318     int nzVar;                /* Number of available slots in azVar[] */
         2319  +  u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
  2293   2320     u8 explain;               /* True if the EXPLAIN flag is found on the query */
  2294   2321   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2295   2322     u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
  2296   2323     int nVtabLock;            /* Number of virtual tables to lock */
  2297   2324   #endif
  2298   2325     int nAlias;               /* Number of aliased result set columns */
  2299   2326     int nHeight;              /* Expression tree height of current sub-select */
  2300   2327   #ifndef SQLITE_OMIT_EXPLAIN
  2301   2328     int iSelectId;            /* ID of current select for EXPLAIN output */
  2302   2329     int iNextSelectId;        /* Next available select ID for EXPLAIN output */
  2303   2330   #endif
  2304   2331     char **azVar;             /* Pointers to names of parameters */
  2305   2332     Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
  2306         -  int *aAlias;              /* Register used to hold aliased result */
  2307   2333     const char *zTail;        /* All SQL text past the last semicolon parsed */
  2308   2334     Table *pNewTable;         /* A table being constructed by CREATE TABLE */
  2309   2335     Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
  2310   2336     const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
  2311   2337     Token sNameToken;         /* Token with unqualified schema object name */
  2312   2338     Token sLastToken;         /* The last token parsed */
  2313   2339   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
  2772   2798   void sqlite3ResetAllSchemasOfConnection(sqlite3*);
  2773   2799   void sqlite3ResetOneSchema(sqlite3*,int);
  2774   2800   void sqlite3CollapseDatabaseArray(sqlite3*);
  2775   2801   void sqlite3BeginParse(Parse*,int);
  2776   2802   void sqlite3CommitInternalChanges(sqlite3*);
  2777   2803   Table *sqlite3ResultSetOfSelect(Parse*,Select*);
  2778   2804   void sqlite3OpenMasterTable(Parse *, int);
         2805  +Index *sqlite3PrimaryKeyIndex(Table*);
         2806  +i16 sqlite3ColumnOfIndex(Index*, i16);
  2779   2807   void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
  2780   2808   void sqlite3AddColumn(Parse*,Token*);
  2781   2809   void sqlite3AddNotNull(Parse*, int);
  2782   2810   void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  2783   2811   void sqlite3AddCheckConstraint(Parse*, Expr*);
  2784   2812   void sqlite3AddColumnType(Parse*,Token*);
  2785   2813   void sqlite3AddDefaultValue(Parse*,ExprSpan*);
  2786   2814   void sqlite3AddCollateType(Parse*, Token*);
  2787         -void sqlite3EndTable(Parse*,Token*,Token*,Select*);
         2815  +void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
  2788   2816   int sqlite3ParseUri(const char*,const char*,unsigned int*,
  2789   2817                       sqlite3_vfs**,char**,char **);
  2790   2818   Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
  2791   2819   int sqlite3CodeOnce(Parse *);
  2792   2820   
  2793   2821   Bitvec *sqlite3BitvecCreate(u32);
  2794   2822   int sqlite3BitvecTest(Bitvec*, u32);
................................................................................
  2833   2861                                         Token*, Select*, Expr*, IdList*);
  2834   2862   void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
  2835   2863   int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
  2836   2864   void sqlite3SrcListShiftJoinType(SrcList*);
  2837   2865   void sqlite3SrcListAssignCursors(Parse*, SrcList*);
  2838   2866   void sqlite3IdListDelete(sqlite3*, IdList*);
  2839   2867   void sqlite3SrcListDelete(sqlite3*, SrcList*);
         2868  +Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
  2840   2869   Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
  2841   2870                             Expr*, int, int);
  2842   2871   void sqlite3DropIndex(Parse*, SrcList*, int);
  2843   2872   int sqlite3Select(Parse*, Select*, SelectDest*);
  2844   2873   Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
  2845   2874                            Expr*,ExprList*,u16,Expr*,Expr*);
  2846   2875   void sqlite3SelectDelete(sqlite3*, Select*);
................................................................................
  2855   2884   WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
  2856   2885   void sqlite3WhereEnd(WhereInfo*);
  2857   2886   u64 sqlite3WhereOutputRowCount(WhereInfo*);
  2858   2887   int sqlite3WhereIsDistinct(WhereInfo*);
  2859   2888   int sqlite3WhereIsOrdered(WhereInfo*);
  2860   2889   int sqlite3WhereContinueLabel(WhereInfo*);
  2861   2890   int sqlite3WhereBreakLabel(WhereInfo*);
  2862         -int sqlite3WhereOkOnePass(WhereInfo*);
         2891  +int sqlite3WhereOkOnePass(WhereInfo*, int*);
  2863   2892   int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
  2864   2893   void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
  2865   2894   void sqlite3ExprCodeMove(Parse*, int, int, int);
  2866   2895   void sqlite3ExprCacheStore(Parse*, int, int, int);
  2867   2896   void sqlite3ExprCachePush(Parse*);
  2868   2897   void sqlite3ExprCachePop(Parse*, int);
  2869   2898   void sqlite3ExprCacheRemove(Parse*, int, int);
................................................................................
  2909   2938   int sqlite3ExprIsConstantNotJoin(Expr*);
  2910   2939   int sqlite3ExprIsConstantOrFunction(Expr*);
  2911   2940   int sqlite3ExprIsInteger(Expr*, int*);
  2912   2941   int sqlite3ExprCanBeNull(const Expr*);
  2913   2942   void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
  2914   2943   int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  2915   2944   int sqlite3IsRowid(const char*);
  2916         -void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
  2917         -void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
         2945  +void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
         2946  +void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
  2918   2947   int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
  2919         -void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
  2920         -                                     int*,int,int,int,int,int*);
  2921         -void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
  2922         -int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
         2948  +void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
         2949  +                                     u8,u8,int,int*);
         2950  +void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
         2951  +int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, int*, int*);
  2923   2952   void sqlite3BeginWriteOperation(Parse*, int, int);
  2924   2953   void sqlite3MultiWrite(Parse*);
  2925   2954   void sqlite3MayAbort(Parse*);
  2926         -void sqlite3HaltConstraint(Parse*, int, int, char*, int);
         2955  +void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
         2956  +void sqlite3UniqueConstraint(Parse*, int, Index*);
         2957  +void sqlite3RowidConstraint(Parse*, int, Table*);
  2927   2958   Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
  2928   2959   ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
  2929   2960   SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
  2930   2961   IdList *sqlite3IdListDup(sqlite3*,IdList*);
  2931   2962   Select *sqlite3SelectDup(sqlite3*,Select*,int);
  2932   2963   void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  2933   2964   FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
................................................................................
  3137   3168   void sqlite3DefaultRowEst(Index*);
  3138   3169   void sqlite3RegisterLikeFunctions(sqlite3*, int);
  3139   3170   int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
  3140   3171   void sqlite3MinimumFileFormat(Parse*, int, int);
  3141   3172   void sqlite3SchemaClear(void *);
  3142   3173   Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
  3143   3174   int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
  3144         -KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int);
  3145         -KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
         3175  +KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
         3176  +void sqlite3KeyInfoUnref(KeyInfo*);
         3177  +KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
         3178  +KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
         3179  +#ifdef SQLITE_DEBUG
         3180  +int sqlite3KeyInfoIsWriteable(KeyInfo*);
         3181  +#endif
  3146   3182   int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
  3147   3183     void (*)(sqlite3_context*,int,sqlite3_value **),
  3148   3184     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
  3149   3185     FuncDestructor *pDestructor
  3150   3186   );
  3151   3187   int sqlite3ApiExit(sqlite3 *db, int);
  3152   3188   int sqlite3OpenTempDatabase(Parse *);

Changes to src/test6.c.

   617    617       pWrapper->zName = (char *)zName;
   618    618       pWrapper->pRealFile = pReal;
   619    619       rc = sqlite3OsFileSize(pReal, &iSize);
   620    620       pWrapper->iSize = (int)iSize;
   621    621       pWrapper->flags = flags;
   622    622     }
   623    623     if( rc==SQLITE_OK ){
   624         -    pWrapper->nData = (4096 + pWrapper->iSize);
          624  +    pWrapper->nData = (int)(4096 + pWrapper->iSize);
   625    625       pWrapper->zData = crash_malloc(pWrapper->nData);
   626    626       if( pWrapper->zData ){
   627    627         /* os_unix.c contains an assert() that fails if the caller attempts
   628    628         ** to read data from the 512-byte locking region of a file opened
   629    629         ** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file
   630    630         ** never contains valid data anyhow. So avoid doing such a read here.
   631    631         **
................................................................................
   633    633         ** to the xRead() method reads less than 128KB of data.
   634    634         */
   635    635         const int isDb = (flags&SQLITE_OPEN_MAIN_DB);
   636    636         i64 iOff;
   637    637   
   638    638         memset(pWrapper->zData, 0, pWrapper->nData);
   639    639         for(iOff=0; iOff<pWrapper->iSize; iOff += 512){
   640         -        int nRead = pWrapper->iSize - (int)iOff;
          640  +        int nRead = (int)(pWrapper->iSize - iOff);
   641    641           if( nRead>512 ) nRead = 512;
   642    642           if( isDb && iOff==PENDING_BYTE ) continue;
   643    643           rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], nRead, iOff);
   644    644         }
   645    645       }else{
   646    646         rc = SQLITE_NOMEM;
   647    647       }

Changes to src/test_config.c.

   254    254   #ifdef SQLITE_OMIT_COMPOUND_SELECT
   255    255     Tcl_SetVar2(interp, "sqlite_options", "compound", "0", TCL_GLOBAL_ONLY);
   256    256   #else
   257    257     Tcl_SetVar2(interp, "sqlite_options", "compound", "1", TCL_GLOBAL_ONLY);
   258    258   #endif
   259    259   
   260    260     Tcl_SetVar2(interp, "sqlite_options", "conflict", "1", TCL_GLOBAL_ONLY);
   261         -
   262         -#if SQLITE_OS_UNIX
   263    261     Tcl_SetVar2(interp, "sqlite_options", "crashtest", "1", TCL_GLOBAL_ONLY);
   264         -#else
   265         -  Tcl_SetVar2(interp, "sqlite_options", "crashtest", "0", TCL_GLOBAL_ONLY);
   266         -#endif
   267    262   
   268    263   #ifdef SQLITE_OMIT_DATETIME_FUNCS
   269    264     Tcl_SetVar2(interp, "sqlite_options", "datetime", "0", TCL_GLOBAL_ONLY);
   270    265   #else
   271    266     Tcl_SetVar2(interp, "sqlite_options", "datetime", "1", TCL_GLOBAL_ONLY);
   272    267   #endif
   273    268   

Changes to src/test_multiplex.c.

   751    751     void *pBuf,
   752    752     int iAmt,
   753    753     sqlite3_int64 iOfst
   754    754   ){
   755    755     multiplexConn *p = (multiplexConn*)pConn;
   756    756     multiplexGroup *pGroup = p->pGroup;
   757    757     int rc = SQLITE_OK;
   758         -  multiplexEnter();
          758  +  int nMutex = 0;
          759  +  multiplexEnter(); nMutex++;
   759    760     if( !pGroup->bEnabled ){
   760    761       sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
          762  +    multiplexLeave(); nMutex--;
   761    763       if( pSubOpen==0 ){
   762    764         rc = SQLITE_IOERR_READ;
   763    765       }else{
   764    766         rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
   765    767       }
   766    768     }else{
   767    769       while( iAmt > 0 ){
   768    770         int i = (int)(iOfst / pGroup->szChunk);
   769         -      sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, i, &rc, NULL, 1);
          771  +      sqlite3_file *pSubOpen;
          772  +      if( nMutex==0 ){ multiplexEnter(); nMutex++; }
          773  +      pSubOpen = multiplexSubOpen(pGroup, i, &rc, NULL, 1);
          774  +      multiplexLeave(); nMutex--;
   770    775         if( pSubOpen ){
   771    776           int extra = ((int)(iOfst % pGroup->szChunk) + iAmt) - pGroup->szChunk;
   772    777           if( extra<0 ) extra = 0;
   773    778           iAmt -= extra;
   774    779           rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt,
   775    780                                          iOfst % pGroup->szChunk);
   776    781           if( rc!=SQLITE_OK ) break;
................................................................................
   779    784           iAmt = extra;
   780    785         }else{
   781    786           rc = SQLITE_IOERR_READ;
   782    787           break;
   783    788         }
   784    789       }
   785    790     }
   786         -  multiplexLeave();
          791  +  assert( nMutex==0 || nMutex==1 );
          792  +  if( nMutex ) multiplexLeave();
   787    793     return rc;
   788    794   }
   789    795   
   790    796   /* Pass xWrite requests thru to the original VFS after
   791    797   ** determining the correct chunk to operate on.
   792    798   ** Break up writes across chunk boundaries.
   793    799   */

Changes to src/test_stat.c.

   393    393     StatTable *pTab = (StatTable *)pCursor->pVtab;
   394    394     Btree *pBt = pTab->db->aDb[0].pBt;
   395    395     Pager *pPager = sqlite3BtreePager(pBt);
   396    396   
   397    397     sqlite3_free(pCsr->zPath);
   398    398     pCsr->zPath = 0;
   399    399   
          400  +statNextRestart:
   400    401     if( pCsr->aPage[0].pPg==0 ){
   401    402       rc = sqlite3_step(pCsr->pStmt);
   402    403       if( rc==SQLITE_ROW ){
   403    404         int nPage;
   404    405         u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
   405    406         sqlite3PagerPagecount(pPager, &nPage);
   406    407         if( nPage==0 ){
................................................................................
   444    445           statSizeAndOffset(pCsr);
   445    446           return SQLITE_OK;
   446    447         }
   447    448         if( p->iRightChildPg ) break;
   448    449         p->iCell++;
   449    450       }
   450    451   
   451         -    while( !p->iRightChildPg || p->iCell>p->nCell ){
          452  +    if( !p->iRightChildPg || p->iCell>p->nCell ){
   452    453         statClearPage(p);
   453    454         if( pCsr->iPage==0 ) return statNext(pCursor);
   454    455         pCsr->iPage--;
   455         -      p = &pCsr->aPage[pCsr->iPage];
          456  +      goto statNextRestart; /* Tail recursion */
   456    457       }
   457    458       pCsr->iPage++;
   458    459       assert( p==&pCsr->aPage[pCsr->iPage-1] );
   459    460   
   460    461       if( p->iCell==p->nCell ){
   461    462         p[1].iPgno = p->iRightChildPg;
   462    463       }else{

Changes to src/tokenize.c.

   502    502       */
   503    503       sqlite3DeleteTable(db, pParse->pNewTable);
   504    504     }
   505    505   
   506    506     sqlite3DeleteTrigger(db, pParse->pNewTrigger);
   507    507     for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
   508    508     sqlite3DbFree(db, pParse->azVar);
   509         -  sqlite3DbFree(db, pParse->aAlias);
   510    509     while( pParse->pAinc ){
   511    510       AutoincInfo *p = pParse->pAinc;
   512    511       pParse->pAinc = p->pNext;
   513    512       sqlite3DbFree(db, p);
   514    513     }
   515    514     while( pParse->pZombieTab ){
   516    515       Table *p = pParse->pZombieTab;

Changes to src/trigger.c.

   780    780         sqlite3VdbeAddOp0(v, OP_ResetCount);
   781    781       }
   782    782     }
   783    783   
   784    784     return 0;
   785    785   }
   786    786   
   787         -#ifdef SQLITE_DEBUG
          787  +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   788    788   /*
   789    789   ** This function is used to add VdbeComment() annotations to a VDBE
   790    790   ** program. It is not used in production code, only for debugging.
   791    791   */
   792    792   static const char *onErrorText(int onError){
   793    793     switch( onError ){
   794    794       case OE_Abort:    return "abort";

Changes to src/update.c.

    91     91     SrcList *pTabList,     /* The table in which we should change things */
    92     92     ExprList *pChanges,    /* Things to be changed */
    93     93     Expr *pWhere,          /* The WHERE clause.  May be null */
    94     94     int onError            /* How to handle constraint errors */
    95     95   ){
    96     96     int i, j;              /* Loop counters */
    97     97     Table *pTab;           /* The table to be updated */
    98         -  int addr = 0;          /* VDBE instruction address of the start of the loop */
           98  +  int addrTop = 0;       /* VDBE instruction address of the start of the loop */
    99     99     WhereInfo *pWInfo;     /* Information about the WHERE clause */
   100    100     Vdbe *v;               /* The virtual database engine */
   101    101     Index *pIdx;           /* For looping over indices */
          102  +  Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
   102    103     int nIdx;              /* Number of indices that need updating */
   103         -  int iCur;              /* VDBE Cursor number of pTab */
          104  +  int iDataCur;          /* Cursor for the canonical data btree */
          105  +  int iIdxCur;           /* Cursor for the first index */
   104    106     sqlite3 *db;           /* The database structure */
   105    107     int *aRegIdx = 0;      /* One register assigned to each index to be updated */
   106    108     int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
   107    109                            ** an expression for the i-th column of the table.
   108    110                            ** aXRef[i]==-1 if the i-th column is not changed. */
   109         -  int chngRowid;         /* True if the record number is being changed */
          111  +  u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
          112  +  u8 chngRowid;          /* Rowid changed in a normal table */
          113  +  u8 chngKey;            /* Either chngPk or chngRowid */
   110    114     Expr *pRowidExpr = 0;  /* Expression defining the new record number */
   111    115     int openAll = 0;       /* True if all indices need to be opened */
   112    116     AuthContext sContext;  /* The authorization context */
   113    117     NameContext sNC;       /* The name-context to resolve expressions in */
   114    118     int iDb;               /* Database containing the table being updated */
   115    119     int okOnePass;         /* True for one-pass algorithm without the FIFO */
   116    120     int hasFK;             /* True if foreign key processing is required */
          121  +  int labelBreak;        /* Jump here to break out of UPDATE loop */
          122  +  int labelContinue;     /* Jump here to continue next step of UPDATE loop */
   117    123   
   118    124   #ifndef SQLITE_OMIT_TRIGGER
   119    125     int isView;            /* True when updating a view (INSTEAD OF trigger) */
   120    126     Trigger *pTrigger;     /* List of triggers on pTab, if required */
   121    127     int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
   122    128   #endif
   123    129     int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
          130  +  int iEph = 0;          /* Ephemeral table holding all primary key values */
          131  +  int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
          132  +  int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
   124    133   
   125    134     /* Register Allocations */
   126    135     int regRowCount = 0;   /* A count of rows changed */
   127    136     int regOldRowid;       /* The old rowid */
   128    137     int regNewRowid;       /* The new rowid */
   129    138     int regNew;            /* Content of the NEW.* table in triggers */
   130    139     int regOld = 0;        /* Content of OLD.* table in triggers */
   131    140     int regRowSet = 0;     /* Rowset of rows to be updated */
          141  +  int regKey = 0;        /* composite PRIMARY KEY value */
   132    142   
   133    143     memset(&sContext, 0, sizeof(sContext));
   134    144     db = pParse->db;
   135    145     if( pParse->nErr || db->mallocFailed ){
   136    146       goto update_cleanup;
   137    147     }
   138    148     assert( pTabList->nSrc==1 );
................................................................................
   171    181     for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
   172    182   
   173    183     /* Allocate a cursors for the main database table and for all indices.
   174    184     ** The index cursors might not be used, but if they are used they
   175    185     ** need to occur right after the database cursor.  So go ahead and
   176    186     ** allocate enough space, just in case.
   177    187     */
   178         -  pTabList->a[0].iCursor = iCur = pParse->nTab++;
   179         -  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
          188  +  pTabList->a[0].iCursor = iDataCur = pParse->nTab++;
          189  +  iIdxCur = iDataCur+1;
          190  +  pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
          191  +  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
          192  +    if( pIdx->autoIndex==2 && pPk!=0 ){
          193  +      iDataCur = pParse->nTab;
          194  +      pTabList->a[0].iCursor = iDataCur;
          195  +    }
   180    196       pParse->nTab++;
   181    197     }
   182    198   
   183    199     /* Initialize the name-context */
   184    200     memset(&sNC, 0, sizeof(sNC));
   185    201     sNC.pParse = pParse;
   186    202     sNC.pSrcList = pTabList;
................................................................................
   187    203   
   188    204     /* Resolve the column names in all the expressions of the
   189    205     ** of the UPDATE statement.  Also find the column index
   190    206     ** for each column to be updated in the pChanges array.  For each
   191    207     ** column to be updated, make sure we have authorization to change
   192    208     ** that column.
   193    209     */
   194         -  chngRowid = 0;
          210  +  chngRowid = chngPk = 0;
   195    211     for(i=0; i<pChanges->nExpr; i++){
   196    212       if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
   197    213         goto update_cleanup;
   198    214       }
   199    215       for(j=0; j<pTab->nCol; j++){
   200    216         if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
   201    217           if( j==pTab->iPKey ){
   202    218             chngRowid = 1;
   203    219             pRowidExpr = pChanges->a[i].pExpr;
          220  +        }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
          221  +          chngPk = 1;
   204    222           }
   205    223           aXRef[j] = i;
   206    224           break;
   207    225         }
   208    226       }
   209    227       if( j>=pTab->nCol ){
   210         -      if( sqlite3IsRowid(pChanges->a[i].zName) ){
          228  +      if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
   211    229           j = -1;
   212    230           chngRowid = 1;
   213    231           pRowidExpr = pChanges->a[i].pExpr;
   214    232         }else{
   215    233           sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
   216    234           pParse->checkSchema = 1;
   217    235           goto update_cleanup;
................................................................................
   227    245           goto update_cleanup;
   228    246         }else if( rc==SQLITE_IGNORE ){
   229    247           aXRef[j] = -1;
   230    248         }
   231    249       }
   232    250   #endif
   233    251     }
          252  +  assert( (chngRowid & chngPk)==0 );
          253  +  assert( chngRowid==0 || chngRowid==1 );
          254  +  assert( chngPk==0 || chngPk==1 );
          255  +  chngKey = chngRowid + chngPk;
   234    256   
   235         -  hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
          257  +  hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
   236    258   
   237    259     /* Allocate memory for the array aRegIdx[].  There is one entry in the
   238    260     ** array for each index associated with table being updated.  Fill in
   239    261     ** the value with a register number for indices that are to be used
   240    262     ** and with zero for unused indices.
   241    263     */
   242         -  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
   243    264     if( nIdx>0 ){
   244    265       aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
   245    266       if( aRegIdx==0 ) goto update_cleanup;
   246    267     }
   247    268     for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
   248    269       int reg;
   249         -    if( hasFK || chngRowid || pIdx->pPartIdxWhere ){
          270  +    if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
   250    271         reg = ++pParse->nMem;
   251    272       }else{
   252    273         reg = 0;
   253         -      for(i=0; i<pIdx->nColumn; i++){
          274  +      for(i=0; i<pIdx->nKeyCol; i++){
   254    275           if( aXRef[pIdx->aiColumn[i]]>=0 ){
   255    276             reg = ++pParse->nMem;
   256    277             break;
   257    278           }
   258    279         }
   259    280       }
   260    281       aRegIdx[j] = reg;
................................................................................
   276    297       goto update_cleanup;
   277    298     }
   278    299   #endif
   279    300   
   280    301     /* Allocate required registers. */
   281    302     regRowSet = ++pParse->nMem;
   282    303     regOldRowid = regNewRowid = ++pParse->nMem;
   283         -  if( pTrigger || hasFK ){
          304  +  if( chngPk || pTrigger || hasFK ){
   284    305       regOld = pParse->nMem + 1;
   285    306       pParse->nMem += pTab->nCol;
   286    307     }
   287         -  if( chngRowid || pTrigger || hasFK ){
          308  +  if( chngKey || pTrigger || hasFK ){
   288    309       regNewRowid = ++pParse->nMem;
   289    310     }
   290    311     regNew = pParse->nMem + 1;
   291    312     pParse->nMem += pTab->nCol;
   292    313   
   293    314     /* Start the view context. */
   294    315     if( isView ){
................................................................................
   296    317     }
   297    318   
   298    319     /* If we are trying to update a view, realize that view into
   299    320     ** a ephemeral table.
   300    321     */
   301    322   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
   302    323     if( isView ){
   303         -    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
          324  +    sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
   304    325     }
   305    326   #endif
   306    327   
   307    328     /* Resolve the column names in all the expressions in the
   308    329     ** WHERE clause.
   309    330     */
   310    331     if( sqlite3ResolveExprNames(&sNC, pWhere) ){
   311    332       goto update_cleanup;
   312    333     }
   313    334   
   314    335     /* Begin the database scan
   315    336     */
   316         -  sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
   317         -  pWInfo = sqlite3WhereBegin(
   318         -      pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0
   319         -  );
   320         -  if( pWInfo==0 ) goto update_cleanup;
   321         -  okOnePass = sqlite3WhereOkOnePass(pWInfo);
   322         -
   323         -  /* Remember the rowid of every item to be updated.
   324         -  */
   325         -  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
   326         -  if( !okOnePass ){
   327         -    sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
   328         -  }
   329         -
   330         -  /* End the database scan loop.
   331         -  */
   332         -  sqlite3WhereEnd(pWInfo);
          337  +  if( HasRowid(pTab) ){
          338  +    sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
          339  +    pWInfo = sqlite3WhereBegin(
          340  +        pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur
          341  +    );
          342  +    if( pWInfo==0 ) goto update_cleanup;
          343  +    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
          344  +  
          345  +    /* Remember the rowid of every item to be updated.
          346  +    */
          347  +    sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
          348  +    if( !okOnePass ){
          349  +      sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
          350  +    }
          351  +  
          352  +    /* End the database scan loop.
          353  +    */
          354  +    sqlite3WhereEnd(pWInfo);
          355  +  }else{
          356  +    int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
          357  +    i16 nPk;         /* Number of components of the PRIMARY KEY */
          358  +    int addrOpen;    /* Address of the OpenEphemeral instruction */
          359  +
          360  +    assert( pPk!=0 );
          361  +    nPk = pPk->nKeyCol;
          362  +    iPk = pParse->nMem+1;
          363  +    pParse->nMem += nPk;
          364  +    regKey = ++pParse->nMem;
          365  +    iEph = pParse->nTab++;
          366  +    sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
          367  +    addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
          368  +    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
          369  +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 
          370  +                               WHERE_ONEPASS_DESIRED, iIdxCur);
          371  +    if( pWInfo==0 ) goto update_cleanup;
          372  +    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
          373  +    for(i=0; i<nPk; i++){
          374  +      sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
          375  +                                      iPk+i);
          376  +    }
          377  +    if( okOnePass ){
          378  +      sqlite3VdbeChangeToNoop(v, addrOpen);
          379  +      nKey = nPk;
          380  +      regKey = iPk;
          381  +    }else{
          382  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
          383  +                        sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
          384  +      sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey);
          385  +    }
          386  +    sqlite3WhereEnd(pWInfo);
          387  +  }
   333    388   
   334    389     /* Initialize the count of updated rows
   335    390     */
   336    391     if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
   337    392       regRowCount = ++pParse->nMem;
   338    393       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
   339    394     }
   340    395   
          396  +  labelBreak = sqlite3VdbeMakeLabel(v);
   341    397     if( !isView ){
   342    398       /* 
   343    399       ** Open every index that needs updating.  Note that if any
   344    400       ** index could potentially invoke a REPLACE conflict resolution 
   345    401       ** action, then we need to open all indices because we might need
   346    402       ** to be deleting some records.
   347    403       */
   348         -    if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); 
          404  +    if( !okOnePass && HasRowid(pTab) ){
          405  +      sqlite3OpenTable(pParse, iDataCur, iDb, pTab, OP_OpenWrite); 
          406  +    }
          407  +    sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
   349    408       if( onError==OE_Replace ){
   350    409         openAll = 1;
   351    410       }else{
   352    411         openAll = 0;
   353    412         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   354    413           if( pIdx->onError==OE_Replace ){
   355    414             openAll = 1;
   356    415             break;
   357    416           }
   358    417         }
   359    418       }
   360    419       for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
          420  +      int iThisCur = iIdxCur+i;
   361    421         assert( aRegIdx );
   362         -      if( openAll || aRegIdx[i]>0 ){
   363         -        KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   364         -        sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
   365         -                       (char*)pKey, P4_KEYINFO_HANDOFF);
   366         -        assert( pParse->nTab>iCur+i+1 );
          422  +      if( (openAll || aRegIdx[i]>0)
          423  +       && iThisCur!=aiCurOnePass[1]
          424  +      ){
          425  +        assert( iThisCur!=aiCurOnePass[0] );
          426  +        sqlite3VdbeAddOp3(v, OP_OpenWrite, iThisCur, pIdx->tnum, iDb);
          427  +        sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
          428  +        assert( pParse->nTab>iThisCur );
          429  +        VdbeComment((v, "%s", pIdx->zName));
          430  +        if( okOnePass && pPk && iThisCur==iDataCur ){
          431  +          sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak,
          432  +                               regKey, nKey);
          433  +        }
   367    434         }
   368    435       }
   369    436     }
   370    437   
   371    438     /* Top of the update loop */
   372    439     if( okOnePass ){
   373         -    int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
   374         -    addr = sqlite3VdbeAddOp0(v, OP_Goto);
   375         -    sqlite3VdbeJumpHere(v, a1);
          440  +    labelContinue = labelBreak;
          441  +    sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
          442  +  }else if( pPk ){
          443  +    labelContinue = sqlite3VdbeMakeLabel(v);
          444  +    sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak);
          445  +    addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
          446  +    sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
   376    447     }else{
   377         -    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
          448  +    labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
          449  +                             regOldRowid);
          450  +    sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
   378    451     }
   379    452   
   380         -  /* Make cursor iCur point to the record that is being updated. If
   381         -  ** this record does not exist for some reason (deleted by a trigger,
   382         -  ** for example, then jump to the next iteration of the RowSet loop.  */
   383         -  sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
   384         -
   385    453     /* If the record number will change, set register regNewRowid to
   386    454     ** contain the new value. If the record number is not being modified,
   387    455     ** then regNewRowid is the same register as regOldRowid, which is
   388    456     ** already populated.  */
   389         -  assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
          457  +  assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
   390    458     if( chngRowid ){
   391    459       sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
   392    460       sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
   393    461     }
   394    462   
   395         -  /* If there are triggers on this table, populate an array of registers 
   396         -  ** with the required old.* column data.  */
   397         -  if( hasFK || pTrigger ){
          463  +  /* Compute the old pre-UPDATE content of the row being changed, if that
          464  +  ** information is needed */
          465  +  if( chngPk || hasFK || pTrigger ){
   398    466       u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
   399    467       oldmask |= sqlite3TriggerColmask(pParse, 
   400    468           pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
   401    469       );
   402    470       for(i=0; i<pTab->nCol; i++){
   403         -      if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){
   404         -        sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
          471  +      if( oldmask==0xffffffff
          472  +       || (i<32 && (oldmask & (1<<i)))
          473  +       || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
          474  +      ){
          475  +        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
   405    476         }else{
   406    477           sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
   407    478         }
   408    479       }
   409         -    if( chngRowid==0 ){
          480  +    if( chngRowid==0 && pPk==0 ){
   410    481         sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
   411    482       }
   412    483     }
   413    484   
   414    485     /* Populate the array of registers beginning at regNew with the new
   415    486     ** row data. This array is used to check constaints, create the new
   416    487     ** table and index records, and as the values for any new.* references
................................................................................
   439    510           /* This branch loads the value of a column that will not be changed 
   440    511           ** into a register. This is done if there are no BEFORE triggers, or
   441    512           ** if there are one or more BEFORE triggers that use this value via
   442    513           ** a new.* reference in a trigger program.
   443    514           */
   444    515           testcase( i==31 );
   445    516           testcase( i==32 );
   446         -        sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
   447         -        sqlite3ColumnDefault(v, pTab, i, regNew+i);
          517  +        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
   448    518         }
   449    519       }
   450    520     }
   451    521   
   452    522     /* Fire any BEFORE UPDATE triggers. This happens before constraints are
   453    523     ** verified. One could argue that this is wrong.
   454    524     */
   455    525     if( tmask&TRIGGER_BEFORE ){
   456    526       sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
   457    527       sqlite3TableAffinityStr(v, pTab);
   458    528       sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
   459         -        TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
          529  +        TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
   460    530   
   461    531       /* The row-trigger may have deleted the row being updated. In this
   462    532       ** case, jump to the next row. No updates or AFTER triggers are 
   463    533       ** required. This behavior - what happens when the row being updated
   464    534       ** is deleted or renamed by a BEFORE trigger - is left undefined in the
   465    535       ** documentation.
   466    536       */
   467         -    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
          537  +    if( pPk ){
          538  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
          539  +    }else{
          540  +      sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
          541  +    }
   468    542   
   469    543       /* If it did not delete it, the row-trigger may still have modified 
   470    544       ** some of the columns of the row being updated. Load the values for 
   471    545       ** all columns not modified by the update statement into their 
   472    546       ** registers in case this has happened.
   473    547       */
   474    548       for(i=0; i<pTab->nCol; i++){
   475    549         if( aXRef[i]<0 && i!=pTab->iPKey ){
   476         -        sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
   477         -        sqlite3ColumnDefault(v, pTab, i, regNew+i);
          550  +        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
   478    551         }
   479    552       }
   480    553     }
   481    554   
   482    555     if( !isView ){
   483         -    int j1;                       /* Address of jump instruction */
          556  +    int j1 = 0;           /* Address of jump instruction */
          557  +    int bReplace = 0;     /* True if REPLACE conflict resolution might happen */
   484    558   
   485    559       /* Do constraint checks. */
   486         -    sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
   487         -        aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
          560  +    assert( regOldRowid>0 );
          561  +    sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
          562  +        regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
   488    563   
   489    564       /* Do FK constraint checks. */
   490    565       if( hasFK ){
   491         -      sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngRowid);
          566  +      sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
   492    567       }
   493    568   
   494    569       /* Delete the index entries associated with the current record.  */
   495         -    j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
   496         -    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
          570  +    if( bReplace || chngKey ){
          571  +      if( pPk ){
          572  +        j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
          573  +      }else{
          574  +        j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
          575  +      }
          576  +    }
          577  +    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx);
   497    578   
   498    579       /* If changing the rowid value, or if there are foreign key constraints
   499    580       ** to process, delete the old record. Otherwise, add a noop OP_Delete
   500    581       ** to invoke the pre-update hook.
   501    582       **
   502    583       ** That (regNew==regnewRowid+1) is true is also important for the 
   503    584       ** pre-update hook. If the caller invokes preupdate_new(), the returned
   504    585       ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
   505    586       ** is the column index supplied by the user.
   506    587       */
   507    588       assert( regNew==regNewRowid+1 );
   508         -    sqlite3VdbeAddOp3(v, OP_Delete, iCur,
   509         -        OPFLAG_ISUPDATE | ((hasFK || chngRowid) ? 0 : OPFLAG_ISNOOP),
          589  +    sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
          590  +        OPFLAG_ISUPDATE | ((hasFK || chngKey || pPk!=0) ? 0 : OPFLAG_ISNOOP),
   510    591           regNewRowid
   511    592       );
   512    593       if( !pParse->nested ){
   513    594         sqlite3VdbeChangeP4(v, -1, (char*)pTab, P4_TABLE);
   514    595       }
   515         -    sqlite3VdbeJumpHere(v, j1);
          596  +    if( bReplace || chngKey ){
          597  +      if( sqlite3VdbeCurrentAddr(v)==j1+1 ){
          598  +        sqlite3VdbeChangeToNoop(v, j1);
          599  +      }else{
          600  +        sqlite3VdbeJumpHere(v, j1);
          601  +      }
          602  +    }
   516    603   
   517    604       if( hasFK ){
   518         -      sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngRowid);
          605  +      sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
   519    606       }
   520    607     
   521    608       /* Insert the new index entries and the new record. */
   522         -    sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
          609  +    sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
          610  +                             regNewRowid, aRegIdx, 1, 0, 0);
   523    611   
   524    612       /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
   525    613       ** handle rows (possibly in other tables) that refer via a foreign key
   526    614       ** to the row just updated. */ 
   527    615       if( hasFK ){
   528         -      sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngRowid);
          616  +      sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
   529    617       }
   530    618     }
   531    619   
   532    620     /* Increment the row counter 
   533    621     */
   534    622     if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
   535    623       sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
   536    624     }
   537    625   
   538    626     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
   539         -      TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
          627  +      TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
   540    628   
   541    629     /* Repeat the above with the next record to be updated, until
   542    630     ** all record selected by the WHERE clause have been updated.
   543    631     */
   544         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
   545         -  sqlite3VdbeJumpHere(v, addr);
          632  +  if( okOnePass ){
          633  +    /* Nothing to do at end-of-loop for a single-pass */
          634  +  }else if( pPk ){
          635  +    sqlite3VdbeResolveLabel(v, labelContinue);
          636  +    sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop);
          637  +  }else{
          638  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue);
          639  +  }
          640  +  sqlite3VdbeResolveLabel(v, labelBreak);
   546    641   
   547    642     /* Close all tables */
   548    643     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
   549    644       assert( aRegIdx );
   550    645       if( openAll || aRegIdx[i]>0 ){
   551         -      sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
          646  +      sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
   552    647       }
   553    648     }
   554         -  sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
          649  +  if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
   555    650   
   556    651     /* Update the sqlite_sequence table by storing the content of the
   557    652     ** maximum rowid counter values recorded while inserting into
   558    653     ** autoincrement tables.
   559    654     */
   560    655     if( pParse->nested==0 && pParse->pTriggerTab==0 ){
   561    656       sqlite3AutoincrementEnd(pParse);

Changes to src/vacuum.c.

   230    230   
   231    231     /* Query the schema of the main database. Create a mirror schema
   232    232     ** in the temporary database.
   233    233     */
   234    234     rc = execExecSql(db, pzErrMsg,
   235    235         "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
   236    236         "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
   237         -      "   AND rootpage>0"
          237  +      "   AND coalesce(rootpage,1)>0"
   238    238     );
   239    239     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   240    240     rc = execExecSql(db, pzErrMsg,
   241    241         "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
   242    242         "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
   243    243     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   244    244     rc = execExecSql(db, pzErrMsg,
................................................................................
   251    251     ** the contents to the temporary database.
   252    252     */
   253    253     rc = execExecSql(db, pzErrMsg,
   254    254         "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
   255    255         "|| ' SELECT * FROM main.' || quote(name) || ';'"
   256    256         "FROM main.sqlite_master "
   257    257         "WHERE type = 'table' AND name!='sqlite_sequence' "
   258         -      "  AND rootpage>0"
          258  +      "  AND coalesce(rootpage,1)>0"
   259    259     );
   260    260     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   261    261   
   262    262     /* Copy over the sequence table
   263    263     */
   264    264     rc = execExecSql(db, pzErrMsg,
   265    265         "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "

Changes to src/vdbe.c.

   810    810     pcDest = (int)pIn1->u.i;
   811    811     pIn1->u.i = pc;
   812    812     REGISTER_TRACE(pOp->p1, pIn1);
   813    813     pc = pcDest;
   814    814     break;
   815    815   }
   816    816   
   817         -/* Opcode:  HaltIfNull  P1 P2 P3 P4 *
          817  +/* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
          818  +** Synopsis:  if r[P3] null then halt
   818    819   **
   819    820   ** Check the value in register P3.  If it is NULL then Halt using
   820    821   ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
   821    822   ** value in register P3 is not NULL, then this routine is a no-op.
          823  +** The P5 parameter should be 1.
   822    824   */
   823    825   case OP_HaltIfNull: {      /* in3 */
   824    826     pIn3 = &aMem[pOp->p3];
   825    827     if( (pIn3->flags & MEM_Null)==0 ) break;
   826    828     /* Fall through into OP_Halt */
   827    829   }
   828    830   
   829         -/* Opcode:  Halt P1 P2 * P4 *
          831  +/* Opcode:  Halt P1 P2 * P4 P5
   830    832   **
   831    833   ** Exit immediately.  All open cursors, etc are closed
   832    834   ** automatically.
   833    835   **
   834    836   ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
   835    837   ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
   836    838   ** For errors, it can be some other value.  If P1!=0 then P2 will determine
   837    839   ** whether or not to rollback the current transaction.  Do not rollback
   838    840   ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
   839    841   ** then back out all changes that have occurred during this execution of the
   840    842   ** VDBE, but do not rollback the transaction. 
   841    843   **
   842    844   ** If P4 is not null then it is an error message string.
          845  +**
          846  +** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
          847  +**
          848  +**    0:  (no change)
          849  +**    1:  NOT NULL contraint failed: P4
          850  +**    2:  UNIQUE constraint failed: P4
          851  +**    3:  CHECK constraint failed: P4
          852  +**    4:  FOREIGN KEY constraint failed: P4
          853  +**
          854  +** If P5 is not zero and P4 is NULL, then everything after the ":" is
          855  +** omitted.
   843    856   **
   844    857   ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
   845    858   ** every program.  So a jump past the last instruction of the program
   846    859   ** is the same as executing Halt.
   847    860   */
   848    861   case OP_Halt: {
          862  +  const char *zType;
          863  +  const char *zLogFmt;
          864  +
   849    865     if( pOp->p1==SQLITE_OK && p->pFrame ){
   850    866       /* Halt the sub-program. Return control to the parent frame. */
   851    867       VdbeFrame *pFrame = p->pFrame;
   852    868       p->pFrame = pFrame->pParent;
   853    869       p->nFrame--;
   854    870       sqlite3VdbeSetChanges(db, p->nChange);
   855    871       pc = sqlite3VdbeFrameRestore(pFrame);
................................................................................
   862    878         ** as the p2 of the calling OP_Program.  */
   863    879         pc = p->aOp[pc].p2-1;
   864    880       }
   865    881       aOp = p->aOp;
   866    882       aMem = p->aMem;
   867    883       break;
   868    884     }
   869         -
   870    885     p->rc = pOp->p1;
   871    886     p->errorAction = (u8)pOp->p2;
   872    887     p->pc = pc;
   873         -  if( pOp->p4.z ){
   874         -    assert( p->rc!=SQLITE_OK );
   875         -    sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
   876         -    testcase( sqlite3GlobalConfig.xLog!=0 );
   877         -    sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
   878         -  }else if( p->rc ){
   879         -    testcase( sqlite3GlobalConfig.xLog!=0 );
   880         -    sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
          888  +  if( p->rc ){
          889  +    if( pOp->p5 ){
          890  +      static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
          891  +                                             "FOREIGN KEY" };
          892  +      assert( pOp->p5>=1 && pOp->p5<=4 );
          893  +      testcase( pOp->p5==1 );
          894  +      testcase( pOp->p5==2 );
          895  +      testcase( pOp->p5==3 );
          896  +      testcase( pOp->p5==4 );
          897  +      zType = azType[pOp->p5-1];
          898  +    }else{
          899  +      zType = 0;
          900  +    }
          901  +    assert( zType!=0 || pOp->p4.z!=0 );
          902  +    zLogFmt = "abort at %d in [%s]: %s";
          903  +    if( zType && pOp->p4.z ){
          904  +      sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s", 
          905  +                       zType, pOp->p4.z);
          906  +    }else if( pOp->p4.z ){
          907  +      sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
          908  +    }else{
          909  +      sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
          910  +    }
          911  +    sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
   881    912     }
   882    913     rc = sqlite3VdbeHalt(p);
   883    914     assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
   884    915     if( rc==SQLITE_BUSY ){
   885    916       p->rc = rc = SQLITE_BUSY;
   886    917     }else{
   887    918       assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
................................................................................
   888    919       assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
   889    920       rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
   890    921     }
   891    922     goto vdbe_return;
   892    923   }
   893    924   
   894    925   /* Opcode: Integer P1 P2 * * *
          926  +** Synopsis: r[P2]=P1
   895    927   **
   896    928   ** The 32-bit integer value P1 is written into register P2.
   897    929   */
   898    930   case OP_Integer: {         /* out2-prerelease */
   899    931     pOut->u.i = pOp->p1;
   900    932     break;
   901    933   }
   902    934   
   903    935   /* Opcode: Int64 * P2 * P4 *
          936  +** Synopsis: r[P2]=P4
   904    937   **
   905    938   ** P4 is a pointer to a 64-bit integer value.
   906    939   ** Write that value into register P2.
   907    940   */
   908    941   case OP_Int64: {           /* out2-prerelease */
   909    942     assert( pOp->p4.pI64!=0 );
   910    943     pOut->u.i = *pOp->p4.pI64;
   911    944     break;
   912    945   }
   913    946   
   914    947   #ifndef SQLITE_OMIT_FLOATING_POINT
   915    948   /* Opcode: Real * P2 * P4 *
          949  +** Synopsis: r[P2]=P4
   916    950   **
   917    951   ** P4 is a pointer to a 64-bit floating point value.
   918    952   ** Write that value into register P2.
   919    953   */
   920    954   case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
   921    955     pOut->flags = MEM_Real;
   922    956     assert( !sqlite3IsNaN(*pOp->p4.pReal) );
   923    957     pOut->r = *pOp->p4.pReal;
   924    958     break;
   925    959   }
   926    960   #endif
   927    961   
   928    962   /* Opcode: String8 * P2 * P4 *
          963  +** Synopsis: r[P2]='P4'
   929    964   **
   930    965   ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
   931    966   ** into an OP_String before it is executed for the first time.
   932    967   */
   933    968   case OP_String8: {         /* same as TK_STRING, out2-prerelease */
   934    969     assert( pOp->p4.z!=0 );
   935    970     pOp->opcode = OP_String;
................................................................................
   956    991     if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   957    992       goto too_big;
   958    993     }
   959    994     /* Fall through to the next case, OP_String */
   960    995   }
   961    996     
   962    997   /* Opcode: String P1 P2 * P4 *
          998  +** Synopsis: r[P2]='P4' (len=P1)
   963    999   **
   964   1000   ** The string value P4 of length P1 (bytes) is stored in register P2.
   965   1001   */
   966   1002   case OP_String: {          /* out2-prerelease */
   967   1003     assert( pOp->p4.z!=0 );
   968   1004     pOut->flags = MEM_Str|MEM_Static|MEM_Term;
   969   1005     pOut->z = pOp->p4.z;
................................................................................
   970   1006     pOut->n = pOp->p1;
   971   1007     pOut->enc = encoding;
   972   1008     UPDATE_MAX_BLOBSIZE(pOut);
   973   1009     break;
   974   1010   }
   975   1011   
   976   1012   /* Opcode: Null P1 P2 P3 * *
         1013  +** Synopsis:  r[P2..P3]=NULL
   977   1014   **
   978   1015   ** Write a NULL into registers P2.  If P3 greater than P2, then also write
   979   1016   ** NULL into register P3 and every register in between P2 and P3.  If P3
   980   1017   ** is less than P2 (typically P3 is zero) then only register P2 is
   981   1018   ** set to NULL.
   982   1019   **
   983   1020   ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
................................................................................
   998   1035       cnt--;
   999   1036     }
  1000   1037     break;
  1001   1038   }
  1002   1039   
  1003   1040   
  1004   1041   /* Opcode: Blob P1 P2 * P4
         1042  +** Synopsis: r[P2]=P4 (len=P1)
  1005   1043   **
  1006   1044   ** P4 points to a blob of data P1 bytes long.  Store this
  1007   1045   ** blob in register P2.
  1008   1046   */
  1009   1047   case OP_Blob: {                /* out2-prerelease */
  1010   1048     assert( pOp->p1 <= SQLITE_MAX_LENGTH );
  1011   1049     sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
  1012   1050     pOut->enc = encoding;
  1013   1051     UPDATE_MAX_BLOBSIZE(pOut);
  1014   1052     break;
  1015   1053   }
  1016   1054   
  1017   1055   /* Opcode: Variable P1 P2 * P4 *
         1056  +** Synopsis: r[P2]=parameter(P1,P4)
  1018   1057   **
  1019   1058   ** Transfer the values of bound parameter P1 into register P2
  1020   1059   **
  1021   1060   ** If the parameter is named, then its name appears in P4 and P3==1.
  1022   1061   ** The P4 value is used by sqlite3_bind_parameter_name().
  1023   1062   */
  1024   1063   case OP_Variable: {            /* out2-prerelease */
................................................................................
  1032   1071     }
  1033   1072     sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
  1034   1073     UPDATE_MAX_BLOBSIZE(pOut);
  1035   1074     break;
  1036   1075   }
  1037   1076   
  1038   1077   /* Opcode: Move P1 P2 P3 * *
         1078  +** Synopsis:  r[P2@P3]=r[P1@P3]
  1039   1079   **
  1040   1080   ** Move the values in register P1..P1+P3 over into
  1041   1081   ** registers P2..P2+P3.  Registers P1..P1+P3 are
  1042   1082   ** left holding a NULL.  It is an error for register ranges
  1043   1083   ** P1..P1+P3 and P2..P2+P3 to overlap.
  1044   1084   */
  1045   1085   case OP_Move: {
................................................................................
  1074   1114       pIn1++;
  1075   1115       pOut++;
  1076   1116     }
  1077   1117     break;
  1078   1118   }
  1079   1119   
  1080   1120   /* Opcode: Copy P1 P2 P3 * *
         1121  +** Synopsis: r[P2@P3]=r[P1@P3]
  1081   1122   **
  1082   1123   ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
  1083   1124   **
  1084   1125   ** This instruction makes a deep copy of the value.  A duplicate
  1085   1126   ** is made of any string or blob constant.  See also OP_SCopy.
  1086   1127   */
  1087   1128   case OP_Copy: {
................................................................................
  1102   1143       pOut++;
  1103   1144       pIn1++;
  1104   1145     }
  1105   1146     break;
  1106   1147   }
  1107   1148   
  1108   1149   /* Opcode: SCopy P1 P2 * * *
         1150  +** Synopsis: r[P2]=r[P1]
  1109   1151   **
  1110   1152   ** Make a shallow copy of register P1 into register P2.
  1111   1153   **
  1112   1154   ** This instruction makes a shallow copy of the value.  If the value
  1113   1155   ** is a string or blob, then the copy is only a pointer to the
  1114   1156   ** original and hence if the original changes so will the copy.
  1115   1157   ** Worse, if the original is deallocated, the copy becomes invalid.
  1116   1158   ** Thus the program must guarantee that the original will not change
  1117   1159   ** during the lifetime of the copy.  Use OP_Copy to make a complete
  1118   1160   ** copy.
  1119   1161   */
  1120         -case OP_SCopy: {            /* in1, out2 */
         1162  +case OP_SCopy: {            /* out2 */
  1121   1163     pIn1 = &aMem[pOp->p1];
  1122   1164     pOut = &aMem[pOp->p2];
  1123   1165     assert( pOut!=pIn1 );
  1124   1166     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1125   1167   #ifdef SQLITE_DEBUG
  1126   1168     if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
  1127   1169   #endif
  1128         -  REGISTER_TRACE(pOp->p2, pOut);
  1129   1170     break;
  1130   1171   }
  1131   1172   
  1132   1173   /* Opcode: ResultRow P1 P2 * * *
         1174  +** Synopsis:  output=r[P1@P2]
  1133   1175   **
  1134   1176   ** The registers P1 through P1+P2-1 contain a single row of
  1135   1177   ** results. This opcode causes the sqlite3_step() call to terminate
  1136   1178   ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
  1137   1179   ** structure to provide access to the top P1 values as the result
  1138   1180   ** row.
  1139   1181   */
................................................................................
  1197   1239     */
  1198   1240     p->pc = pc + 1;
  1199   1241     rc = SQLITE_ROW;
  1200   1242     goto vdbe_return;
  1201   1243   }
  1202   1244   
  1203   1245   /* Opcode: Concat P1 P2 P3 * *
         1246  +** Synopsis: r[P3]=r[P2]+r[P1]
  1204   1247   **
  1205   1248   ** Add the text in register P1 onto the end of the text in
  1206   1249   ** register P2 and store the result in register P3.
  1207   1250   ** If either the P1 or P2 text are NULL then store NULL in P3.
  1208   1251   **
  1209   1252   **   P3 = P2 || P1
  1210   1253   **
................................................................................
  1234   1277     if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
  1235   1278       goto no_mem;
  1236   1279     }
  1237   1280     if( pOut!=pIn2 ){
  1238   1281       memcpy(pOut->z, pIn2->z, pIn2->n);
  1239   1282     }
  1240   1283     memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
  1241         -  pOut->z[nByte] = 0;
         1284  +  pOut->z[nByte]=0;
  1242   1285     pOut->z[nByte+1] = 0;
  1243   1286     pOut->flags |= MEM_Term;
  1244   1287     pOut->n = (int)nByte;
  1245   1288     pOut->enc = encoding;
  1246   1289     UPDATE_MAX_BLOBSIZE(pOut);
  1247   1290     break;
  1248   1291   }
  1249   1292   
  1250   1293   /* Opcode: Add P1 P2 P3 * *
         1294  +** Synopsis:  r[P3]=r[P1]+r[P2]
  1251   1295   **
  1252   1296   ** Add the value in register P1 to the value in register P2
  1253   1297   ** and store the result in register P3.
  1254   1298   ** If either input is NULL, the result is NULL.
  1255   1299   */
  1256   1300   /* Opcode: Multiply P1 P2 P3 * *
         1301  +** Synopsis:  r[P3]=r[P1]*r[P2]
  1257   1302   **
  1258   1303   **
  1259   1304   ** Multiply the value in register P1 by the value in register P2
  1260   1305   ** and store the result in register P3.
  1261   1306   ** If either input is NULL, the result is NULL.
  1262   1307   */
  1263   1308   /* Opcode: Subtract P1 P2 P3 * *
         1309  +** Synopsis:  r[P3]=r[P2]-r[P1]
  1264   1310   **
  1265   1311   ** Subtract the value in register P1 from the value in register P2
  1266   1312   ** and store the result in register P3.
  1267   1313   ** If either input is NULL, the result is NULL.
  1268   1314   */
  1269   1315   /* Opcode: Divide P1 P2 P3 * *
         1316  +** Synopsis:  r[P3]=r[P1]/r[P2]
  1270   1317   **
  1271   1318   ** Divide the value in register P1 by the value in register P2
  1272   1319   ** and store the result in register P3 (P3=P2/P1). If the value in 
  1273   1320   ** register P1 is zero, then the result is NULL. If either input is 
  1274   1321   ** NULL, the result is NULL.
  1275   1322   */
  1276   1323   /* Opcode: Remainder P1 P2 P3 * *
         1324  +** Synopsis:  r[P3]=r[P1]%r[P2]
  1277   1325   **
  1278   1326   ** Compute the remainder after integer division of the value in
  1279   1327   ** register P1 by the value in register P2 and store the result in P3. 
  1280   1328   ** If the value in register P2 is zero the result is NULL.
  1281   1329   ** If either operand is NULL, the result is NULL.
  1282   1330   */
  1283   1331   case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
................................................................................
  1387   1435     if( pOp->p1 ){
  1388   1436       sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
  1389   1437     }
  1390   1438     break;
  1391   1439   }
  1392   1440   
  1393   1441   /* Opcode: Function P1 P2 P3 P4 P5
         1442  +** Synopsis: r[P3]=func(r[P2@P5])
  1394   1443   **
  1395   1444   ** Invoke a user function (P4 is a pointer to a Function structure that
  1396   1445   ** defines the function) with P5 arguments taken from register P2 and
  1397   1446   ** successors.  The result of the function is stored in register P3.
  1398   1447   ** Register P3 must not be one of the function inputs.
  1399   1448   **
  1400   1449   ** P1 is a 32-bit bitmask indicating whether or not each argument to the 
................................................................................
  1494   1543   
  1495   1544     REGISTER_TRACE(pOp->p3, pOut);
  1496   1545     UPDATE_MAX_BLOBSIZE(pOut);
  1497   1546     break;
  1498   1547   }
  1499   1548   
  1500   1549   /* Opcode: BitAnd P1 P2 P3 * *
         1550  +** Synopsis:  r[P3]=r[P1]&r[P2]
  1501   1551   **
  1502   1552   ** Take the bit-wise AND of the values in register P1 and P2 and
  1503   1553   ** store the result in register P3.
  1504   1554   ** If either input is NULL, the result is NULL.
  1505   1555   */
  1506   1556   /* Opcode: BitOr P1 P2 P3 * *
         1557  +** Synopsis:  r[P3]=r[P1]|r[P2]
  1507   1558   **
  1508   1559   ** Take the bit-wise OR of the values in register P1 and P2 and
  1509   1560   ** store the result in register P3.
  1510   1561   ** If either input is NULL, the result is NULL.
  1511   1562   */
  1512   1563   /* Opcode: ShiftLeft P1 P2 P3 * *
         1564  +** Synopsis:  r[P3]=r[P2]<<r[P1]
  1513   1565   **
  1514   1566   ** Shift the integer value in register P2 to the left by the
  1515   1567   ** number of bits specified by the integer in register P1.
  1516   1568   ** Store the result in register P3.
  1517   1569   ** If either input is NULL, the result is NULL.
  1518   1570   */
  1519   1571   /* Opcode: ShiftRight P1 P2 P3 * *
         1572  +** Synopsis:  r[P3]=r[P2]>>r[P1]
  1520   1573   **
  1521   1574   ** Shift the integer value in register P2 to the right by the
  1522   1575   ** number of bits specified by the integer in register P1.
  1523   1576   ** Store the result in register P3.
  1524   1577   ** If either input is NULL, the result is NULL.
  1525   1578   */
  1526   1579   case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
................................................................................
  1572   1625     }
  1573   1626     pOut->u.i = iA;
  1574   1627     MemSetTypeFlag(pOut, MEM_Int);
  1575   1628     break;
  1576   1629   }
  1577   1630   
  1578   1631   /* Opcode: AddImm  P1 P2 * * *
         1632  +** Synopsis:  r[P1]=r[P1]+P2
  1579   1633   ** 
  1580   1634   ** Add the constant P2 to the value in register P1.
  1581   1635   ** The result is always an integer.
  1582   1636   **
  1583   1637   ** To force any register to be an integer, just add 0.
  1584   1638   */
  1585   1639   case OP_AddImm: {            /* in1 */
................................................................................
  1730   1784       sqlite3VdbeMemRealify(pIn1);
  1731   1785     }
  1732   1786     break;
  1733   1787   }
  1734   1788   #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
  1735   1789   
  1736   1790   /* Opcode: Lt P1 P2 P3 P4 P5
         1791  +** Synopsis: if r[P1]<r[P3] goto P3
  1737   1792   **
  1738   1793   ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
  1739   1794   ** jump to address P2.  
  1740   1795   **
  1741   1796   ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
  1742   1797   ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
  1743   1798   ** bit is clear then fall through if either operand is NULL.
................................................................................
  1764   1819   ** store a boolean result (either 0, or 1, or NULL) in register P2.
  1765   1820   **
  1766   1821   ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
  1767   1822   ** equal to one another, provided that they do not have their MEM_Cleared
  1768   1823   ** bit set.
  1769   1824   */
  1770   1825   /* Opcode: Ne P1 P2 P3 P4 P5
         1826  +** Synopsis: if r[P1]!=r[P3] goto P2
  1771   1827   **
  1772   1828   ** This works just like the Lt opcode except that the jump is taken if
  1773   1829   ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
  1774   1830   ** additional information.
  1775   1831   **
  1776   1832   ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
  1777   1833   ** true or false and is never NULL.  If both operands are NULL then the result
  1778   1834   ** of comparison is false.  If either operand is NULL then the result is true.
  1779   1835   ** If neither operand is NULL the result is the same as it would be if
  1780   1836   ** the SQLITE_NULLEQ flag were omitted from P5.
  1781   1837   */
  1782   1838   /* Opcode: Eq P1 P2 P3 P4 P5
         1839  +** Synopsis: if r[P1]==r[P3] goto P2
  1783   1840   **
  1784   1841   ** This works just like the Lt opcode except that the jump is taken if
  1785   1842   ** the operands in registers P1 and P3 are equal.
  1786   1843   ** See the Lt opcode for additional information.
  1787   1844   **
  1788   1845   ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
  1789   1846   ** true or false and is never NULL.  If both operands are NULL then the result
  1790   1847   ** of comparison is true.  If either operand is NULL then the result is false.
  1791   1848   ** If neither operand is NULL the result is the same as it would be if
  1792   1849   ** the SQLITE_NULLEQ flag were omitted from P5.
  1793   1850   */
  1794   1851   /* Opcode: Le P1 P2 P3 P4 P5
         1852  +** Synopsis: if r[P1]<=r[P3] goto P2
  1795   1853   **
  1796   1854   ** This works just like the Lt opcode except that the jump is taken if
  1797   1855   ** the content of register P3 is less than or equal to the content of
  1798   1856   ** register P1.  See the Lt opcode for additional information.
  1799   1857   */
  1800   1858   /* Opcode: Gt P1 P2 P3 P4 P5
         1859  +** Synopsis: if r[P1]>r[P3] goto P2
  1801   1860   **
  1802   1861   ** This works just like the Lt opcode except that the jump is taken if
  1803   1862   ** the content of register P3 is greater than the content of
  1804   1863   ** register P1.  See the Lt opcode for additional information.
  1805   1864   */
  1806   1865   /* Opcode: Ge P1 P2 P3 P4 P5
         1866  +** Synopsis: if r[P1]>=r[P3] goto P2
  1807   1867   **
  1808   1868   ** This works just like the Lt opcode except that the jump is taken if
  1809   1869   ** the content of register P3 is greater than or equal to the content of
  1810   1870   ** register P1.  See the Lt opcode for additional information.
  1811   1871   */
  1812   1872   case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
  1813   1873   case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
................................................................................
  1990   2050     }else{
  1991   2051       pc = pOp->p3 - 1;
  1992   2052     }
  1993   2053     break;
  1994   2054   }
  1995   2055   
  1996   2056   /* Opcode: And P1 P2 P3 * *
         2057  +** Synopsis: r[P3]=(r[P1] && r[P2])
  1997   2058   **
  1998   2059   ** Take the logical AND of the values in registers P1 and P2 and
  1999   2060   ** write the result into register P3.
  2000   2061   **
  2001   2062   ** If either P1 or P2 is 0 (false) then the result is 0 even if
  2002   2063   ** the other input is NULL.  A NULL and true or two NULLs give
  2003   2064   ** a NULL output.
  2004   2065   */
  2005   2066   /* Opcode: Or P1 P2 P3 * *
         2067  +** Synopsis: r[P3]=(r[P1] || r[P2])
  2006   2068   **
  2007   2069   ** Take the logical OR of the values in register P1 and P2 and
  2008   2070   ** store the answer in register P3.
  2009   2071   **
  2010   2072   ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
  2011   2073   ** even if the other input is NULL.  A NULL and false or two NULLs
  2012   2074   ** give a NULL output.
................................................................................
  2042   2104       pOut->u.i = v1;
  2043   2105       MemSetTypeFlag(pOut, MEM_Int);
  2044   2106     }
  2045   2107     break;
  2046   2108   }
  2047   2109   
  2048   2110   /* Opcode: Not P1 P2 * * *
         2111  +** Synopsis: r[P2]= !r[P1]
  2049   2112   **
  2050   2113   ** Interpret the value in register P1 as a boolean value.  Store the
  2051   2114   ** boolean complement in register P2.  If the value in register P1 is 
  2052   2115   ** NULL, then a NULL is stored in P2.
  2053   2116   */
  2054   2117   case OP_Not: {                /* same as TK_NOT, in1, out2 */
  2055   2118     pIn1 = &aMem[pOp->p1];
................................................................................
  2059   2122     }else{
  2060   2123       sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
  2061   2124     }
  2062   2125     break;
  2063   2126   }
  2064   2127   
  2065   2128   /* Opcode: BitNot P1 P2 * * *
         2129  +** Synopsis: r[P1]= ~r[P1]
  2066   2130   **
  2067   2131   ** Interpret the content of register P1 as an integer.  Store the
  2068   2132   ** ones-complement of the P1 value into register P2.  If P1 holds
  2069   2133   ** a NULL then store a NULL in P2.
  2070   2134   */
  2071   2135   case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
  2072   2136     pIn1 = &aMem[pOp->p1];
................................................................................
  2123   2187     if( c ){
  2124   2188       pc = pOp->p2-1;
  2125   2189     }
  2126   2190     break;
  2127   2191   }
  2128   2192   
  2129   2193   /* Opcode: IsNull P1 P2 * * *
         2194  +** Synopsis:  if r[P1]==NULL goto P2
  2130   2195   **
  2131   2196   ** Jump to P2 if the value in register P1 is NULL.
  2132   2197   */
  2133   2198   case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
  2134   2199     pIn1 = &aMem[pOp->p1];
  2135   2200     if( (pIn1->flags & MEM_Null)!=0 ){
  2136   2201       pc = pOp->p2 - 1;
  2137   2202     }
  2138   2203     break;
  2139   2204   }
  2140   2205   
  2141   2206   /* Opcode: NotNull P1 P2 * * *
         2207  +** Synopsis: if r[P1]!=NULL goto P2
  2142   2208   **
  2143   2209   ** Jump to P2 if the value in register P1 is not NULL.  
  2144   2210   */
  2145   2211   case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
  2146   2212     pIn1 = &aMem[pOp->p1];
  2147   2213     if( (pIn1->flags & MEM_Null)==0 ){
  2148   2214       pc = pOp->p2 - 1;
  2149   2215     }
  2150   2216     break;
  2151   2217   }
  2152   2218   
  2153   2219   /* Opcode: Column P1 P2 P3 P4 P5
         2220  +** Synopsis:  r[P3]=PX
  2154   2221   **
  2155   2222   ** Interpret the data that cursor P1 points to as a structure built using
  2156   2223   ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
  2157   2224   ** information about the format of the data.)  Extract the P2-th column
  2158   2225   ** from this record.  If there are less that (P2+1) 
  2159   2226   ** values in the record, extract a NULL.
  2160   2227   **
................................................................................
  2247   2314         assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
  2248   2315         payloadSize = (u32)payloadSize64;
  2249   2316       }else{
  2250   2317         assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2251   2318         VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &payloadSize);
  2252   2319         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
  2253   2320       }
  2254         -  }else if( ALWAYS(pC->pseudoTableReg>0) ){
         2321  +  }else{
         2322  +    assert( pC->pseudoTableReg>0 );
  2255   2323       pReg = &aMem[pC->pseudoTableReg];
  2256   2324       if( pC->multiPseudo ){
  2257   2325         sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
  2258   2326         Deephemeralize(pDest);
  2259   2327         goto op_column_out;
  2260   2328       }
  2261   2329       assert( pReg->flags & MEM_Blob );
  2262   2330       assert( memIsValid(pReg) );
  2263   2331       payloadSize = pReg->n;
  2264   2332       zRec = pReg->z;
  2265   2333       pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  2266   2334       assert( payloadSize==0 || zRec!=0 );
  2267         -  }else{
  2268         -    /* Consider the row to be NULL */
  2269         -    payloadSize = 0;
  2270   2335     }
  2271   2336   
  2272   2337     /* If payloadSize is 0, then just store a NULL.  This can happen because of
  2273   2338     ** nullRow or because of a corrupt database. */
  2274   2339     if( payloadSize==0 ){
  2275   2340       MemSetTypeFlag(pDest, MEM_Null);
  2276   2341       goto op_column_out;
................................................................................
  2483   2548   op_column_out:
  2484   2549     UPDATE_MAX_BLOBSIZE(pDest);
  2485   2550     REGISTER_TRACE(pOp->p3, pDest);
  2486   2551     break;
  2487   2552   }
  2488   2553   
  2489   2554   /* Opcode: Affinity P1 P2 * P4 *
         2555  +** Synopsis: affinity(r[P1@P2])
  2490   2556   **
  2491   2557   ** Apply affinities to a range of P2 registers starting with P1.
  2492   2558   **
  2493   2559   ** P4 is a string that is P2 characters long. The nth character of the
  2494   2560   ** string indicates the column affinity that should be used for the nth
  2495   2561   ** memory cell in the range.
  2496   2562   */
................................................................................
  2509   2575       applyAffinity(pIn1, cAff, encoding);
  2510   2576       pIn1++;
  2511   2577     }
  2512   2578     break;
  2513   2579   }
  2514   2580   
  2515   2581   /* Opcode: MakeRecord P1 P2 P3 P4 *
         2582  +** Synopsis: r[P3]=mkrec(r[P1@P2])
  2516   2583   **
  2517   2584   ** Convert P2 registers beginning with P1 into the [record format]
  2518   2585   ** use as a data record in a database table or as a key
  2519   2586   ** in an index.  The OP_Column opcode can decode the record later.
  2520   2587   **
  2521   2588   ** P4 may be a string that is P2 characters long.  The nth character of the
  2522   2589   ** string indicates the column affinity that should be used for the nth
................................................................................
  2641   2708     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
  2642   2709     REGISTER_TRACE(pOp->p3, pOut);
  2643   2710     UPDATE_MAX_BLOBSIZE(pOut);
  2644   2711     break;
  2645   2712   }
  2646   2713   
  2647   2714   /* Opcode: Count P1 P2 * * *
         2715  +** Synopsis: r[P2]=count()
  2648   2716   **
  2649   2717   ** Store the number of entries (an integer value) in the table or index 
  2650   2718   ** opened by cursor P1 in register P2
  2651   2719   */
  2652   2720   #ifndef SQLITE_OMIT_BTREECOUNT
  2653   2721   case OP_Count: {         /* out2-prerelease */
  2654   2722     i64 nEntry;
  2655   2723     BtCursor *pCrsr;
  2656   2724   
  2657   2725     pCrsr = p->apCsr[pOp->p1]->pCursor;
  2658         -  if( ALWAYS(pCrsr) ){
  2659         -    rc = sqlite3BtreeCount(pCrsr, &nEntry);
  2660         -  }else{
  2661         -    nEntry = 0;
  2662         -  }
         2726  +  assert( pCrsr );
         2727  +  rc = sqlite3BtreeCount(pCrsr, &nEntry);
  2663   2728     pOut->u.i = nEntry;
  2664   2729     break;
  2665   2730   }
  2666   2731   #endif
  2667   2732   
  2668   2733   /* Opcode: Savepoint P1 * * P4 *
  2669   2734   **
................................................................................
  3117   3182       p->expired = 1;
  3118   3183       rc = SQLITE_SCHEMA;
  3119   3184     }
  3120   3185     break;
  3121   3186   }
  3122   3187   
  3123   3188   /* Opcode: OpenRead P1 P2 P3 P4 P5
         3189  +** Synopsis: root=P2 iDb=P3
  3124   3190   **
  3125   3191   ** Open a read-only cursor for the database table whose root page is
  3126   3192   ** P2 in a database file.  The database file is determined by P3. 
  3127   3193   ** P3==0 means the main database, P3==1 means the database used for 
  3128   3194   ** temporary tables, and P3>1 means used the corresponding attached
  3129   3195   ** database.  Give the new cursor an identifier of P1.  The P1
  3130   3196   ** values need not be contiguous but all P1 values should be small integers.
................................................................................
  3147   3213   ** structure, then said structure defines the content and collating 
  3148   3214   ** sequence of the index being opened. Otherwise, if P4 is an integer 
  3149   3215   ** value, it is set to the number of columns in the table.
  3150   3216   **
  3151   3217   ** See also OpenWrite.
  3152   3218   */
  3153   3219   /* Opcode: OpenWrite P1 P2 P3 P4 P5
         3220  +** Synopsis: root=P2 iDb=P3
  3154   3221   **
  3155   3222   ** Open a read/write cursor named P1 on the table or index whose root
  3156   3223   ** page is P2.  Or if P5!=0 use the content of register P2 to find the
  3157   3224   ** root page.
  3158   3225   **
  3159   3226   ** The P4 value may be either an integer (P4_INT32) or a pointer to
  3160   3227   ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
................................................................................
  3223   3290       if( NEVER(p2<2) ) {
  3224   3291         rc = SQLITE_CORRUPT_BKPT;
  3225   3292         goto abort_due_to_error;
  3226   3293       }
  3227   3294     }
  3228   3295     if( pOp->p4type==P4_KEYINFO ){
  3229   3296       pKeyInfo = pOp->p4.pKeyInfo;
  3230         -    pKeyInfo->enc = ENC(p->db);
  3231         -    nField = pKeyInfo->nField+1;
         3297  +    assert( pKeyInfo->enc==ENC(db) );
         3298  +    assert( pKeyInfo->db==db );
         3299  +    nField = pKeyInfo->nField+pKeyInfo->nXField;
  3232   3300     }else if( pOp->p4type==P4_INT32 ){
  3233   3301       nField = pOp->p4.i;
  3234   3302     }
  3235   3303     assert( pOp->p1>=0 );
  3236   3304     pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
  3237   3305     if( pCur==0 ) goto no_mem;
  3238   3306     pCur->nullRow = 1;
................................................................................
  3252   3320     ** since moved into the btree layer.  */  
  3253   3321     pCur->isTable = pOp->p4type!=P4_KEYINFO;
  3254   3322     pCur->isIndex = !pCur->isTable;
  3255   3323     break;
  3256   3324   }
  3257   3325   
  3258   3326   /* Opcode: OpenEphemeral P1 P2 * P4 P5
         3327  +** Synopsis: nColumn=P2
  3259   3328   **
  3260   3329   ** Open a new cursor P1 to a transient table.
  3261   3330   ** The cursor is always opened read/write even if 
  3262   3331   ** the main database is read-only.  The ephemeral
  3263   3332   ** table is deleted automatically when the cursor is closed.
  3264   3333   **
  3265   3334   ** P2 is the number of columns in the ephemeral table.
  3266   3335   ** The cursor points to a BTree table if P4==0 and to a BTree index
  3267   3336   ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
  3268   3337   ** that defines the format of keys in the index.
  3269   3338   **
  3270         -** This opcode was once called OpenTemp.  But that created
  3271         -** confusion because the term "temp table", might refer either
  3272         -** to a TEMP table at the SQL level, or to a table opened by
  3273         -** this opcode.  Then this opcode was call OpenVirtual.  But
  3274         -** that created confusion with the whole virtual-table idea.
  3275         -**
  3276   3339   ** The P5 parameter can be a mask of the BTREE_* flags defined
  3277   3340   ** in btree.h.  These flags control aspects of the operation of
  3278   3341   ** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
  3279   3342   ** added automatically.
  3280   3343   */
  3281   3344   /* Opcode: OpenAutoindex P1 P2 * P4 *
         3345  +** Synopsis: nColumn=P2
  3282   3346   **
  3283   3347   ** This opcode works the same as OP_OpenEphemeral.  It has a
  3284   3348   ** different name to distinguish its use.  Tables created using
  3285   3349   ** by this opcode will be used for automatically created transient
  3286   3350   ** indices in joins.
  3287   3351   */
  3288   3352   case OP_OpenAutoindex: 
  3289   3353   case OP_OpenEphemeral: {
  3290   3354     VdbeCursor *pCx;
         3355  +  KeyInfo *pKeyInfo;
         3356  +
  3291   3357     static const int vfsFlags = 
  3292   3358         SQLITE_OPEN_READWRITE |
  3293   3359         SQLITE_OPEN_CREATE |
  3294   3360         SQLITE_OPEN_EXCLUSIVE |
  3295   3361         SQLITE_OPEN_DELETEONCLOSE |
  3296   3362         SQLITE_OPEN_TRANSIENT_DB;
  3297         -
  3298   3363     assert( pOp->p1>=0 );
  3299   3364     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3300   3365     if( pCx==0 ) goto no_mem;
  3301   3366     pCx->nullRow = 1;
  3302   3367     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
  3303   3368                           BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
  3304   3369     if( rc==SQLITE_OK ){
................................................................................
  3306   3371     }
  3307   3372     if( rc==SQLITE_OK ){
  3308   3373       /* If a transient index is required, create it by calling
  3309   3374       ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
  3310   3375       ** opening it. If a transient table is required, just use the
  3311   3376       ** automatically created table with root-page 1 (an BLOB_INTKEY table).
  3312   3377       */
  3313         -    if( pOp->p4.pKeyInfo ){
         3378  +    if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
  3314   3379         int pgno;
  3315   3380         assert( pOp->p4type==P4_KEYINFO );
  3316   3381         rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); 
  3317   3382         if( rc==SQLITE_OK ){
  3318   3383           assert( pgno==MASTER_ROOT+1 );
  3319         -        rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, 
  3320         -                                (KeyInfo*)pOp->p4.z, pCx->pCursor);
  3321         -        pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3322         -        pCx->pKeyInfo->enc = ENC(p->db);
         3384  +        assert( pKeyInfo->db==db );
         3385  +        assert( pKeyInfo->enc==ENC(db) );
         3386  +        pCx->pKeyInfo = pKeyInfo;
         3387  +        rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
  3323   3388         }
  3324   3389         pCx->isTable = 0;
  3325   3390       }else{
  3326   3391         rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
  3327   3392         pCx->isTable = 1;
  3328   3393       }
  3329   3394     }
  3330   3395     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3331   3396     pCx->isIndex = !pCx->isTable;
  3332   3397     break;
  3333   3398   }
  3334   3399   
  3335         -/* Opcode: SorterOpen P1 P2 * P4 *
         3400  +/* Opcode: SorterOpen P1 * * P4 *
  3336   3401   **
  3337   3402   ** This opcode works like OP_OpenEphemeral except that it opens
  3338   3403   ** a transient index that is specifically designed to sort large
  3339   3404   ** tables using an external merge-sort algorithm.
  3340   3405   */
  3341   3406   case OP_SorterOpen: {
  3342   3407     VdbeCursor *pCx;
  3343   3408   
  3344   3409     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3345   3410     if( pCx==0 ) goto no_mem;
  3346   3411     pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3347         -  pCx->pKeyInfo->enc = ENC(p->db);
         3412  +  assert( pCx->pKeyInfo->db==db );
         3413  +  assert( pCx->pKeyInfo->enc==ENC(db) );
  3348   3414     pCx->isSorter = 1;
  3349   3415     rc = sqlite3VdbeSorterInit(db, pCx);
  3350   3416     break;
  3351   3417   }
  3352   3418   
  3353   3419   /* Opcode: OpenPseudo P1 P2 P3 * P5
         3420  +** Synopsis: content in r[P2@P3]
  3354   3421   **
  3355   3422   ** Open a new cursor that points to a fake table that contains a single
  3356   3423   ** row of data.  The content of that one row in the content of memory
  3357   3424   ** register P2 when P5==0.  In other words, cursor P1 becomes an alias for the 
  3358   3425   ** MEM_Blob content contained in register P2.  When P5==1, then the
  3359   3426   ** row is represented by P3 consecutive registers beginning with P2.
  3360   3427   **
................................................................................
  3389   3456     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3390   3457     sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
  3391   3458     p->apCsr[pOp->p1] = 0;
  3392   3459     break;
  3393   3460   }
  3394   3461   
  3395   3462   /* Opcode: SeekGe P1 P2 P3 P4 *
         3463  +** Synopsis: key=r[P3@P4]
  3396   3464   **
  3397   3465   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
  3398   3466   ** use the value in register P3 as the key.  If cursor P1 refers 
  3399   3467   ** to an SQL index, then P3 is the first in an array of P4 registers 
  3400   3468   ** that are used as an unpacked index key. 
  3401   3469   **
  3402   3470   ** Reposition cursor P1 so that  it points to the smallest entry that 
  3403   3471   ** is greater than or equal to the key value. If there are no records 
  3404   3472   ** greater than or equal to the key and P2 is not zero, then jump to P2.
  3405   3473   **
  3406   3474   ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
  3407   3475   */
  3408   3476   /* Opcode: SeekGt P1 P2 P3 P4 *
         3477  +** Synopsis: key=r[P3@P4]
  3409   3478   **
  3410   3479   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
  3411   3480   ** use the value in register P3 as a key. If cursor P1 refers 
  3412   3481   ** to an SQL index, then P3 is the first in an array of P4 registers 
  3413   3482   ** that are used as an unpacked index key. 
  3414   3483   **
  3415   3484   ** Reposition cursor P1 so that  it points to the smallest entry that 
  3416   3485   ** is greater than the key value. If there are no records greater than 
  3417   3486   ** the key and P2 is not zero, then jump to P2.
  3418   3487   **
  3419   3488   ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
  3420   3489   */
  3421   3490   /* Opcode: SeekLt P1 P2 P3 P4 * 
         3491  +** Synopsis: key=r[P3@P4]
  3422   3492   **
  3423   3493   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
  3424   3494   ** use the value in register P3 as a key. If cursor P1 refers 
  3425   3495   ** to an SQL index, then P3 is the first in an array of P4 registers 
  3426   3496   ** that are used as an unpacked index key. 
  3427   3497   **
  3428   3498   ** Reposition cursor P1 so that  it points to the largest entry that 
  3429   3499   ** is less than the key value. If there are no records less than 
  3430   3500   ** the key and P2 is not zero, then jump to P2.
  3431   3501   **
  3432   3502   ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
  3433   3503   */
  3434   3504   /* Opcode: SeekLe P1 P2 P3 P4 *
         3505  +** Synopsis: key=r[P3@P4]
  3435   3506   **
  3436   3507   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
  3437   3508   ** use the value in register P3 as a key. If cursor P1 refers 
  3438   3509   ** to an SQL index, then P3 is the first in an array of P4 registers 
  3439   3510   ** that are used as an unpacked index key. 
  3440   3511   **
  3441   3512   ** Reposition cursor P1 so that it points to the largest entry that 
................................................................................
  3460   3531     pC = p->apCsr[pOp->p1];
  3461   3532     assert( pC!=0 );
  3462   3533     assert( pC->pseudoTableReg==0 );
  3463   3534     assert( OP_SeekLe == OP_SeekLt+1 );
  3464   3535     assert( OP_SeekGe == OP_SeekLt+2 );
  3465   3536     assert( OP_SeekGt == OP_SeekLt+3 );
  3466   3537     assert( pC->isOrdered );
  3467         -  if( ALWAYS(pC->pCursor!=0) ){
  3468         -    oc = pOp->opcode;
  3469         -    pC->nullRow = 0;
  3470         -    if( pC->isTable ){
  3471         -      /* The input value in P3 might be of any type: integer, real, string,
  3472         -      ** blob, or NULL.  But it needs to be an integer before we can do
  3473         -      ** the seek, so covert it. */
  3474         -      pIn3 = &aMem[pOp->p3];
  3475         -      applyNumericAffinity(pIn3);
  3476         -      iKey = sqlite3VdbeIntValue(pIn3);
  3477         -      pC->rowidIsValid = 0;
  3478         -
  3479         -      /* If the P3 value could not be converted into an integer without
  3480         -      ** loss of information, then special processing is required... */
  3481         -      if( (pIn3->flags & MEM_Int)==0 ){
  3482         -        if( (pIn3->flags & MEM_Real)==0 ){
  3483         -          /* If the P3 value cannot be converted into any kind of a number,
  3484         -          ** then the seek is not possible, so jump to P2 */
  3485         -          pc = pOp->p2 - 1;
  3486         -          break;
  3487         -        }
  3488         -        /* If we reach this point, then the P3 value must be a floating
  3489         -        ** point number. */
  3490         -        assert( (pIn3->flags & MEM_Real)!=0 );
  3491         -
  3492         -        if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
  3493         -          /* The P3 value is too large in magnitude to be expressed as an
  3494         -          ** integer. */
  3495         -          res = 1;
  3496         -          if( pIn3->r<0 ){
  3497         -            if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
  3498         -              rc = sqlite3BtreeFirst(pC->pCursor, &res);
  3499         -              if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3500         -            }
  3501         -          }else{
  3502         -            if( oc<=OP_SeekLe ){  assert( oc==OP_SeekLt || oc==OP_SeekLe );
  3503         -              rc = sqlite3BtreeLast(pC->pCursor, &res);
  3504         -              if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3505         -            }
         3538  +  assert( pC->pCursor!=0 );
         3539  +  oc = pOp->opcode;
         3540  +  pC->nullRow = 0;
         3541  +  if( pC->isTable ){
         3542  +    /* The input value in P3 might be of any type: integer, real, string,
         3543  +    ** blob, or NULL.  But it needs to be an integer before we can do
         3544  +    ** the seek, so covert it. */
         3545  +    pIn3 = &aMem[pOp->p3];
         3546  +    applyNumericAffinity(pIn3);
         3547  +    iKey = sqlite3VdbeIntValue(pIn3);
         3548  +    pC->rowidIsValid = 0;
         3549  +
         3550  +    /* If the P3 value could not be converted into an integer without
         3551  +    ** loss of information, then special processing is required... */
         3552  +    if( (pIn3->flags & MEM_Int)==0 ){
         3553  +      if( (pIn3->flags & MEM_Real)==0 ){
         3554  +        /* If the P3 value cannot be converted into any kind of a number,
         3555  +        ** then the seek is not possible, so jump to P2 */
         3556  +        pc = pOp->p2 - 1;
         3557  +        break;
         3558  +      }
         3559  +      /* If we reach this point, then the P3 value must be a floating
         3560  +      ** point number. */
         3561  +      assert( (pIn3->flags & MEM_Real)!=0 );
         3562  +
         3563  +      if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
         3564  +        /* The P3 value is too large in magnitude to be expressed as an
         3565  +        ** integer. */
         3566  +        res = 1;
         3567  +        if( pIn3->r<0 ){
         3568  +          if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
         3569  +            rc = sqlite3BtreeFirst(pC->pCursor, &res);
         3570  +            if( rc!=SQLITE_OK ) goto abort_due_to_error;
         3571  +          }
         3572  +        }else{
         3573  +          if( oc<=OP_SeekLe ){  assert( oc==OP_SeekLt || oc==OP_SeekLe );
         3574  +            rc = sqlite3BtreeLast(pC->pCursor, &res);
         3575  +            if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3506   3576             }
  3507         -          if( res ){
  3508         -            pc = pOp->p2 - 1;
  3509         -          }
  3510         -          break;
  3511         -        }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
  3512         -          /* Use the ceiling() function to convert real->int */
  3513         -          if( pIn3->r > (double)iKey ) iKey++;
  3514         -        }else{
  3515         -          /* Use the floor() function to convert real->int */
  3516         -          assert( oc==OP_SeekLe || oc==OP_SeekGt );
  3517         -          if( pIn3->r < (double)iKey ) iKey--;
         3577  +        }
         3578  +        if( res ){
         3579  +          pc = pOp->p2 - 1;
  3518   3580           }
  3519         -      } 
  3520         -      rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
  3521         -      if( rc!=SQLITE_OK ){
  3522         -        goto abort_due_to_error;
  3523         -      }
  3524         -      if( res==0 ){
  3525         -        pC->rowidIsValid = 1;
  3526         -        pC->lastRowid = iKey;
  3527         -      }
  3528         -    }else{
  3529         -      nField = pOp->p4.i;
  3530         -      assert( pOp->p4type==P4_INT32 );
  3531         -      assert( nField>0 );
  3532         -      r.pKeyInfo = pC->pKeyInfo;
  3533         -      r.nField = (u16)nField;
  3534         -
  3535         -      /* The next line of code computes as follows, only faster:
  3536         -      **   if( oc==OP_SeekGt || oc==OP_SeekLe ){
  3537         -      **     r.flags = UNPACKED_INCRKEY;
  3538         -      **   }else{
  3539         -      **     r.flags = 0;
  3540         -      **   }
  3541         -      */
  3542         -      r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
  3543         -      assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
  3544         -      assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
  3545         -      assert( oc!=OP_SeekGe || r.flags==0 );
  3546         -      assert( oc!=OP_SeekLt || r.flags==0 );
  3547         -
  3548         -      r.aMem = &aMem[pOp->p3];
         3581  +        break;
         3582  +      }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
         3583  +        /* Use the ceiling() function to convert real->int */
         3584  +        if( pIn3->r > (double)iKey ) iKey++;
         3585  +      }else{
         3586  +        /* Use the floor() function to convert real->int */
         3587  +        assert( oc==OP_SeekLe || oc==OP_SeekGt );
         3588  +        if( pIn3->r < (double)iKey ) iKey--;
         3589  +      }
         3590  +    } 
         3591  +    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
         3592  +    if( rc!=SQLITE_OK ){
         3593  +      goto abort_due_to_error;
         3594  +    }
         3595  +    if( res==0 ){
         3596  +      pC->rowidIsValid = 1;
         3597  +      pC->lastRowid = iKey;
         3598  +    }
         3599  +  }else{
         3600  +    nField = pOp->p4.i;
         3601  +    assert( pOp->p4type==P4_INT32 );
         3602  +    assert( nField>0 );
         3603  +    r.pKeyInfo = pC->pKeyInfo;
         3604  +    r.nField = (u16)nField;
         3605  +
         3606  +    /* The next line of code computes as follows, only faster:
         3607  +    **   if( oc==OP_SeekGt || oc==OP_SeekLe ){
         3608  +    **     r.flags = UNPACKED_INCRKEY;
         3609  +    **   }else{
         3610  +    **     r.flags = 0;
         3611  +    **   }
         3612  +    */
         3613  +    r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
         3614  +    assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
         3615  +    assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
         3616  +    assert( oc!=OP_SeekGe || r.flags==0 );
         3617  +    assert( oc!=OP_SeekLt || r.flags==0 );
         3618  +
         3619  +    r.aMem = &aMem[pOp->p3];
  3549   3620   #ifdef SQLITE_DEBUG
  3550         -      { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
         3621  +    { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3551   3622   #endif
  3552         -      ExpandBlob(r.aMem);
  3553         -      rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
  3554         -      if( rc!=SQLITE_OK ){
  3555         -        goto abort_due_to_error;
  3556         -      }
  3557         -      pC->rowidIsValid = 0;
         3623  +    ExpandBlob(r.aMem);
         3624  +    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
         3625  +    if( rc!=SQLITE_OK ){
         3626  +      goto abort_due_to_error;
  3558   3627       }
  3559         -    pC->deferredMoveto = 0;
  3560         -    pC->cacheStatus = CACHE_STALE;
         3628  +    pC->rowidIsValid = 0;
         3629  +  }
         3630  +  pC->deferredMoveto = 0;
         3631  +  pC->cacheStatus = CACHE_STALE;
  3561   3632   #ifdef SQLITE_TEST
  3562         -    sqlite3_search_count++;
         3633  +  sqlite3_search_count++;
  3563   3634   #endif
  3564         -    if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
  3565         -      if( res<0 || (res==0 && oc==OP_SeekGt) ){
  3566         -        rc = sqlite3BtreeNext(pC->pCursor, &res);
  3567         -        if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3568         -        pC->rowidIsValid = 0;
  3569         -      }else{
  3570         -        res = 0;
  3571         -      }
         3635  +  if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
         3636  +    if( res<0 || (res==0 && oc==OP_SeekGt) ){
         3637  +      rc = sqlite3BtreeNext(pC->pCursor, &res);
         3638  +      if( rc!=SQLITE_OK ) goto abort_due_to_error;
         3639  +      pC->rowidIsValid = 0;
         3640  +    }else{
         3641  +      res = 0;
         3642  +    }
         3643  +  }else{
         3644  +    assert( oc==OP_SeekLt || oc==OP_SeekLe );
         3645  +    if( res>0 || (res==0 && oc==OP_SeekLt) ){
         3646  +      rc = sqlite3BtreePrevious(pC->pCursor, &res);
         3647  +      if( rc!=SQLITE_OK ) goto abort_due_to_error;
         3648  +      pC->rowidIsValid = 0;
  3572   3649       }else{
  3573         -      assert( oc==OP_SeekLt || oc==OP_SeekLe );
  3574         -      if( res>0 || (res==0 && oc==OP_SeekLt) ){
  3575         -        rc = sqlite3BtreePrevious(pC->pCursor, &res);
  3576         -        if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3577         -        pC->rowidIsValid = 0;
  3578         -      }else{
  3579         -        /* res might be negative because the table is empty.  Check to
  3580         -        ** see if this is the case.
  3581         -        */
  3582         -        res = sqlite3BtreeEof(pC->pCursor);
  3583         -      }
         3650  +      /* res might be negative because the table is empty.  Check to
         3651  +      ** see if this is the case.
         3652  +      */
         3653  +      res = sqlite3BtreeEof(pC->pCursor);
  3584   3654       }
  3585         -    assert( pOp->p2>0 );
  3586         -    if( res ){
  3587         -      pc = pOp->p2 - 1;
  3588         -    }
  3589         -  }else{
  3590         -    /* This happens when attempting to open the sqlite3_master table
  3591         -    ** for read access returns SQLITE_EMPTY. In this case always
  3592         -    ** take the jump (since there are no records in the table).
  3593         -    */
         3655  +  }
         3656  +  assert( pOp->p2>0 );
         3657  +  if( res ){
  3594   3658       pc = pOp->p2 - 1;
  3595   3659     }
  3596   3660     break;
  3597   3661   }
  3598   3662   
  3599   3663   /* Opcode: Seek P1 P2 * * *
         3664  +** Synopsis:  intkey=r[P2]
  3600   3665   **
  3601   3666   ** P1 is an open table cursor and P2 is a rowid integer.  Arrange
  3602   3667   ** for P1 to move so that it points to the rowid given by P2.
  3603   3668   **
  3604   3669   ** This is actually a deferred seek.  Nothing actually happens until
  3605   3670   ** the cursor is used to read a record.  That way, if no reads
  3606   3671   ** occur, no unnecessary I/O happens.
................................................................................
  3607   3672   */
  3608   3673   case OP_Seek: {    /* in2 */
  3609   3674     VdbeCursor *pC;
  3610   3675   
  3611   3676     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3612   3677     pC = p->apCsr[pOp->p1];
  3613   3678     assert( pC!=0 );
  3614         -  if( ALWAYS(pC->pCursor!=0) ){
  3615         -    assert( pC->isTable );
  3616         -    pC->nullRow = 0;
  3617         -    pIn2 = &aMem[pOp->p2];
  3618         -    pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
  3619         -    pC->rowidIsValid = 0;
  3620         -    pC->deferredMoveto = 1;
  3621         -  }
         3679  +  assert( pC->pCursor!=0 );
         3680  +  assert( pC->isTable );
         3681  +  pC->nullRow = 0;
         3682  +  pIn2 = &aMem[pOp->p2];
         3683  +  pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
         3684  +  pC->rowidIsValid = 0;
         3685  +  pC->deferredMoveto = 1;
  3622   3686     break;
  3623   3687   }
  3624   3688     
  3625   3689   
  3626   3690   /* Opcode: Found P1 P2 P3 P4 *
         3691  +** Synopsis: key=r[P3@P4]
  3627   3692   **
  3628   3693   ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
  3629   3694   ** P4>0 then register P3 is the first of P4 registers that form an unpacked
  3630   3695   ** record.
  3631   3696   **
  3632   3697   ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
  3633   3698   ** is a prefix of any entry in P1 then a jump is made to P2 and
  3634   3699   ** P1 is left pointing at the matching entry.
         3700  +**
         3701  +** See also: NotFound, NoConflict, NotExists. SeekGe
  3635   3702   */
  3636   3703   /* Opcode: NotFound P1 P2 P3 P4 *
         3704  +** Synopsis: key=r[P3@P4]
  3637   3705   **
  3638   3706   ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
  3639   3707   ** P4>0 then register P3 is the first of P4 registers that form an unpacked
  3640   3708   ** record.
  3641   3709   ** 
  3642   3710   ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
  3643   3711   ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1 
  3644   3712   ** does contain an entry whose prefix matches the P3/P4 record then control
  3645   3713   ** falls through to the next instruction and P1 is left pointing at the
  3646   3714   ** matching entry.
  3647   3715   **
  3648         -** See also: Found, NotExists, IsUnique
         3716  +** See also: Found, NotExists, NoConflict
  3649   3717   */
         3718  +/* Opcode: NoConflict P1 P2 P3 P4 *
         3719  +** Synopsis: key=r[P3@P4]
         3720  +**
         3721  +** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
         3722  +** P4>0 then register P3 is the first of P4 registers that form an unpacked
         3723  +** record.
         3724  +** 
         3725  +** Cursor P1 is on an index btree.  If the record identified by P3 and P4
         3726  +** contains any NULL value, jump immediately to P2.  If all terms of the
         3727  +** record are not-NULL then a check is done to determine if any row in the
         3728  +** P1 index btree has a matching key prefix.  If there are no matches, jump
         3729  +** immediately to P2.  If there is a match, fall through and leave the P1
         3730  +** cursor pointing to the matching row.
         3731  +**
         3732  +** This opcode is similar to OP_NotFound with the exceptions that the
         3733  +** branch is always taken if any part of the search key input is NULL.
         3734  +**
         3735  +** See also: NotFound, Found, NotExists
         3736  +*/
         3737  +case OP_NoConflict:     /* jump, in3 */
  3650   3738   case OP_NotFound:       /* jump, in3 */
  3651   3739   case OP_Found: {        /* jump, in3 */
  3652   3740     int alreadyExists;
         3741  +  int ii;
  3653   3742     VdbeCursor *pC;
  3654   3743     int res;
  3655   3744     char *pFree;
  3656   3745     UnpackedRecord *pIdxKey;
  3657   3746     UnpackedRecord r;
  3658         -  char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
         3747  +  char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
  3659   3748   
  3660   3749   #ifdef SQLITE_TEST
  3661         -  sqlite3_found_count++;
         3750  +  if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
  3662   3751   #endif
  3663   3752   
  3664   3753     alreadyExists = 0;
  3665   3754     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3666   3755     assert( pOp->p4type==P4_INT32 );
  3667   3756     pC = p->apCsr[pOp->p1];
  3668   3757     assert( pC!=0 );
  3669   3758     pIn3 = &aMem[pOp->p3];
  3670         -  if( ALWAYS(pC->pCursor!=0) ){
  3671         -
  3672         -    assert( pC->isTable==0 );
  3673         -    if( pOp->p4.i>0 ){
  3674         -      r.pKeyInfo = pC->pKeyInfo;
  3675         -      r.nField = (u16)pOp->p4.i;
  3676         -      r.aMem = pIn3;
         3759  +  assert( pC->pCursor!=0 );
         3760  +  assert( pC->isTable==0 );
         3761  +  if( pOp->p4.i>0 ){
         3762  +    r.pKeyInfo = pC->pKeyInfo;
         3763  +    r.nField = (u16)pOp->p4.i;
         3764  +    r.aMem = pIn3;
  3677   3765   #ifdef SQLITE_DEBUG
  3678         -      { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3679         -#endif
  3680         -      r.flags = UNPACKED_PREFIX_MATCH;
  3681         -      pIdxKey = &r;
  3682         -    }else{
  3683         -      pIdxKey = sqlite3VdbeAllocUnpackedRecord(
  3684         -          pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
  3685         -      ); 
  3686         -      if( pIdxKey==0 ) goto no_mem;
  3687         -      assert( pIn3->flags & MEM_Blob );
  3688         -      assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
  3689         -      sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
  3690         -      pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
  3691         -    }
  3692         -    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
  3693         -    if( pOp->p4.i==0 ){
  3694         -      sqlite3DbFree(db, pFree);
  3695         -    }
  3696         -    if( rc!=SQLITE_OK ){
  3697         -      break;
  3698         -    }
  3699         -    alreadyExists = (res==0);
  3700         -    pC->deferredMoveto = 0;
  3701         -    pC->cacheStatus = CACHE_STALE;
  3702         -  }
         3766  +    {
         3767  +      int i;
         3768  +      for(i=0; i<r.nField; i++){
         3769  +        assert( memIsValid(&r.aMem[i]) );
         3770  +        if( i ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
         3771  +      }
         3772  +    }
         3773  +#endif
         3774  +    r.flags = UNPACKED_PREFIX_MATCH;
         3775  +    pIdxKey = &r;
         3776  +  }else{
         3777  +    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
         3778  +        pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
         3779  +    ); 
         3780  +    if( pIdxKey==0 ) goto no_mem;
         3781  +    assert( pIn3->flags & MEM_Blob );
         3782  +    assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
         3783  +    sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
         3784  +    pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
         3785  +  }
         3786  +  if( pOp->opcode==OP_NoConflict ){
         3787  +    /* For the OP_NoConflict opcode, take the jump if any of the
         3788  +    ** input fields are NULL, since any key with a NULL will not
         3789  +    ** conflict */
         3790  +    for(ii=0; ii<r.nField; ii++){
         3791  +      if( r.aMem[ii].flags & MEM_Null ){
         3792  +        pc = pOp->p2 - 1;
         3793  +        break;
         3794  +      }
         3795  +    }
         3796  +  }
         3797  +  rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
         3798  +  if( pOp->p4.i==0 ){
         3799  +    sqlite3DbFree(db, pFree);
         3800  +  }
         3801  +  if( rc!=SQLITE_OK ){
         3802  +    break;
         3803  +  }
         3804  +  pC->seekResult = res;
         3805  +  alreadyExists = (res==0);
         3806  +  pC->nullRow = 1-alreadyExists;
         3807  +  pC->deferredMoveto = 0;
         3808  +  pC->cacheStatus = CACHE_STALE;
  3703   3809     if( pOp->opcode==OP_Found ){
  3704   3810       if( alreadyExists ) pc = pOp->p2 - 1;
  3705   3811     }else{
  3706   3812       if( !alreadyExists ) pc = pOp->p2 - 1;
  3707   3813     }
  3708   3814     break;
  3709   3815   }
  3710   3816   
  3711         -/* Opcode: IsUnique P1 P2 P3 P4 *
  3712         -**
  3713         -** Cursor P1 is open on an index b-tree - that is to say, a btree which
  3714         -** no data and where the key are records generated by OP_MakeRecord with
  3715         -** the list field being the integer ROWID of the entry that the index
  3716         -** entry refers to.
  3717         -**
  3718         -** The P3 register contains an integer record number. Call this record 
  3719         -** number R. Register P4 is the first in a set of N contiguous registers
  3720         -** that make up an unpacked index key that can be used with cursor P1.
  3721         -** The value of N can be inferred from the cursor. N includes the rowid
  3722         -** value appended to the end of the index record. This rowid value may
  3723         -** or may not be the same as R.
  3724         -**
  3725         -** If any of the N registers beginning with register P4 contains a NULL
  3726         -** value, jump immediately to P2.
  3727         -**
  3728         -** Otherwise, this instruction checks if cursor P1 contains an entry
  3729         -** where the first (N-1) fields match but the rowid value at the end
  3730         -** of the index entry is not R. If there is no such entry, control jumps
  3731         -** to instruction P2. Otherwise, the rowid of the conflicting index
  3732         -** entry is copied to register P3 and control falls through to the next
  3733         -** instruction.
  3734         -**
  3735         -** See also: NotFound, NotExists, Found
  3736         -*/
  3737         -case OP_IsUnique: {        /* jump, in3 */
  3738         -  u16 ii;
  3739         -  VdbeCursor *pCx;
  3740         -  BtCursor *pCrsr;
  3741         -  u16 nField;
  3742         -  Mem *aMx;
  3743         -  UnpackedRecord r;                  /* B-Tree index search key */
  3744         -  i64 R;                             /* Rowid stored in register P3 */
  3745         -
  3746         -  pIn3 = &aMem[pOp->p3];
  3747         -  aMx = &aMem[pOp->p4.i];
  3748         -  /* Assert that the values of parameters P1 and P4 are in range. */
  3749         -  assert( pOp->p4type==P4_INT32 );
  3750         -  assert( pOp->p4.i>0 && pOp->p4.i<=(p->nMem-p->nCursor) );
  3751         -  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3752         -
  3753         -  /* Find the index cursor. */
  3754         -  pCx = p->apCsr[pOp->p1];
  3755         -  assert( pCx->deferredMoveto==0 );
  3756         -  pCx->seekResult = 0;
  3757         -  pCx->cacheStatus = CACHE_STALE;
  3758         -  pCrsr = pCx->pCursor;
  3759         -
  3760         -  /* If any of the values are NULL, take the jump. */
  3761         -  nField = pCx->pKeyInfo->nField;
  3762         -  for(ii=0; ii<nField; ii++){
  3763         -    if( aMx[ii].flags & MEM_Null ){
  3764         -      pc = pOp->p2 - 1;
  3765         -      pCrsr = 0;
  3766         -      break;
  3767         -    }
  3768         -  }
  3769         -  assert( (aMx[nField].flags & MEM_Null)==0 );
  3770         -
  3771         -  if( pCrsr!=0 ){
  3772         -    /* Populate the index search key. */
  3773         -    r.pKeyInfo = pCx->pKeyInfo;
  3774         -    r.nField = nField + 1;
  3775         -    r.flags = UNPACKED_PREFIX_SEARCH;
  3776         -    r.aMem = aMx;
  3777         -#ifdef SQLITE_DEBUG
  3778         -    { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3779         -#endif
  3780         -
  3781         -    /* Extract the value of R from register P3. */
  3782         -    sqlite3VdbeMemIntegerify(pIn3);
  3783         -    R = pIn3->u.i;
  3784         -
  3785         -    /* Search the B-Tree index. If no conflicting record is found, jump
  3786         -    ** to P2. Otherwise, copy the rowid of the conflicting record to
  3787         -    ** register P3 and fall through to the next instruction.  */
  3788         -    rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult);
  3789         -    if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){
  3790         -      pc = pOp->p2 - 1;
  3791         -    }else{
  3792         -      pIn3->u.i = r.rowid;
  3793         -    }
  3794         -  }
  3795         -  break;
  3796         -}
  3797         -
  3798   3817   /* Opcode: NotExists P1 P2 P3 * *
         3818  +** Synopsis: intkey=r[P3]
  3799   3819   **
  3800         -** Use the content of register P3 as an integer key.  If a record 
  3801         -** with that key does not exist in table of P1, then jump to P2. 
  3802         -** If the record does exist, then fall through.  The cursor is left 
  3803         -** pointing to the record if it exists.
         3820  +** P1 is the index of a cursor open on an SQL table btree (with integer
         3821  +** keys).  P3 is an integer rowid.  If P1 does not contain a record with
         3822  +** rowid P3 then jump immediately to P2.  If P1 does contain a record
         3823  +** with rowid P3 then leave the cursor pointing at that record and fall
         3824  +** through to the next instruction.
  3804   3825   **
  3805         -** The difference between this operation and NotFound is that this
  3806         -** operation assumes the key is an integer and that P1 is a table whereas
  3807         -** NotFound assumes key is a blob constructed from MakeRecord and
  3808         -** P1 is an index.
         3826  +** The OP_NotFound opcode performs the same operation on index btrees
         3827  +** (with arbitrary multi-value keys).
  3809   3828   **
  3810         -** See also: Found, NotFound, IsUnique
         3829  +** See also: Found, NotFound, NoConflict
  3811   3830   */
  3812   3831   case OP_NotExists: {        /* jump, in3 */
  3813   3832     VdbeCursor *pC;
  3814   3833     BtCursor *pCrsr;
  3815   3834     int res;
  3816   3835     u64 iKey;
  3817   3836   
................................................................................
  3819   3838     assert( pIn3->flags & MEM_Int );
  3820   3839     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3821   3840     pC = p->apCsr[pOp->p1];
  3822   3841     assert( pC!=0 );
  3823   3842     assert( pC->isTable );
  3824   3843     assert( pC->pseudoTableReg==0 );
  3825   3844     pCrsr = pC->pCursor;
  3826         -  if( ALWAYS(pCrsr!=0) ){
  3827         -    res = 0;
  3828         -    iKey = pIn3->u.i;
  3829         -    rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
  3830         -    pC->lastRowid = pIn3->u.i;
  3831         -    pC->rowidIsValid = res==0 ?1:0;
  3832         -    pC->nullRow = 0;
  3833         -    pC->cacheStatus = CACHE_STALE;
  3834         -    pC->deferredMoveto = 0;
  3835         -    if( res!=0 ){
  3836         -      pc = pOp->p2 - 1;
  3837         -      assert( pC->rowidIsValid==0 );
  3838         -    }
  3839         -    pC->seekResult = res;
  3840         -  }else{
  3841         -    /* This happens when an attempt to open a read cursor on the 
  3842         -    ** sqlite_master table returns SQLITE_EMPTY.
  3843         -    */
         3845  +  assert( pCrsr!=0 );
         3846  +  res = 0;
         3847  +  iKey = pIn3->u.i;
         3848  +  rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
         3849  +  pC->lastRowid = pIn3->u.i;
         3850  +  pC->rowidIsValid = res==0 ?1:0;
         3851  +  pC->nullRow = 0;
         3852  +  pC->cacheStatus = CACHE_STALE;
         3853  +  pC->deferredMoveto = 0;
         3854  +  if( res!=0 ){
  3844   3855       pc = pOp->p2 - 1;
  3845   3856       assert( pC->rowidIsValid==0 );
  3846         -    pC->seekResult = 0;
  3847   3857     }
         3858  +  pC->seekResult = res;
  3848   3859     break;
  3849   3860   }
  3850   3861   
  3851   3862   /* Opcode: Sequence P1 P2 * * *
         3863  +** Synopsis: r[P2]=rowid
  3852   3864   **
  3853   3865   ** Find the next available sequence number for cursor P1.
  3854   3866   ** Write the sequence number into register P2.
  3855   3867   ** The sequence number on the cursor is incremented after this
  3856   3868   ** instruction.  
  3857   3869   */
  3858   3870   case OP_Sequence: {           /* out2-prerelease */
................................................................................
  3860   3872     assert( p->apCsr[pOp->p1]!=0 );
  3861   3873     pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
  3862   3874     break;
  3863   3875   }
  3864   3876   
  3865   3877   
  3866   3878   /* Opcode: NewRowid P1 P2 P3 * *
         3879  +** Synopsis: r[P2]=rowid
  3867   3880   **
  3868   3881   ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
  3869   3882   ** The record number is not previously used as a key in the database
  3870   3883   ** table that cursor P1 points to.  The new record number is written
  3871   3884   ** written to register P2.
  3872   3885   **
  3873   3886   ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
................................................................................
  4009   4022       pC->cacheStatus = CACHE_STALE;
  4010   4023     }
  4011   4024     pOut->u.i = v;
  4012   4025     break;
  4013   4026   }
  4014   4027   
  4015   4028   /* Opcode: Insert P1 P2 P3 P4 P5
         4029  +** Synopsis: intkey=r[P3] data=r[P2]
  4016   4030   **
  4017   4031   ** Write an entry into the table of cursor P1.  A new entry is
  4018   4032   ** created if it doesn't already exist or the data for an existing
  4019   4033   ** entry is overwritten.  The data is the value MEM_Blob stored in register
  4020   4034   ** number P2. The key is stored in register P3. The key must
  4021   4035   ** be a MEM_Int.
  4022   4036   **
................................................................................
  4048   4062   ** value of register P2 will then change.  Make sure this does not
  4049   4063   ** cause any problems.)
  4050   4064   **
  4051   4065   ** This instruction only works on tables.  The equivalent instruction
  4052   4066   ** for indices is OP_IdxInsert.
  4053   4067   */
  4054   4068   /* Opcode: InsertInt P1 P2 P3 P4 P5
         4069  +** Synopsis:  intkey=P3 data=r[P2]
  4055   4070   **
  4056   4071   ** This works exactly like OP_Insert except that the key is the
  4057   4072   ** integer value P3, not the value of the integer stored in register P3.
  4058   4073   */
  4059   4074   case OP_Insert: 
  4060   4075   case OP_InsertInt: {
  4061   4076     Mem *pData;       /* MEM cell holding data for the record to be inserted */
................................................................................
  4062   4077     Mem *pKey;        /* MEM cell holding key  for the record */
  4063   4078     i64 iKey;         /* The integer ROWID or key for the record to be inserted */
  4064   4079     VdbeCursor *pC;   /* Cursor to table into which insert is written */
  4065   4080     int nZero;        /* Number of zero-bytes to append */
  4066   4081     int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
  4067   4082     const char *zDb;  /* database name - used by the update hook */
  4068   4083     Table *pTab;      /* Table structure - used by update and pre-update hooks */
  4069         -  int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
         4084  +  int op = 0;       /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
  4070   4085   
  4071   4086     pData = &aMem[pOp->p2];
  4072   4087     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4073   4088     assert( memIsValid(pData) );
  4074   4089     pC = p->apCsr[pOp->p1];
  4075   4090     assert( pC!=0 );
  4076   4091     assert( pC->pCursor!=0 );
  4077   4092     assert( pC->pseudoTableReg==0 );
  4078   4093     assert( pC->isTable );
  4079         -  assert( pOp->p4type==P4_TABLE || pOp->p4type==P4_NOTUSED );
         4094  +  assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
  4080   4095     REGISTER_TRACE(pOp->p2, pData);
  4081   4096   
  4082   4097     if( pOp->opcode==OP_Insert ){
  4083   4098       pKey = &aMem[pOp->p3];
  4084   4099       assert( pKey->flags & MEM_Int );
  4085   4100       assert( memIsValid(pKey) );
  4086   4101       REGISTER_TRACE(pOp->p3, pKey);
................................................................................
  4128   4143                             pOp->p5 & OPFLAG_APPEND, seekResult
  4129   4144     );
  4130   4145     pC->rowidIsValid = 0;
  4131   4146     pC->deferredMoveto = 0;
  4132   4147     pC->cacheStatus = CACHE_STALE;
  4133   4148   
  4134   4149     /* Invoke the update-hook if required. */
  4135         -  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
         4150  +  if( rc==SQLITE_OK && db->xUpdateCallback && op ){
  4136   4151       db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, iKey);
  4137   4152     }
  4138   4153     break;
  4139   4154   }
  4140   4155   
  4141   4156   /* Opcode: Delete P1 P2 P3 P4 *
  4142   4157   **
................................................................................
  4239   4254   */
  4240   4255   case OP_ResetCount: {
  4241   4256     sqlite3VdbeSetChanges(db, p->nChange);
  4242   4257     p->nChange = 0;
  4243   4258     break;
  4244   4259   }
  4245   4260   
  4246         -/* Opcode: SorterCompare P1 P2 P3
         4261  +/* Opcode: SorterCompare P1 P2 P3 P4
         4262  +** Synopsis:  if key(P1)!=rtrim(r[P3],P4) goto P2
  4247   4263   **
  4248         -** P1 is a sorter cursor. This instruction compares the record blob in 
  4249         -** register P3 with the entry that the sorter cursor currently points to.
  4250         -** If, excluding the rowid fields at the end, the two records are a match,
  4251         -** fall through to the next instruction. Otherwise, jump to instruction P2.
         4264  +** P1 is a sorter cursor. This instruction compares a prefix of the
         4265  +** the record blob in register P3 against a prefix of the entry that 
         4266  +** the sorter cursor currently points to.  The final P4 fields of both
         4267  +** the P3 and sorter record are ignored.
         4268  +**
         4269  +** If either P3 or the sorter contains a NULL in one of their significant
         4270  +** fields (not counting the P4 fields at the end which are ignored) then
         4271  +** the comparison is assumed to be equal.
         4272  +**
         4273  +** Fall through to next instruction if the two records compare equal to
         4274  +** each other.  Jump to P2 if they are different.
  4252   4275   */
  4253   4276   case OP_SorterCompare: {
  4254   4277     VdbeCursor *pC;
  4255   4278     int res;
         4279  +  int nIgnore;
  4256   4280   
  4257   4281     pC = p->apCsr[pOp->p1];
  4258   4282     assert( isSorter(pC) );
         4283  +  assert( pOp->p4type==P4_INT32 );
  4259   4284     pIn3 = &aMem[pOp->p3];
  4260         -  rc = sqlite3VdbeSorterCompare(pC, pIn3, &res);
         4285  +  nIgnore = pOp->p4.i;
         4286  +  rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
  4261   4287     if( res ){
  4262   4288       pc = pOp->p2-1;
  4263   4289     }
  4264   4290     break;
  4265   4291   };
  4266   4292   
  4267   4293   /* Opcode: SorterData P1 P2 * * *
         4294  +** Synopsis: r[P2]=data
  4268   4295   **
  4269   4296   ** Write into register P2 the current sorter data for sorter cursor P1.
  4270   4297   */
  4271   4298   case OP_SorterData: {
  4272   4299     VdbeCursor *pC;
  4273   4300   
  4274   4301     pOut = &aMem[pOp->p2];
................................................................................
  4275   4302     pC = p->apCsr[pOp->p1];
  4276   4303     assert( pC->isSorter );
  4277   4304     rc = sqlite3VdbeSorterRowkey(pC, pOut);
  4278   4305     break;
  4279   4306   }
  4280   4307   
  4281   4308   /* Opcode: RowData P1 P2 * * *
         4309  +** Synopsis: r[P2]=data
  4282   4310   **
  4283   4311   ** Write into register P2 the complete row data for cursor P1.
  4284   4312   ** There is no interpretation of the data.  
  4285   4313   ** It is just copied onto the P2 register exactly as 
  4286   4314   ** it is found in the database file.
  4287   4315   **
  4288   4316   ** If the P1 cursor must be pointing to a valid row (not a NULL row)
  4289   4317   ** of a real table, not a pseudo-table.
  4290   4318   */
  4291   4319   /* Opcode: RowKey P1 P2 * * *
         4320  +** Synopsis: r[P2]=key
  4292   4321   **
  4293   4322   ** Write into register P2 the complete row key for cursor P1.
  4294   4323   ** There is no interpretation of the data.  
  4295   4324   ** The key is copied onto the P3 register exactly as 
  4296   4325   ** it is found in the database file.
  4297   4326   **
  4298   4327   ** If the P1 cursor must be pointing to a valid row (not a NULL row)
................................................................................
  4353   4382     if( pC->isIndex ){
  4354   4383       rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
  4355   4384     }else{
  4356   4385       rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
  4357   4386     }
  4358   4387     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
  4359   4388     UPDATE_MAX_BLOBSIZE(pOut);
         4389  +  REGISTER_TRACE(pOp->p2, pOut);
  4360   4390     break;
  4361   4391   }
  4362   4392   
  4363   4393   /* Opcode: Rowid P1 P2 * * *
         4394  +** Synopsis: r[P2]=rowid
  4364   4395   **
  4365   4396   ** Store in register P2 an integer which is the key of the table entry that
  4366   4397   ** P1 is currently point to.
  4367   4398   **
  4368   4399   ** P1 can be either an ordinary table or a virtual table.  There used to
  4369   4400   ** be a separate OP_VRowid opcode for use with virtual tables, but this
  4370   4401   ** one opcode now works for both table types.
................................................................................
  4442   4473     int res;
  4443   4474   
  4444   4475     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4445   4476     pC = p->apCsr[pOp->p1];
  4446   4477     assert( pC!=0 );
  4447   4478     pCrsr = pC->pCursor;
  4448   4479     res = 0;
  4449         -  if( ALWAYS(pCrsr!=0) ){
  4450         -    rc = sqlite3BtreeLast(pCrsr, &res);
  4451         -  }
         4480  +  assert( pCrsr!=0 );
         4481  +  rc = sqlite3BtreeLast(pCrsr, &res);
  4452   4482     pC->nullRow = (u8)res;
  4453   4483     pC->deferredMoveto = 0;
  4454   4484     pC->rowidIsValid = 0;
  4455   4485     pC->cacheStatus = CACHE_STALE;
  4456   4486     if( pOp->p2>0 && res ){
  4457   4487       pc = pOp->p2 - 1;
  4458   4488     }
................................................................................
  4514   4544     assert( pOp->p2>0 && pOp->p2<p->nOp );
  4515   4545     if( res ){
  4516   4546       pc = pOp->p2 - 1;
  4517   4547     }
  4518   4548     break;
  4519   4549   }
  4520   4550   
  4521         -/* Opcode: Next P1 P2 * P4 P5
         4551  +/* Opcode: Next P1 P2 * * P5
  4522   4552   **
  4523   4553   ** Advance cursor P1 so that it points to the next key/data pair in its
  4524   4554   ** table or index.  If there are no more key/value pairs then fall through
  4525   4555   ** to the following instruction.  But if the cursor advance was successful,
  4526   4556   ** jump immediately to P2.
  4527   4557   **
  4528   4558   ** The P1 cursor must be for a real table, not a pseudo-table.
................................................................................
  4584   4614   #endif
  4585   4615     }
  4586   4616     pC->rowidIsValid = 0;
  4587   4617     goto check_for_interrupt;
  4588   4618   }
  4589   4619   
  4590   4620   /* Opcode: IdxInsert P1 P2 P3 * P5
         4621  +** Synopsis: key=r[P2]
  4591   4622   **
  4592   4623   ** Register P2 holds an SQL index key made using the
  4593   4624   ** MakeRecord instructions.  This opcode writes that key
  4594   4625   ** into the index P1.  Data for the entry is nil.
  4595   4626   **
  4596   4627   ** P3 is a flag that provides a hint to the b-tree layer that this
  4597   4628   ** insert is likely to be an append.
................................................................................
  4609   4640     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4610   4641     pC = p->apCsr[pOp->p1];
  4611   4642     assert( pC!=0 );
  4612   4643     assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
  4613   4644     pIn2 = &aMem[pOp->p2];
  4614   4645     assert( pIn2->flags & MEM_Blob );
  4615   4646     pCrsr = pC->pCursor;
  4616         -  if( ALWAYS(pCrsr!=0) ){
  4617         -    assert( pC->isTable==0 );
  4618         -    rc = ExpandBlob(pIn2);
  4619         -    if( rc==SQLITE_OK ){
  4620         -      if( isSorter(pC) ){
  4621         -        rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
  4622         -      }else{
  4623         -        nKey = pIn2->n;
  4624         -        zKey = pIn2->z;
  4625         -        rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, 
  4626         -            ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
  4627         -            );
  4628         -        assert( pC->deferredMoveto==0 );
  4629         -        pC->cacheStatus = CACHE_STALE;
  4630         -      }
         4647  +  if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
         4648  +  assert( pCrsr!=0 );
         4649  +  assert( pC->isTable==0 );
         4650  +  rc = ExpandBlob(pIn2);
         4651  +  if( rc==SQLITE_OK ){
         4652  +    if( isSorter(pC) ){
         4653  +      rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
         4654  +    }else{
         4655  +      nKey = pIn2->n;
         4656  +      zKey = pIn2->z;
         4657  +      rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, 
         4658  +          ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
         4659  +          );
         4660  +      assert( pC->deferredMoveto==0 );
         4661  +      pC->cacheStatus = CACHE_STALE;
  4631   4662       }
  4632   4663     }
  4633   4664     break;
  4634   4665   }
  4635   4666   
  4636   4667   /* Opcode: IdxDelete P1 P2 P3 * *
         4668  +** Synopsis: key=r[P2@P3]
  4637   4669   **
  4638   4670   ** The content of P3 registers starting at register P2 form
  4639   4671   ** an unpacked index key. This opcode removes that entry from the 
  4640   4672   ** index opened by cursor P1.
  4641   4673   */
  4642   4674   case OP_IdxDelete: {
  4643   4675     VdbeCursor *pC;
................................................................................
  4647   4679   
  4648   4680     assert( pOp->p3>0 );
  4649   4681     assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
  4650   4682     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4651   4683     pC = p->apCsr[pOp->p1];
  4652   4684     assert( pC!=0 );
  4653   4685     pCrsr = pC->pCursor;
  4654         -  if( ALWAYS(pCrsr!=0) ){
  4655         -    r.pKeyInfo = pC->pKeyInfo;
  4656         -    r.nField = (u16)pOp->p3;
  4657         -    r.flags = 0;
  4658         -    r.aMem = &aMem[pOp->p2];
         4686  +  assert( pCrsr!=0 );
         4687  +  assert( pOp->p5==0 );
         4688  +  r.pKeyInfo = pC->pKeyInfo;
         4689  +  r.nField = (u16)pOp->p3;
         4690  +  r.flags = UNPACKED_PREFIX_MATCH;
         4691  +  r.aMem = &aMem[pOp->p2];
  4659   4692   #ifdef SQLITE_DEBUG
  4660         -    { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
         4693  +  { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  4661   4694   #endif
  4662         -    rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
  4663         -    if( rc==SQLITE_OK && res==0 ){
  4664         -      rc = sqlite3BtreeDelete(pCrsr);
  4665         -    }
  4666         -    assert( pC->deferredMoveto==0 );
  4667         -    pC->cacheStatus = CACHE_STALE;
         4695  +  rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
         4696  +  if( rc==SQLITE_OK && res==0 ){
         4697  +    rc = sqlite3BtreeDelete(pCrsr);
  4668   4698     }
         4699  +  assert( pC->deferredMoveto==0 );
         4700  +  pC->cacheStatus = CACHE_STALE;
  4669   4701     break;
  4670   4702   }
  4671   4703   
  4672   4704   /* Opcode: IdxRowid P1 P2 * * *
         4705  +** Synopsis: r[P2]=rowid
  4673   4706   **
  4674   4707   ** Write into register P2 an integer which is the last entry in the record at
  4675   4708   ** the end of the index key pointed to by cursor P1.  This integer should be
  4676   4709   ** the rowid of the table entry to which this index entry points.
  4677   4710   **
  4678   4711   ** See also: Rowid, MakeRecord.
  4679   4712   */
................................................................................
  4682   4715     VdbeCursor *pC;
  4683   4716     i64 rowid;
  4684   4717   
  4685   4718     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4686   4719     pC = p->apCsr[pOp->p1];
  4687   4720     assert( pC!=0 );
  4688   4721     pCrsr = pC->pCursor;
         4722  +  assert( pCrsr!=0 );
  4689   4723     pOut->flags = MEM_Null;
  4690         -  if( ALWAYS(pCrsr!=0) ){
  4691         -    rc = sqlite3VdbeCursorMoveto(pC);
  4692         -    if( NEVER(rc) ) goto abort_due_to_error;
  4693         -    assert( pC->deferredMoveto==0 );
  4694         -    assert( pC->isTable==0 );
  4695         -    if( !pC->nullRow ){
  4696         -      rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
  4697         -      if( rc!=SQLITE_OK ){
  4698         -        goto abort_due_to_error;
  4699         -      }
  4700         -      pOut->u.i = rowid;
  4701         -      pOut->flags = MEM_Int;
         4724  +  rc = sqlite3VdbeCursorMoveto(pC);
         4725  +  if( NEVER(rc) ) goto abort_due_to_error;
         4726  +  assert( pC->deferredMoveto==0 );
         4727  +  assert( pC->isTable==0 );
         4728  +  if( !pC->nullRow ){
         4729  +    rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
         4730  +    if( rc!=SQLITE_OK ){
         4731  +      goto abort_due_to_error;
  4702   4732       }
         4733  +    pOut->u.i = rowid;
         4734  +    pOut->flags = MEM_Int;
  4703   4735     }
  4704   4736     break;
  4705   4737   }
  4706   4738   
  4707   4739   /* Opcode: IdxGE P1 P2 P3 P4 P5
         4740  +** Synopsis: key=r[P3@P4]
  4708   4741   **
  4709   4742   ** The P4 register values beginning with P3 form an unpacked index 
  4710   4743   ** key that omits the ROWID.  Compare this key value against the index 
  4711   4744   ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
  4712   4745   **
  4713   4746   ** If the P1 index entry is greater than or equal to the key value
  4714   4747   ** then jump to P2.  Otherwise fall through to the next instruction.
................................................................................
  4715   4748   **
  4716   4749   ** If P5 is non-zero then the key value is increased by an epsilon 
  4717   4750   ** prior to the comparison.  This make the opcode work like IdxGT except
  4718   4751   ** that if the key from register P3 is a prefix of the key in the cursor,
  4719   4752   ** the result is false whereas it would be true with IdxGT.
  4720   4753   */
  4721   4754   /* Opcode: IdxLT P1 P2 P3 P4 P5
         4755  +** Synopsis: key=r[P3@P4]
  4722   4756   **
  4723   4757   ** The P4 register values beginning with P3 form an unpacked index 
  4724   4758   ** key that omits the ROWID.  Compare this key value against the index 
  4725   4759   ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
  4726   4760   **
  4727   4761   ** If the P1 index entry is less than the key value then jump to P2.
  4728   4762   ** Otherwise fall through to the next instruction.
................................................................................
  4736   4770     int res;
  4737   4771     UnpackedRecord r;
  4738   4772   
  4739   4773     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4740   4774     pC = p->apCsr[pOp->p1];
  4741   4775     assert( pC!=0 );
  4742   4776     assert( pC->isOrdered );
  4743         -  if( ALWAYS(pC->pCursor!=0) ){
  4744         -    assert( pC->deferredMoveto==0 );
  4745         -    assert( pOp->p5==0 || pOp->p5==1 );
  4746         -    assert( pOp->p4type==P4_INT32 );
  4747         -    r.pKeyInfo = pC->pKeyInfo;
  4748         -    r.nField = (u16)pOp->p4.i;
  4749         -    if( pOp->p5 ){
  4750         -      r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
  4751         -    }else{
  4752         -      r.flags = UNPACKED_PREFIX_MATCH;
  4753         -    }
  4754         -    r.aMem = &aMem[pOp->p3];
         4777  +  assert( pC->pCursor!=0);
         4778  +  assert( pC->deferredMoveto==0 );
         4779  +  assert( pOp->p5==0 || pOp->p5==1 );
         4780  +  assert( pOp->p4type==P4_INT32 );
         4781  +  r.pKeyInfo = pC->pKeyInfo;
         4782  +  r.nField = (u16)pOp->p4.i;
         4783  +  if( pOp->p5 ){
         4784  +    r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
         4785  +  }else{
         4786  +    r.flags = UNPACKED_PREFIX_MATCH;
         4787  +  }
         4788  +  r.aMem = &aMem[pOp->p3];
  4755   4789   #ifdef SQLITE_DEBUG
  4756         -    { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
         4790  +  { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  4757   4791   #endif
  4758         -    rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
  4759         -    if( pOp->opcode==OP_IdxLT ){
  4760         -      res = -res;
  4761         -    }else{
  4762         -      assert( pOp->opcode==OP_IdxGE );
  4763         -      res++;
  4764         -    }
  4765         -    if( res>0 ){
  4766         -      pc = pOp->p2 - 1 ;
  4767         -    }
         4792  +  rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
         4793  +  if( pOp->opcode==OP_IdxLT ){
         4794  +    res = -res;
         4795  +  }else{
         4796  +    assert( pOp->opcode==OP_IdxGE );
         4797  +    res++;
         4798  +  }
         4799  +  if( res>0 ){
         4800  +    pc = pOp->p2 - 1 ;
  4768   4801     }
  4769   4802     break;
  4770   4803   }
  4771   4804   
  4772   4805   /* Opcode: Destroy P1 P2 P3 * *
  4773   4806   **
  4774   4807   ** Delete an entire database table or index whose root page in the database
................................................................................
  4867   4900         aMem[pOp->p3].u.i += nChange;
  4868   4901       }
  4869   4902     }
  4870   4903     break;
  4871   4904   }
  4872   4905   
  4873   4906   /* Opcode: CreateTable P1 P2 * * *
         4907  +** Synopsis: r[P2]=root iDb=P1
  4874   4908   **
  4875   4909   ** Allocate a new table in the main database file if P1==0 or in the
  4876   4910   ** auxiliary database file if P1==1 or in an attached database if
  4877   4911   ** P1>1.  Write the root page number of the new table into
  4878   4912   ** register P2
  4879   4913   **
  4880   4914   ** The difference between a table and an index is this:  A table must
  4881   4915   ** have a 4-byte integer key and can have arbitrary data.  An index
  4882   4916   ** has an arbitrary key but no data.
  4883   4917   **
  4884   4918   ** See also: CreateIndex
  4885   4919   */
  4886   4920   /* Opcode: CreateIndex P1 P2 * * *
         4921  +** Synopsis: r[P2]=root iDb=P1
  4887   4922   **
  4888   4923   ** Allocate a new index in the main database file if P1==0 or in the
  4889   4924   ** auxiliary database file if P1==1 or in an attached database if
  4890   4925   ** P1>1.  Write the root page number of the new table into
  4891   4926   ** register P2.
  4892   4927   **
  4893   4928   ** See documentation on OP_CreateTable for additional information.
................................................................................
  5081   5116     UPDATE_MAX_BLOBSIZE(pIn1);
  5082   5117     sqlite3VdbeChangeEncoding(pIn1, encoding);
  5083   5118     break;
  5084   5119   }
  5085   5120   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  5086   5121   
  5087   5122   /* Opcode: RowSetAdd P1 P2 * * *
         5123  +** Synopsis:  rowset(P1)=r[P2]
  5088   5124   **
  5089   5125   ** Insert the integer value held by register P2 into a boolean index
  5090   5126   ** held in register P1.
  5091   5127   **
  5092   5128   ** An assertion fails if P2 is not an integer.
  5093   5129   */
  5094   5130   case OP_RowSetAdd: {       /* in1, in2 */
................................................................................
  5100   5136       if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
  5101   5137     }
  5102   5138     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
  5103   5139     break;
  5104   5140   }
  5105   5141   
  5106   5142   /* Opcode: RowSetRead P1 P2 P3 * *
         5143  +** Synopsis:  r[P3]=rowset(P1)
  5107   5144   **
  5108   5145   ** Extract the smallest value from boolean index P1 and put that value into
  5109   5146   ** register P3.  Or, if boolean index P1 is initially empty, leave P3
  5110   5147   ** unchanged and jump to instruction P2.
  5111   5148   */
  5112   5149   case OP_RowSetRead: {       /* jump, in1, out3 */
  5113   5150     i64 val;
................................................................................
  5123   5160       /* A value was pulled from the index */
  5124   5161       sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
  5125   5162     }
  5126   5163     goto check_for_interrupt;
  5127   5164   }
  5128   5165   
  5129   5166   /* Opcode: RowSetTest P1 P2 P3 P4
         5167  +** Synopsis: if r[P3] in rowset(P1) goto P2
  5130   5168   **
  5131   5169   ** Register P3 is assumed to hold a 64-bit integer value. If register P1
  5132   5170   ** contains a RowSet object and that RowSet object contains
  5133   5171   ** the value held in P3, jump to register P2. Otherwise, insert the
  5134   5172   ** integer in P3 into the RowSet and continue on to the
  5135   5173   ** next opcode.
  5136   5174   **
................................................................................
  5326   5364     break;
  5327   5365   }
  5328   5366   
  5329   5367   #endif /* #ifndef SQLITE_OMIT_TRIGGER */
  5330   5368   
  5331   5369   #ifndef SQLITE_OMIT_FOREIGN_KEY
  5332   5370   /* Opcode: FkCounter P1 P2 * * *
         5371  +** Synopsis: fkctr[P1]+=P2
  5333   5372   **
  5334   5373   ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
  5335   5374   ** If P1 is non-zero, the database constraint counter is incremented 
  5336   5375   ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 
  5337   5376   ** statement counter is incremented (immediate foreign key constraints).
  5338   5377   */
  5339   5378   case OP_FkCounter: {
................................................................................
  5344   5383     }else{
  5345   5384       p->nFkConstraint += pOp->p2;
  5346   5385     }
  5347   5386     break;
  5348   5387   }
  5349   5388   
  5350   5389   /* Opcode: FkIfZero P1 P2 * * *
         5390  +** Synopsis: if fkctr[P1]==0 goto P2
  5351   5391   **
  5352   5392   ** This opcode tests if a foreign key constraint-counter is currently zero.
  5353   5393   ** If so, jump to instruction P2. Otherwise, fall through to the next 
  5354   5394   ** instruction.
  5355   5395   **
  5356   5396   ** If P1 is non-zero, then the jump is taken if the database constraint-counter
  5357   5397   ** is zero (the one that counts deferred constraint violations). If P1 is
................................................................................
  5366   5406     }
  5367   5407     break;
  5368   5408   }
  5369   5409   #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
  5370   5410   
  5371   5411   #ifndef SQLITE_OMIT_AUTOINCREMENT
  5372   5412   /* Opcode: MemMax P1 P2 * * *
         5413  +** Synopsis: r[P1]=max(r[P1],r[P2])
  5373   5414   **
  5374   5415   ** P1 is a register in the root frame of this VM (the root frame is
  5375   5416   ** different from the current frame if this instruction is being executed
  5376   5417   ** within a sub-program). Set the value of register P1 to the maximum of 
  5377   5418   ** its current value and the value in register P2.
  5378   5419   **
  5379   5420   ** This instruction throws an error if the memory cell is not initially
................................................................................
  5396   5437       pIn1->u.i = pIn2->u.i;
  5397   5438     }
  5398   5439     break;
  5399   5440   }
  5400   5441   #endif /* SQLITE_OMIT_AUTOINCREMENT */
  5401   5442   
  5402   5443   /* Opcode: IfPos P1 P2 * * *
         5444  +** Synopsis: if r[P1]>0 goto P2
  5403   5445   **
  5404   5446   ** If the value of register P1 is 1 or greater, jump to P2.
  5405   5447   **
  5406   5448   ** It is illegal to use this instruction on a register that does
  5407   5449   ** not contain an integer.  An assertion fault will result if you try.
  5408   5450   */
  5409   5451   case OP_IfPos: {        /* jump, in1 */
................................................................................
  5412   5454     if( pIn1->u.i>0 ){
  5413   5455        pc = pOp->p2 - 1;
  5414   5456     }
  5415   5457     break;
  5416   5458   }
  5417   5459   
  5418   5460   /* Opcode: IfNeg P1 P2 * * *
         5461  +** Synopsis: if r[P1]<0 goto P2
  5419   5462   **
  5420   5463   ** If the value of register P1 is less than zero, jump to P2. 
  5421   5464   **
  5422   5465   ** It is illegal to use this instruction on a register that does
  5423   5466   ** not contain an integer.  An assertion fault will result if you try.
  5424   5467   */
  5425   5468   case OP_IfNeg: {        /* jump, in1 */
................................................................................
  5428   5471     if( pIn1->u.i<0 ){
  5429   5472        pc = pOp->p2 - 1;
  5430   5473     }
  5431   5474     break;
  5432   5475   }
  5433   5476   
  5434   5477   /* Opcode: IfZero P1 P2 P3 * *
         5478  +** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
  5435   5479   **
  5436   5480   ** The register P1 must contain an integer.  Add literal P3 to the
  5437   5481   ** value in register P1.  If the result is exactly 0, jump to P2. 
  5438   5482   **
  5439   5483   ** It is illegal to use this instruction on a register that does
  5440   5484   ** not contain an integer.  An assertion fault will result if you try.
  5441   5485   */
................................................................................
  5446   5490     if( pIn1->u.i==0 ){
  5447   5491        pc = pOp->p2 - 1;
  5448   5492     }
  5449   5493     break;
  5450   5494   }
  5451   5495   
  5452   5496   /* Opcode: AggStep * P2 P3 P4 P5
         5497  +** Synopsis: accum=r[P3] step(r[P2@P5])
  5453   5498   **
  5454   5499   ** Execute the step function for an aggregate.  The
  5455   5500   ** function has P5 arguments.   P4 is a pointer to the FuncDef
  5456   5501   ** structure that specifies the function.  Use register
  5457   5502   ** P3 as the accumulator.
  5458   5503   **
  5459   5504   ** The P5 arguments are taken from register P2 and its
................................................................................
  5509   5554   
  5510   5555     sqlite3VdbeMemRelease(&ctx.s);
  5511   5556   
  5512   5557     break;
  5513   5558   }
  5514   5559   
  5515   5560   /* Opcode: AggFinal P1 P2 * P4 *
         5561  +** Synopsis: accum=r[P1] N=P2
  5516   5562   **
  5517   5563   ** Execute the finalizer function for an aggregate.  P1 is
  5518   5564   ** the memory location that is the accumulator for the aggregate.
  5519   5565   **
  5520   5566   ** P2 is the number of arguments that the step function takes and
  5521   5567   ** P4 is a pointer to the FuncDef for this function.  The P2
  5522   5568   ** argument is not used by this opcode.  It is only there to disambiguate
................................................................................
  5736   5782       p->expired = 1;
  5737   5783     }
  5738   5784     break;
  5739   5785   }
  5740   5786   
  5741   5787   #ifndef SQLITE_OMIT_SHARED_CACHE
  5742   5788   /* Opcode: TableLock P1 P2 P3 P4 *
         5789  +** Synopsis: iDb=P1 root=P2 write=P3
  5743   5790   **
  5744   5791   ** Obtain a lock on a particular table. This instruction is only used when
  5745   5792   ** the shared-cache feature is enabled. 
  5746   5793   **
  5747   5794   ** P1 is the index of the database in sqlite3.aDb[] of the database
  5748   5795   ** on which the lock is acquired.  A readlock is obtained if P3==0 or
  5749   5796   ** a write lock if P3==1.
................................................................................
  5852   5899     }
  5853   5900     break;
  5854   5901   }
  5855   5902   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5856   5903   
  5857   5904   #ifndef SQLITE_OMIT_VIRTUALTABLE
  5858   5905   /* Opcode: VFilter P1 P2 P3 P4 *
         5906  +** Synopsis: iPlan=r[P3] zPlan='P4'
  5859   5907   **
  5860   5908   ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
  5861   5909   ** the filtered result set is empty.
  5862   5910   **
  5863   5911   ** P4 is either NULL or a string that was generated by the xBestIndex
  5864   5912   ** method of the module.  The interpretation of the P4 string is left
  5865   5913   ** to the module implementation.
................................................................................
  5926   5974   
  5927   5975     break;
  5928   5976   }
  5929   5977   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5930   5978   
  5931   5979   #ifndef SQLITE_OMIT_VIRTUALTABLE
  5932   5980   /* Opcode: VColumn P1 P2 P3 * *
         5981  +** Synopsis: r[P3]=vcolumn(P2)
  5933   5982   **
  5934   5983   ** Store the value of the P2-th column of
  5935   5984   ** the row of the virtual-table that the 
  5936   5985   ** P1 cursor is pointing to into register P3.
  5937   5986   */
  5938   5987   case OP_VColumn: {
  5939   5988     sqlite3_vtab *pVtab;
................................................................................
  6059   6108     }
  6060   6109     break;
  6061   6110   }
  6062   6111   #endif
  6063   6112   
  6064   6113   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6065   6114   /* Opcode: VUpdate P1 P2 P3 P4 *
         6115  +** Synopsis: data=r[P3@P2]
  6066   6116   **
  6067   6117   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
  6068   6118   ** This opcode invokes the corresponding xUpdate method. P2 values
  6069   6119   ** are contiguous memory cells starting at P3 to pass to the xUpdate 
  6070   6120   ** invocation. The value in register (P3+P2-1) corresponds to the 
  6071   6121   ** p2th element of the argv array passed to xUpdate.
  6072   6122   **

Changes to src/vdbe.h.

    58     58       VTable *pVtab;         /* Used when p4type is P4_VTAB */
    59     59       KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
    60     60       int *ai;               /* Used when p4type is P4_INTARRAY */
    61     61       SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
    62     62       Table *pTab;           /* Used when p4type is P4_TABLE */
    63     63       int (*xAdvance)(BtCursor *, int *);
    64     64     } p4;
    65         -#ifdef SQLITE_DEBUG
           65  +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
    66     66     char *zComment;          /* Comment to improve readability */
    67     67   #endif
    68     68   #ifdef VDBE_PROFILE
    69     69     int cnt;                 /* Number of times this instruction was executed */
    70     70     u64 cycles;              /* Total time spent executing this instruction */
    71     71   #endif
    72     72   };
................................................................................
   115    115   #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
   116    116   #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
   117    117   #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
   118    118   #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
   119    119   #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
   120    120   #define P4_TABLE    (-20) /* P4 is a pointer to a Table structure */
   121    121   
   122         -/* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
   123         -** is made.  That copy is freed when the Vdbe is finalized.  But if the
   124         -** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
   125         -** gets freed when the Vdbe is finalized so it still should be obtained
   126         -** from a single sqliteMalloc().  But no copy is made and the calling
   127         -** function should *not* try to free the KeyInfo.
   128         -*/
   129         -#define P4_KEYINFO_HANDOFF (-16)
   130         -#define P4_KEYINFO_STATIC  (-17)
          122  +/* Error message codes for OP_Halt */
          123  +#define P5_ConstraintNotNull 1
          124  +#define P5_ConstraintUnique  2
          125  +#define P5_ConstraintCheck   3
          126  +#define P5_ConstraintFK      4
   131    127   
   132    128   /*
   133    129   ** The Vdbe.aColName array contains 5n Mem structures, where n is the 
   134    130   ** number of columns of data returned by the statement.
   135    131   */
   136    132   #define COLNAME_NAME     0
   137    133   #define COLNAME_DECLTYPE 1
................................................................................
   178    174   void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
   179    175   void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
   180    176   void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
   181    177   void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
   182    178   void sqlite3VdbeJumpHere(Vdbe*, int addr);
   183    179   void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
   184    180   void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
          181  +void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
   185    182   void sqlite3VdbeUsesBtree(Vdbe*, int);
   186    183   VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
   187    184   int sqlite3VdbeMakeLabel(Vdbe*);
   188    185   void sqlite3VdbeRunOnlyOnce(Vdbe*);
   189    186   void sqlite3VdbeDelete(Vdbe*);
   190    187   void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
   191    188   void sqlite3VdbeMakeReady(Vdbe*,Parse*);
................................................................................
   216    213   int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
   217    214   UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
   218    215   
   219    216   #ifndef SQLITE_OMIT_TRIGGER
   220    217   void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
   221    218   #endif
   222    219   
   223         -
   224         -#ifndef NDEBUG
          220  +/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
          221  +** each VDBE opcode.
          222  +**
          223  +** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
          224  +** comments in VDBE programs that show key decision points in the code
          225  +** generator.
          226  +*/
          227  +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   225    228     void sqlite3VdbeComment(Vdbe*, const char*, ...);
   226    229   # define VdbeComment(X)  sqlite3VdbeComment X
   227    230     void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
   228    231   # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
          232  +# ifdef SQLITE_ENABLE_MODULE_COMMENTS
          233  +#   define VdbeModuleComment(X)  sqlite3VdbeNoopComment X
          234  +# else
          235  +#   define VdbeModuleComment(X)
          236  +# endif
   229    237   #else
   230    238   # define VdbeComment(X)
   231    239   # define VdbeNoopComment(X)
          240  +# define VdbeModuleComment(X)
   232    241   #endif
   233    242   
   234    243   #endif

Changes to src/vdbeInt.h.

    78     78     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    79     79     const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
    80     80     i64 seqCount;         /* Sequence counter */
    81     81     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    82     82     i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
    83     83     VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
    84     84   
    85         -  /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
    86         -  ** OP_IsUnique opcode on this cursor. */
           85  +  /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists */
    87     86     int seekResult;
    88     87   
    89     88     /* Cached information about the header for the data record that the
    90     89     ** cursor is currently pointing to.  Only valid if cacheStatus matches
    91     90     ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
    92     91     ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
    93     92     ** the cache is out of date.
................................................................................
   465    464   
   466    465   int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
   467    466   void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   468    467   int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
   469    468   int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
   470    469   int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
   471    470   int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
   472         -int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int *);
          471  +int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
   473    472   
   474    473   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
   475    474     void sqlite3VdbeEnter(Vdbe*);
   476    475     void sqlite3VdbeLeave(Vdbe*);
   477    476   #else
   478    477   # define sqlite3VdbeEnter(X)
   479    478   # define sqlite3VdbeLeave(X)

Changes to src/vdbeaux.c.

   104    104     if( pNew ){
   105    105       p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
   106    106       p->aOp = pNew;
   107    107     }
   108    108     return (pNew ? SQLITE_OK : SQLITE_NOMEM);
   109    109   }
   110    110   
          111  +#ifdef SQLITE_DEBUG
          112  +/* This routine is just a convenient place to set a breakpoint that will
          113  +** fire after each opcode is inserted and displayed using
          114  +** "PRAGMA vdbe_addoptrace=on".
          115  +*/
          116  +static void test_addop_breakpoint(void){
          117  +  static int n = 0;
          118  +  n++;
          119  +}
          120  +#endif
          121  +
   111    122   /*
   112    123   ** Add a new instruction to the list of instructions current in the
   113    124   ** VDBE.  Return the address of the new instruction.
   114    125   **
   115    126   ** Parameters:
   116    127   **
   117    128   **    p               Pointer to the VDBE
................................................................................
   141    152     pOp->opcode = (u8)op;
   142    153     pOp->p5 = 0;
   143    154     pOp->p1 = p1;
   144    155     pOp->p2 = p2;
   145    156     pOp->p3 = p3;
   146    157     pOp->p4.p = 0;
   147    158     pOp->p4type = P4_NOTUSED;
   148         -#ifdef SQLITE_DEBUG
          159  +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   149    160     pOp->zComment = 0;
          161  +#endif
          162  +#ifdef SQLITE_DEBUG
   150    163     if( p->db->flags & SQLITE_VdbeAddopTrace ){
   151    164       sqlite3VdbePrintOp(0, i, &p->aOp[i]);
          165  +    test_addop_breakpoint();
   152    166     }
   153    167   #endif
   154    168   #ifdef VDBE_PROFILE
   155    169     pOp->cycles = 0;
   156    170     pOp->cnt = 0;
   157    171   #endif
   158    172     return i;
................................................................................
   520    534       int i;
   521    535       VdbeOpList const *pIn = aOp;
   522    536       for(i=0; i<nOp; i++, pIn++){
   523    537         int p2 = pIn->p2;
   524    538         VdbeOp *pOut = &p->aOp[i+addr];
   525    539         pOut->opcode = pIn->opcode;
   526    540         pOut->p1 = pIn->p1;
   527         -      if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
          541  +      if( p2<0 ){
          542  +        assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
   528    543           pOut->p2 = addr + ADDR(p2);
   529    544         }else{
   530    545           pOut->p2 = p2;
   531    546         }
   532    547         pOut->p3 = pIn->p3;
   533    548         pOut->p4type = P4_NOTUSED;
   534    549         pOut->p4.p = 0;
   535    550         pOut->p5 = 0;
   536         -#ifdef SQLITE_DEBUG
          551  +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   537    552         pOut->zComment = 0;
          553  +#endif
          554  +#ifdef SQLITE_DEBUG
   538    555         if( p->db->flags & SQLITE_VdbeAddopTrace ){
   539    556           sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
   540    557         }
   541    558   #endif
   542    559       }
   543    560       p->nOp += nOp;
   544    561     }
................................................................................
   618    635   static void freeP4(sqlite3 *db, int p4type, void *p4){
   619    636     if( p4 ){
   620    637       assert( db );
   621    638       switch( p4type ){
   622    639         case P4_REAL:
   623    640         case P4_INT64:
   624    641         case P4_DYNAMIC:
   625         -      case P4_KEYINFO:
   626         -      case P4_INTARRAY:
   627         -      case P4_KEYINFO_HANDOFF: {
          642  +      case P4_INTARRAY: {
   628    643           sqlite3DbFree(db, p4);
   629    644           break;
          645  +      }
          646  +      case P4_KEYINFO: {
          647  +        if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
          648  +        break;
   630    649         }
   631    650         case P4_MPRINTF: {
   632    651           if( db->pnBytesFreed==0 ) sqlite3_free(p4);
   633    652           break;
   634    653         }
   635    654         case P4_FUNCDEF: {
   636    655           freeEphemeralFunction(db, (FuncDef*)p4);
................................................................................
   660    679   ** nOp entries. 
   661    680   */
   662    681   static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
   663    682     if( aOp ){
   664    683       Op *pOp;
   665    684       for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
   666    685         freeP4(db, pOp->p4type, pOp->p4.p);
   667         -#ifdef SQLITE_DEBUG
          686  +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   668    687         sqlite3DbFree(db, pOp->zComment);
   669    688   #endif     
   670    689       }
   671    690     }
   672    691     sqlite3DbFree(db, aOp);
   673    692   }
   674    693   
................................................................................
   688    707   void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
   689    708     if( p->aOp ){
   690    709       VdbeOp *pOp = &p->aOp[addr];
   691    710       sqlite3 *db = p->db;
   692    711       freeP4(db, pOp->p4type, pOp->p4.p);
   693    712       memset(pOp, 0, sizeof(pOp[0]));
   694    713       pOp->opcode = OP_Noop;
          714  +    if( addr==p->nOp-1 ) p->nOp--;
   695    715     }
   696    716   }
   697    717   
   698    718   /*
   699    719   ** Change the value of the P4 operand for a specific instruction.
   700    720   ** This routine is useful when a large program is loaded from a
   701    721   ** static array using sqlite3VdbeAddOpList but we want to make a
   702    722   ** few minor changes to the program.
   703    723   **
   704    724   ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
   705    725   ** the string is made into memory obtained from sqlite3_malloc().
   706    726   ** A value of n==0 means copy bytes of zP4 up to and including the
   707    727   ** first null byte.  If n>0 then copy n+1 bytes of zP4.
   708         -**
   709         -** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
   710         -** A copy is made of the KeyInfo structure into memory obtained from
   711         -** sqlite3_malloc, to be freed when the Vdbe is finalized.
   712         -** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
   713         -** stored in memory that the caller has obtained from sqlite3_malloc. The 
   714         -** caller should not free the allocation, it will be freed when the Vdbe is
   715         -** finalized.
   716    728   ** 
   717    729   ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
   718    730   ** to a string or structure that is guaranteed to exist for the lifetime of
   719    731   ** the Vdbe. In these cases we can just copy the pointer.
   720    732   **
   721    733   ** If addr<0 then change P4 on the most recently inserted instruction.
   722    734   */
................................................................................
   723    735   void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
   724    736     Op *pOp;
   725    737     sqlite3 *db;
   726    738     assert( p!=0 );
   727    739     db = p->db;
   728    740     assert( p->magic==VDBE_MAGIC_INIT );
   729    741     if( p->aOp==0 || db->mallocFailed ){
   730         -    if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
          742  +    if( n!=P4_VTAB ){
   731    743         freeP4(db, n, (void*)*(char**)&zP4);
   732    744       }
   733    745       return;
   734    746     }
   735    747     assert( p->nOp>0 );
   736    748     assert( addr<p->nOp );
   737    749     if( addr<0 ){
................................................................................
   746    758       ** that was cast to a (const char *). */
   747    759       pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
   748    760       pOp->p4type = P4_INT32;
   749    761     }else if( zP4==0 ){
   750    762       pOp->p4.p = 0;
   751    763       pOp->p4type = P4_NOTUSED;
   752    764     }else if( n==P4_KEYINFO ){
   753         -    KeyInfo *pOrig, *pNew;
   754         -
   755         -    pOrig = (KeyInfo*)zP4;
   756         -    pOp->p4.pKeyInfo = pNew = sqlite3KeyInfoAlloc(db, pOrig->nField);
   757         -    if( pNew ){
   758         -      memcpy(pNew->aColl, pOrig->aColl, pOrig->nField*sizeof(pNew->aColl[0]));
   759         -      memcpy(pNew->aSortOrder, pOrig->aSortOrder, pOrig->nField);
   760         -      pOp->p4type = P4_KEYINFO;
   761         -    }else{
   762         -      p->db->mallocFailed = 1;
   763         -      pOp->p4type = P4_NOTUSED;
   764         -    }
   765         -  }else if( n==P4_KEYINFO_HANDOFF ){
   766    765       pOp->p4.p = (void*)zP4;
   767    766       pOp->p4type = P4_KEYINFO;
   768    767     }else if( n==P4_VTAB ){
   769    768       pOp->p4.p = (void*)zP4;
   770    769       pOp->p4type = P4_VTAB;
   771    770       sqlite3VtabLock((VTable *)zP4);
   772    771       assert( ((VTable *)zP4)->db==p->db );
................................................................................
   776    775     }else{
   777    776       if( n==0 ) n = sqlite3Strlen30(zP4);
   778    777       pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
   779    778       pOp->p4type = P4_DYNAMIC;
   780    779     }
   781    780   }
   782    781   
   783         -#ifndef NDEBUG
          782  +/*
          783  +** Set the P4 on the most recently added opcode to the KeyInfo for the
          784  +** index given.
          785  +*/
          786  +void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
          787  +  Vdbe *v = pParse->pVdbe;
          788  +  assert( v!=0 );
          789  +  assert( pIdx!=0 );
          790  +  sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
          791  +                      P4_KEYINFO);
          792  +}
          793  +
          794  +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   784    795   /*
   785    796   ** Change the comment on the most recently coded instruction.  Or
   786    797   ** insert a No-op and add the comment to that new instruction.  This
   787    798   ** makes the code easier to read during debugging.  None of this happens
   788    799   ** in a production build.
   789    800   */
   790    801   static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
................................................................................
   850    861     assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
   851    862     if( p->db->mallocFailed ){
   852    863       return (VdbeOp*)&dummy;
   853    864     }else{
   854    865       return &p->aOp[addr];
   855    866     }
   856    867   }
          868  +
          869  +#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
          870  +/*
          871  +** Return an integer value for one of the parameters to the opcode pOp
          872  +** determined by character c.
          873  +*/
          874  +static int translateP(char c, const Op *pOp){
          875  +  if( c=='1' ) return pOp->p1;
          876  +  if( c=='2' ) return pOp->p2;
          877  +  if( c=='3' ) return pOp->p3;
          878  +  if( c=='4' ) return pOp->p4.i;
          879  +  return pOp->p5;
          880  +}
          881  +
          882  +/*
          883  +** Compute a string for the "comment" field of a VDBE opcode listing
          884  +*/
          885  +static int displayComment(