/ Check-in [60be9c1c]
Login

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

Overview
Comment:Update this branch with latest trunk changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sorter-opt
Files: files | file ages | folders
SHA1: 60be9c1c1ad0d8250a99cadda820dff40a31c94e
User & Date: dan 2015-04-10 08:20:42
Context
2015-04-11
18:45
Merge latest trunk changes with this branch. Closed-Leaf check-in: cf7590f6 user: dan tags: sorter-opt
2015-04-10
08:28
Update this branch with the latest changes from sorter-opt. Leaf check-in: 08c0b19b user: dan tags: insert-select-opt
08:20
Update this branch with latest trunk changes. check-in: 60be9c1c user: dan tags: sorter-opt
07:55
Do not allow virtual table constructors to be called recursively. check-in: 0a72726d user: dan tags: trunk
2015-04-02
09:06
Fix a problem in vdbesort.c to do with caching unpacked records. check-in: 80a00539 user: dan tags: sorter-opt
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

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

Changes to Makefile.msc.

  1149   1149   
  1150   1150   libtclsqlite3.lib:	tclsqlite.lo libsqlite3.lib
  1151   1151   	$(LTLIB) $(LTLIBOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS)
  1152   1152   
  1153   1153   sqlite3.exe:	$(TOP)\src\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h
  1154   1154   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c \
  1155   1155   		/link /pdb:sqlite3sh.pdb $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
         1156  +
         1157  +sqldiff.exe:	$(TOP)\tool\sqldiff.c sqlite3.c sqlite3.h
         1158  +	$(LTLINK) $(TOP)\tool\sqldiff.c sqlite3.c
  1156   1159   
  1157   1160   mptester.exe:	$(TOP)\mptest\mptest.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h
  1158   1161   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(TOP)\mptest\mptest.c \
  1159   1162   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(SHELL_LINK_OPTS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
  1160   1163   
  1161   1164   MPTEST1 = mptester mptest.db $(TOP)/mptest/crash01.test --repeat 20
  1162   1165   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 ){
................................................................................
  2498   2502   **
  2499   2503   ** If the docids in the input doclists are sorted in ascending order,
  2500   2504   ** parameter bDescDoclist should be false. If they are sorted in ascending 
  2501   2505   ** order, it should be passed a non-zero value.
  2502   2506   **
  2503   2507   ** The right-hand input doclist is overwritten by this function.
  2504   2508   */
  2505         -static void fts3DoclistPhraseMerge(
         2509  +static int fts3DoclistPhraseMerge(
  2506   2510     int bDescDoclist,               /* True if arguments are desc */
  2507   2511     int nDist,                      /* Distance from left to right (1=adjacent) */
  2508   2512     char *aLeft, int nLeft,         /* Left doclist */
  2509         -  char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
         2513  +  char **paRight, int *pnRight    /* IN/OUT: Right/output doclist */
  2510   2514   ){
  2511   2515     sqlite3_int64 i1 = 0;
  2512   2516     sqlite3_int64 i2 = 0;
  2513   2517     sqlite3_int64 iPrev = 0;
         2518  +  char *aRight = *paRight;
  2514   2519     char *pEnd1 = &aLeft[nLeft];
  2515   2520     char *pEnd2 = &aRight[*pnRight];
  2516   2521     char *p1 = aLeft;
  2517   2522     char *p2 = aRight;
  2518   2523     char *p;
  2519   2524     int bFirstOut = 0;
  2520         -  char *aOut = aRight;
         2525  +  char *aOut;
  2521   2526   
  2522   2527     assert( nDist>0 );
  2523         -
         2528  +  if( bDescDoclist ){
         2529  +    aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX);
         2530  +    if( aOut==0 ) return SQLITE_NOMEM;
         2531  +  }else{
         2532  +    aOut = aRight;
         2533  +  }
  2524   2534     p = aOut;
         2535  +
  2525   2536     fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
  2526   2537     fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
  2527   2538   
  2528   2539     while( p1 && p2 ){
  2529   2540       sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
  2530   2541       if( iDiff==0 ){
  2531   2542         char *pSave = p;
................................................................................
  2546   2557       }else{
  2547   2558         fts3PoslistCopy(0, &p2);
  2548   2559         fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  2549   2560       }
  2550   2561     }
  2551   2562   
  2552   2563     *pnRight = (int)(p - aOut);
         2564  +  if( bDescDoclist ){
         2565  +    sqlite3_free(aRight);
         2566  +    *paRight = aOut;
         2567  +  }
         2568  +
         2569  +  return SQLITE_OK;
  2553   2570   }
  2554   2571   
  2555   2572   /*
  2556   2573   ** Argument pList points to a position list nList bytes in size. This
  2557   2574   ** function checks to see if the position list contains any entries for
  2558   2575   ** a token in position 0 (of any column). If so, it writes argument iDelta
  2559   2576   ** to the output buffer pOut, followed by a position list consisting only
................................................................................
  2670   2687     Fts3Table *p,                   /* FTS table handle */
  2671   2688     TermSelect *pTS,                /* TermSelect object to merge into */
  2672   2689     char *aDoclist,                 /* Pointer to doclist */
  2673   2690     int nDoclist                    /* Size of aDoclist in bytes */
  2674   2691   ){
  2675   2692     if( pTS->aaOutput[0]==0 ){
  2676   2693       /* If this is the first term selected, copy the doclist to the output
  2677         -    ** buffer using memcpy(). */
  2678         -    pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
         2694  +    ** buffer using memcpy(). 
         2695  +    **
         2696  +    ** Add FTS3_VARINT_MAX bytes of unused space to the end of the 
         2697  +    ** allocation. This is so as to ensure that the buffer is big enough
         2698  +    ** to hold the current doclist AND'd with any other doclist. If the
         2699  +    ** doclists are stored in order=ASC order, this padding would not be
         2700  +    ** required (since the size of [doclistA AND doclistB] is always less
         2701  +    ** than or equal to the size of [doclistA] in that case). But this is
         2702  +    ** not true for order=DESC. For example, a doclist containing (1, -1) 
         2703  +    ** may be smaller than (-1), as in the first example the -1 may be stored
         2704  +    ** as a single-byte delta, whereas in the second it must be stored as a
         2705  +    ** FTS3_VARINT_MAX byte varint.
         2706  +    **
         2707  +    ** Similar padding is added in the fts3DoclistOrMerge() function.
         2708  +    */
         2709  +    pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
  2679   2710       pTS->anOutput[0] = nDoclist;
  2680   2711       if( pTS->aaOutput[0] ){
  2681   2712         memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
  2682   2713       }else{
  2683   2714         return SQLITE_NOMEM;
  2684   2715       }
  2685   2716     }else{
................................................................................
  3927   3958   /*
  3928   3959   ** Arguments pList/nList contain the doclist for token iToken of phrase p.
  3929   3960   ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
  3930   3961   **
  3931   3962   ** This function assumes that pList points to a buffer allocated using
  3932   3963   ** sqlite3_malloc(). This function takes responsibility for eventually
  3933   3964   ** freeing the buffer.
         3965  +**
         3966  +** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
  3934   3967   */
  3935         -static void fts3EvalPhraseMergeToken(
         3968  +static int fts3EvalPhraseMergeToken(
  3936   3969     Fts3Table *pTab,                /* FTS Table pointer */
  3937   3970     Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
  3938   3971     int iToken,                     /* Token pList/nList corresponds to */
  3939   3972     char *pList,                    /* Pointer to doclist */
  3940   3973     int nList                       /* Number of bytes in pList */
  3941   3974   ){
         3975  +  int rc = SQLITE_OK;
  3942   3976     assert( iToken!=p->iDoclistToken );
  3943   3977   
  3944   3978     if( pList==0 ){
  3945   3979       sqlite3_free(p->doclist.aAll);
  3946   3980       p->doclist.aAll = 0;
  3947   3981       p->doclist.nAll = 0;
  3948   3982     }
................................................................................
  3973   4007         pRight = p->doclist.aAll;
  3974   4008         nRight = p->doclist.nAll;
  3975   4009         pLeft = pList;
  3976   4010         nLeft = nList;
  3977   4011         nDiff = p->iDoclistToken - iToken;
  3978   4012       }
  3979   4013   
  3980         -    fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
         4014  +    rc = fts3DoclistPhraseMerge(
         4015  +        pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
         4016  +    );
  3981   4017       sqlite3_free(pLeft);
  3982   4018       p->doclist.aAll = pRight;
  3983   4019       p->doclist.nAll = nRight;
  3984   4020     }
  3985   4021   
  3986   4022     if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
         4023  +  return rc;
  3987   4024   }
  3988   4025   
  3989   4026   /*
  3990   4027   ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
  3991   4028   ** does not take deferred tokens into account.
  3992   4029   **
  3993   4030   ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
................................................................................
  4005   4042       assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
  4006   4043   
  4007   4044       if( pToken->pSegcsr ){
  4008   4045         int nThis = 0;
  4009   4046         char *pThis = 0;
  4010   4047         rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
  4011   4048         if( rc==SQLITE_OK ){
  4012         -        fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
         4049  +        rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
  4013   4050         }
  4014   4051       }
  4015   4052       assert( pToken->pSegcsr==0 );
  4016   4053     }
  4017   4054   
  4018   4055     return rc;
  4019   4056   }
................................................................................
  4807   4844           ** part of a multi-token phrase. Either way, the entire doclist will
  4808   4845           ** (eventually) be loaded into memory. It may as well be now. */
  4809   4846           Fts3PhraseToken *pToken = pTC->pToken;
  4810   4847           int nList = 0;
  4811   4848           char *pList = 0;
  4812   4849           rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
  4813   4850           assert( rc==SQLITE_OK || pList==0 );
         4851  +        if( rc==SQLITE_OK ){
         4852  +          rc = fts3EvalPhraseMergeToken(
         4853  +              pTab, pTC->pPhrase, pTC->iToken,pList,nList
         4854  +          );
         4855  +        }
  4814   4856           if( rc==SQLITE_OK ){
  4815   4857             int nCount;
  4816         -          fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
  4817   4858             nCount = fts3DoclistCountDocids(
  4818   4859                 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
  4819   4860             );
  4820   4861             if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
  4821   4862           }
  4822   4863         }
  4823   4864       }

