/ Check-in [5f14f34f]
Login

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

Overview
Comment:Merge all recent trunk enhancements into the sessions branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 5f14f34f072921e229bc20a5185c0cdb3f464c04
User & Date: drh 2015-04-12 01:33:37
Context
2015-04-15
14:14
Merge all recent trunk fixes and enhancements into the sessions branch. check-in: e65e65f9 user: drh tags: sessions
2015-04-12
01:33
Merge all recent trunk enhancements into the sessions branch. check-in: 5f14f34f user: drh tags: sessions
01:22
Avoid problems when a query has a GROUP BY and an ORDER BY but no FROM clause. check-in: e527d96a user: drh tags: trunk
2015-04-11
17:09
Merge the sessions-diff branch with this one. check-in: 805baa57 user: dan tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   541    541   		-avoid-version
   542    542   
   543    543   sqlite3$(TEXE):	$(TOP)/src/shell.c libsqlite3.la sqlite3.h
   544    544   	$(LTLINK) $(READLINE_FLAGS) \
   545    545   		-o $@ $(TOP)/src/shell.c libsqlite3.la \
   546    546   		$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
   547    547   
          548  +sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
          549  +	$(LTLINK) -o $@ $(TOP)/tool/sqldiff.c	sqlite3.c $(TLIBS)
          550  +
   548    551   mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
   549    552   	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   550    553   		$(TLIBS) -rpath "$(libdir)"
   551    554   
   552    555   MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
   553    556   MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
   554    557   mptest:	mptester$(EXE)

Changes to Makefile.msc.

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

Changes to ext/fts3/fts3.c.

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