Changes to main.mk.

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

Changes to src/build.c.

  3773   3773   ** The operator is "natural cross join".  The A and B operands are stored
  3774   3774   ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
  3775   3775   ** operator with A.  This routine shifts that operator over to B.
  3776   3776   */
  3777   3777   void sqlite3SrcListShiftJoinType(SrcList *p){
  3778   3778     if( p ){
  3779   3779       int i;
  3780         -    assert( p->a || p->nSrc==0 );
  3781   3780       for(i=p->nSrc-1; i>0; i--){
  3782   3781         p->a[i].jointype = p->a[i-1].jointype;
  3783   3782       }
  3784   3783       p->a[0].jointype = 0;
  3785   3784     }
  3786   3785   }
  3787   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/os_unix.c.

  3779   3779   /*
  3780   3780   ** Information and control of an open file handle.
  3781   3781   */
  3782   3782   static int unixFileControl(sqlite3_file *id, int op, void *pArg){
  3783   3783     unixFile *pFile = (unixFile*)id;
  3784   3784     switch( op ){
  3785   3785       case SQLITE_FCNTL_WAL_BLOCK: {
  3786         -      pFile->ctrlFlags |= UNIXFILE_BLOCK;
         3786  +      /* pFile->ctrlFlags |= UNIXFILE_BLOCK; // Deferred feature */
  3787   3787         return SQLITE_OK;
  3788   3788       }
  3789   3789       case SQLITE_FCNTL_LOCKSTATE: {
  3790   3790         *(int*)pArg = pFile->eFileLock;
  3791   3791         return SQLITE_OK;
  3792   3792       }
  3793   3793       case SQLITE_FCNTL_LAST_ERRNO: {

Changes to src/printf.c.

   257    257         if( bArgList ){
   258    258           width = (int)getIntArg(pArgList);
   259    259         }else{
   260    260           width = va_arg(ap,int);
   261    261         }
   262    262         if( width<0 ){
   263    263           flag_leftjustify = 1;
   264         -        width = -width;
          264  +        width = width >= -2147483647 ? -width : 0;
   265    265         }
   266    266         c = *++fmt;
   267    267       }else{
          268  +      unsigned wx = 0;
   268    269         while( c>='0' && c<='9' ){
   269         -        width = width*10 + c - '0';
          270  +        wx = wx*10 + c - '0';
   270    271           c = *++fmt;
   271    272         }
          273  +      testcase( wx>0x7fffffff );
          274  +      width = wx & 0x7fffffff;
   272    275       }
          276  +
   273    277       /* Get the precision */
   274    278       if( c=='.' ){
   275    279         precision = 0;
   276    280         c = *++fmt;
   277    281         if( c=='*' ){
   278    282           if( bArgList ){
   279    283             precision = (int)getIntArg(pArgList);
   280    284           }else{
   281    285             precision = va_arg(ap,int);
   282    286           }
   283         -        if( precision<0 ) precision = -precision;
   284    287           c = *++fmt;
          288  +        if( precision<0 ){
          289  +          precision = precision >= -2147483647 ? -precision : -1;
          290  +        }
   285    291         }else{
          292  +        unsigned px = 0;
   286    293           while( c>='0' && c<='9' ){
   287         -          precision = precision*10 + c - '0';
          294  +          px = px*10 + c - '0';
   288    295             c = *++fmt;
   289    296           }
          297  +        testcase( px>0x7fffffff );
          298  +        precision = px & 0x7fffffff;
   290    299         }
   291    300       }else{
   292    301         precision = -1;
   293    302       }
   294    303       /* Get the conversion type modifier */
   295    304       if( c=='l' ){
   296    305         flag_long = 1;
................................................................................
   446    455             prefix = '-';
   447    456           }else{
   448    457             if( flag_plussign )          prefix = '+';
   449    458             else if( flag_blanksign )    prefix = ' ';
   450    459             else                         prefix = 0;
   451    460           }
   452    461           if( xtype==etGENERIC && precision>0 ) precision--;
   453         -        for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
          462  +        testcase( precision>0xfff );
          463  +        for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
   454    464           if( xtype==etFLOAT ) realvalue += rounder;
   455    465           /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
   456    466           exp = 0;
   457    467           if( sqlite3IsNaN((double)realvalue) ){
   458    468             bufpt = "NaN";
   459    469             length = 3;
   460    470             break;
................................................................................
   501    511             flag_rtz = flag_altform2;
   502    512           }
   503    513           if( xtype==etEXP ){
   504    514             e2 = 0;
   505    515           }else{
   506    516             e2 = exp;
   507    517           }
   508         -        if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
   509         -          bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
          518  +        if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
          519  +          bufpt = zExtra 
          520  +              = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
   510    521             if( bufpt==0 ){
   511    522               setStrAccumError(pAccum, STRACCUM_NOMEM);
   512    523               return;
   513    524             }
   514    525           }
   515    526           zOut = bufpt;
   516    527           nsd = 16 + flag_altform2*10;
................................................................................
   734    745   ** able to accept at least N more bytes of text.
   735    746   **
   736    747   ** Return the number of bytes of text that StrAccum is able to accept
   737    748   ** after the attempted enlargement.  The value returned might be zero.
   738    749   */
   739    750   static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
   740    751     char *zNew;
   741         -  assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
          752  +  assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
   742    753     if( p->accError ){
   743    754       testcase(p->accError==STRACCUM_TOOBIG);
   744    755       testcase(p->accError==STRACCUM_NOMEM);
   745    756       return 0;
   746    757     }
   747    758     if( !p->useMalloc ){
   748    759       N = p->nAlloc - p->nChar - 1;
................................................................................
   783    794     return N;
   784    795   }
   785    796   
   786    797   /*
   787    798   ** Append N copies of character c to the given string buffer.
   788    799   */
   789    800   void sqlite3AppendChar(StrAccum *p, int N, char c){
   790         -  if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
          801  +  testcase( p->nChar + (i64)N > 0x7fffffff );
          802  +  if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
          803  +    return;
          804  +  }
   791    805     while( (N--)>0 ) p->zText[p->nChar++] = c;
   792    806   }
   793    807   
   794    808   /*
   795    809   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
   796    810   ** So enlarge if first, then do the append.
   797    811   **

Changes to src/resolve.c.

  1182   1182       */
  1183   1183       memset(&sNC, 0, sizeof(sNC));
  1184   1184       sNC.pParse = pParse;
  1185   1185       if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
  1186   1186           sqlite3ResolveExprNames(&sNC, p->pOffset) ){
  1187   1187         return WRC_Abort;
  1188   1188       }
         1189  +
         1190  +    /* If the SF_Converted flags is set, then this Select object was
         1191  +    ** was created by the convertCompoundSelectToSubquery() function.
         1192  +    ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
         1193  +    ** as if it were part of the sub-query, not the parent. This block
         1194  +    ** moves the pOrderBy down to the sub-query. It will be moved back
         1195  +    ** after the names have been resolved.  */
         1196  +    if( p->selFlags & SF_Converted ){
         1197  +      Select *pSub = p->pSrc->a[0].pSelect;
         1198  +      assert( p->pSrc->nSrc==1 && isCompound==0 && p->pOrderBy );
         1199  +      assert( pSub->pPrior && pSub->pOrderBy==0 );
         1200  +      pSub->pOrderBy = p->pOrderBy;
         1201  +      p->pOrderBy = 0;
         1202  +    }
  1189   1203     
  1190   1204       /* Recursively resolve names in all subqueries
  1191   1205       */
  1192   1206       for(i=0; i<p->pSrc->nSrc; i++){
  1193   1207         struct SrcList_item *pItem = &p->pSrc->a[i];
  1194   1208         if( pItem->pSelect ){
  1195   1209           NameContext *pNC;         /* Used to iterate name contexts */
................................................................................
  1263   1277       if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
  1264   1278   
  1265   1279       /* The ORDER BY and GROUP BY clauses may not refer to terms in
  1266   1280       ** outer queries 
  1267   1281       */
  1268   1282       sNC.pNext = 0;
  1269   1283       sNC.ncFlags |= NC_AllowAgg;
         1284  +
         1285  +    /* If this is a converted compound query, move the ORDER BY clause from 
         1286  +    ** the sub-query back to the parent query. At this point each term
         1287  +    ** within the ORDER BY clause has been transformed to an integer value.
         1288  +    ** These integers will be replaced by copies of the corresponding result
         1289  +    ** set expressions by the call to resolveOrderGroupBy() below.  */
         1290  +    if( p->selFlags & SF_Converted ){
         1291  +      Select *pSub = p->pSrc->a[0].pSelect;
         1292  +      p->pOrderBy = pSub->pOrderBy;
         1293  +      pSub->pOrderBy = 0;
         1294  +    }
  1270   1295   
  1271   1296       /* Process the ORDER BY clause for singleton SELECT statements.
  1272   1297       ** The ORDER BY clause for compounds SELECT statements is handled
  1273   1298       ** below, after all of the result-sets for all of the elements of
  1274   1299       ** the compound have been resolved.
  1275   1300       */
  1276   1301       if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){

Changes to src/select.c.

  3880   3880     p->pWhere = 0;
  3881   3881     pNew->pGroupBy = 0;
  3882   3882     pNew->pHaving = 0;
  3883   3883     pNew->pOrderBy = 0;
  3884   3884     p->pPrior = 0;
  3885   3885     p->pNext = 0;
  3886   3886     p->selFlags &= ~SF_Compound;
         3887  +  assert( (p->selFlags & SF_Converted)==0 );
         3888  +  p->selFlags |= SF_Converted;
  3887   3889     assert( pNew->pPrior!=0 );
  3888   3890     pNew->pPrior->pNext = pNew;
  3889   3891     pNew->pLimit = 0;
  3890   3892     pNew->pOffset = 0;
  3891   3893     return WRC_Continue;
  3892   3894   }
  3893   3895   

Changes to src/sqliteInt.h.

  2386   2386   #define SF_Compound        0x0040  /* Part of a compound query */
  2387   2387   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  2388   2388   #define SF_AllValues       0x0100  /* All terms of compound are VALUES */
  2389   2389   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  2390   2390   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
  2391   2391   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
  2392   2392   #define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
         2393  +#define SF_Converted       0x2000  /* By convertCompoundSelectToSubquery() */
  2393   2394   
  2394   2395   
  2395   2396   /*
  2396   2397   ** The results of a SELECT can be distributed in several ways, as defined
  2397   2398   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2398   2399   ** Type".
  2399   2400   **

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/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 = db->pVtabCtx;
   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  +  if( !pCtx || pCtx->bDeclared ){
   722    738       sqlite3Error(db, SQLITE_MISUSE);
   723    739       sqlite3_mutex_leave(db->mutex);
   724    740       return SQLITE_MISUSE_BKPT;
   725    741     }
          742  +  pTab = pCtx->pTab;
   726    743     assert( (pTab->tabFlags & TF_Virtual)!=0 );
   727    744   
   728    745     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
   729    746     if( pParse==0 ){
   730    747       rc = SQLITE_NOMEM;
   731    748     }else{
   732    749       pParse->declareVtab = 1;
................................................................................
   741    758       ){
   742    759         if( !pTab->aCol ){
   743    760           pTab->aCol = pParse->pNewTable->aCol;
   744    761           pTab->nCol = pParse->pNewTable->nCol;
   745    762           pParse->pNewTable->nCol = 0;
   746    763           pParse->pNewTable->aCol = 0;
   747    764         }
   748         -      db->pVtabCtx->pTab = 0;
          765  +      pCtx->bDeclared = 1;
   749    766       }else{
   750    767         sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
   751    768         sqlite3DbFree(db, zErr);
   752    769         rc = SQLITE_ERROR;
   753    770       }
   754    771       pParse->declareVtab = 0;
   755    772     

Changes to test/e_walauto.test.

    11     11   #
    12     12   
    13     13   set testdir [file dirname $argv0]
    14     14   source $testdir/tester.tcl
    15     15   source $testdir/wal_common.tcl
    16     16   set testprefix e_walauto
    17     17   
           18  +# Do not run this test on OpenBSD, as it depends on read() and mmap both
           19  +# accessing the same coherent view of the "test.db-shm" file. This doesn't
           20  +# work on OpenBSD.
           21  +#
           22  +if {$tcl_platform(os) == "OpenBSD"} {
           23  +  finish_test
           24  +  return
           25  +}
    18     26   
    19     27   proc read_nbackfill {} {
    20     28     seek $::shmfd 96
    21     29     binary scan [read $::shmfd 4] n nBackfill
    22     30     set nBackfill
    23     31   }
    24     32   proc read_mxframe {} {

Changes to test/fts3fault2.test.

   150    150         );
   151    151       }
   152    152       execsql { SELECT docid FROM ft WHERE ft MATCH 'th*' }
   153    153     } -test {
   154    154       faultsim_test_result {0 {1 2}}
   155    155     }
   156    156   }
          157  +
          158  +reset_db
          159  +do_test 6.0 {
          160  +  execsql {
          161  +    CREATE VIRTUAL TABLE t6 USING fts4(x,order=DESC);
          162  +    INSERT INTO t6(docid, x) VALUES(-1,'a b');
          163  +    INSERT INTO t6(docid, x) VALUES(1, 'b');
          164  +  }
          165  +  faultsim_save_and_close
          166  +} {}
          167  +
          168  +do_faultsim_test 6.1 -faults oom* -prep {
          169  +  faultsim_restore_and_reopen
          170  +  db eval {SELECT * FROM sqlite_master}
          171  +} -body {
          172  +  execsql { SELECT docid FROM t6 WHERE t6 MATCH '"a* b"' }
          173  +} -test {
          174  +  faultsim_test_result {0 -1}
          175  +}
   157    176   
   158    177   finish_test

Changes to test/fts3prefix.test.

   269    269     CREATE VIRTUAL TABLE t2 USING fts4(prefix=);
   270    270     INSERT INTO t1 VALUES('He dressed himself in cycling clothes');
   271    271     INSERT INTO t2 VALUES('He dressed himself in cycling clothes');
   272    272   } {}
   273    273   do_execsql_test 6.5.2 {
   274    274     SELECT md5sum(quote(root)) FROM t1_segdir;
   275    275   } [db eval {SELECT md5sum(quote(root)) FROM t2_segdir}]
          276  +
          277  +
          278  +do_execsql_test 7.0 {
          279  +  CREATE VIRTUAL TABLE t6 USING fts4(x,order=DESC);
          280  +  INSERT INTO t6(docid, x) VALUES(-1,'a b');
          281  +  INSERT INTO t6(docid, x) VALUES(1, 'b');
          282  +}
          283  +do_execsql_test 7.1 {
          284  +  SELECT docid FROM t6 WHERE t6 MATCH '"a* b"';
          285  +} {-1}
          286  +do_execsql_test 7.2 {
          287  +  SELECT docid FROM t6 WHERE t6 MATCH 'a*';
          288  +} {-1}
          289  +do_execsql_test 7.3 {
          290  +  SELECT docid FROM t6 WHERE t6 MATCH 'a* b';
          291  +} {-1}
          292  +
          293  +
   276    294   
   277    295   finish_test

Changes to test/fts4content.test.

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

Changes to test/printf.test.

   468    468     sqlite3_mprintf_int {abc: (% 6d) (% 6x) (% 6o) :xyz}\
   469    469          0xff676981 0xff676981 0xff676981
   470    470   } {abc: (-9999999) (ff676981) (37731664601) :xyz}
   471    471   do_test printf-1.16.7 {
   472    472     sqlite3_mprintf_int {abc: (%#6d) (%#6x) (%#6o) :xyz}\
   473    473          0xff676981 0xff676981 0xff676981
   474    474   } {abc: (-9999999) (0xff676981) (037731664601) :xyz}
          475  +do_test printf-1.17.1 {
          476  +  sqlite3_mprintf_int {abd: %2147483647d %2147483647x %2147483647o} 1 1 1
          477  +} {}
          478  +do_test printf-1.17.2 {
          479  +  sqlite3_mprintf_int {abd: %*d %x} 2147483647 1 1
          480  +} {}
          481  +do_test printf-1.17.3 {
          482  +  sqlite3_mprintf_int {abd: %*d %x} -2147483648 1 1
          483  +} {abd: 1 1}
          484  +do_test printf-1.17.4 {
          485  +  sqlite3_mprintf_int {abd: %.2147483648d %x %x} 1 1 1
          486  +} {/.*/}
   475    487   do_test printf-2.1.1.1 {
   476    488     sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 0.001
   477    489   } {abc: (0.0) :xyz}
   478    490   do_test printf-2.1.1.2 {
   479    491     sqlite3_mprintf_double {abc: (%*.*e) :xyz} 1 1 0.001
   480    492   } {abc: (1.0e-03) :xyz}
   481    493   do_test printf-2.1.1.3 {
................................................................................
   522    534   } {abc: 1 1 (0.0) :xyz}
   523    535   do_test printf-2.1.2.8 {
   524    536     sqlite3_mprintf_double {abc: %d %d (%1.1e) :xyz} 1 1 1.0e-20
   525    537   } {abc: 1 1 (1.0e-20) :xyz}
   526    538   do_test printf-2.1.2.9 {
   527    539     sqlite3_mprintf_double {abc: %d %d (%1.1g) :xyz} 1 1 1.0e-20
   528    540   } {abc: 1 1 (1e-20) :xyz}
          541  +do_test printf-2.1.2.10 {
          542  +  sqlite3_mprintf_double {abc: %*.*f}  2000000000 1000000000 1.0e-20
          543  +} {abc: }
   529    544   do_test printf-2.1.3.1 {
   530    545     sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 1.0
   531    546   } {abc: (1.0) :xyz}
   532    547   do_test printf-2.1.3.2 {
   533    548     sqlite3_mprintf_double {abc: (%*.*e) :xyz} 1 1 1.0
   534    549   } {abc: (1.0e+00) :xyz}
   535    550   do_test printf-2.1.3.3 {
................................................................................
  3462   3477   } [format {%d %d A String: (%s)} 1 2 {This is the string}]
  3463   3478   do_test printf-3.5 {
  3464   3479     sqlite3_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string}
  3465   3480   } [format {%d %d A String: (%30s)} 1 2 {This is the string}]
  3466   3481   do_test printf-3.6 {
  3467   3482     sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string}
  3468   3483   } [format {%d %d A String: (%-30s)} 1 2 {This is the string}]
         3484  +do_test printf-3.7 {
         3485  +  sqlite3_mprintf_str {%d A String: (%*s)} 1 2147483647 {This is the string}
         3486  +} []
         3487  +do_test printf-3.8 {
         3488  +  sqlite3_mprintf_str {%d A String: (%*s)} 1 -2147483648 {This is the string}
         3489  +} {1 A String: (This is the string)}
         3490  +do_test printf-3.9 {
         3491  +  sqlite3_mprintf_str {%d A String: (%.*s)} 1 -2147483648 {This is the string}
         3492  +} {1 A String: (This is the string)}
  3469   3493   do_test snprintf-3.11 {
  3470   3494     sqlite3_snprintf_str 2 {x%d %d %s} 10 10 {This is the string}
  3471   3495   } {x}
  3472   3496   do_test snprintf-3.12 {
  3473   3497     sqlite3_snprintf_str 3 {x%d %d %s} 10 10 {This is the string}
  3474   3498   } {x1}
  3475   3499   do_test snprintf-3.13 {
................................................................................
  3681   3705   } {Inf}
  3682   3706   do_test printf-13.5 {
  3683   3707     sqlite3_mprintf_hexdouble %.20f fff0000000000000
  3684   3708   } {-Inf}
  3685   3709   do_test printf-13.6 {
  3686   3710     sqlite3_mprintf_hexdouble %.20f fff8000000000000
  3687   3711   } {NaN}
         3712  +do_test printf-13.7 {
         3713  +  sqlite3_mprintf_hexdouble %2147483648.10000f 4693b8b5b5056e17
         3714  +} {/100000000000000000000000000000000.00/}
  3688   3715   
  3689   3716   do_test printf-14.1 {
  3690   3717     sqlite3_mprintf_str {abc-%y-123} 0 0 {not used}
  3691   3718   } {abc-}
  3692   3719   do_test printf-14.2 {
  3693   3720     sqlite3_mprintf_n_test {xyzzy}
  3694   3721   } 5

Changes to test/selectA.test.

  1370   1370   do_execsql_test 4.2.2 {
  1371   1371     SELECT c, f(d,c,d,c,d) FROM t7
  1372   1372     UNION ALL
  1373   1373     SELECT a, b FROM t6 
  1374   1374     ORDER BY 1,2
  1375   1375   } {/2 . 3 . 4 . 5 . 6 . 7 ./}
  1376   1376   
         1377  +
         1378  +proc strip_rnd {explain} {
         1379  +  regexp -all {sqlite_sq_[0123456789ABCDEF]*} $explain sqlite_sq
         1380  +}
         1381  +
         1382  +proc do_same_test {tn q1 args} {
         1383  +  set r2 [strip_rnd [db eval "EXPLAIN $q1"]]
         1384  +  set i 1
         1385  +  foreach q $args {
         1386  +    set tst [subst -nocommands {strip_rnd [db eval "EXPLAIN $q"]}]
         1387  +    uplevel do_test $tn.$i [list $tst] [list $r2]
         1388  +    incr i
         1389  +  }
         1390  +}
         1391  +
         1392  +do_execsql_test 5.0 {
         1393  +  CREATE TABLE t8(a, b);
         1394  +  CREATE TABLE t9(c, d);
         1395  +} {}
         1396  +
         1397  +do_same_test 5.1 {
         1398  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY a;
         1399  +} {
         1400  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY t8.a;
         1401  +} {
         1402  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY 1;
         1403  +} {
         1404  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY c;
         1405  +} {
         1406  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY t9.c;
         1407  +}
         1408  +
         1409  +do_same_test 5.2 {
         1410  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY a COLLATE NOCASE
         1411  +} {
         1412  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY t8.a COLLATE NOCASE
         1413  +} {
         1414  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY 1 COLLATE NOCASE
         1415  +} {
         1416  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY c COLLATE NOCASE
         1417  +} {
         1418  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY t9.c COLLATE NOCASE
         1419  +}
         1420  +
         1421  +do_same_test 5.3 {
         1422  +  SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY b, c COLLATE NOCASE
         1423  +} {
         1424  +  SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY 2, 1 COLLATE NOCASE
         1425  +} {
         1426  +  SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY d, a COLLATE NOCASE
         1427  +} {
         1428  +  SELECT a, b FROM t8 EXCEPT SELECT * FROM t9 ORDER BY t9.d, c COLLATE NOCASE
         1429  +} {
         1430  +  SELECT * FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY d, t8.a COLLATE NOCASE
         1431  +}
         1432  +
         1433  +do_catchsql_test 5.4 {
         1434  +  SELECT * FROM t8 UNION SELECT * FROM t9 ORDER BY a+b COLLATE NOCASE
         1435  +} {1 {1st ORDER BY term does not match any column in the result set}}
         1436  +
  1377   1437   
  1378   1438   finish_test