Changes to ext/session/sqlite3session.c.

   775    775   */
   776    776   static int sessionPreupdateEqual(
   777    777     sqlite3_session *pSession,      /* Session object that owns SessionTable */
   778    778     SessionTable *pTab,             /* Table associated with change */
   779    779     SessionChange *pChange,         /* Change to compare to */
   780    780     int op                          /* Current pre-update operation */
   781    781   ){
   782         -  sqlite3 *db = pSession->db;
   783    782     int iCol;                       /* Used to iterate through columns */
   784    783     u8 *a = pChange->aRecord;       /* Cursor used to scan change record */
   785    784   
   786    785     assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
   787    786     for(iCol=0; iCol<pTab->nCol; iCol++){
   788    787       if( !pTab->abPK[iCol] ){
   789    788         a += sessionSerialLen(a);

Changes to main.mk.

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

Changes to src/build.c.

  2759   2759       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
  2760   2760                            pIndex->nKeyCol); VdbeCoverage(v);
  2761   2761       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
  2762   2762     }else{
  2763   2763       addr2 = sqlite3VdbeCurrentAddr(v);
  2764   2764     }
  2765   2765     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
  2766         -  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
         2766  +  sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
         2767  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
  2767   2768     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  2768   2769     sqlite3ReleaseTempReg(pParse, regRecord);
  2769   2770     sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
  2770   2771     sqlite3VdbeJumpHere(v, addr1);
  2771   2772   
  2772   2773     sqlite3VdbeAddOp1(v, OP_Close, iTab);
  2773   2774     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
................................................................................
  3772   3773   ** The operator is "natural cross join".  The A and B operands are stored
  3773   3774   ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
  3774   3775   ** operator with A.  This routine shifts that operator over to B.
  3775   3776   */
  3776   3777   void sqlite3SrcListShiftJoinType(SrcList *p){
  3777   3778     if( p ){
  3778   3779       int i;
  3779         -    assert( p->a || p->nSrc==0 );
  3780   3780       for(i=p->nSrc-1; i>0; i--){
  3781   3781         p->a[i].jointype = p->a[i-1].jointype;
  3782   3782       }
  3783   3783       p->a[0].jointype = 0;
  3784   3784     }
  3785   3785   }
  3786   3786   

Changes to src/complete.c.

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

Changes to src/fkey.c.

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

Changes to src/insert.c.

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

Changes to src/msvc.h.

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

Changes to src/sqliteInt.h.

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

Changes to src/trigger.c.

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

Changes to src/vacuum.c.

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

Changes to src/vdbe.c.

  2584   2584   */
  2585   2585   case OP_MakeRecord: {
  2586   2586     u8 *zNewRecord;        /* A buffer to hold the data for the new record */
  2587   2587     Mem *pRec;             /* The new record */
  2588   2588     u64 nData;             /* Number of bytes of data space */
  2589   2589     int nHdr;              /* Number of bytes of header space */
  2590   2590     i64 nByte;             /* Data space required for this record */
  2591         -  int nZero;             /* Number of zero bytes at the end of the record */
         2591  +  i64 nZero;             /* Number of zero bytes at the end of the record */
  2592   2592     int nVarint;           /* Number of bytes in a varint */
  2593   2593     u32 serial_type;       /* Type field */
  2594   2594     Mem *pData0;           /* First field to be combined into the record */
  2595   2595     Mem *pLast;            /* Last field of the record */
  2596   2596     int nField;            /* Number of fields in the record */
  2597   2597     char *zAffinity;       /* The affinity string for the record */
  2598   2598     int file_format;       /* File format to use for encoding */
................................................................................
  2676   2676     }else{
  2677   2677       /* Rare case of a really large header */
  2678   2678       nVarint = sqlite3VarintLen(nHdr);
  2679   2679       nHdr += nVarint;
  2680   2680       if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
  2681   2681     }
  2682   2682     nByte = nHdr+nData;
  2683         -  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
         2683  +  if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2684   2684       goto too_big;
  2685   2685     }
  2686   2686   
  2687   2687     /* Make sure the output register has a buffer large enough to store 
  2688   2688     ** the new record. The output register (pOp->p3) is not allowed to
  2689   2689     ** be one of the input registers (because the following call to
  2690   2690     ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
................................................................................
  4542   4542     pC->cacheStatus = CACHE_STALE;
  4543   4543     if( pC->pCursor ){
  4544   4544       sqlite3BtreeClearCursor(pC->pCursor);
  4545   4545     }
  4546   4546     break;
  4547   4547   }
  4548   4548   
  4549         -/* Opcode: Last P1 P2 * * *
         4549  +/* Opcode: Last P1 P2 P3 * *
  4550   4550   **
  4551   4551   ** The next use of the Rowid or Column or Prev instruction for P1 
  4552   4552   ** will refer to the last entry in the database table or index.
  4553   4553   ** If the table or index is empty and P2>0, then jump immediately to P2.
  4554   4554   ** If P2 is 0 or if the table or index is not empty, fall through
  4555   4555   ** to the following instruction.
  4556   4556   **
................................................................................
  4569   4569     pCrsr = pC->pCursor;
  4570   4570     res = 0;
  4571   4571     assert( pCrsr!=0 );
  4572   4572     rc = sqlite3BtreeLast(pCrsr, &res);
  4573   4573     pC->nullRow = (u8)res;
  4574   4574     pC->deferredMoveto = 0;
  4575   4575     pC->cacheStatus = CACHE_STALE;
         4576  +  pC->seekResult = pOp->p3;
  4576   4577   #ifdef SQLITE_DEBUG
  4577   4578     pC->seekOp = OP_Last;
  4578   4579   #endif
  4579   4580     if( pOp->p2>0 ){
  4580   4581       VdbeBranchTaken(res!=0,2);
  4581   4582       if( res ) pc = pOp->p2 - 1;
  4582   4583     }

Changes to src/vdbe.h.

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

Changes to src/vdbeaux.c.

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

Changes to src/vdbesort.c.

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

Changes to src/vtab.c.

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

Changes to src/where.c.

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

Changes to test/fkey2.test.

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

Changes to test/fts4content.test.

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

Changes to test/null.test.

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

Changes to test/orderby1.test.

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

Changes to test/whereD.test.

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

Changes to test/zeroblob.test.

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

Added tool/sqldiff.c.

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