Changes to test/walblock.test.

    11     11   #
    12     12   
    13     13   set testdir [file dirname $argv0]
    14     14   source $testdir/tester.tcl
    15     15   source $testdir/lock_common.tcl
    16     16   source $testdir/wal_common.tcl
    17     17   
           18  +finish_test; return;    #  Feature currently not implemented.
    18     19   ifcapable !wal {finish_test ; return }
    19     20   if {$::tcl_platform(platform)!="unix"} { finish_test ; return }
    20     21   set testprefix walblock
    21     22   
    22     23   catch { db close }
    23     24   testvfs tvfs -fullshm 1
    24     25   foreach f [glob test.db*] { forcedelete $f }
................................................................................
   107    108     after 500 {set ::continue 1}
   108    109     vwait ::continue
   109    110     set ::out 
   110    111   } {1 2 3 4 5 6 7 8 9 10}
   111    112   
   112    113   
   113    114   finish_test
   114         -
   115         -
   116         -
   117         -

Added tool/showlocks.c.

            1  +/*
            2  +** This file implements a simple command-line utility that shows all of the
            3  +** Posix Advisory Locks on a file.
            4  +**
            5  +** Usage:
            6  +**
            7  +**     showlocks FILENAME
            8  +**
            9  +** To compile:  gcc -o showlocks showlocks.c
           10  +*/
           11  +#include <stdio.h>
           12  +#include <unistd.h>
           13  +#include <fcntl.h>
           14  +#include <stdlib.h>
           15  +#include <string.h>
           16  +
           17  +/* This utility only looks for locks in the first 2 billion bytes */
           18  +#define MX_LCK 2147483647
           19  +
           20  +/*
           21  +** Print all locks on the inode of "fd" that occur in between
           22  +** lwr and upr, inclusive.
           23  +*/
           24  +static int showLocksInRange(int fd, off_t lwr, off_t upr){
           25  +  int cnt = 0;
           26  +  struct flock x;
           27  +
           28  +  x.l_type = F_WRLCK;
           29  +  x.l_whence = SEEK_SET;
           30  +  x.l_start = lwr;
           31  +  x.l_len = upr-lwr;
           32  +  fcntl(fd, F_GETLK, &x);
           33  +  if( x.l_type==F_UNLCK ) return 0;
           34  +  printf("start: %-12d len: %-5d pid: %-5d type: %s\n",
           35  +       (int)x.l_start, (int)x.l_len,
           36  +       x.l_pid, x.l_type==F_WRLCK ? "WRLCK" : "RDLCK");
           37  +  cnt++;
           38  +  if( x.l_start>lwr ){
           39  +    cnt += showLocksInRange(fd, lwr, x.l_start-1);
           40  +  }
           41  +  if( x.l_start+x.l_len<upr ){
           42  +    cnt += showLocksInRange(fd, x.l_start+x.l_len+1, upr);
           43  +  }
           44  +  return cnt;
           45  +}
           46  +
           47  +int main(int argc, char **argv){
           48  +  int fd;
           49  +  int cnt;
           50  +
           51  +  if( argc!=2 ){
           52  +    fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
           53  +    return 1;
           54  +  }
           55  +  fd = open(argv[1], O_RDWR, 0);
           56  +  if( fd<0 ){
           57  +    fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]);
           58  +    return 1;
           59  +  }
           60  +  cnt = showLocksInRange(fd, 0, MX_LCK);
           61  +  if( cnt==0 ) printf("no locks\n");  
           62  +  close(fd);
           63  +  return 0;
           64  +}

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  +** Print sketchy documentation for this utility program
          719  +*/
          720  +static void showHelp(void){
          721  +  printf("Usage: %s [options] DB1 DB2\n", g.zArgv0);
          722  +  printf(
          723  +"Output SQL text that would transform DB1 into DB2.\n"
          724  +"Options:\n"
          725  +"  --primarykey          Use schema-defined PRIMARY KEYs\n"
          726  +"  --schema              Show only differences in the schema\n"
          727  +"  --table TAB           Show only differences in table TAB\n"
          728  +  );
          729  +}
          730  +
          731  +int main(int argc, char **argv){
          732  +  const char *zDb1 = 0;
          733  +  const char *zDb2 = 0;
          734  +  int i;
          735  +  int rc;
          736  +  char *zErrMsg = 0;
          737  +  char *zSql;
          738  +  sqlite3_stmt *pStmt;
          739  +  char *zTab = 0;
          740  +
          741  +  g.zArgv0 = argv[0];
          742  +  for(i=1; i<argc; i++){
          743  +    const char *z = argv[i];
          744  +    if( z[0]=='-' ){
          745  +      z++;
          746  +      if( z[0]=='-' ) z++;
          747  +      if( strcmp(z,"debug")==0 ){
          748  +        g.fDebug = strtol(argv[++i], 0, 0);
          749  +      }else
          750  +      if( strcmp(z,"help")==0 ){
          751  +        showHelp();
          752  +        return 0;
          753  +      }else
          754  +      if( strcmp(z,"primarykey")==0 ){
          755  +        g.bSchemaPK = 1;
          756  +      }else
          757  +      if( strcmp(z,"schema")==0 ){
          758  +        g.bSchemaOnly = 1;
          759  +      }else
          760  +      if( strcmp(z,"table")==0 ){
          761  +        zTab = argv[++i];
          762  +      }else
          763  +      {
          764  +        cmdlineError("unknown option: %s", argv[i]);
          765  +      }
          766  +    }else if( zDb1==0 ){
          767  +      zDb1 = argv[i];
          768  +    }else if( zDb2==0 ){
          769  +      zDb2 = argv[i];
          770  +    }else{
          771  +      cmdlineError("unknown argument: %s", argv[i]);
          772  +    }
          773  +  }
          774  +  if( zDb2==0 ){
          775  +    cmdlineError("two database arguments required");
          776  +  }
          777  +  rc = sqlite3_open(zDb1, &g.db);
          778  +  if( rc ){
          779  +    cmdlineError("cannot open database file \"%s\"", zDb1);
          780  +  }
          781  +  rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_master", 0, 0, &zErrMsg);
          782  +  if( rc || zErrMsg ){
          783  +    cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1);
          784  +  }
          785  +  zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2);
          786  +  rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
          787  +  if( rc || zErrMsg ){
          788  +    cmdlineError("cannot attach database \"%s\"", zDb2);
          789  +  }
          790  +  rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg);
          791  +  if( rc || zErrMsg ){
          792  +    cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb2);
          793  +  }
          794  +
          795  +  if( zTab ){
          796  +    diff_one_table(zTab);
          797  +  }else{
          798  +    /* Handle tables one by one */
          799  +    pStmt = db_prepare(
          800  +      "SELECT name FROM main.sqlite_master\n"
          801  +      " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
          802  +      " UNION\n"
          803  +      "SELECT name FROM aux.sqlite_master\n"
          804  +      " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
          805  +      " ORDER BY name"
          806  +    );
          807  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
          808  +      diff_one_table((const char*)sqlite3_column_text(pStmt, 0));
          809  +    }
          810  +    sqlite3_finalize(pStmt);
          811  +  }
          812  +
          813  +  /* TBD: Handle trigger differences */
          814  +  /* TBD: Handle view differences */
          815  +  sqlite3_close(g.db);
          816  +  return 0;
          817  +}