/ Check-in [a9db017e]
Login

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

Overview
Comment:Merge recent trunk changes into the sessions branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1:a9db017eabdefafcda87c497e8bafa07002ac0fe
User & Date: drh 2014-07-24 16:23:51
Context
2014-07-29
12:40
Merge recent trunk changes, and especially the fix for the R-Tree problem described in ticket [d2889096e7bdeac6]. check-in: 8f1beead user: drh tags: sessions
2014-07-24
16:23
Merge recent trunk changes into the sessions branch. check-in: a9db017e user: drh tags: sessions
12:39
Add the readfile(FILENAME) and writefile(FILENAME,CONTENT) SQL functions to the command-line shell. check-in: fb1048cb user: drh tags: trunk
2014-06-30
20:02
Merge the latest trunk enhancements into the sessions branch. check-in: a5d94eab user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

  1006   1006   	rm -f lemon$(BEXE) lempar.c parse.* sqlite*.tar.gz
  1007   1007   	rm -f mkkeywordhash$(BEXE) keywordhash.h
  1008   1008   	rm -f *.da *.bb *.bbg gmon.out
  1009   1009   	rm -rf quota2a quota2b quota2c
  1010   1010   	rm -rf tsrc .target_source
  1011   1011   	rm -f tclsqlite3$(TEXE)
  1012   1012   	rm -f testfixture$(TEXE) test.db
         1013  +	rm -f LogEst$(TEXE) fts3view$(TEXE) rollback-test$(TEXE) showdb$(TEXE)
         1014  +	rm -f showjournal$(TEXE) showstat4$(TEXE) showwal$(TEXE) speedtest1$(TEXE)
         1015  +	rm -f wordcount$(TEXE)
  1013   1016   	rm -f sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
  1014   1017   	rm -f sqlite3.c
  1015   1018   	rm -f sqlite3rc.h
  1016   1019   	rm -f shell.c sqlite3ext.h
  1017   1020   	rm -f sqlite3_analyzer$(TEXE) sqlite3_analyzer.c
  1018   1021   	rm -f sqlite-*-output.vsix
  1019   1022   	rm -f mptester mptester.exe

Changes to Makefile.msc.

  1471   1471   showjournal.exe:	$(TOP)\tool\showjournal.c $(SQLITE3C)
  1472   1472   	$(LTLINK) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
  1473   1473   		$(TOP)\tool\showjournal.c $(SQLITE3C)
  1474   1474   
  1475   1475   showwal.exe:	$(TOP)\tool\showwal.c $(SQLITE3C)
  1476   1476   	$(LTLINK) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
  1477   1477   		$(TOP)\tool\showwal.c $(SQLITE3C)
         1478  +
         1479  +fts3view.exe:	$(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C)
         1480  +	$(LTLINK) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         1481  +		$(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C)
  1478   1482   
  1479   1483   rollback-test.exe:	$(TOP)\tool\rollback-test.c $(SQLITE3C)
  1480   1484   	$(LTLINK) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
  1481   1485   		$(TOP)\tool\rollback-test.c $(SQLITE3C)
  1482   1486   
  1483   1487   LogEst.exe:	$(TOP)\tool\logest.c sqlite3.h
  1484   1488   	$(LTLINK) -Fe$@ $(TOP)\tool\LogEst.c
................................................................................
  1504   1508   	-rmdir /Q/S quota2b
  1505   1509   	-rmdir /Q/S quota2c
  1506   1510   	-rmdir /Q/S tsrc
  1507   1511   	del /Q .target_source
  1508   1512   	del /Q tclsqlite3.exe tclsqlite3.exp
  1509   1513   	del /Q testloadext.dll testloadext.exp
  1510   1514   	del /Q testfixture.exe testfixture.exp test.db
         1515  +	del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe
         1516  +	del /Q showjournal.exe showstat4.exe showwal.exe speedtest1.exe
         1517  +	del /Q wordcount.exe
  1511   1518   	del /Q sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
  1512   1519   	del /Q sqlite3.c sqlite3-*.c
  1513   1520   	del /Q sqlite3rc.h
  1514   1521   	del /Q shell.c sqlite3ext.h
  1515   1522   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.exp sqlite3_analyzer.c
  1516   1523   	del /Q sqlite-*-output.vsix
  1517   1524   	del /Q mptester.exe

Changes to ext/fts3/fts3.c.

  3746   3746   **
  3747   3747   ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
  3748   3748   ** to by the argument to point to the "simple" tokenizer implementation.
  3749   3749   ** And so on.
  3750   3750   */
  3751   3751   void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  3752   3752   void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  3753         -#ifdef SQLITE_ENABLE_FTS4_UNICODE61
         3753  +#ifndef SQLITE_DISABLE_FTS3_UNICODE
  3754   3754   void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
  3755   3755   #endif
  3756   3756   #ifdef SQLITE_ENABLE_ICU
  3757   3757   void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  3758   3758   #endif
  3759   3759   
  3760   3760   /*
................................................................................
  3764   3764   ** function is called by the sqlite3_extension_init() entry point.
  3765   3765   */
  3766   3766   int sqlite3Fts3Init(sqlite3 *db){
  3767   3767     int rc = SQLITE_OK;
  3768   3768     Fts3Hash *pHash = 0;
  3769   3769     const sqlite3_tokenizer_module *pSimple = 0;
  3770   3770     const sqlite3_tokenizer_module *pPorter = 0;
  3771         -#ifdef SQLITE_ENABLE_FTS4_UNICODE61
         3771  +#ifndef SQLITE_DISABLE_FTS3_UNICODE
  3772   3772     const sqlite3_tokenizer_module *pUnicode = 0;
  3773   3773   #endif
  3774   3774   
  3775   3775   #ifdef SQLITE_ENABLE_ICU
  3776   3776     const sqlite3_tokenizer_module *pIcu = 0;
  3777   3777     sqlite3Fts3IcuTokenizerModule(&pIcu);
  3778   3778   #endif
  3779   3779   
  3780         -#ifdef SQLITE_ENABLE_FTS4_UNICODE61
         3780  +#ifndef SQLITE_DISABLE_FTS3_UNICODE
  3781   3781     sqlite3Fts3UnicodeTokenizer(&pUnicode);
  3782   3782   #endif
  3783   3783   
  3784   3784   #ifdef SQLITE_TEST
  3785   3785     rc = sqlite3Fts3InitTerm(db);
  3786   3786     if( rc!=SQLITE_OK ) return rc;
  3787   3787   #endif
................................................................................
  3801   3801     }
  3802   3802   
  3803   3803     /* Load the built-in tokenizers into the hash table */
  3804   3804     if( rc==SQLITE_OK ){
  3805   3805       if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
  3806   3806        || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) 
  3807   3807   
  3808         -#ifdef SQLITE_ENABLE_FTS4_UNICODE61
         3808  +#ifndef SQLITE_DISABLE_FTS3_UNICODE
  3809   3809        || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode) 
  3810   3810   #endif
  3811   3811   #ifdef SQLITE_ENABLE_ICU
  3812   3812        || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
  3813   3813   #endif
  3814   3814       ){
  3815   3815         rc = SQLITE_NOMEM;

Changes to ext/fts3/fts3Int.h.

   581    581   int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
   582    582   int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
   583    583   
   584    584   /* fts3_tokenize_vtab.c */
   585    585   int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
   586    586   
   587    587   /* fts3_unicode2.c (functions generated by parsing unicode text files) */
   588         -#ifdef SQLITE_ENABLE_FTS4_UNICODE61
          588  +#ifndef SQLITE_DISABLE_FTS3_UNICODE
   589    589   int sqlite3FtsUnicodeFold(int, int);
   590    590   int sqlite3FtsUnicodeIsalnum(int);
   591    591   int sqlite3FtsUnicodeIsdiacritic(int);
   592    592   #endif
   593    593   
   594    594   #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
   595    595   #endif /* _FTSINT_H */

Changes to ext/fts3/fts3_unicode.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13     13   ** Implementation of the "unicode" full-text-search tokenizer.
    14     14   */
    15     15   
    16         -#ifdef SQLITE_ENABLE_FTS4_UNICODE61
           16  +#ifndef SQLITE_DISABLE_FTS3_UNICODE
    17     17   
    18     18   #include "fts3Int.h"
    19     19   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
    20     20   
    21     21   #include <assert.h>
    22     22   #include <stdlib.h>
    23     23   #include <stdio.h>
................................................................................
   227    227     pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
   228    228     if( pNew==NULL ) return SQLITE_NOMEM;
   229    229     memset(pNew, 0, sizeof(unicode_tokenizer));
   230    230     pNew->bRemoveDiacritic = 1;
   231    231   
   232    232     for(i=0; rc==SQLITE_OK && i<nArg; i++){
   233    233       const char *z = azArg[i];
   234         -    int n = strlen(z);
          234  +    int n = (int)strlen(z);
   235    235   
   236    236       if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
   237    237         pNew->bRemoveDiacritic = 1;
   238    238       }
   239    239       else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
   240    240         pNew->bRemoveDiacritic = 0;
   241    241       }
................................................................................
   359    359       if( z>=zTerm ) break;
   360    360       READ_UTF8(z, zTerm, iCode);
   361    361     }while( unicodeIsAlnum(p, iCode) 
   362    362          || sqlite3FtsUnicodeIsdiacritic(iCode)
   363    363     );
   364    364   
   365    365     /* Set the output variables and return. */
   366         -  pCsr->iOff = (z - pCsr->aInput);
          366  +  pCsr->iOff = (int)(z - pCsr->aInput);
   367    367     *paToken = pCsr->zToken;
   368         -  *pnToken = zOut - pCsr->zToken;
   369         -  *piStart = (zStart - pCsr->aInput);
   370         -  *piEnd = (zEnd - pCsr->aInput);
          368  +  *pnToken = (int)(zOut - pCsr->zToken);
          369  +  *piStart = (int)(zStart - pCsr->aInput);
          370  +  *piEnd = (int)(zEnd - pCsr->aInput);
   371    371     *piPos = pCsr->iToken++;
   372    372     return SQLITE_OK;
   373    373   }
   374    374   
   375    375   /*
   376    376   ** Set *ppModule to a pointer to the sqlite3_tokenizer_module 
   377    377   ** structure for the unicode tokenizer.
................................................................................
   386    386       unicodeNext,
   387    387       0,
   388    388     };
   389    389     *ppModule = &module;
   390    390   }
   391    391   
   392    392   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
   393         -#endif /* ifndef SQLITE_ENABLE_FTS4_UNICODE61 */
          393  +#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */

Changes to ext/fts3/fts3_unicode2.c.

    11     11   ******************************************************************************
    12     12   */
    13     13   
    14     14   /*
    15     15   ** DO NOT EDIT THIS MACHINE GENERATED FILE.
    16     16   */
    17     17   
    18         -#if defined(SQLITE_ENABLE_FTS4_UNICODE61)
           18  +#ifndef SQLITE_DISABLE_FTS3_UNICODE
    19     19   #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
    20     20   
    21     21   #include <assert.h>
    22     22   
    23     23   /*
    24     24   ** Return true if the argument corresponds to a unicode codepoint
    25     25   ** classified as either a letter or a number. Otherwise false.
................................................................................
   358    358     else if( c>=66560 && c<66600 ){
   359    359       ret = c + 40;
   360    360     }
   361    361   
   362    362     return ret;
   363    363   }
   364    364   #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
   365         -#endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */
          365  +#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */

Changes to ext/fts3/tool/fts3view.c.

   372    372     n = 0;
   373    373     while( sqlite3_step(pStmt)==SQLITE_ROW ){
   374    374       n = sqlite3_column_int(pStmt, 0);
   375    375     }
   376    376     sqlite3_finalize(pStmt);
   377    377     nLeaf = nSeg - nIdx;
   378    378     printf("Leaf segments larger than %5d bytes.... %9d   %5.2f%%\n",
   379         -         pgsz-45, n, n*100.0/nLeaf);
          379  +         pgsz-45, n, nLeaf>0 ? n*100.0/nLeaf : 0.0);
   380    380   
   381    381     pStmt = prepare(db, "SELECT max(level%%1024) FROM '%q_segdir'", zTab);
   382    382     mxLevel = 0;
   383    383     while( sqlite3_step(pStmt)==SQLITE_ROW ){
   384    384       mxLevel = sqlite3_column_int(pStmt, 0);
   385    385     }
   386    386     sqlite3_finalize(pStmt);
................................................................................
   550    550   ){
   551    551     sqlite3_int64 iChild;
   552    552     sqlite3_int64 iPrefix;
   553    553     sqlite3_int64 nTerm;
   554    554     sqlite3_int64 n;
   555    555     sqlite3_int64 iDocsz;
   556    556     int iHeight;
   557         -  int i = 0;
          557  +  sqlite3_int64 i = 0;
   558    558     int cnt = 0;
   559    559     char zTerm[1000];
   560    560   
   561    561     i += getVarint(aData, &n);
   562    562     iHeight = (int)n;
   563    563     printf("height: %d\n", iHeight);
   564    564     if( iHeight>0 ){
................................................................................
   572    572         iPrefix = 0;
   573    573       }
   574    574       i += getVarint(aData+i, &nTerm);
   575    575       if( iPrefix+nTerm+1 >= sizeof(zTerm) ){
   576    576         fprintf(stderr, "term to long\n");
   577    577         exit(1);
   578    578       }
   579         -    memcpy(zTerm+iPrefix, aData+i, nTerm);
          579  +    memcpy(zTerm+iPrefix, aData+i, (size_t)nTerm);
   580    580       zTerm[iPrefix+nTerm] = 0;
   581    581       i += nTerm;
   582    582       if( iHeight==0 ){
   583    583         i += getVarint(aData+i, &iDocsz);
   584         -      printf("term: %-25s doclist %7lld bytes offset %d\n", zTerm, iDocsz, i);
          584  +      printf("term: %-25s doclist %7lld bytes offset %lld\n", zTerm, iDocsz, i);
   585    585         i += iDocsz;
   586    586       }else{
   587    587         printf("term: %-25s child %lld\n", zTerm, ++iChild);
   588    588       }
   589    589     }
   590    590   }
   591    591     
................................................................................
   745    745   ** is azExtra[2].
   746    746   **
   747    747   ** If the --raw option is present in azExtra, then a hex dump is provided.
   748    748   ** Otherwise a decoding is shown.
   749    749   */
   750    750   static void showDoclist(sqlite3 *db, const char *zTab){
   751    751     const unsigned char *aData;
   752         -  sqlite3_int64 offset, nData;
          752  +  sqlite3_int64 offset;
          753  +  int nData;
   753    754     sqlite3_stmt *pStmt;
   754    755   
   755    756     offset = atoi64(azExtra[1]);
   756         -  nData = atoi64(azExtra[2]);
          757  +  nData = atoi(azExtra[2]);
   757    758     pStmt = prepareToGetSegment(db, zTab, azExtra[0]);
   758    759     if( sqlite3_step(pStmt)!=SQLITE_ROW ){
   759    760       sqlite3_finalize(pStmt);
   760    761       return;
   761    762     }
   762    763     aData = sqlite3_column_blob(pStmt, 0);
   763         -  printf("Doclist at %s offset %lld of size %lld bytes:\n",
          764  +  printf("Doclist at %s offset %lld of size %d bytes:\n",
   764    765            azExtra[0], offset, nData);
   765    766     if( findOption("raw", 0, 0)!=0 ){
   766    767       printBlob(aData+offset, nData);
   767    768     }else{
   768    769       decodeDoclist(aData+offset, nData);
   769    770     }
   770    771     sqlite3_finalize(pStmt);

Changes to ext/misc/spellfix.c.

  1889   1889     sqlite3 *db,
  1890   1890     void *pAux,
  1891   1891     int argc, const char *const*argv,
  1892   1892     sqlite3_vtab **ppVTab,
  1893   1893     char **pzErr
  1894   1894   ){
  1895   1895     spellfix1_vtab *pNew = 0;
  1896         -  const char *zModule = argv[0];
         1896  +  /* const char *zModule = argv[0]; // not used */
  1897   1897     const char *zDbName = argv[1];
  1898   1898     const char *zTableName = argv[2];
  1899   1899     int nDbName;
  1900   1900     int rc = SQLITE_OK;
  1901   1901     int i;
  1902   1902   
  1903   1903     nDbName = (int)strlen(zDbName);
................................................................................
  1943   1943            "  k2 TEXT\n"
  1944   1944            ");\n",
  1945   1945            zDbName, zTableName
  1946   1946         );
  1947   1947         spellfix1DbExec(&rc, db,
  1948   1948            "CREATE INDEX IF NOT EXISTS \"%w\".\"%w_vocab_index_langid_k2\" "
  1949   1949               "ON \"%w_vocab\"(langid,k2);",
  1950         -         zDbName, zModule, zTableName
         1950  +         zDbName, zTableName, zTableName
  1951   1951         );
  1952   1952       }
  1953   1953       for(i=3; rc==SQLITE_OK && i<argc; i++){
  1954   1954         if( strncmp(argv[i],"edit_cost_table=",16)==0 && pNew->zCostTable==0 ){
  1955   1955           pNew->zCostTable = spellfix1Dequote(&argv[i][16]);
  1956   1956           if( pNew->zCostTable==0 ) rc = SQLITE_NOMEM;
  1957   1957           continue;

Changes to main.mk.

   650    650   showjournal$(EXE):	$(TOP)/tool/showjournal.c sqlite3.o
   651    651   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showjournal$(EXE) \
   652    652   		$(TOP)/tool/showjournal.c sqlite3.o $(THREADLIB)
   653    653   
   654    654   showwal$(EXE):	$(TOP)/tool/showwal.c sqlite3.o
   655    655   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showwal$(EXE) \
   656    656   		$(TOP)/tool/showwal.c sqlite3.o $(THREADLIB)
          657  +
          658  +fts3view$(EXE):	$(TOP)/ext/fts3/tool/fts3view.c sqlite3.o
          659  +	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o fts3view$(EXE) \
          660  +		$(TOP)/ext/fts3/tool/fts3view.c sqlite3.o $(THREADLIB)
   657    661   
   658    662   rollback-test$(EXE):	$(TOP)/tool/rollback-test.c sqlite3.o
   659    663   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o rollback-test$(EXE) \
   660    664   		$(TOP)/tool/rollback-test.c sqlite3.o $(THREADLIB)
   661    665   
   662    666   LogEst$(EXE):	$(TOP)/tool/logest.c sqlite3.h
   663    667   	$(TCC) -o LogEst$(EXE) $(TOP)/tool/logest.c
................................................................................
   698    702   	rm -rf quota2a quota2b quota2c
   699    703   	rm -rf tsrc target_source
   700    704   	rm -f testloadext.dll libtestloadext.so
   701    705   	rm -f amalgamation-testfixture amalgamation-testfixture.exe
   702    706   	rm -f fts3-testfixture fts3-testfixture.exe
   703    707   	rm -f testfixture testfixture.exe
   704    708   	rm -f threadtest3 threadtest3.exe
          709  +	rm -f LogEst LogEst.exe
          710  +	rm -f fts3view fts3view.exe
          711  +	rm -f rollback-test rollback-test.exe
          712  +	rm -f showdb showdb.exe
          713  +	rm -f showjournal showjournal.exe
          714  +	rm -f showstat4 showstat4.exe
          715  +	rm -f showwal showwal.exe
          716  +	rm -f speedtest1 speedtest1.exe
          717  +	rm -f wordcount wordcount.exe
   705    718   	rm -f sqlite3.c sqlite3-*.c fts?amal.c tclsqlite3.c
   706    719   	rm -f sqlite3rc.h
   707    720   	rm -f shell.c sqlite3ext.h
   708    721   	rm -f sqlite3_analyzer sqlite3_analyzer.exe sqlite3_analyzer.c
   709    722   	rm -f sqlite-*-output.vsix
   710    723   	rm -f mptester mptester.exe
   711         -	rm -f showdb

Changes to src/analyze.c.

  1105   1105       **   regChng = 1
  1106   1106       **   if( idx(1) != regPrev(1) ) goto chng_addr_1
  1107   1107       **   ...
  1108   1108       **   regChng = N
  1109   1109       **   goto chng_addr_N
  1110   1110       */
  1111   1111       addrNextRow = sqlite3VdbeCurrentAddr(v);
  1112         -    for(i=0; i<nCol; i++){
         1112  +    for(i=0; i<nCol-1; i++){
  1113   1113         char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
  1114   1114         sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
  1115   1115         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
  1116   1116         aGotoChng[i] = 
  1117   1117         sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
  1118   1118         sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
  1119   1119         VdbeCoverage(v);
  1120   1120       }
  1121         -    sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
         1121  +    sqlite3VdbeAddOp2(v, OP_Integer, nCol-1, regChng);
  1122   1122       aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
  1123   1123   
  1124   1124       /*
  1125   1125       **  chng_addr_0:
  1126   1126       **   regPrev(0) = idx(0)
  1127   1127       **  chng_addr_1:
  1128   1128       **   regPrev(1) = idx(1)
  1129   1129       **  ...
  1130   1130       */
  1131   1131       sqlite3VdbeJumpHere(v, addrGotoChng0);
  1132         -    for(i=0; i<nCol; i++){
         1132  +    for(i=0; i<nCol-1; i++){
  1133   1133         sqlite3VdbeJumpHere(v, aGotoChng[i]);
  1134   1134         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
  1135   1135       }
  1136   1136   
  1137   1137       /*
  1138   1138       **  chng_addr_N:
  1139   1139       **   regRowid = idx(rowid)            // STAT34 only
................................................................................
  1316   1316     sqlite3 *db = pParse->db;
  1317   1317     int iDb;
  1318   1318     int i;
  1319   1319     char *z, *zDb;
  1320   1320     Table *pTab;
  1321   1321     Index *pIdx;
  1322   1322     Token *pTableName;
         1323  +  Vdbe *v;
  1323   1324   
  1324   1325     /* Read the database schema. If an error occurs, leave an error message
  1325   1326     ** and code in pParse and return NULL. */
  1326   1327     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
  1327   1328     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  1328   1329       return;
  1329   1330     }
................................................................................
  1363   1364           }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
  1364   1365             analyzeTable(pParse, pTab, 0);
  1365   1366           }
  1366   1367           sqlite3DbFree(db, z);
  1367   1368         }
  1368   1369       }   
  1369   1370     }
         1371  +  v = sqlite3GetVdbe(pParse);
         1372  +  if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
  1370   1373   }
  1371   1374   
  1372   1375   /*
  1373   1376   ** Used to pass information from the analyzer reader through to the
  1374   1377   ** callback routine.
  1375   1378   */
  1376   1379   typedef struct analysisInfo analysisInfo;
................................................................................
  1421   1424       if( *z==' ' ) z++;
  1422   1425     }
  1423   1426   #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
  1424   1427     assert( pIndex!=0 );
  1425   1428   #else
  1426   1429     if( pIndex )
  1427   1430   #endif
  1428         -  {
  1429         -    if( strcmp(z, "unordered")==0 ){
         1431  +  while( z[0] ){
         1432  +    if( sqlite3_strglob("unordered*", z)==0 ){
  1430   1433         pIndex->bUnordered = 1;
  1431   1434       }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
  1432         -      int v32 = 0;
  1433         -      sqlite3GetInt32(z+3, &v32);
  1434         -      pIndex->szIdxRow = sqlite3LogEst(v32);
         1435  +      pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
         1436  +    }
         1437  +#ifdef SQLITE_ENABLE_COSTMULT
         1438  +    else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
         1439  +      pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
  1435   1440       }
         1441  +#endif
         1442  +    while( z[0]!=0 && z[0]!=' ' ) z++;
         1443  +    while( z[0]==' ' ) z++;
  1436   1444     }
  1437   1445   }
  1438   1446   
  1439   1447   /*
  1440   1448   ** This callback is invoked once for each index when reading the
  1441   1449   ** sqlite_stat1 table.  
  1442   1450   **
................................................................................
  1469   1477       pIndex = sqlite3PrimaryKeyIndex(pTable);
  1470   1478     }else{
  1471   1479       pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
  1472   1480     }
  1473   1481     z = argv[2];
  1474   1482   
  1475   1483     if( pIndex ){
         1484  +    pIndex->bUnordered = 0;
  1476   1485       decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex);
  1477   1486       if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
  1478   1487     }else{
  1479   1488       Index fakeIdx;
  1480   1489       fakeIdx.szIdxRow = pTable->szTabRow;
         1490  +#ifdef SQLITE_ENABLE_COSTMULT
         1491  +    fakeIdx.pTable = pTable;
         1492  +#endif
  1481   1493       decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
  1482   1494       pTable->szTabRow = fakeIdx.szIdxRow;
  1483   1495     }
  1484   1496   
  1485   1497     return 0;
  1486   1498   }
  1487   1499   
................................................................................
  1515   1527   ** stored in pIdx->aSample[]. 
  1516   1528   */
  1517   1529   static void initAvgEq(Index *pIdx){
  1518   1530     if( pIdx ){
  1519   1531       IndexSample *aSample = pIdx->aSample;
  1520   1532       IndexSample *pFinal = &aSample[pIdx->nSample-1];
  1521   1533       int iCol;
  1522         -    for(iCol=0; iCol<pIdx->nKeyCol; iCol++){
         1534  +    int nCol = 1;
         1535  +    if( pIdx->nSampleCol>1 ){
         1536  +      /* If this is stat4 data, then calculate aAvgEq[] values for all
         1537  +      ** sample columns except the last. The last is always set to 1, as
         1538  +      ** once the trailing PK fields are considered all index keys are
         1539  +      ** unique.  */
         1540  +      nCol = pIdx->nSampleCol-1;
         1541  +      pIdx->aAvgEq[nCol] = 1;
         1542  +    }
         1543  +    for(iCol=0; iCol<nCol; iCol++){
  1523   1544         int i;                    /* Used to iterate through samples */
  1524   1545         tRowcnt sumEq = 0;        /* Sum of the nEq values */
  1525   1546         tRowcnt nSum = 0;         /* Number of terms contributing to sumEq */
  1526   1547         tRowcnt avgEq = 0;
  1527   1548         tRowcnt nDLt = pFinal->anDLt[iCol];
  1528   1549   
  1529   1550         /* Set nSum to the number of distinct (iCol+1) field prefixes that
................................................................................
  1538   1559           }
  1539   1560         }
  1540   1561         if( nDLt>nSum ){
  1541   1562           avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
  1542   1563         }
  1543   1564         if( avgEq==0 ) avgEq = 1;
  1544   1565         pIdx->aAvgEq[iCol] = avgEq;
  1545         -      if( pIdx->nSampleCol==1 ) break;
  1546   1566       }
  1547   1567     }
  1548   1568   }
  1549   1569   
  1550   1570   /*
  1551   1571   ** Look up an index by name.  Or, if the name of a WITHOUT ROWID table
  1552   1572   ** is supplied instead, find the PRIMARY KEY index for that table.
................................................................................
  1597   1617     }
  1598   1618     rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
  1599   1619     sqlite3DbFree(db, zSql);
  1600   1620     if( rc ) return rc;
  1601   1621   
  1602   1622     while( sqlite3_step(pStmt)==SQLITE_ROW ){
  1603   1623       int nIdxCol = 1;              /* Number of columns in stat4 records */
  1604         -    int nAvgCol = 1;              /* Number of entries in Index.aAvgEq */
  1605   1624   
  1606   1625       char *zIndex;   /* Index name */
  1607   1626       Index *pIdx;    /* Pointer to the index object */
  1608   1627       int nSample;    /* Number of samples */
  1609   1628       int nByte;      /* Bytes of space required */
  1610   1629       int i;          /* Bytes of space required */
  1611   1630       tRowcnt *pSpace;
................................................................................
  1615   1634       nSample = sqlite3_column_int(pStmt, 1);
  1616   1635       pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
  1617   1636       assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
  1618   1637       /* Index.nSample is non-zero at this point if data has already been
  1619   1638       ** loaded from the stat4 table. In this case ignore stat3 data.  */
  1620   1639       if( pIdx==0 || pIdx->nSample ) continue;
  1621   1640       if( bStat3==0 ){
  1622         -      nIdxCol = pIdx->nKeyCol+1;
  1623         -      nAvgCol = pIdx->nKeyCol;
         1641  +      assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
         1642  +      if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
         1643  +        nIdxCol = pIdx->nKeyCol;
         1644  +      }else{
         1645  +        nIdxCol = pIdx->nColumn;
         1646  +      }
  1624   1647       }
  1625   1648       pIdx->nSampleCol = nIdxCol;
  1626   1649       nByte = sizeof(IndexSample) * nSample;
  1627   1650       nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
  1628         -    nByte += nAvgCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
         1651  +    nByte += nIdxCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
  1629   1652   
  1630   1653       pIdx->aSample = sqlite3DbMallocZero(db, nByte);
  1631   1654       if( pIdx->aSample==0 ){
  1632   1655         sqlite3_finalize(pStmt);
  1633   1656         return SQLITE_NOMEM;
  1634   1657       }
  1635   1658       pSpace = (tRowcnt*)&pIdx->aSample[nSample];
  1636         -    pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
         1659  +    pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
  1637   1660       for(i=0; i<nSample; i++){
  1638   1661         pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
  1639   1662         pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
  1640   1663         pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
  1641   1664       }
  1642   1665       assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
  1643   1666     }

Changes to src/build.c.

   109    109                         p->zName, P4_STATIC);
   110    110     }
   111    111   }
   112    112   #else
   113    113     #define codeTableLocks(x)
   114    114   #endif
   115    115   
          116  +/*
          117  +** Return TRUE if the given yDbMask object is empty - if it contains no
          118  +** 1 bits.  This routine is used by the DbMaskAllZero() and DbMaskNotZero()
          119  +** macros when SQLITE_MAX_ATTACHED is greater than 30.
          120  +*/
          121  +#if SQLITE_MAX_ATTACHED>30
          122  +int sqlite3DbMaskAllZero(yDbMask m){
          123  +  int i;
          124  +  for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
          125  +  return 1;
          126  +}
          127  +#endif
          128  +
   116    129   /*
   117    130   ** This routine is called after a single SQL statement has been
   118    131   ** parsed and a VDBE program to execute that statement has been
   119    132   ** prepared.  This routine puts the finishing touches on the
   120    133   ** VDBE program and resets the pParse structure for the next
   121    134   ** parse.
   122    135   **
................................................................................
   145    158   
   146    159       /* The cookie mask contains one bit for each database file open.
   147    160       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
   148    161       ** set for each database that is used.  Generate code to start a
   149    162       ** transaction on each used database and to verify the schema cookie
   150    163       ** on each used database.
   151    164       */
   152         -    if( db->mallocFailed==0 && (pParse->cookieMask || pParse->pConstExpr) ){
   153         -      yDbMask mask;
          165  +    if( db->mallocFailed==0 
          166  +     && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
          167  +    ){
   154    168         int iDb, i;
   155    169         assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
   156    170         sqlite3VdbeJumpHere(v, 0);
   157         -      for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
   158         -        if( (mask & pParse->cookieMask)==0 ) continue;
          171  +      for(iDb=0; iDb<db->nDb; iDb++){
          172  +        if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
   159    173           sqlite3VdbeUsesBtree(v, iDb);
   160    174           sqlite3VdbeAddOp4Int(v,
   161    175             OP_Transaction,                    /* Opcode */
   162    176             iDb,                               /* P1 */
   163         -          (mask & pParse->writeMask)!=0,     /* P2 */
          177  +          DbMaskTest(pParse->writeMask,iDb), /* P2 */
   164    178             pParse->cookieValue[iDb],          /* P3 */
   165    179             db->aDb[iDb].pSchema->iGeneration  /* P4 */
   166    180           );
   167    181           if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
   168    182         }
   169    183   #ifndef SQLITE_OMIT_VIRTUALTABLE
   170    184         for(i=0; i<pParse->nVtabLock; i++){
................................................................................
   212    226     }else{
   213    227       pParse->rc = SQLITE_ERROR;
   214    228     }
   215    229     pParse->nTab = 0;
   216    230     pParse->nMem = 0;
   217    231     pParse->nSet = 0;
   218    232     pParse->nVar = 0;
   219         -  pParse->cookieMask = 0;
          233  +  DbMaskZero(pParse->cookieMask);
   220    234   }
   221    235   
   222    236   /*
   223    237   ** Run the parser and code generator recursively in order to generate
   224    238   ** code for the SQL statement given onto the end of the pParse context
   225    239   ** currently under construction.  When the parser is run recursively
   226    240   ** this way, the final OP_Halt is not appended and other initialization
................................................................................
  3839   3853   ** for database iDb.  The code to actually verify the schema cookie
  3840   3854   ** will occur at the end of the top-level VDBE and will be generated
  3841   3855   ** later, by sqlite3FinishCoding().
  3842   3856   */
  3843   3857   void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
  3844   3858     Parse *pToplevel = sqlite3ParseToplevel(pParse);
  3845   3859     sqlite3 *db = pToplevel->db;
  3846         -  yDbMask mask;
  3847   3860   
  3848   3861     assert( iDb>=0 && iDb<db->nDb );
  3849   3862     assert( db->aDb[iDb].pBt!=0 || iDb==1 );
  3850   3863     assert( iDb<SQLITE_MAX_ATTACHED+2 );
  3851   3864     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  3852         -  mask = ((yDbMask)1)<<iDb;
  3853         -  if( (pToplevel->cookieMask & mask)==0 ){
  3854         -    pToplevel->cookieMask |= mask;
         3865  +  if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
         3866  +    DbMaskSet(pToplevel->cookieMask, iDb);
  3855   3867       pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
  3856   3868       if( !OMIT_TEMPDB && iDb==1 ){
  3857   3869         sqlite3OpenTempDatabase(pToplevel);
  3858   3870       }
  3859   3871     }
  3860   3872   }
  3861   3873   
................................................................................
  3886   3898   ** rollback the whole transaction.  For operations where all constraints
  3887   3899   ** can be checked before any changes are made to the database, it is never
  3888   3900   ** necessary to undo a write and the checkpoint should not be set.
  3889   3901   */
  3890   3902   void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
  3891   3903     Parse *pToplevel = sqlite3ParseToplevel(pParse);
  3892   3904     sqlite3CodeVerifySchema(pParse, iDb);
  3893         -  pToplevel->writeMask |= ((yDbMask)1)<<iDb;
         3905  +  DbMaskSet(pToplevel->writeMask, iDb);
  3894   3906     pToplevel->isMultiWrite |= setStatement;
  3895   3907   }
  3896   3908   
  3897   3909   /*
  3898   3910   ** Indicate that the statement currently under construction might write
  3899   3911   ** more than one entry (example: deleting one row then inserting another,
  3900   3912   ** inserting multiple rows in a table, or inserting a row and index entries.)

Changes to src/expr.c.

  2071   2071       if( negFlag ) i = -i;
  2072   2072       sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
  2073   2073     }else{
  2074   2074       int c;
  2075   2075       i64 value;
  2076   2076       const char *z = pExpr->u.zToken;
  2077   2077       assert( z!=0 );
  2078         -    c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
         2078  +    c = sqlite3DecOrHexToI64(z, &value);
  2079   2079       if( c==0 || (c==2 && negFlag) ){
  2080   2080         char *zV;
  2081   2081         if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
  2082   2082         zV = dup8bytes(v, (char*)&value);
  2083   2083         sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
  2084   2084       }else{
  2085   2085   #ifdef SQLITE_OMIT_FLOATING_POINT
  2086   2086         sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
  2087   2087   #else
  2088         -      codeReal(v, z, negFlag, iMem);
         2088  +#ifndef SQLITE_OMIT_HEX_INTEGER
         2089  +      if( sqlite3_strnicmp(z,"0x",2)==0 ){
         2090  +        sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
         2091  +      }else
         2092  +#endif
         2093  +      {
         2094  +        codeReal(v, z, negFlag, iMem);
         2095  +      }
  2089   2096   #endif
  2090   2097       }
  2091   2098     }
  2092   2099   }
  2093   2100   
  2094   2101   /*
  2095   2102   ** Clear a cache entry.

Changes to src/main.c.

  2118   2118   #endif
  2119   2119   #if SQLITE_MAX_VDBE_OP<40
  2120   2120   # error SQLITE_MAX_VDBE_OP must be at least 40
  2121   2121   #endif
  2122   2122   #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
  2123   2123   # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
  2124   2124   #endif
  2125         -#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
  2126         -# error SQLITE_MAX_ATTACHED must be between 0 and 62
         2125  +#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
         2126  +# error SQLITE_MAX_ATTACHED must be between 0 and 125
  2127   2127   #endif
  2128   2128   #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
  2129   2129   # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
  2130   2130   #endif
  2131   2131   #if SQLITE_MAX_COLUMN>32767
  2132   2132   # error SQLITE_MAX_COLUMN must not exceed 32767
  2133   2133   #endif
................................................................................
  3426   3426   sqlite3_int64 sqlite3_uri_int64(
  3427   3427     const char *zFilename,    /* Filename as passed to xOpen */
  3428   3428     const char *zParam,       /* URI parameter sought */
  3429   3429     sqlite3_int64 bDflt       /* return if parameter is missing */
  3430   3430   ){
  3431   3431     const char *z = sqlite3_uri_parameter(zFilename, zParam);
  3432   3432     sqlite3_int64 v;
  3433         -  if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
         3433  +  if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){
  3434   3434       bDflt = v;
  3435   3435     }
  3436   3436     return bDflt;
  3437   3437   }
  3438   3438   
  3439   3439   /*
  3440   3440   ** Return the Btree pointer identified by zDbName.  Return NULL if not found.

Changes to src/pragma.c.

  1044   1044     **
  1045   1045     ** Get or set the size limit on rollback journal files.
  1046   1046     */
  1047   1047     case PragTyp_JOURNAL_SIZE_LIMIT: {
  1048   1048       Pager *pPager = sqlite3BtreePager(pDb->pBt);
  1049   1049       i64 iLimit = -2;
  1050   1050       if( zRight ){
  1051         -      sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
         1051  +      sqlite3DecOrHexToI64(zRight, &iLimit);
  1052   1052         if( iLimit<-1 ) iLimit = -1;
  1053   1053       }
  1054   1054       iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
  1055   1055       returnSingleInt(pParse, "journal_size_limit", iLimit);
  1056   1056       break;
  1057   1057     }
  1058   1058   
................................................................................
  1172   1172     */
  1173   1173     case PragTyp_MMAP_SIZE: {
  1174   1174       sqlite3_int64 sz;
  1175   1175   #if SQLITE_MAX_MMAP_SIZE>0
  1176   1176       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1177   1177       if( zRight ){
  1178   1178         int ii;
  1179         -      sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
         1179  +      sqlite3DecOrHexToI64(zRight, &sz);
  1180   1180         if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
  1181   1181         if( pId2->n==0 ) db->szMmap = sz;
  1182   1182         for(ii=db->nDb-1; ii>=0; ii--){
  1183   1183           if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
  1184   1184             sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
  1185   1185           }
  1186   1186         }
................................................................................
  2215   2215     **   PRAGMA soft_heap_limit = N
  2216   2216     **
  2217   2217     ** Call sqlite3_soft_heap_limit64(N).  Return the result.  If N is omitted,
  2218   2218     ** use -1.
  2219   2219     */
  2220   2220     case PragTyp_SOFT_HEAP_LIMIT: {
  2221   2221       sqlite3_int64 N;
  2222         -    if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
         2222  +    if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
  2223   2223         sqlite3_soft_heap_limit64(N);
  2224   2224       }
  2225   2225       returnSingleInt(pParse, "soft_heap_limit",  sqlite3_soft_heap_limit64(-1));
  2226   2226       break;
  2227   2227     }
  2228   2228   
  2229   2229   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)

Changes to src/shell.c.

    60     60   # define read_history(X)
    61     61   # define write_history(X)
    62     62   # define stifle_history(X)
    63     63   #endif
    64     64   
    65     65   #if defined(_WIN32) || defined(WIN32)
    66     66   # include <io.h>
           67  +# include <fcntl.h>
    67     68   #define isatty(h) _isatty(h)
    68     69   #ifndef access
    69     70   # define access(f,m) _access((f),(m))
    70     71   #endif
    71     72   #undef popen
    72     73   #define popen _popen
    73     74   #undef pclose
................................................................................
   454    455     FILE *traceOut;        /* Output for sqlite3_trace() */
   455    456     int nErr;              /* Number of errors seen */
   456    457     int mode;              /* An output mode setting */
   457    458     int writableSchema;    /* True if PRAGMA writable_schema=ON */
   458    459     int showHeader;        /* True to show column names in List or Column mode */
   459    460     char *zDestTable;      /* Name of destination table when MODE_Insert */
   460    461     char separator[20];    /* Separator character for MODE_List */
          462  +  char newline[20];      /* Record separator in MODE_Csv */
   461    463     int colWidth[100];     /* Requested width of each column when in column mode*/
   462    464     int actualWidth[100];  /* Actual width of each column */
   463    465     char nullvalue[20];    /* The text to print when a NULL comes back from
   464    466                            ** the database */
   465    467     struct previous_mode_data explainPrev;
   466    468                            /* Holds the mode information just before
   467    469                            ** .explain ON */
................................................................................
   655    657     1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
   656    658     1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
   657    659   };
   658    660   
   659    661   /*
   660    662   ** Output a single term of CSV.  Actually, p->separator is used for
   661    663   ** the separator, which may or may not be a comma.  p->nullvalue is
   662         -** the null value.  Strings are quoted if necessary.
          664  +** the null value.  Strings are quoted if necessary.  The separator
          665  +** is only issued if bSep is true.
   663    666   */
   664    667   static void output_csv(struct callback_data *p, const char *z, int bSep){
   665    668     FILE *out = p->out;
   666    669     if( z==0 ){
   667    670       fprintf(out,"%s",p->nullvalue);
   668    671     }else{
   669    672       int i;
................................................................................
   851    854           output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
   852    855           if(i<nArg-1) fprintf(p->out, "%s", p->separator);
   853    856         }
   854    857         fprintf(p->out,"\n");
   855    858         break;
   856    859       }
   857    860       case MODE_Csv: {
          861  +#if defined(WIN32) || defined(_WIN32)
          862  +      fflush(p->out);
          863  +      _setmode(_fileno(p->out), _O_BINARY);
          864  +#endif
   858    865         if( p->cnt++==0 && p->showHeader ){
   859    866           for(i=0; i<nArg; i++){
   860    867             output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
   861    868           }
   862         -        fprintf(p->out,"\n");
          869  +        fprintf(p->out,"%s",p->newline);
   863    870         }
   864         -      if( azArg==0 ) break;
   865         -      for(i=0; i<nArg; i++){
   866         -        output_csv(p, azArg[i], i<nArg-1);
          871  +      if( azArg>0 ){
          872  +        for(i=0; i<nArg; i++){
          873  +          output_csv(p, azArg[i], i<nArg-1);
          874  +        }
          875  +        fprintf(p->out,"%s",p->newline);
   867    876         }
   868         -      fprintf(p->out,"\n");
          877  +#if defined(WIN32) || defined(_WIN32)
          878  +      fflush(p->out);
          879  +      _setmode(_fileno(p->out), _O_TEXT);
          880  +#endif
   869    881         break;
   870    882       }
   871    883       case MODE_Insert: {
   872    884         p->cnt++;
   873    885         if( azArg==0 ) break;
   874    886         fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
   875    887         for(i=0; i<nArg; i++){
................................................................................
  1615   1627     ".quit                  Exit this program\n"
  1616   1628     ".read FILENAME         Execute SQL in FILENAME\n"
  1617   1629     ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
  1618   1630     ".save FILE             Write in-memory database into FILE\n"
  1619   1631     ".schema ?TABLE?        Show the CREATE statements\n"
  1620   1632     "                         If TABLE specified, only show tables matching\n"
  1621   1633     "                         LIKE pattern TABLE.\n"
  1622         -  ".separator STRING      Change separator used by output mode and .import\n"
         1634  +  ".separator STRING ?NL? Change separator used by output mode and .import\n"
         1635  +  "                         NL is the end-of-line mark for CSV\n"
  1623   1636     ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
  1624   1637     ".show                  Show the current values for various settings\n"
  1625   1638     ".stats on|off          Turn stats on or off\n"
  1626   1639     ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
  1627   1640     ".tables ?TABLE?        List names of tables\n"
  1628   1641     "                         If TABLE specified, only list tables matching\n"
  1629   1642     "                         LIKE pattern TABLE.\n"
................................................................................
  1633   1646     ".vfsname ?AUX?         Print the name of the VFS stack\n"
  1634   1647     ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
  1635   1648     "                         Negative values right-justify\n"
  1636   1649   ;
  1637   1650   
  1638   1651   /* Forward reference */
  1639   1652   static int process_input(struct callback_data *p, FILE *in);
         1653  +/*
         1654  +** Implementation of the "readfile(X)" SQL function.  The entire content
         1655  +** of the file named X is read and returned as a BLOB.  NULL is returned
         1656  +** if the file does not exist or is unreadable.
         1657  +*/
         1658  +static void readfileFunc(
         1659  +  sqlite3_context *context,
         1660  +  int argc,
         1661  +  sqlite3_value **argv
         1662  +){
         1663  +  const char *zName;
         1664  +  FILE *in;
         1665  +  long nIn;
         1666  +  void *pBuf;
         1667  +
         1668  +  zName = (const char*)sqlite3_value_text(argv[0]);
         1669  +  if( zName==0 ) return;
         1670  +  in = fopen(zName, "rb");
         1671  +  if( in==0 ) return;
         1672  +  fseek(in, 0, SEEK_END);
         1673  +  nIn = ftell(in);
         1674  +  rewind(in);
         1675  +  pBuf = sqlite3_malloc( nIn );
         1676  +  if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
         1677  +    sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
         1678  +  }else{
         1679  +    sqlite3_free(pBuf);
         1680  +  }
         1681  +  fclose(in);
         1682  +}
         1683  +
         1684  +/*
         1685  +** Implementation of the "writefile(X,Y)" SQL function.  The argument Y
         1686  +** is written into file X.  The number of bytes written is returned.  Or
         1687  +** NULL is returned if something goes wrong, such as being unable to open
         1688  +** file X for writing.
         1689  +*/
         1690  +static void writefileFunc(
         1691  +  sqlite3_context *context,
         1692  +  int argc,
         1693  +  sqlite3_value **argv
         1694  +){
         1695  +  FILE *out;
         1696  +  const char *z;
         1697  +  int n;
         1698  +  sqlite3_int64 rc;
         1699  +  const char *zFile;
         1700  +
         1701  +  zFile = (const char*)sqlite3_value_text(argv[0]);
         1702  +  if( zFile==0 ) return;
         1703  +  out = fopen(zFile, "wb");
         1704  +  if( out==0 ) return;
         1705  +  z = (const char*)sqlite3_value_blob(argv[1]);
         1706  +  if( z==0 ){
         1707  +    n = 0;
         1708  +    rc = 0;
         1709  +  }else{
         1710  +    n = sqlite3_value_bytes(argv[1]);
         1711  +    rc = fwrite(z, 1, n, out);
         1712  +  }
         1713  +  fclose(out);
         1714  +  sqlite3_result_int64(context, rc);
         1715  +}
  1640   1716   
  1641   1717   /*
  1642   1718   ** Make sure the database is open.  If it is not, then open it.  If
  1643   1719   ** the database fails to open, print an error message and exit.
  1644   1720   */
  1645   1721   static void open_db(struct callback_data *p, int keepAlive){
  1646   1722     if( p->db==0 ){
................................................................................
  1656   1732             p->zDbFilename, sqlite3_errmsg(db));
  1657   1733         if( keepAlive ) return;
  1658   1734         exit(1);
  1659   1735       }
  1660   1736   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1661   1737       sqlite3_enable_load_extension(p->db, 1);
  1662   1738   #endif
         1739  +    sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
         1740  +                            readfileFunc, 0, 0);
         1741  +    sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
         1742  +                            writefileFunc, 0, 0);
  1663   1743     }
  1664   1744   }
  1665   1745   
  1666   1746   /*
  1667   1747   ** Do C-language style dequoting.
  1668   1748   **
  1669   1749   **    \t    -> tab
................................................................................
  2412   2492         memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
  2413   2493       }
  2414   2494     }else
  2415   2495   
  2416   2496     if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
  2417   2497       struct callback_data data;
  2418   2498       char *zErrMsg = 0;
         2499  +    int doStats = 0;
  2419   2500       if( nArg!=1 ){
  2420   2501         fprintf(stderr, "Usage: .fullschema\n");
  2421   2502         rc = 1;
  2422   2503         goto meta_command_exit;
  2423   2504       }
  2424   2505       open_db(p, 0);
  2425   2506       memcpy(&data, p, sizeof(data));
................................................................................
  2430   2511          "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
  2431   2512          "     FROM sqlite_master UNION ALL"
  2432   2513          "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
  2433   2514          "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
  2434   2515          "ORDER BY rowid",
  2435   2516          callback, &data, &zErrMsg
  2436   2517       );
  2437         -    sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master;'",
  2438         -                 callback, &data, &zErrMsg);
  2439         -    data.mode = MODE_Insert;
  2440         -    data.zDestTable = "sqlite_stat1";
  2441         -    shell_exec(p->db, "SELECT * FROM sqlite_stat1",
  2442         -               shell_callback, &data,&zErrMsg);
  2443         -    data.zDestTable = "sqlite_stat3";
  2444         -    shell_exec(p->db, "SELECT * FROM sqlite_stat3",
  2445         -               shell_callback, &data,&zErrMsg);
  2446         -    data.zDestTable = "sqlite_stat4";
  2447         -    shell_exec(p->db, "SELECT * FROM sqlite_stat4",
  2448         -               shell_callback, &data, &zErrMsg);
  2449         -    data.mode = MODE_Semi;
  2450         -    shell_exec(p->db, "SELECT 'ANALYZE sqlite_master;'",
  2451         -               shell_callback, &data, &zErrMsg);
         2518  +    if( rc==SQLITE_OK ){
         2519  +      sqlite3_stmt *pStmt;
         2520  +      rc = sqlite3_prepare_v2(p->db,
         2521  +               "SELECT rowid FROM sqlite_master"
         2522  +               " WHERE name GLOB 'sqlite_stat[134]'",
         2523  +               -1, &pStmt, 0);
         2524  +      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
         2525  +      sqlite3_finalize(pStmt);
         2526  +    }
         2527  +    if( doStats==0 ){
         2528  +      fprintf(p->out, "/* No STAT tables available */\n");
         2529  +    }else{
         2530  +      fprintf(p->out, "ANALYZE sqlite_master;\n");
         2531  +      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
         2532  +                   callback, &data, &zErrMsg);
         2533  +      data.mode = MODE_Insert;
         2534  +      data.zDestTable = "sqlite_stat1";
         2535  +      shell_exec(p->db, "SELECT * FROM sqlite_stat1",
         2536  +                 shell_callback, &data,&zErrMsg);
         2537  +      data.zDestTable = "sqlite_stat3";
         2538  +      shell_exec(p->db, "SELECT * FROM sqlite_stat3",
         2539  +                 shell_callback, &data,&zErrMsg);
         2540  +      data.zDestTable = "sqlite_stat4";
         2541  +      shell_exec(p->db, "SELECT * FROM sqlite_stat4",
         2542  +                 shell_callback, &data, &zErrMsg);
         2543  +      fprintf(p->out, "ANALYZE sqlite_master;\n");
         2544  +    }
  2452   2545     }else
  2453   2546   
  2454   2547     if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
  2455   2548       if( nArg==2 ){
  2456   2549         p->showHeader = booleanValue(azArg[1]);
  2457   2550       }else{
  2458   2551         fprintf(stderr, "Usage: .headers on|off\n");
................................................................................
  2734   2827         p->mode = MODE_Html;
  2735   2828       }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
  2736   2829         p->mode = MODE_Tcl;
  2737   2830         sqlite3_snprintf(sizeof(p->separator), p->separator, " ");
  2738   2831       }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
  2739   2832         p->mode = MODE_Csv;
  2740   2833         sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
         2834  +      sqlite3_snprintf(sizeof(p->newline), p->newline, "\r\n");
  2741   2835       }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
  2742   2836         p->mode = MODE_List;
  2743   2837         sqlite3_snprintf(sizeof(p->separator), p->separator, "\t");
  2744   2838       }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
  2745   2839         p->mode = MODE_Insert;
  2746   2840         set_table_name(p, nArg>=3 ? azArg[2] : "table");
  2747   2841       }else {
................................................................................
  3012   3106           fprintf(p->out, "%s", zBuf);
  3013   3107         }
  3014   3108       }
  3015   3109     }else
  3016   3110   #endif
  3017   3111   
  3018   3112     if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
  3019         -    if( nArg==2 ){
  3020         -      sqlite3_snprintf(sizeof(p->separator), p->separator,
  3021         -                       "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
  3022         -    }else{
  3023         -      fprintf(stderr, "Usage: .separator STRING\n");
         3113  +    if( nArg<2 || nArg>3 ){
         3114  +      fprintf(stderr, "Usage: .separator SEPARATOR ?NEWLINE?\n");
  3024   3115         rc = 1;
  3025   3116       }
         3117  +    if( nArg>=2 ){
         3118  +      sqlite3_snprintf(sizeof(p->separator), p->separator, azArg[1]);
         3119  +    }
         3120  +    if( nArg>=3 ){
         3121  +      sqlite3_snprintf(sizeof(p->newline), p->newline, azArg[2]);
         3122  +    }
  3026   3123     }else
  3027   3124   
  3028   3125     if( c=='s'
  3029   3126      && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
  3030   3127     ){
  3031   3128       char *zCmd;
  3032   3129       int i;
................................................................................
  3059   3156       fprintf(p->out,"%9.9s: ", "nullvalue");
  3060   3157         output_c_string(p->out, p->nullvalue);
  3061   3158         fprintf(p->out, "\n");
  3062   3159       fprintf(p->out,"%9.9s: %s\n","output",
  3063   3160               strlen30(p->outfile) ? p->outfile : "stdout");
  3064   3161       fprintf(p->out,"%9.9s: ", "separator");
  3065   3162         output_c_string(p->out, p->separator);
         3163  +      fprintf(p->out," ");
         3164  +      output_c_string(p->out, p->newline);
  3066   3165         fprintf(p->out, "\n");
  3067   3166       fprintf(p->out,"%9.9s: %s\n","stats", p->statsOn ? "on" : "off");
  3068   3167       fprintf(p->out,"%9.9s: ","width");
  3069   3168       for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
  3070   3169         fprintf(p->out,"%d ",p->colWidth[i]);
  3071   3170       }
  3072   3171       fprintf(p->out,"\n");
................................................................................
  3675   3774     "   -interactive         force interactive I/O\n"
  3676   3775     "   -line                set output mode to 'line'\n"
  3677   3776     "   -list                set output mode to 'list'\n"
  3678   3777     "   -mmap N              default mmap size set to N\n"
  3679   3778   #ifdef SQLITE_ENABLE_MULTIPLEX
  3680   3779     "   -multiplex           enable the multiplexor VFS\n"
  3681   3780   #endif
         3781  +  "   -newline SEP         set newline character(s) for CSV\n"
  3682   3782     "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
  3683   3783     "   -separator SEP       set output field separator. Default: '|'\n"
  3684   3784     "   -stats               print memory stats before each finalize\n"
  3685   3785     "   -version             show SQLite version\n"
  3686   3786     "   -vfs NAME            use NAME as the default VFS\n"
  3687   3787   #ifdef SQLITE_ENABLE_VFSTRACE
  3688   3788     "   -vfstrace            enable tracing of all VFS calls\n"
................................................................................
  3704   3804   /*
  3705   3805   ** Initialize the state information in data
  3706   3806   */
  3707   3807   static void main_init(struct callback_data *data) {
  3708   3808     memset(data, 0, sizeof(*data));
  3709   3809     data->mode = MODE_List;
  3710   3810     memcpy(data->separator,"|", 2);
         3811  +  memcpy(data->newline,"\r\n", 3);
  3711   3812     data->showHeader = 0;
  3712   3813     sqlite3_config(SQLITE_CONFIG_URI, 1);
  3713   3814     sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
  3714   3815     sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
  3715   3816     sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
  3716   3817     sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
  3717   3818   }
................................................................................
  3796   3897         fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
  3797   3898         fprintf(stderr,"Use -help for a list of options.\n");
  3798   3899         return 1;
  3799   3900       }
  3800   3901       if( z[1]=='-' ) z++;
  3801   3902       if( strcmp(z,"-separator")==0
  3802   3903        || strcmp(z,"-nullvalue")==0
         3904  +     || strcmp(z,"-newline")==0
  3803   3905        || strcmp(z,"-cmd")==0
  3804   3906       ){
  3805   3907         (void)cmdline_option_value(argc, argv, ++i);
  3806   3908       }else if( strcmp(z,"-init")==0 ){
  3807   3909         zInitFile = cmdline_option_value(argc, argv, ++i);
  3808   3910       }else if( strcmp(z,"-batch")==0 ){
  3809   3911         /* Need to check for batch mode here to so we can avoid printing
................................................................................
  3905   4007         data.mode = MODE_Column;
  3906   4008       }else if( strcmp(z,"-csv")==0 ){
  3907   4009         data.mode = MODE_Csv;
  3908   4010         memcpy(data.separator,",",2);
  3909   4011       }else if( strcmp(z,"-separator")==0 ){
  3910   4012         sqlite3_snprintf(sizeof(data.separator), data.separator,
  3911   4013                          "%s",cmdline_option_value(argc,argv,++i));
         4014  +    }else if( strcmp(z,"-newline")==0 ){
         4015  +      sqlite3_snprintf(sizeof(data.newline), data.newline,
         4016  +                       "%s",cmdline_option_value(argc,argv,++i));
  3912   4017       }else if( strcmp(z,"-nullvalue")==0 ){
  3913   4018         sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
  3914   4019                          "%s",cmdline_option_value(argc,argv,++i));
  3915   4020       }else if( strcmp(z,"-header")==0 ){
  3916   4021         data.showHeader = 1;
  3917   4022       }else if( strcmp(z,"-noheader")==0 ){
  3918   4023         data.showHeader = 0;

Changes to src/sqlite.h.in.

  2028   2028   */
  2029   2029   int sqlite3_complete(const char *sql);
  2030   2030   int sqlite3_complete16(const void *sql);
  2031   2031   
  2032   2032   /*
  2033   2033   ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  2034   2034   **
  2035         -** ^This routine sets a callback function that might be invoked whenever
  2036         -** an attempt is made to open a database table that another thread
  2037         -** or process has locked.
         2035  +** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
         2036  +** that might be invoked with argument P whenever
         2037  +** an attempt is made to access a database table associated with
         2038  +** [database connection] D when another thread
         2039  +** or process has the table locked.
         2040  +** The sqlite3_busy_handler() interface is used to implement
         2041  +** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
  2038   2042   **
  2039   2043   ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
  2040   2044   ** is returned immediately upon encountering the lock.  ^If the busy callback
  2041   2045   ** is not NULL, then the callback might be invoked with two arguments.
  2042   2046   **
  2043   2047   ** ^The first argument to the busy handler is a copy of the void* pointer which
  2044   2048   ** is the third argument to sqlite3_busy_handler().  ^The second argument to
  2045   2049   ** the busy handler callback is the number of times that the busy handler has
  2046         -** been invoked for this locking event.  ^If the
         2050  +** been invoked for the same locking event.  ^If the
  2047   2051   ** busy callback returns 0, then no additional attempts are made to
  2048         -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
         2052  +** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned
         2053  +** to the application.
  2049   2054   ** ^If the callback returns non-zero, then another attempt
  2050         -** is made to open the database for reading and the cycle repeats.
         2055  +** is made to access the database and the cycle repeats.
  2051   2056   **
  2052   2057   ** The presence of a busy handler does not guarantee that it will be invoked
  2053   2058   ** when there is lock contention. ^If SQLite determines that invoking the busy
  2054   2059   ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
  2055         -** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
         2060  +** or [SQLITE_IOERR_BLOCKED] to the application instead of invoking the 
         2061  +** busy handler.
  2056   2062   ** Consider a scenario where one process is holding a read lock that
  2057   2063   ** it is trying to promote to a reserved lock and
  2058   2064   ** a second process is holding a reserved lock that it is trying
  2059   2065   ** to promote to an exclusive lock.  The first process cannot proceed
  2060   2066   ** because it is blocked by the second and the second process cannot
  2061   2067   ** proceed because it is blocked by the first.  If both processes
  2062   2068   ** invoke the busy handlers, neither will make any progress.  Therefore,
................................................................................
  2080   2086   ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
  2081   2087   ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
  2082   2088   ** this is important.
  2083   2089   **
  2084   2090   ** ^(There can only be a single busy handler defined for each
  2085   2091   ** [database connection].  Setting a new busy handler clears any
  2086   2092   ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
  2087         -** will also set or clear the busy handler.
         2093  +** or evaluating [PRAGMA busy_timeout=N] will change the
         2094  +** busy handler and thus clear any previously set busy handler.
  2088   2095   **
  2089   2096   ** The busy callback should not take any actions which modify the
  2090         -** database connection that invoked the busy handler.  Any such actions
         2097  +** database connection that invoked the busy handler.  In other words,
         2098  +** the busy handler is not reentrant.  Any such actions
  2091   2099   ** result in undefined behavior.
  2092   2100   ** 
  2093   2101   ** A busy handler must not close the database connection
  2094   2102   ** or [prepared statement] that invoked the busy handler.
  2095   2103   */
  2096   2104   int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  2097   2105   
................................................................................
  2108   2116   ** ^Calling this routine with an argument less than or equal to zero
  2109   2117   ** turns off all busy handlers.
  2110   2118   **
  2111   2119   ** ^(There can only be a single busy handler for a particular
  2112   2120   ** [database connection] any any given moment.  If another busy handler
  2113   2121   ** was defined  (using [sqlite3_busy_handler()]) prior to calling
  2114   2122   ** this routine, that other busy handler is cleared.)^
         2123  +**
         2124  +** See also:  [PRAGMA busy_timeout]
  2115   2125   */
  2116   2126   int sqlite3_busy_timeout(sqlite3*, int ms);
  2117   2127   
  2118   2128   /*
  2119   2129   ** CAPI3REF: Convenience Routines For Running Queries
  2120   2130   **
  2121   2131   ** This is a legacy interface that is preserved for backwards compatibility.
................................................................................
  4696   4706   **
  4697   4707   ** ^(If this global variable is made to point to a string which is
  4698   4708   ** the name of a folder (a.k.a. directory), then all temporary files
  4699   4709   ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
  4700   4710   ** will be placed in that directory.)^  ^If this variable
  4701   4711   ** is a NULL pointer, then SQLite performs a search for an appropriate
  4702   4712   ** temporary file directory.
         4713  +**
         4714  +** Applications are strongly discouraged from using this global variable.
         4715  +** It is required to set a temporary folder on Windows Runtime (WinRT).
         4716  +** But for all other platforms, it is highly recommended that applications
         4717  +** neither read nor write this variable.  This global variable is a relic
         4718  +** that exists for backwards compatibility of legacy applications and should
         4719  +** be avoided in new projects.
  4703   4720   **
  4704   4721   ** It is not safe to read or modify this variable in more than one
  4705   4722   ** thread at a time.  It is not safe to read or modify this variable
  4706   4723   ** if a [database connection] is being used at the same time in a separate
  4707   4724   ** thread.
  4708   4725   ** It is intended that this variable be set once
  4709   4726   ** as part of process initialization and before any SQLite interface
................................................................................
  4715   4732   ** the [temp_store_directory pragma] always assumes that any string
  4716   4733   ** that this variable points to is held in memory obtained from 
  4717   4734   ** [sqlite3_malloc] and the pragma may attempt to free that memory
  4718   4735   ** using [sqlite3_free].
  4719   4736   ** Hence, if this variable is modified directly, either it should be
  4720   4737   ** made NULL or made to point to memory obtained from [sqlite3_malloc]
  4721   4738   ** or else the use of the [temp_store_directory pragma] should be avoided.
         4739  +** Except when requested by the [temp_store_directory pragma], SQLite
         4740  +** does not free the memory that sqlite3_temp_directory points to.  If
         4741  +** the application wants that memory to be freed, it must do
         4742  +** so itself, taking care to only do so after all [database connection]
         4743  +** objects have been destroyed.
  4722   4744   **
  4723   4745   ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
  4724   4746   ** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
  4725   4747   ** features that require the use of temporary files may fail.  Here is an
  4726   4748   ** example of how to do this using C++ with the Windows Runtime:
  4727   4749   **
  4728   4750   ** <blockquote><pre>
................................................................................
  7134   7156   ** ^The callback registered by this function replaces any existing callback
  7135   7157   ** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
  7136   7158   ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
  7137   7159   ** configured by this function.
  7138   7160   **
  7139   7161   ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
  7140   7162   ** from SQL.
         7163  +**
         7164  +** ^Checkpoints initiated by this mechanism are
         7165  +** [sqlite3_wal_checkpoint_v2|PASSIVE].
  7141   7166   **
  7142   7167   ** ^Every new [database connection] defaults to having the auto-checkpoint
  7143   7168   ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
  7144   7169   ** pages.  The use of this interface
  7145   7170   ** is only necessary if the default setting is found to be suboptimal
  7146   7171   ** for a particular application.
  7147   7172   */
................................................................................
  7151   7176   ** CAPI3REF: Checkpoint a database
  7152   7177   **
  7153   7178   ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
  7154   7179   ** on [database connection] D to be [checkpointed].  ^If X is NULL or an
  7155   7180   ** empty string, then a checkpoint is run on all databases of
  7156   7181   ** connection D.  ^If the database connection D is not in
  7157   7182   ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
         7183  +** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a
         7184  +** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint.
         7185  +** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL
         7186  +** or RESET checkpoint.
  7158   7187   **
  7159   7188   ** ^The [wal_checkpoint pragma] can be used to invoke this interface
  7160   7189   ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
  7161   7190   ** [wal_autocheckpoint pragma] can be used to cause this interface to be
  7162   7191   ** run whenever the WAL reaches a certain size threshold.
  7163   7192   **
  7164   7193   ** See also: [sqlite3_wal_checkpoint_v2()]
................................................................................
  7173   7202   ** eMode parameter:
  7174   7203   **
  7175   7204   ** <dl>
  7176   7205   ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
  7177   7206   **   Checkpoint as many frames as possible without waiting for any database 
  7178   7207   **   readers or writers to finish. Sync the db file if all frames in the log
  7179   7208   **   are checkpointed. This mode is the same as calling 
  7180         -**   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
         7209  +**   sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback]
         7210  +**   is never invoked.
  7181   7211   **
  7182   7212   ** <dt>SQLITE_CHECKPOINT_FULL<dd>
  7183         -**   This mode blocks (calls the busy-handler callback) until there is no
         7213  +**   This mode blocks (it invokes the
         7214  +**   [sqlite3_busy_handler|busy-handler callback]) until there is no
  7184   7215   **   database writer and all readers are reading from the most recent database
  7185   7216   **   snapshot. It then checkpoints all frames in the log file and syncs the
  7186   7217   **   database file. This call blocks database writers while it is running,
  7187   7218   **   but not database readers.
  7188   7219   **
  7189   7220   ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
  7190   7221   **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
  7191         -**   checkpointing the log file it blocks (calls the busy-handler callback)
         7222  +**   checkpointing the log file it blocks (calls the 
         7223  +**   [sqlite3_busy_handler|busy-handler callback])
  7192   7224   **   until all readers are reading from the database file only. This ensures 
  7193   7225   **   that the next client to write to the database file restarts the log file 
  7194   7226   **   from the beginning. This call blocks database writers while it is running,
  7195   7227   **   but not database readers.
  7196   7228   ** </dl>
  7197   7229   **
  7198   7230   ** If pnLog is not NULL, then *pnLog is set to the total number of frames in

Changes to src/sqliteInt.h.

  1481   1481   #endif
  1482   1482     LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
  1483   1483     int tnum;            /* Root BTree node for this table (see note above) */
  1484   1484     i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
  1485   1485     i16 nCol;            /* Number of columns in this table */
  1486   1486     u16 nRef;            /* Number of pointers to this Table */
  1487   1487     LogEst szTabRow;     /* Estimated size of each table row in bytes */
         1488  +#ifdef SQLITE_ENABLE_COSTMULT
         1489  +  LogEst costMult;     /* Cost multiplier for using this table */
         1490  +#endif
  1488   1491     u8 tabFlags;         /* Mask of TF_* values */
  1489   1492     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1490   1493   #ifndef SQLITE_OMIT_ALTERTABLE
  1491   1494     int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
  1492   1495   #endif
  1493   1496   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1494   1497     int nModuleArg;      /* Number of arguments to the module */
................................................................................
  2140   2143   #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
  2141   2144   #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
  2142   2145   #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
  2143   2146   #define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
  2144   2147   #define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
  2145   2148   #define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
  2146   2149   #define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
         2150  +#define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
  2147   2151   
  2148   2152   /* Allowed return values from sqlite3WhereIsDistinct()
  2149   2153   */
  2150   2154   #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
  2151   2155   #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
  2152   2156   #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
  2153   2157   #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
................................................................................
  2396   2400     u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
  2397   2401   };
  2398   2402   
  2399   2403   /*
  2400   2404   ** The yDbMask datatype for the bitmask of all attached databases.
  2401   2405   */
  2402   2406   #if SQLITE_MAX_ATTACHED>30
  2403         -  typedef sqlite3_uint64 yDbMask;
         2407  +  typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
         2408  +# define DbMaskTest(M,I)    (((M)[(I)/8]&(1<<((I)&7)))!=0)
         2409  +# define DbMaskZero(M)      memset((M),0,sizeof(M))
         2410  +# define DbMaskSet(M,I)     (M)[(I)/8]|=(1<<((I)&7))
         2411  +# define DbMaskAllZero(M)   sqlite3DbMaskAllZero(M)
         2412  +# define DbMaskNonZero(M)   (sqlite3DbMaskAllZero(M)==0)
  2404   2413   #else
  2405   2414     typedef unsigned int yDbMask;
         2415  +# define DbMaskTest(M,I)    (((M)&(((yDbMask)1)<<(I)))!=0)
         2416  +# define DbMaskZero(M)      (M)=0
         2417  +# define DbMaskSet(M,I)     (M)|=(((yDbMask)1)<<(I))
         2418  +# define DbMaskAllZero(M)   (M)==0
         2419  +# define DbMaskNonZero(M)   (M)!=0
  2406   2420   #endif
  2407   2421   
  2408   2422   /*
  2409   2423   ** An SQL parser context.  A copy of this structure is passed through
  2410   2424   ** the parser and down into all the parser action routine in order to
  2411   2425   ** carry around information that is global to the entire parse.
  2412   2426   **
................................................................................
  3075   3089   
  3076   3090   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
  3077   3091     int sqlite3ViewGetColumnNames(Parse*,Table*);
  3078   3092   #else
  3079   3093   # define sqlite3ViewGetColumnNames(A,B) 0
  3080   3094   #endif
  3081   3095   
         3096  +#if SQLITE_MAX_ATTACHED>30
         3097  +  int sqlite3DbMaskAllZero(yDbMask);
         3098  +#endif
  3082   3099   void sqlite3DropTable(Parse*, SrcList*, int, int);
  3083   3100   void sqlite3CodeDropTable(Parse*, Table*, int, int);
  3084   3101   void sqlite3DeleteTable(sqlite3*, Table*);
  3085   3102   #ifndef SQLITE_OMIT_AUTOINCREMENT
  3086   3103     void sqlite3AutoincrementBegin(Parse *pParse);
  3087   3104     void sqlite3AutoincrementEnd(Parse *pParse);
  3088   3105   #else
................................................................................
  3325   3342   
  3326   3343   const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
  3327   3344   void sqlite3TableAffinity(Vdbe*, Table*, int);
  3328   3345   char sqlite3CompareAffinity(Expr *pExpr, char aff2);
  3329   3346   int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
  3330   3347   char sqlite3ExprAffinity(Expr *pExpr);
  3331   3348   int sqlite3Atoi64(const char*, i64*, int, u8);
         3349  +int sqlite3DecOrHexToI64(const char*, i64*);
  3332   3350   void sqlite3Error(sqlite3*, int, const char*,...);
  3333   3351   void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  3334   3352   u8 sqlite3HexToInt(int h);
  3335   3353   int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  3336   3354   
  3337   3355   #if defined(SQLITE_TEST) 
  3338   3356   const char *sqlite3ErrName(int);

Changes to src/test_config.c.

   332    332   
   333    333   #ifdef SQLITE_ENABLE_FTS3
   334    334     Tcl_SetVar2(interp, "sqlite_options", "fts3", "1", TCL_GLOBAL_ONLY);
   335    335   #else
   336    336     Tcl_SetVar2(interp, "sqlite_options", "fts3", "0", TCL_GLOBAL_ONLY);
   337    337   #endif
   338    338   
   339         -#if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_ENABLE_FTS4_UNICODE61)
          339  +#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_DISABLE_FTS3_UNICODE)
   340    340     Tcl_SetVar2(interp, "sqlite_options", "fts3_unicode", "1", TCL_GLOBAL_ONLY);
   341    341   #else
   342    342     Tcl_SetVar2(interp, "sqlite_options", "fts3_unicode", "0", TCL_GLOBAL_ONLY);
   343    343   #endif
   344    344   
   345    345   #ifdef SQLITE_DISABLE_FTS4_DEFERRED
   346    346     Tcl_SetVar2(interp, "sqlite_options", "fts4_deferred", "0", TCL_GLOBAL_ONLY);

Changes to src/tokenize.c.

   266    266       case '0': case '1': case '2': case '3': case '4':
   267    267       case '5': case '6': case '7': case '8': case '9': {
   268    268         testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
   269    269         testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
   270    270         testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
   271    271         testcase( z[0]=='9' );
   272    272         *tokenType = TK_INTEGER;
          273  +#ifndef SQLITE_OMIT_HEX_INTEGER
          274  +      if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
          275  +        for(i=3; sqlite3Isxdigit(z[i]); i++){}
          276  +        return i;
          277  +      }
          278  +#endif
   273    279         for(i=0; sqlite3Isdigit(z[i]); i++){}
   274    280   #ifndef SQLITE_OMIT_FLOATING_POINT
   275    281         if( z[i]=='.' ){
   276    282           i++;
   277    283           while( sqlite3Isdigit(z[i]) ){ i++; }
   278    284           *tokenType = TK_FLOAT;
   279    285         }

Changes to src/util.c.

   471    471       testcase( c==(-1) );
   472    472       testcase( c==0 );
   473    473       testcase( c==(+1) );
   474    474     }
   475    475     return c;
   476    476   }
   477    477   
   478         -
   479    478   /*
   480         -** Convert zNum to a 64-bit signed integer.
          479  +** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
          480  +** routine does *not* accept hexadecimal notation.
   481    481   **
   482    482   ** If the zNum value is representable as a 64-bit twos-complement 
   483    483   ** integer, then write that value into *pNum and return 0.
   484    484   **
   485    485   ** If zNum is exactly 9223372036854775808, return 2.  This special
   486    486   ** case is broken out because while 9223372036854775808 cannot be a 
   487    487   ** signed 64-bit integer, its negative -9223372036854775808 can be.
................................................................................
   560    560         /* zNum is exactly 9223372036854775808.  Fits if negative.  The
   561    561         ** special case 2 overflow if positive */
   562    562         assert( u-1==LARGEST_INT64 );
   563    563         return neg ? 0 : 2;
   564    564       }
   565    565     }
   566    566   }
          567  +
          568  +/*
          569  +** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
          570  +** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
          571  +** whereas sqlite3Atoi64() does not.
          572  +**
          573  +** Returns:
          574  +**
          575  +**     0    Successful transformation.  Fits in a 64-bit signed integer.
          576  +**     1    Integer too large for a 64-bit signed integer or is malformed
          577  +**     2    Special case of 9223372036854775808
          578  +*/
          579  +int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
          580  +#ifndef SQLITE_OMIT_HEX_INTEGER
          581  +  if( z[0]=='0'
          582  +   && (z[1]=='x' || z[1]=='X')
          583  +   && sqlite3Isxdigit(z[2])
          584  +  ){
          585  +    u64 u = 0;
          586  +    int i, k;
          587  +    for(i=2; z[i]=='0'; i++){}
          588  +    for(k=i; sqlite3Isxdigit(z[k]); k++){
          589  +      u = u*16 + sqlite3HexToInt(z[k]);
          590  +    }
          591  +    memcpy(pOut, &u, 8);
          592  +    return (z[k]==0 && k-i<=16) ? 0 : 1;
          593  +  }else
          594  +#endif /* SQLITE_OMIT_HEX_INTEGER */
          595  +  {
          596  +    return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
          597  +  }
          598  +}
   567    599   
   568    600   /*
   569    601   ** If zNum represents an integer that will fit in 32-bits, then set
   570    602   ** *pValue to that integer and return true.  Otherwise return false.
          603  +**
          604  +** This routine accepts both decimal and hexadecimal notation for integers.
   571    605   **
   572    606   ** Any non-numeric characters that following zNum are ignored.
   573    607   ** This is different from sqlite3Atoi64() which requires the
   574    608   ** input number to be zero-terminated.
   575    609   */
   576    610   int sqlite3GetInt32(const char *zNum, int *pValue){
   577    611     sqlite_int64 v = 0;
................................................................................
   579    613     int neg = 0;
   580    614     if( zNum[0]=='-' ){
   581    615       neg = 1;
   582    616       zNum++;
   583    617     }else if( zNum[0]=='+' ){
   584    618       zNum++;
   585    619     }
   586         -  while( zNum[0]=='0' ) zNum++;
          620  +#ifndef SQLITE_OMIT_HEX_INTEGER
          621  +  else if( zNum[0]=='0'
          622  +        && (zNum[1]=='x' || zNum[1]=='X')
          623  +        && sqlite3Isxdigit(zNum[2])
          624  +  ){
          625  +    u32 u = 0;
          626  +    zNum += 2;
          627  +    while( zNum[0]=='0' ) zNum++;
          628  +    for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
          629  +      u = u*16 + sqlite3HexToInt(zNum[i]);
          630  +    }
          631  +    if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
          632  +      memcpy(pValue, &u, 4);
          633  +      return 1;
          634  +    }else{
          635  +      return 0;
          636  +    }
          637  +  }
          638  +#endif
   587    639     for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
   588    640       v = v*10 + c;
   589    641     }
   590    642   
   591    643     /* The longest decimal representation of a 32 bit integer is 10 digits:
   592    644     **
   593    645     **             1234567890

Changes to src/vdbe.c.

  3040   3040     Btree *pBt;
  3041   3041     int iMeta;
  3042   3042     int iGen;
  3043   3043   
  3044   3044     assert( p->bIsReader );
  3045   3045     assert( p->readOnly==0 || pOp->p2==0 );
  3046   3046     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  3047         -  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
         3047  +  assert( DbMaskTest(p->btreeMask, pOp->p1) );
  3048   3048     if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
  3049   3049       rc = SQLITE_READONLY;
  3050   3050       goto abort_due_to_error;
  3051   3051     }
  3052   3052     pBt = db->aDb[pOp->p1].pBt;
  3053   3053   
  3054   3054     if( pBt ){
................................................................................
  3135   3135   
  3136   3136     assert( p->bIsReader );
  3137   3137     iDb = pOp->p1;
  3138   3138     iCookie = pOp->p3;
  3139   3139     assert( pOp->p3<SQLITE_N_BTREE_META );
  3140   3140     assert( iDb>=0 && iDb<db->nDb );
  3141   3141     assert( db->aDb[iDb].pBt!=0 );
  3142         -  assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
         3142  +  assert( DbMaskTest(p->btreeMask, iDb) );
  3143   3143   
  3144   3144     sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
  3145   3145     pOut->u.i = iMeta;
  3146   3146     break;
  3147   3147   }
  3148   3148   
  3149   3149   /* Opcode: SetCookie P1 P2 P3 * *
................................................................................
  3156   3156   **
  3157   3157   ** A transaction must be started before executing this opcode.
  3158   3158   */
  3159   3159   case OP_SetCookie: {       /* in3 */
  3160   3160     Db *pDb;
  3161   3161     assert( pOp->p2<SQLITE_N_BTREE_META );
  3162   3162     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  3163         -  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
         3163  +  assert( DbMaskTest(p->btreeMask, pOp->p1) );
  3164   3164     assert( p->readOnly==0 );
  3165   3165     pDb = &db->aDb[pOp->p1];
  3166   3166     assert( pDb->pBt!=0 );
  3167   3167     assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
  3168   3168     pIn3 = &aMem[pOp->p3];
  3169   3169     sqlite3VdbeMemIntegerify(pIn3);
  3170   3170     /* See note about index shifting on OP_ReadCookie */
................................................................................
  3211   3211   **
  3212   3212   ** The P4 value may be either an integer (P4_INT32) or a pointer to
  3213   3213   ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
  3214   3214   ** structure, then said structure defines the content and collating 
  3215   3215   ** sequence of the index being opened. Otherwise, if P4 is an integer 
  3216   3216   ** value, it is set to the number of columns in the table.
  3217   3217   **
  3218         -** See also OpenWrite.
         3218  +** See also: OpenWrite, ReopenIdx
         3219  +*/
         3220  +/* Opcode: ReopenIdx P1 P2 P3 P4 P5
         3221  +** Synopsis: root=P2 iDb=P3
         3222  +**
         3223  +** The ReopenIdx opcode works exactly like ReadOpen except that it first
         3224  +** checks to see if the cursor on P1 is already open with a root page
         3225  +** number of P2 and if it is this opcode becomes a no-op.  In other words,
         3226  +** if the cursor is already open, do not reopen it.
         3227  +**
         3228  +** The ReopenIdx opcode may only be used with P5==0 and with P4 being
         3229  +** a P4_KEYINFO object.  Furthermore, the P3 value must be the same as
         3230  +** every other ReopenIdx or OpenRead for the same cursor number.
         3231  +**
         3232  +** See the OpenRead opcode documentation for additional information.
  3219   3233   */
  3220   3234   /* Opcode: OpenWrite P1 P2 P3 P4 P5
  3221   3235   ** Synopsis: root=P2 iDb=P3
  3222   3236   **
  3223   3237   ** Open a read/write cursor named P1 on the table or index whose root
  3224   3238   ** page is P2.  Or if P5!=0 use the content of register P2 to find the
  3225   3239   ** root page.
................................................................................
  3233   3247   **
  3234   3248   ** This instruction works just like OpenRead except that it opens the cursor
  3235   3249   ** in read/write mode.  For a given table, there can be one or more read-only
  3236   3250   ** cursors or a single read/write cursor but not both.
  3237   3251   **
  3238   3252   ** See also OpenRead.
  3239   3253   */
         3254  +case OP_ReopenIdx: {
         3255  +  VdbeCursor *pCur;
         3256  +
         3257  +  assert( pOp->p5==0 );
         3258  +  assert( pOp->p4type==P4_KEYINFO );
         3259  +  pCur = p->apCsr[pOp->p1];
         3260  +  if( pCur && pCur->pgnoRoot==pOp->p2 ){
         3261  +    assert( pCur->iDb==pOp->p3 );      /* Guaranteed by the code generator */
         3262  +    break;
         3263  +  }
         3264  +  /* If the cursor is not currently open or is open on a different
         3265  +  ** index, then fall through into OP_OpenRead to force a reopen */
         3266  +}
  3240   3267   case OP_OpenRead:
  3241   3268   case OP_OpenWrite: {
  3242   3269     int nField;
  3243   3270     KeyInfo *pKeyInfo;
  3244   3271     int p2;
  3245   3272     int iDb;
  3246   3273     int wrFlag;
................................................................................
  3247   3274     Btree *pX;
  3248   3275     VdbeCursor *pCur;
  3249   3276     Db *pDb;
  3250   3277   
  3251   3278     assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
  3252   3279     assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
  3253   3280     assert( p->bIsReader );
  3254         -  assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
         3281  +  assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
         3282  +          || p->readOnly==0 );
  3255   3283   
  3256   3284     if( p->expired ){
  3257   3285       rc = SQLITE_ABORT;
  3258   3286       break;
  3259   3287     }
  3260   3288   
  3261   3289     nField = 0;
  3262   3290     pKeyInfo = 0;
  3263   3291     p2 = pOp->p2;
  3264   3292     iDb = pOp->p3;
  3265   3293     assert( iDb>=0 && iDb<db->nDb );
  3266         -  assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
         3294  +  assert( DbMaskTest(p->btreeMask, iDb) );
  3267   3295     pDb = &db->aDb[iDb];
  3268   3296     pX = pDb->pBt;
  3269   3297     assert( pX!=0 );
  3270   3298     if( pOp->opcode==OP_OpenWrite ){
  3271   3299       wrFlag = 1;
  3272   3300       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  3273   3301       if( pDb->pSchema->file_format < p->minWriteFileFormat ){
................................................................................
  3304   3332     assert( pOp->p1>=0 );
  3305   3333     assert( nField>=0 );
  3306   3334     testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
  3307   3335     pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
  3308   3336     if( pCur==0 ) goto no_mem;
  3309   3337     pCur->nullRow = 1;
  3310   3338     pCur->isOrdered = 1;
         3339  +  pCur->pgnoRoot = p2;
  3311   3340     rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
  3312   3341     pCur->pKeyInfo = pKeyInfo;
  3313   3342     assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
  3314   3343     sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
  3315   3344   
  3316   3345     /* Since it performs no memory allocation or IO, the only value that
  3317   3346     ** sqlite3BtreeCursor() may return is SQLITE_OK. */
................................................................................
  4901   4930     pOut->flags = MEM_Null;
  4902   4931     if( iCnt>1 ){
  4903   4932       rc = SQLITE_LOCKED;
  4904   4933       p->errorAction = OE_Abort;
  4905   4934     }else{
  4906   4935       iDb = pOp->p3;
  4907   4936       assert( iCnt==1 );
  4908         -    assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
         4937  +    assert( DbMaskTest(p->btreeMask, iDb) );
  4909   4938       iMoved = 0;  /* Not needed.  Only to silence a warning. */
  4910   4939       rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
  4911   4940       pOut->flags = MEM_Int;
  4912   4941       pOut->u.i = iMoved;
  4913   4942   #ifndef SQLITE_OMIT_AUTOVACUUM
  4914   4943       if( rc==SQLITE_OK && iMoved!=0 ){
  4915   4944         sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
................................................................................
  4941   4970   ** See also: Destroy
  4942   4971   */
  4943   4972   case OP_Clear: {
  4944   4973     int nChange;
  4945   4974    
  4946   4975     nChange = 0;
  4947   4976     assert( p->readOnly==0 );
  4948         -  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
         4977  +  assert( DbMaskTest(p->btreeMask, pOp->p2) );
  4949   4978     rc = sqlite3BtreeClearTable(
  4950   4979         db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
  4951   4980     );
  4952   4981     if( pOp->p3 ){
  4953   4982       p->nChange += nChange;
  4954   4983       if( pOp->p3>0 ){
  4955   4984         assert( memIsValid(&aMem[pOp->p3]) );
................................................................................
  5011   5040   case OP_CreateTable: {          /* out2-prerelease */
  5012   5041     int pgno;
  5013   5042     int flags;
  5014   5043     Db *pDb;
  5015   5044   
  5016   5045     pgno = 0;
  5017   5046     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5018         -  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
         5047  +  assert( DbMaskTest(p->btreeMask, pOp->p1) );
  5019   5048     assert( p->readOnly==0 );
  5020   5049     pDb = &db->aDb[pOp->p1];
  5021   5050     assert( pDb->pBt!=0 );
  5022   5051     if( pOp->opcode==OP_CreateTable ){
  5023   5052       /* flags = BTREE_INTKEY; */
  5024   5053       flags = BTREE_INTKEY;
  5025   5054     }else{
................................................................................
  5176   5205     assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
  5177   5206     pIn1 = &aMem[pOp->p1];
  5178   5207     for(j=0; j<nRoot; j++){
  5179   5208       aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
  5180   5209     }
  5181   5210     aRoot[j] = 0;
  5182   5211     assert( pOp->p5<db->nDb );
  5183         -  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
         5212  +  assert( DbMaskTest(p->btreeMask, pOp->p5) );
  5184   5213     z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
  5185   5214                                    (int)pnErr->u.i, &nErr);
  5186   5215     sqlite3DbFree(db, aRoot);
  5187   5216     pnErr->u.i -= nErr;
  5188   5217     sqlite3VdbeMemSetNull(pIn1);
  5189   5218     if( nErr==0 ){
  5190   5219       assert( z==0 );
................................................................................
  5836   5865   ** the P1 database. If the vacuum has finished, jump to instruction
  5837   5866   ** P2. Otherwise, fall through to the next instruction.
  5838   5867   */
  5839   5868   case OP_IncrVacuum: {        /* jump */
  5840   5869     Btree *pBt;
  5841   5870   
  5842   5871     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5843         -  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
         5872  +  assert( DbMaskTest(p->btreeMask, pOp->p1) );
  5844   5873     assert( p->readOnly==0 );
  5845   5874     pBt = db->aDb[pOp->p1].pBt;
  5846   5875     rc = sqlite3BtreeIncrVacuum(pBt);
  5847   5876     VdbeBranchTaken(rc==SQLITE_DONE,2);
  5848   5877     if( rc==SQLITE_DONE ){
  5849   5878       pc = pOp->p2 - 1;
  5850   5879       rc = SQLITE_OK;
................................................................................
  5851   5880     }
  5852   5881     break;
  5853   5882   }
  5854   5883   #endif
  5855   5884   
  5856   5885   /* Opcode: Expire P1 * * * *
  5857   5886   **
  5858         -** Cause precompiled statements to become expired. An expired statement
  5859         -** fails with an error code of SQLITE_SCHEMA if it is ever executed 
  5860         -** (via sqlite3_step()).
         5887  +** Cause precompiled statements to expire.  When an expired statement
         5888  +** is executed using sqlite3_step() it will either automatically
         5889  +** reprepare itself (if it was originally created using sqlite3_prepare_v2())
         5890  +** or it will fail with SQLITE_SCHEMA.
  5861   5891   ** 
  5862   5892   ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
  5863         -** then only the currently executing statement is affected. 
         5893  +** then only the currently executing statement is expired.
  5864   5894   */
  5865   5895   case OP_Expire: {
  5866   5896     if( !pOp->p1 ){
  5867   5897       sqlite3ExpirePreparedStatements(db);
  5868   5898     }else{
  5869   5899       p->expired = 1;
  5870   5900     }
................................................................................
  5888   5918   ** used to generate an error message if the lock cannot be obtained.
  5889   5919   */
  5890   5920   case OP_TableLock: {
  5891   5921     u8 isWriteLock = (u8)pOp->p3;
  5892   5922     if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
  5893   5923       int p1 = pOp->p1; 
  5894   5924       assert( p1>=0 && p1<db->nDb );
  5895         -    assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
         5925  +    assert( DbMaskTest(p->btreeMask, p1) );
  5896   5926       assert( isWriteLock==0 || isWriteLock==1 );
  5897   5927       rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
  5898   5928       if( (rc&0xFF)==SQLITE_LOCKED ){
  5899   5929         const char *z = pOp->p4.z;
  5900   5930         sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
  5901   5931       }
  5902   5932     }
................................................................................
  6338   6368       sqlite3DbFree(db, z);
  6339   6369     }
  6340   6370   #ifdef SQLITE_USE_FCNTL_TRACE
  6341   6371     zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
  6342   6372     if( zTrace ){
  6343   6373       int i;
  6344   6374       for(i=0; i<db->nDb; i++){
  6345         -      if( (MASKBIT(i) & p->btreeMask)==0 ) continue;
         6375  +      if( DbMaskTest(p->btreeMask, i)==0 ) continue;
  6346   6376         sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
  6347   6377       }
  6348   6378     }
  6349   6379   #endif /* SQLITE_USE_FCNTL_TRACE */
  6350   6380   #ifdef SQLITE_DEBUG
  6351   6381     if( (db->flags & SQLITE_SqlTrace)!=0
  6352   6382      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0

Changes to src/vdbeInt.h.

    72     72     u8 nullRow;           /* True if pointing to a row with no data */
    73     73     u8 rowidIsValid;      /* True if lastRowid is valid */
    74     74     u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
    75     75     Bool isEphemeral:1;   /* True for an ephemeral table */
    76     76     Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
    77     77     Bool isTable:1;       /* True if a table requiring integer keys */
    78     78     Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
           79  +  Pgno pgnoRoot;        /* Root page of the open btree cursor */
    79     80     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    80     81     i64 seqCount;         /* Sequence counter */
    81     82     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    82     83     i64 lastRowid;        /* Rowid being deleted by OP_Delete */
    83     84     VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
    84     85   
    85     86     /* Cached information about the header for the data record that the

Changes to src/vdbeapi.c.

  1319   1319   }
  1320   1320   
  1321   1321   /*
  1322   1322   ** Return true if the prepared statement is in need of being reset.
  1323   1323   */
  1324   1324   int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
  1325   1325     Vdbe *v = (Vdbe*)pStmt;
  1326         -  return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
         1326  +  return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN;
  1327   1327   }
  1328   1328   
  1329   1329   /*
  1330   1330   ** Return a pointer to the next prepared statement after pStmt associated
  1331   1331   ** with database connection pDb.  If pStmt is NULL, return the first
  1332   1332   ** prepared statement for the database connection.  Return NULL if there
  1333   1333   ** are no more.

Changes to src/vdbeaux.c.

   496    496         pOp->p2 = aLabel[-1-pOp->p2];
   497    497       }
   498    498     }
   499    499     sqlite3DbFree(p->db, pParse->aLabel);
   500    500     pParse->aLabel = 0;
   501    501     pParse->nLabel = 0;
   502    502     *pMaxFuncArgs = nMaxArgs;
   503         -  assert( p->bIsReader!=0 || p->btreeMask==0 );
          503  +  assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
   504    504   }
   505    505   
   506    506   /*
   507    507   ** Return the address of the next instruction to be inserted.
   508    508   */
   509    509   int sqlite3VdbeCurrentAddr(Vdbe *p){
   510    510     assert( p->magic==VDBE_MAGIC_INIT );
................................................................................
   523    523   ** returned program.
   524    524   */
   525    525   VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
   526    526     VdbeOp *aOp = p->aOp;
   527    527     assert( aOp && !p->db->mallocFailed );
   528    528   
   529    529     /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
   530         -  assert( p->btreeMask==0 );
          530  +  assert( DbMaskAllZero(p->btreeMask) );
   531    531   
   532    532     resolveP2Values(p, pnMaxArg);
   533    533     *pnOp = p->nOp;
   534    534     p->aOp = 0;
   535    535     return aOp;
   536    536   }
   537    537   
................................................................................
  1108   1108   ** attached databases that will be use.  A mask of these databases
  1109   1109   ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
  1110   1110   ** p->btreeMask of databases that will require a lock.
  1111   1111   */
  1112   1112   void sqlite3VdbeUsesBtree(Vdbe *p, int i){
  1113   1113     assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
  1114   1114     assert( i<(int)sizeof(p->btreeMask)*8 );
  1115         -  p->btreeMask |= ((yDbMask)1)<<i;
         1115  +  DbMaskSet(p->btreeMask, i);
  1116   1116     if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
  1117         -    p->lockMask |= ((yDbMask)1)<<i;
         1117  +    DbMaskSet(p->lockMask, i);
  1118   1118     }
  1119   1119   }
  1120   1120   
  1121   1121   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
  1122   1122   /*
  1123   1123   ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
  1124   1124   ** this routine obtains the mutex associated with each BtShared structure
................................................................................
  1138   1138   ** statement p will ever use.  Let N be the number of bits in p->btreeMask
  1139   1139   ** corresponding to btrees that use shared cache.  Then the runtime of
  1140   1140   ** this routine is N*N.  But as N is rarely more than 1, this should not
  1141   1141   ** be a problem.
  1142   1142   */
  1143   1143   void sqlite3VdbeEnter(Vdbe *p){
  1144   1144     int i;
  1145         -  yDbMask mask;
  1146   1145     sqlite3 *db;
  1147   1146     Db *aDb;
  1148   1147     int nDb;
  1149         -  if( p->lockMask==0 ) return;  /* The common case */
         1148  +  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
  1150   1149     db = p->db;
  1151   1150     aDb = db->aDb;
  1152   1151     nDb = db->nDb;
  1153         -  for(i=0, mask=1; i<nDb; i++, mask += mask){
  1154         -    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
         1152  +  for(i=0; i<nDb; i++){
         1153  +    if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
  1155   1154         sqlite3BtreeEnter(aDb[i].pBt);
  1156   1155       }
  1157   1156     }
  1158   1157   }
  1159   1158   #endif
  1160   1159   
  1161   1160   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
  1162   1161   /*
  1163   1162   ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
  1164   1163   */
  1165   1164   void sqlite3VdbeLeave(Vdbe *p){
  1166   1165     int i;
  1167         -  yDbMask mask;
  1168   1166     sqlite3 *db;
  1169   1167     Db *aDb;
  1170   1168     int nDb;
  1171         -  if( p->lockMask==0 ) return;  /* The common case */
         1169  +  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
  1172   1170     db = p->db;
  1173   1171     aDb = db->aDb;
  1174   1172     nDb = db->nDb;
  1175         -  for(i=0, mask=1; i<nDb; i++, mask += mask){
  1176         -    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
         1173  +  for(i=0; i<nDb; i++){
         1174  +    if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
  1177   1175         sqlite3BtreeLeave(aDb[i].pBt);
  1178   1176       }
  1179   1177     }
  1180   1178   }
  1181   1179   #endif
  1182   1180   
  1183   1181   #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
................................................................................
  2140   2138   static void checkActiveVdbeCnt(sqlite3 *db){
  2141   2139     Vdbe *p;
  2142   2140     int cnt = 0;
  2143   2141     int nWrite = 0;
  2144   2142     int nRead = 0;
  2145   2143     p = db->pVdbe;
  2146   2144     while( p ){
  2147         -    if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
         2145  +    if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
  2148   2146         cnt++;
  2149   2147         if( p->readOnly==0 ) nWrite++;
  2150   2148         if( p->bIsReader ) nRead++;
  2151   2149       }
  2152   2150       p = p->pNext;
  2153   2151     }
  2154   2152     assert( cnt==db->nVdbeActive );

Changes to src/where.c.

  1955   1955         iLower = 0;
  1956   1956         iUpper = aSample[0].anLt[iCol];
  1957   1957       }else{
  1958   1958         i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
  1959   1959         iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
  1960   1960         iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
  1961   1961       }
  1962         -    aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
         1962  +    aStat[1] = pIdx->aAvgEq[iCol];
  1963   1963       if( iLower>=iUpper ){
  1964   1964         iGap = 0;
  1965   1965       }else{
  1966   1966         iGap = iUpper - iLower;
  1967   1967       }
  1968   1968       if( roundUp ){
  1969   1969         iGap = (iGap*2)/3;
................................................................................
  3418   3418       int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
  3419   3419       int regRowset = 0;                        /* Register for RowSet object */
  3420   3420       int regRowid = 0;                         /* Register holding rowid */
  3421   3421       int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
  3422   3422       int iRetInit;                             /* Address of regReturn init */
  3423   3423       int untestedTerms = 0;             /* Some terms not completely tested */
  3424   3424       int ii;                            /* Loop counter */
         3425  +    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
  3425   3426       Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
  3426   3427       Table *pTab = pTabItem->pTab;
  3427   3428      
  3428   3429       pTerm = pLoop->aLTerm[0];
  3429   3430       assert( pTerm!=0 );
  3430   3431       assert( pTerm->eOperator & WO_OR );
  3431   3432       assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
................................................................................
  3513   3514         }
  3514   3515       }
  3515   3516   
  3516   3517       /* Run a separate WHERE clause for each term of the OR clause.  After
  3517   3518       ** eliminating duplicates from other WHERE clauses, the action for each
  3518   3519       ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
  3519   3520       */
         3521  +    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
         3522  +                  WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY;
  3520   3523       for(ii=0; ii<pOrWc->nTerm; ii++){
  3521   3524         WhereTerm *pOrTerm = &pOrWc->a[ii];
  3522   3525         if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
  3523   3526           WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
  3524   3527           Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
  3525   3528           int j1 = 0;                     /* Address of jump operation */
  3526   3529           if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
  3527   3530             pAndExpr->pLeft = pOrExpr;
  3528   3531             pOrExpr = pAndExpr;
  3529   3532           }
  3530   3533           /* Loop through table entries that match term pOrTerm. */
  3531   3534           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
  3532         -                        WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
  3533         -                        WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
         3535  +                                      wctrlFlags, iCovCur);
  3534   3536           assert( pSubWInfo || pParse->nErr || db->mallocFailed );
  3535   3537           if( pSubWInfo ){
  3536   3538             WhereLoop *pSubLoop;
  3537   3539             explainOneScan(
  3538   3540                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  3539   3541             );
  3540   3542             /* This is the sub-WHERE clause body.  First skip over
................................................................................
  3617   3619             assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
  3618   3620             if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
  3619   3621              && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
  3620   3622              && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
  3621   3623             ){
  3622   3624               assert( pSubWInfo->a[0].iIdxCur==iCovCur );
  3623   3625               pCov = pSubLoop->u.btree.pIndex;
         3626  +            wctrlFlags |= WHERE_REOPEN_IDX;
  3624   3627             }else{
  3625   3628               pCov = 0;
  3626   3629             }
  3627   3630   
  3628   3631             /* Finish the loop through table entries that match term pOrTerm. */
  3629   3632             sqlite3WhereEnd(pSubWInfo);
  3630   3633           }
................................................................................
  4223   4226       }
  4224   4227       if( j<0 ){
  4225   4228         pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1);
  4226   4229       }
  4227   4230     }
  4228   4231   }
  4229   4232   
         4233  +/*
         4234  +** Adjust the cost C by the costMult facter T.  This only occurs if
         4235  +** compiled with -DSQLITE_ENABLE_COSTMULT
         4236  +*/
         4237  +#ifdef SQLITE_ENABLE_COSTMULT
         4238  +# define ApplyCostMultiplier(C,T)  C += T
         4239  +#else
         4240  +# define ApplyCostMultiplier(C,T)
         4241  +#endif
         4242  +
  4230   4243   /*
  4231   4244   ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the 
  4232   4245   ** index pIndex. Try to match one more.
  4233   4246   **
  4234   4247   ** When this function is called, pBuilder->pNew->nOut contains the 
  4235   4248   ** number of rows expected to be visited by filtering using the nEq 
  4236   4249   ** terms only. If it is modified, this value is restored before this 
................................................................................
  4419   4432             if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
  4420   4433               testcase( eOp & WO_EQ );
  4421   4434               testcase( eOp & WO_ISNULL );
  4422   4435               rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
  4423   4436             }else{
  4424   4437               rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
  4425   4438             }
  4426         -          assert( rc!=SQLITE_OK || nOut>0 );
  4427   4439             if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
  4428   4440             if( rc!=SQLITE_OK ) break;          /* Jump out of the pTerm loop */
  4429   4441             if( nOut ){
  4430   4442               pNew->nOut = sqlite3LogEst(nOut);
  4431   4443               if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
  4432   4444               pNew->nOut -= nIn;
  4433   4445             }
................................................................................
  4451   4463       ** seek only. Then, if this is a non-covering index, add the cost of
  4452   4464       ** visiting the rows in the main table.  */
  4453   4465       rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
  4454   4466       pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
  4455   4467       if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
  4456   4468         pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
  4457   4469       }
         4470  +    ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
  4458   4471   
  4459   4472       nOutUnadjusted = pNew->nOut;
  4460   4473       pNew->rRun += nInMul + nIn;
  4461   4474       pNew->nOut += nInMul + nIn;
  4462   4475       whereLoopOutputAdjust(pBuilder->pWC, pNew);
  4463   4476       rc = whereLoopInsert(pBuilder, pNew);
  4464   4477   
................................................................................
  4570   4583   **
  4571   4584   **     cost = nSeek * (log(nRow) + K * nVisit)          // covering index
  4572   4585   **     cost = nSeek * (log(nRow) + (K+3.0) * nVisit)    // non-covering index
  4573   4586   **
  4574   4587   ** Normally, nSeek is 1. nSeek values greater than 1 come about if the 
  4575   4588   ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when 
  4576   4589   ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
         4590  +**
         4591  +** The estimated values (nRow, nVisit, nSeek) often contain a large amount
         4592  +** of uncertainty.  For this reason, scoring is designed to pick plans that
         4593  +** "do the least harm" if the estimates are inaccurate.  For example, a
         4594  +** log(nRow) factor is omitted from a non-covering index scan in order to
         4595  +** bias the scoring in favor of using an index, since the worst-case
         4596  +** performance of using an index is far better than the worst-case performance
         4597  +** of a full table scan.
  4577   4598   */
  4578   4599   static int whereLoopAddBtree(
  4579   4600     WhereLoopBuilder *pBuilder, /* WHERE clause information */
  4580   4601     Bitmask mExtra              /* Extra prerequesites for using this table */
  4581   4602   ){
  4582   4603     WhereInfo *pWInfo;          /* WHERE analysis context */
  4583   4604     Index *pProbe;              /* An index we are evaluating */
................................................................................
  4657   4678           pNew->u.btree.pIndex = 0;
  4658   4679           pNew->nLTerm = 1;
  4659   4680           pNew->aLTerm[0] = pTerm;
  4660   4681           /* TUNING: One-time cost for computing the automatic index is
  4661   4682           ** approximately 7*N*log2(N) where N is the number of rows in
  4662   4683           ** the table being indexed. */
  4663   4684           pNew->rSetup = rLogSize + rSize + 28;  assert( 28==sqlite3LogEst(7) );
         4685  +        ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
  4664   4686           /* TUNING: Each index lookup yields 20 rows in the table.  This
  4665   4687           ** is more than the usual guess of 10 rows, since we have no way
  4666   4688           ** of knowning how selective the index will ultimately be.  It would
  4667   4689           ** not be unreasonable to make this value much larger. */
  4668   4690           pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
  4669   4691           pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
  4670   4692           pNew->wsFlags = WHERE_AUTO_INDEX;
................................................................................
  4698   4720         /* Integer primary key index */
  4699   4721         pNew->wsFlags = WHERE_IPK;
  4700   4722   
  4701   4723         /* Full table scan */
  4702   4724         pNew->iSortIdx = b ? iSortIdx : 0;
  4703   4725         /* TUNING: Cost of full table scan is (N*3.0). */
  4704   4726         pNew->rRun = rSize + 16;
         4727  +      ApplyCostMultiplier(pNew->rRun, pTab->costMult);
  4705   4728         whereLoopOutputAdjust(pWC, pNew);
  4706   4729         rc = whereLoopInsert(pBuilder, pNew);
  4707   4730         pNew->nOut = rSize;
  4708   4731         if( rc ) break;
  4709   4732       }else{
  4710   4733         Bitmask m;
  4711   4734         if( pProbe->isCovering ){
................................................................................
  4733   4756           ** between 1.1 and 3.0, depending on the relative sizes of the
  4734   4757           ** index and table rows. If this is a non-covering index scan,
  4735   4758           ** also add the cost of visiting table rows (N*3.0).  */
  4736   4759           pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
  4737   4760           if( m!=0 ){
  4738   4761             pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
  4739   4762           }
  4740         -
         4763  +        ApplyCostMultiplier(pNew->rRun, pTab->costMult);
  4741   4764           whereLoopOutputAdjust(pWC, pNew);
  4742   4765           rc = whereLoopInsert(pBuilder, pNew);
  4743   4766           pNew->nOut = rSize;
  4744   4767           if( rc ) break;
  4745   4768         }
  4746   4769       }
  4747   4770   
................................................................................
  6208   6231             iIndexCur++;
  6209   6232             pJ = pJ->pNext;
  6210   6233           }
  6211   6234           op = OP_OpenWrite;
  6212   6235           pWInfo->aiCurOnePass[1] = iIndexCur;
  6213   6236         }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
  6214   6237           iIndexCur = iIdxCur;
         6238  +        if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx;
  6215   6239         }else{
  6216   6240           iIndexCur = pParse->nTab++;
  6217   6241         }
  6218   6242         pLevel->iIdxCur = iIndexCur;
  6219   6243         assert( pIx->pSchema==pTab->pSchema );
  6220   6244         assert( iIndexCur>=0 );
  6221   6245         if( op ){

Changes to test/analyze6.test.

    87     87     eqp {SELECT * FROM t201 WHERE y=5}
    88     88   } {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
    89     89   do_test analyze6-2.3 {
    90     90     eqp {SELECT * FROM t201 WHERE x=5}
    91     91   } {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
    92     92   do_test analyze6-2.4 {
    93     93     execsql {
    94         -    INSERT INTO t201 VALUES(1,2,3);
           94  +    INSERT INTO t201 VALUES(1,2,3),(2,3,4),(3,4,5);
    95     95       ANALYZE t201;
    96     96     }
    97     97     eqp {SELECT * FROM t201 WHERE z=5}
    98     98   } {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
    99     99   do_test analyze6-2.5 {
   100    100     eqp {SELECT * FROM t201 WHERE y=5}
   101    101   } {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}

Changes to test/analyze9.test.

  1015   1015   foreach {tn where res} {
  1016   1016     1 "c='one' AND a='B' AND d < 20"   {/*INDEX i3 (c=? AND a=?)*/}
  1017   1017     2 "c='one' AND a='A' AND d < 20"   {/*INDEX i4 (d<?)*/}
  1018   1018   } {
  1019   1019     do_eqp_test 22.2.$tn "SELECT * FROM t3 WHERE $where" $res
  1020   1020   }
  1021   1021   
  1022         -finish_test
         1022  +proc int_to_char {i} {
         1023  +  set ret ""
         1024  +  set char [list a b c d e f g h i j]
         1025  +  foreach {div} {1000 100 10 1} {
         1026  +    append ret [lindex $char [expr ($i / $div) % 10]]
         1027  +  }
         1028  +  set ret
         1029  +}
         1030  +db func int_to_char int_to_char
         1031  +
         1032  +do_execsql_test 23.0 {
         1033  +  CREATE TABLE t4(
         1034  +    a COLLATE nocase, b, c, 
         1035  +    d, e, f, 
         1036  +    PRIMARY KEY(c, b, a)
         1037  +  ) WITHOUT ROWID;
         1038  +  CREATE INDEX i41 ON t4(e);
         1039  +  CREATE INDEX i42 ON t4(f);
         1040  +
         1041  +  WITH data(a, b, c, d, e, f) AS (
         1042  +    SELECT int_to_char(0), 'xyz', 'zyx', '*', 0, 0
         1043  +    UNION ALL
         1044  +    SELECT 
         1045  +      int_to_char(f+1), b, c, d, (e+1) % 2, f+1
         1046  +    FROM data WHERE f<1024
         1047  +  )
         1048  +  INSERT INTO t4 SELECT a, b, c, d, e, f FROM data;
         1049  +  ANALYZE;
         1050  +} {}
         1051  +
         1052  +do_eqp_test 23.1 {
         1053  +  SELECT * FROM t4 WHERE 
         1054  +    (e=1 AND b='xyz' AND c='zyx' AND a<'AEA') AND f<300
         1055  +} {
         1056  +  0 0 0 {SEARCH TABLE t4 USING INDEX i41 (e=? AND c=? AND b=? AND a<?)}
         1057  +}
         1058  +do_eqp_test 23.2 {
         1059  +  SELECT * FROM t4 WHERE 
         1060  +    (e=1 AND b='xyz' AND c='zyx' AND a<'JJJ') AND f<300
         1061  +} {
         1062  +  0 0 0 {SEARCH TABLE t4 USING INDEX i42 (f<?)}
         1063  +}
         1064  +
         1065  +do_execsql_test 24.0 {
         1066  +  CREATE TABLE t5(c, d, b, e, a, PRIMARY KEY(a, b, c)) WITHOUT ROWID;
         1067  +  WITH data(a, b, c, d, e) AS (
         1068  +    SELECT 'z', 'y', 0, 0, 0
         1069  +    UNION ALL
         1070  +    SELECT 
         1071  +      a, CASE WHEN b='y' THEN 'n' ELSE 'y' END, c+1, e/250, e+1 
         1072  +    FROM data
         1073  +    WHERE e<1000
         1074  +  )
         1075  +  INSERT INTO t5(a, b, c, d, e) SELECT * FROM data;
         1076  +  CREATE INDEX t5d ON t5(d);
         1077  +  CREATE INDEX t5e ON t5(e);
         1078  +  ANALYZE;
         1079  +}
         1080  +
         1081  +foreach {tn where eqp} {
         1082  +  1 "d=0 AND a='z' AND b='n' AND e<200" {/*t5d (d=? AND a=? AND b=?)*/}
         1083  +  2 "d=0 AND a='z' AND b='n' AND e<100" {/*t5e (e<?)*/}
         1084  +
         1085  +  3 "d=0 AND e<300"                     {/*t5d (d=?)*/}
         1086  +  4 "d=0 AND e<200"                     {/*t5e (e<?)*/}
         1087  +} {
         1088  +  do_eqp_test 24.$tn "SeLeCt * FROM t5 WHERE $where" $eqp
         1089  +}
  1023   1090   
  1024         -
         1091  +finish_test

Added test/analyzeC.test.

            1  +# 2014-07-22
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file contains automated tests used to verify that the text terms
           13  +# at the end of sqlite_stat1.stat are processed correctly.
           14  +#
           15  +#  (1) "unordered" means that the index cannot be used for ORDER BY
           16  +#      or for range queries
           17  +#
           18  +#  (2) "sz=NNN" sets the relative size of the index entries
           19  +#
           20  +#  (3) All other fields are silently ignored
           21  +#
           22  +
           23  +set testdir [file dirname $argv0]
           24  +source $testdir/tester.tcl
           25  +set testprefix analyzeC
           26  +
           27  +# Baseline case.  Range queries work OK.  Indexes can be used for
           28  +# ORDER BY.
           29  +#
           30  +do_execsql_test 1.0 {
           31  +  CREATE TABLE t1(a,b,c);
           32  +  INSERT INTO t1(a,b,c)
           33  +    VALUES(1,2,3),(7,8,9),(4,5,6),(10,11,12),(4,8,12),(1,11,111);
           34  +  CREATE INDEX t1a ON t1(a);
           35  +  CREATE INDEX t1b ON t1(b);
           36  +  ANALYZE;
           37  +  DELETE FROM sqlite_stat1;
           38  +  INSERT INTO sqlite_stat1(tbl,idx,stat)
           39  +    VALUES('t1','t1a','12345 2'),('t1','t1b','12345 4');
           40  +  ANALYZE sqlite_master;
           41  +  SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
           42  +} {4 5 6 # 7 8 9 # 4 8 12 #}
           43  +do_execsql_test 1.1 {
           44  +  EXPLAIN QUERY PLAN
           45  +  SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
           46  +} {/.* USING INDEX t1a .a>. AND a<...*/}
           47  +do_execsql_test 1.2 {
           48  +  SELECT c FROM t1 ORDER BY a;
           49  +} {3 111 6 12 9 12}
           50  +do_execsql_test 1.3 {
           51  +  EXPLAIN QUERY PLAN
           52  +  SELECT c FROM t1 ORDER BY a;
           53  +} {/.*SCAN TABLE t1 USING INDEX t1a.*/}
           54  +do_execsql_test 1.3x {
           55  +  EXPLAIN QUERY PLAN
           56  +  SELECT c FROM t1 ORDER BY a;
           57  +} {~/.*B-TREE FOR ORDER BY.*/}
           58  +
           59  +# Now mark the t1a index as "unordered".  Range queries and ORDER BY no
           60  +# longer use the index, but equality queries do.
           61  +#
           62  +do_execsql_test 2.0 {
           63  +  UPDATE sqlite_stat1 SET stat='12345 2 unordered' WHERE idx='t1a';
           64  +  ANALYZE sqlite_master;
           65  +  SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
           66  +} {4 5 6 # 7 8 9 # 4 8 12 #}
           67  +do_execsql_test 2.1 {
           68  +  EXPLAIN QUERY PLAN
           69  +  SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
           70  +} {~/.*USING INDEX.*/}
           71  +do_execsql_test 2.2 {
           72  +  SELECT c FROM t1 ORDER BY a;
           73  +} {3 111 6 12 9 12}
           74  +do_execsql_test 2.3 {
           75  +  EXPLAIN QUERY PLAN
           76  +  SELECT c FROM t1 ORDER BY a;
           77  +} {~/.*USING INDEX.*/}
           78  +do_execsql_test 2.3x {
           79  +  EXPLAIN QUERY PLAN
           80  +  SELECT c FROM t1 ORDER BY a;
           81  +} {/.*B-TREE FOR ORDER BY.*/}
           82  +
           83  +# Ignore extraneous text parameters in the sqlite_stat1.stat field.
           84  +#
           85  +do_execsql_test 3.0 {
           86  +  UPDATE sqlite_stat1 SET stat='12345 2 whatever=5 unordered xyzzy=11'
           87  +   WHERE idx='t1a';
           88  +  ANALYZE sqlite_master;
           89  +  SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
           90  +} {4 5 6 # 7 8 9 # 4 8 12 #}
           91  +do_execsql_test 3.1 {
           92  +  EXPLAIN QUERY PLAN
           93  +  SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
           94  +} {~/.*USING INDEX.*/}
           95  +do_execsql_test 3.2 {
           96  +  SELECT c FROM t1 ORDER BY a;
           97  +} {3 111 6 12 9 12}
           98  +do_execsql_test 3.3 {
           99  +  EXPLAIN QUERY PLAN
          100  +  SELECT c FROM t1 ORDER BY a;
          101  +} {~/.*USING INDEX.*/}
          102  +do_execsql_test 3.3x {
          103  +  EXPLAIN QUERY PLAN
          104  +  SELECT c FROM t1 ORDER BY a;
          105  +} {/.*B-TREE FOR ORDER BY.*/}
          106  +
          107  +# The sz=NNN parameter determines which index to scan
          108  +#
          109  +do_execsql_test 4.0 {
          110  +  DROP INDEX t1a;
          111  +  CREATE INDEX t1ab ON t1(a,b);
          112  +  CREATE INDEX t1ca ON t1(c,a);
          113  +  DELETE FROM sqlite_stat1;
          114  +  INSERT INTO sqlite_stat1(tbl,idx,stat)
          115  +    VALUES('t1','t1ab','12345 3 2 sz=10'),('t1','t1ca','12345 3 2 sz=20');
          116  +  ANALYZE sqlite_master;
          117  +  SELECT count(a) FROM t1;
          118  +} {6}
          119  +do_execsql_test 4.1 {
          120  +  EXPLAIN QUERY PLAN
          121  +  SELECT count(a) FROM t1;
          122  +} {/.*INDEX t1ab.*/}
          123  +do_execsql_test 4.2 {
          124  +  DELETE FROM sqlite_stat1;
          125  +  INSERT INTO sqlite_stat1(tbl,idx,stat)
          126  +    VALUES('t1','t1ab','12345 3 2 sz=20'),('t1','t1ca','12345 3 2 sz=10');
          127  +  ANALYZE sqlite_master;
          128  +  SELECT count(a) FROM t1;
          129  +} {6}
          130  +do_execsql_test 4.3 {
          131  +  EXPLAIN QUERY PLAN
          132  +  SELECT count(a) FROM t1;
          133  +} {/.*INDEX t1ca.*/}
          134  +
          135  +
          136  +# The sz=NNN parameter works even if there is other extraneous text
          137  +# in the sqlite_stat1.stat column.
          138  +#
          139  +do_execsql_test 5.0 {
          140  +  DELETE FROM sqlite_stat1;
          141  +  INSERT INTO sqlite_stat1(tbl,idx,stat)
          142  +    VALUES('t1','t1ab','12345 3 2 x=5 sz=10 y=10'),
          143  +          ('t1','t1ca','12345 3 2 whatever sz=20 junk');
          144  +  ANALYZE sqlite_master;
          145  +  SELECT count(a) FROM t1;
          146  +} {6}
          147  +do_execsql_test 5.1 {
          148  +  EXPLAIN QUERY PLAN
          149  +  SELECT count(a) FROM t1;
          150  +} {/.*INDEX t1ab.*/}
          151  +do_execsql_test 5.2 {
          152  +  DELETE FROM sqlite_stat1;
          153  +  INSERT INTO sqlite_stat1(tbl,idx,stat)
          154  +    VALUES('t1','t1ca','12345 3 2 x=5 sz=10 y=10'),
          155  +          ('t1','t1ab','12345 3 2 whatever sz=20 junk');
          156  +  ANALYZE sqlite_master;
          157  +  SELECT count(a) FROM t1;
          158  +} {6}
          159  +do_execsql_test 5.3 {
          160  +  EXPLAIN QUERY PLAN
          161  +  SELECT count(a) FROM t1;
          162  +} {/.*INDEX t1ca.*/}
          163  +
          164  +
          165  +
          166  +
          167  +finish_test

Changes to test/capi3d.test.

   139    139     sqlite3_stmt_busy $STMT
   140    140   } {0}
   141    141   
   142    142   do_test capi3d-3.99 {
   143    143     sqlite3_finalize $STMT
   144    144     sqlite3_stmt_busy 0
   145    145   } {0}
          146  +
          147  +#--------------------------------------------------------------------------
          148  +# Test the sqlite3_stmt_busy() function with ROLLBACK statements.
          149  +#
          150  +reset_db
          151  +
          152  +do_execsql_test capi3d-4.1 {
          153  +  CREATE TABLE t4(x,y);
          154  +  BEGIN;
          155  +}
          156  +
          157  +do_test capi3d-4.2.1 {
          158  +  breakpoint
          159  +  set ::s1 [sqlite3_prepare_v2 db "ROLLBACK" -1 notused]
          160  +  sqlite3_step $::s1
          161  +} {SQLITE_DONE}
          162  +
          163  +do_test capi3d-4.2.2 {
          164  +  sqlite3_stmt_busy $::s1
          165  +} {1}
          166  +
          167  +do_catchsql_test capi3d-4.2.3 {
          168  +  VACUUM
          169  +} {1 {cannot VACUUM - SQL statements in progress}}
          170  +
          171  +do_test capi3d-4.2.4 {
          172  +  sqlite3_reset $::s1
          173  +} {SQLITE_OK}
          174  +
          175  +do_catchsql_test capi3d-4.2.5 {
          176  +  VACUUM
          177  +} {0 {}}
          178  +
          179  +do_test capi3d-4.2.6 {
          180  +  sqlite3_finalize $::s1
          181  +} {SQLITE_OK}
          182  +
   146    183   
   147    184   finish_test

Added test/hexlit.test.

            1  +# 2014-07-23
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file implements tests for hexadecimal literals
           13  +
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +
           18  +proc hexlit1 {tnum val ans} {
           19  +  do_execsql_test hexlit-$tnum "SELECT $val" $ans
           20  +}
           21  +
           22  +hexlit1 100 0x0 0
           23  +hexlit1 101 0x0000000000000000000000000000000000000000000001 1
           24  +hexlit1 102 0x2 2
           25  +hexlit1 103 0x4 4
           26  +hexlit1 104 0x8 8
           27  +hexlit1 105 0x00000000000000000000000000000000000000000000010 16
           28  +hexlit1 103 0x20 32
           29  +hexlit1 106 0x40 64
           30  +hexlit1 107 0x80 128
           31  +hexlit1 108 0x100 256
           32  +hexlit1 109 0x200 512
           33  +hexlit1 110 0X400 1024
           34  +hexlit1 111 0x800 2048
           35  +hexlit1 112 0x1000 4096
           36  +hexlit1 113 0x2000 8192
           37  +hexlit1 114 0x4000 16384
           38  +hexlit1 115 0x8000 32768
           39  +hexlit1 116 0x10000 65536
           40  +hexlit1 117 0x20000 131072
           41  +hexlit1 118 0x40000 262144
           42  +hexlit1 119 0x80000 524288
           43  +hexlit1 120 0x100000 1048576
           44  +hexlit1 121 0x200000 2097152
           45  +hexlit1 122 0x400000 4194304
           46  +hexlit1 123 0x800000 8388608
           47  +hexlit1 124 0x1000000 16777216
           48  +hexlit1 125 0x2000000 33554432
           49  +hexlit1 126 0x4000000 67108864
           50  +hexlit1 127 0x8000000 134217728
           51  +hexlit1 128 0x10000000 268435456
           52  +hexlit1 129 0x20000000 536870912
           53  +hexlit1 130 0x40000000 1073741824
           54  +hexlit1 131 0x80000000 2147483648
           55  +hexlit1 132 0x100000000 4294967296
           56  +hexlit1 133 0x200000000 8589934592
           57  +hexlit1 134 0x400000000 17179869184
           58  +hexlit1 135 0x800000000 34359738368
           59  +hexlit1 136 0x1000000000 68719476736
           60  +hexlit1 137 0x2000000000 137438953472
           61  +hexlit1 138 0x4000000000 274877906944
           62  +hexlit1 139 0x8000000000 549755813888
           63  +hexlit1 140 0x10000000000 1099511627776
           64  +hexlit1 141 0x20000000000 2199023255552
           65  +hexlit1 142 0x40000000000 4398046511104
           66  +hexlit1 143 0x80000000000 8796093022208
           67  +hexlit1 144 0x100000000000 17592186044416
           68  +hexlit1 145 0x200000000000 35184372088832
           69  +hexlit1 146 0x400000000000 70368744177664
           70  +hexlit1 147 0x800000000000 140737488355328
           71  +hexlit1 148 0x1000000000000 281474976710656
           72  +hexlit1 149 0x2000000000000 562949953421312
           73  +hexlit1 150 0x4000000000000 1125899906842624
           74  +hexlit1 151 0x8000000000000 2251799813685248
           75  +hexlit1 152 0x10000000000000 4503599627370496
           76  +hexlit1 153 0x20000000000000 9007199254740992
           77  +hexlit1 154 0x40000000000000 18014398509481984
           78  +hexlit1 155 0x80000000000000 36028797018963968
           79  +hexlit1 156 0x100000000000000 72057594037927936
           80  +hexlit1 157 0x200000000000000 144115188075855872
           81  +hexlit1 158 0x400000000000000 288230376151711744
           82  +hexlit1 159 0x800000000000000 576460752303423488
           83  +hexlit1 160 0X1000000000000000 1152921504606846976
           84  +hexlit1 161 0x2000000000000000 2305843009213693952
           85  +hexlit1 162 0X4000000000000000 4611686018427387904
           86  +hexlit1 163 0x8000000000000000 -9223372036854775808
           87  +hexlit1 164 0XFFFFFFFFFFFFFFFF -1
           88  +
           89  +for {set n 1} {$n < 0x10} {incr n} {
           90  +  hexlit1 200.$n.1 0X[format %03X $n] $n
           91  +  hexlit1 200.$n.2 0x[format %03X $n] $n
           92  +  hexlit1 200.$n.3 0X[format %03x $n] $n
           93  +  hexlit1 200.$n.4 0x[format %03x $n] $n
           94  +}
           95  +
           96  +# String literals that look like hex do not get cast or coerced.
           97  +#
           98  +do_execsql_test hexlit-300 {
           99  +  CREATE TABLE t1(x INT, y REAL);
          100  +  INSERT INTO t1 VALUES('1234','4567'),('0x1234','0x4567');
          101  +  SELECT typeof(x), x, typeof(y), y, '#' FROM t1 ORDER BY rowid;
          102  +} {integer 1234 real 4567.0 # text 0x1234 text 0x4567 #}
          103  +do_execsql_test hexlit-301 {
          104  +  SELECT CAST('0x1234' AS INTEGER);
          105  +} {0}
          106  +
          107  +# Oversized hex literals are rejected
          108  +#
          109  +do_catchsql_test hexlist-400 {
          110  +  SELECT 0x10000000000000000;
          111  +} {1 {hex literal too big: 0x10000000000000000}}
          112  +
          113  +
          114  +finish_test

Changes to test/join.test.

    31     31       CREATE TABLE t2(b,c,d);
    32     32       INSERT INTO t2 VALUES(1,2,3);
    33     33       INSERT INTO t2 VALUES(2,3,4);
    34     34       INSERT INTO t2 VALUES(3,4,5);
    35     35       SELECT * FROM t2;
    36     36     }  
    37     37   } {1 2 3 2 3 4 3 4 5}
           38  +
           39  +# A FROM clause of the form:  "<table>, <table> ON <expr>" is not
           40  +# allowed by the SQLite syntax diagram, nor by any other SQL database
           41  +# engine that we are aware of.  Nevertheless, historic versions of
           42  +# SQLite have allowed it.  We need to continue to support it moving
           43  +# forward to prevent breakage of legacy applications.  Though, we will
           44  +# not advertise it as being supported.
           45  +#
           46  +do_execsql_test join-1.2.1 {
           47  +  SELECT t1.rowid, t2.rowid, '|' FROM t1, t2 ON t1.a=t2.b;
           48  +} {1 1 | 2 2 | 3 3 |}
    38     49   
    39     50   do_test join-1.3 {
    40     51     execsql2 {
    41     52       SELECT * FROM t1 NATURAL JOIN t2;
    42     53     }
    43     54   } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
    44     55   do_test join-1.3.1 {

Changes to test/shell1.test.

   584    584   CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
   585    585   CREATE VIEW v1 AS SELECT y+1 FROM v2;}}
   586    586   db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
   587    587   
   588    588   # .separator STRING      Change separator used by output mode and .import
   589    589   do_test shell1-3.22.1 {
   590    590     catchcmd "test.db" ".separator"
   591         -} {1 {Usage: .separator STRING}}
          591  +} {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
   592    592   do_test shell1-3.22.2 {
   593    593     catchcmd "test.db" ".separator FOO"
   594    594   } {0 {}}
   595    595   do_test shell1-3.22.3 {
          596  +  catchcmd "test.db" ".separator ABC XYZ"
          597  +} {0 {}}
          598  +do_test shell1-3.22.4 {
   596    599     # too many arguments
   597         -  catchcmd "test.db" ".separator FOO BAD"
   598         -} {1 {Usage: .separator STRING}}
          600  +  catchcmd "test.db" ".separator FOO BAD BAD2"
          601  +} {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
   599    602   
   600    603   # .show                  Show the current values for various settings
   601    604   do_test shell1-3.23.1 {
   602    605     set res [catchcmd "test.db" ".show"]
   603    606     list [regexp {echo:} $res] \
   604    607          [regexp {explain:} $res] \
   605    608          [regexp {headers:} $res] \

Changes to test/shell5.test.

    51     51     # too many arguments
    52     52     catchcmd "test.db" ".import FOO BAR BAD"
    53     53   } {1 {Usage: .import FILE TABLE}}
    54     54   
    55     55   # .separator STRING      Change separator used by output mode and .import
    56     56   do_test shell5-1.2.1 {
    57     57     catchcmd "test.db" ".separator"
    58         -} {1 {Usage: .separator STRING}}
           58  +} {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
    59     59   do_test shell5-1.2.2 {
    60         -  catchcmd "test.db" ".separator FOO"
           60  +  catchcmd "test.db" ".separator ONE"
    61     61   } {0 {}}
    62     62   do_test shell5-1.2.3 {
           63  +  catchcmd "test.db" ".separator ONE TWO"
           64  +} {0 {}}
           65  +do_test shell5-1.2.4 {
    63     66     # too many arguments
    64         -  catchcmd "test.db" ".separator FOO BAD"
    65         -} {1 {Usage: .separator STRING}}
           67  +  catchcmd "test.db" ".separator ONE TWO THREE"
           68  +} {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
    66     69   
    67     70   # separator should default to "|"
    68     71   do_test shell5-1.3.1 {
    69     72     set res [catchcmd "test.db" ".show"]
    70     73     list [regexp {separator: \"\|\"} $res]
    71     74   } {1}
    72     75   

Changes to test/where2.test.

   747    747     EXPLAIN QUERY PLAN
   748    748       SELECT a.x, b.x
   749    749         FROM t12 AS a JOIN t12 AS b ON a.y=b.x
   750    750        WHERE (b.x=$abc OR b.y=$abc);
   751    751   } {/.*SEARCH TABLE t12 AS b .*SEARCH TABLE t12 AS b .*/}
   752    752   }
   753    753   
          754  +# Verify that all necessary OP_OpenRead opcodes occur in the OR optimization.
          755  +#
          756  +do_execsql_test where2-13.1 {
          757  +  CREATE TABLE t13(a,b);
          758  +  CREATE INDEX t13a ON t13(a);
          759  +  INSERT INTO t13 VALUES(4,5);
          760  +  SELECT * FROM t13 WHERE (1=2 AND a=3) OR a=4;
          761  +} {4 5}
   754    762   
   755    763   finish_test

Changes to tool/showdb.c.

     5      5   #include <ctype.h>
     6      6   #include <sys/types.h>
     7      7   #include <sys/stat.h>
     8      8   #include <fcntl.h>
     9      9   
    10     10   #if !defined(_MSC_VER)
    11     11   #include <unistd.h>
           12  +#else
           13  +#include <io.h>
    12     14   #endif
    13     15   
    14     16   #include <stdlib.h>
    15     17   #include <string.h>
    16     18   #include "sqlite3.h"
    17     19   
    18     20   
................................................................................
   137    139     int ofst, int nByte,       /* Start and size of decode */
   138    140     const char *zMsg           /* Message to append */
   139    141   ){
   140    142     int i, j;
   141    143     int val = aData[ofst];
   142    144     char zBuf[100];
   143    145     sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
   144         -  i = strlen(zBuf);
          146  +  i = (int)strlen(zBuf);
   145    147     for(j=1; j<4; j++){
   146    148       if( j>=nByte ){
   147    149         sprintf(&zBuf[i], "   ");
   148    150       }else{
   149    151         sprintf(&zBuf[i], " %02x", aData[ofst+j]);
   150    152         val = val*256 + aData[ofst+j];
   151    153       }
   152         -    i += strlen(&zBuf[i]);
          154  +    i += (int)strlen(&zBuf[i]);
   153    155     }
   154    156     sprintf(&zBuf[i], "   %9d", val);
   155    157     printf("%s  %s\n", zBuf, zMsg);
   156    158   }
   157    159   
   158    160   /*
   159    161   ** Decode the database header.
................................................................................
   186    188     print_decode_line(aData, 92, 4, "Change counter for version number");
   187    189     print_decode_line(aData, 96, 4, "SQLite version number");
   188    190   }
   189    191   
   190    192   /*
   191    193   ** Describe cell content.
   192    194   */
   193         -static int describeContent(
          195  +static i64 describeContent(
   194    196     unsigned char *a,       /* Cell content */
   195         -  int nLocal,             /* Bytes in a[] */
          197  +  i64 nLocal,             /* Bytes in a[] */
   196    198     char *zDesc             /* Write description here */
   197    199   ){
   198         -  int nDesc = 0;
   199         -  int n, i, j;
   200         -  i64 x, v;
          200  +  i64 nDesc = 0;
          201  +  int n, j;
          202  +  i64 i, x, v;
   201    203     const unsigned char *pData;
   202    204     const unsigned char *pLimit;
   203    205     char sep = ' ';
   204    206   
   205    207     pLimit = &a[nLocal];
   206    208     n = decodeVarint(a, &x);
   207    209     pData = &a[x];
................................................................................
   233    235         sprintf(zDesc, "real");
   234    236         pData += 8;
   235    237       }else if( x==8 ){
   236    238         sprintf(zDesc, "0");
   237    239       }else if( x==9 ){
   238    240         sprintf(zDesc, "1");
   239    241       }else if( x>=12 ){
   240         -      int size = (x-12)/2;
          242  +      i64 size = (x-12)/2;
   241    243         if( (x&1)==0 ){
   242         -        sprintf(zDesc, "blob(%d)", size);
          244  +        sprintf(zDesc, "blob(%lld)", size);
   243    245         }else{
   244         -        sprintf(zDesc, "txt(%d)", size);
          246  +        sprintf(zDesc, "txt(%lld)", size);
   245    247         }
   246    248         pData += size;
   247    249       }
   248         -    j = strlen(zDesc);
          250  +    j = (int)strlen(zDesc);
   249    251       zDesc += j;
   250    252       nDesc += j;
   251    253     }
   252    254     return nDesc;
   253    255   }
   254    256   
   255    257   /*
   256    258   ** Compute the local payload size given the total payload size and
   257    259   ** the page size.
   258    260   */
   259         -static int localPayload(i64 nPayload, char cType){
   260         -  int maxLocal;
   261         -  int minLocal;
   262         -  int surplus;
   263         -  int nLocal;
          261  +static i64 localPayload(i64 nPayload, char cType){
          262  +  i64 maxLocal;
          263  +  i64 minLocal;
          264  +  i64 surplus;
          265  +  i64 nLocal;
   264    266     if( cType==13 ){
   265    267       /* Table leaf */
   266    268       maxLocal = pagesize-35;
   267    269       minLocal = (pagesize-12)*32/255-23;
   268    270     }else{
   269    271       maxLocal = (pagesize-12)*64/255-23;
   270    272       minLocal = (pagesize-12)*32/255-23;
................................................................................
   284    286     
   285    287   
   286    288   /*
   287    289   ** Create a description for a single cell.
   288    290   **
   289    291   ** The return value is the local cell size.
   290    292   */
   291         -static int describeCell(
          293  +static i64 describeCell(
   292    294     unsigned char cType,    /* Page type */
   293    295     unsigned char *a,       /* Cell content */
   294    296     int showCellContent,    /* Show cell content if true */
   295    297     char **pzDesc           /* Store description here */
   296    298   ){
   297    299     int i;
   298         -  int nDesc = 0;
          300  +  i64 nDesc = 0;
   299    301     int n = 0;
   300    302     int leftChild;
   301    303     i64 nPayload;
   302    304     i64 rowid;
   303         -  int nLocal;
          305  +  i64 nLocal;
   304    306     static char zDesc[1000];
   305    307     i = 0;
   306    308     if( cType<=5 ){
   307    309       leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
   308    310       a += 4;
   309    311       n += 4;
   310    312       sprintf(zDesc, "lx: %d ", leftChild);
................................................................................
   369    371   static void decodeCell(
   370    372     unsigned char *a,       /* Page content (without the page-1 header) */
   371    373     unsigned pgno,          /* Page number */
   372    374     int iCell,              /* Cell index */
   373    375     int szPgHdr,            /* Size of the page header.  0 or 100 */
   374    376     int ofst                /* Cell begins at a[ofst] */
   375    377   ){
   376         -  int i, j, k;
          378  +  int i, j;
   377    379     int leftChild;
          380  +  i64 k;
   378    381     i64 nPayload;
   379    382     i64 rowid;
   380    383     i64 nHdr;
   381    384     i64 iType;
   382         -  int nLocal;
          385  +  i64 nLocal;
   383    386     unsigned char *x = a + ofst;
   384    387     unsigned char *end;
   385    388     unsigned char cType = a[0];
   386    389     int nCol = 0;
   387    390     int szCol[2000];
   388    391     int ofstCol[2000];
   389    392     int typeCol[2000];
................................................................................
   396    399       x += 4;
   397    400     }
   398    401     if( cType!=5 ){
   399    402       i = decodeVarint(x, &nPayload);
   400    403       printBytes(a, x, i);
   401    404       nLocal = localPayload(nPayload, cType);
   402    405       if( nLocal==nPayload ){
   403         -      printf("payload-size: %d\n", (int)nPayload);
          406  +      printf("payload-size: %lld\n", nPayload);
   404    407       }else{
   405         -      printf("payload-size: %d (%d local, %d overflow)\n",
   406         -             (int)nPayload, nLocal, (int)(nPayload-nLocal));
          408  +      printf("payload-size: %lld (%lld local, %lld overflow)\n",
          409  +             nPayload, nLocal, nPayload-nLocal);
   407    410       }
   408    411       x += i;
   409    412     }else{
   410    413       nPayload = nLocal = 0;
   411    414     }
   412    415     end = x + nLocal;
   413    416     if( cType==5 || cType==13 ){
................................................................................
   448    451              sprintf(zNm, (iType&1)==0 ? "blob(%d)" : "text(%d)", sz);
   449    452              zTypeName = zNm;
   450    453              break;
   451    454            }
   452    455          }
   453    456          printf("%s\n", zTypeName);
   454    457          szCol[nCol] = sz;
   455         -       ofstCol[nCol] = k;
          458  +       ofstCol[nCol] = (int)k;
   456    459          typeCol[nCol] = (int)iType;
   457    460          k += sz;
   458    461          nCol++;
   459    462          j += i;
   460    463       }
   461    464       for(i=0; i<nCol && ofstCol[i]+szCol[i]<=nLocal; i++){
   462    465          int s = ofstCol[i];
................................................................................
   502    505            printf("%s\n", zConst);
   503    506          }
   504    507          j = ofstCol[i] + szCol[i];
   505    508       }
   506    509     }
   507    510     if( j<nLocal ){
   508    511       printBytes(a, x+j, 0);
   509         -    printf("... %d bytes of content ...\n", nLocal-j);
          512  +    printf("... %lld bytes of content ...\n", nLocal-j);
   510    513     }
   511    514     if( nLocal<nPayload ){
   512    515       printBytes(a, x+nLocal, 4);
   513    516       printf("overflow-page: %d\n", decodeInt32(x+nLocal));
   514    517     }
   515    518   }
   516    519   
................................................................................
   581    584       memset(zMap, '1', hdrSize);
   582    585       memset(&zMap[hdrSize], 'H', iCellPtr);
   583    586       memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
   584    587     }
   585    588     for(i=0; i<nCell; i++){
   586    589       int cofst = iCellPtr + i*2;
   587    590       char *zDesc;
   588         -    int n;
          591  +    i64 n;
   589    592   
   590    593       cofst = a[cofst]*256 + a[cofst+1];
   591    594       n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
   592    595       if( showMap ){
   593    596         char zBuf[30];
   594         -      memset(&zMap[cofst], '*', n);
          597  +      memset(&zMap[cofst], '*', (size_t)n);
   595    598         zMap[cofst] = '[';
   596    599         zMap[cofst+n-1] = ']';
   597    600         sprintf(zBuf, "%d", i);
   598         -      j = strlen(zBuf);
          601  +      j = (int)strlen(zBuf);
   599    602         if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
   600    603       }
   601    604       if( cellToDecode==(-2) ){
   602    605         printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
   603    606       }else if( cellToDecode==(-1) || cellToDecode==i ){
   604    607         decodeCell(a, pgno, i, hdrSize, cofst-hdrSize);
   605    608       }
................................................................................
   688    691     int pgno,               /* page containing the cell */
   689    692     int cellno              /* Index of the cell on the page */
   690    693   ){
   691    694     int i;
   692    695     int n = 0;
   693    696     i64 nPayload;
   694    697     i64 rowid;
   695         -  int nLocal;
          698  +  i64 nLocal;
   696    699     i = 0;
   697    700     if( cType<=5 ){
   698    701       a += 4;
   699    702       n += 4;
   700    703     }
   701    704     if( cType!=5 ){
   702    705       i = decodeVarint(a, &nPayload);
................................................................................
   889    892     zPageUse = 0;
   890    893   }
   891    894   
   892    895   /*
   893    896   ** Try to figure out how every page in the database file is being used.
   894    897   */
   895    898   static void ptrmap_coverage_report(const char *zDbName){
   896         -  unsigned int pgno;
          899  +  int pgno;
   897    900     unsigned char *aHdr;
   898    901     unsigned char *a;
   899    902     int usable;
   900    903     int perPage;
   901         -  unsigned int i;
          904  +  int i;
   902    905   
   903    906     /* Avoid the pathological case */
   904    907     if( mxPage<1 ){
   905    908       printf("empty database\n");
   906    909       return;
   907    910     }
   908    911   

Changes to tool/showwal.c.

     2      2   ** A utility for printing content from a write-ahead log file.
     3      3   */
     4      4   #include <stdio.h>
     5      5   #include <ctype.h>
     6      6   #include <sys/types.h>
     7      7   #include <sys/stat.h>
     8      8   #include <fcntl.h>
            9  +
           10  +#if !defined(_MSC_VER)
     9     11   #include <unistd.h>
           12  +#else
           13  +#include <io.h>
           14  +#endif
           15  +
    10     16   #include <stdlib.h>
    11     17   #include <string.h>
    12     18   
    13     19   
    14     20   static int pagesize = 1024;     /* Size of a database page */
    15     21   static int fd = -1;             /* File descriptor for reading the WAL file */
    16     22   static int mxFrame = 0;         /* Last frame */
................................................................................
   168    174     int asHex,                 /* If true, output value as hex */
   169    175     const char *zMsg           /* Message to append */
   170    176   ){
   171    177     int i, j;
   172    178     int val = aData[ofst];
   173    179     char zBuf[100];
   174    180     sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
   175         -  i = strlen(zBuf);
          181  +  i = (int)strlen(zBuf);
   176    182     for(j=1; j<4; j++){
   177    183       if( j>=nByte ){
   178    184         sprintf(&zBuf[i], "   ");
   179    185       }else{
   180    186         sprintf(&zBuf[i], " %02x", aData[ofst+j]);
   181    187         val = val*256 + aData[ofst+j];
   182    188       }
   183         -    i += strlen(&zBuf[i]);
          189  +    i += (int)strlen(&zBuf[i]);
   184    190     }
   185    191     if( asHex ){
   186    192       sprintf(&zBuf[i], "  0x%08x", val);
   187    193     }else{
   188    194       sprintf(&zBuf[i], "   %9d", val);
   189    195     }
   190    196     printf("%s  %s\n", zBuf, zMsg);
................................................................................
   269    275       }
   270    276     }
   271    277     free(aData);
   272    278   }
   273    279   /*
   274    280   ** Describe cell content.
   275    281   */
   276         -static int describeContent(
          282  +static i64 describeContent(
   277    283     unsigned char *a,       /* Cell content */
   278         -  int nLocal,             /* Bytes in a[] */
          284  +  i64 nLocal,             /* Bytes in a[] */
   279    285     char *zDesc             /* Write description here */
   280    286   ){
   281    287     int nDesc = 0;
   282         -  int n, i, j;
   283         -  i64 x, v;
          288  +  int n, j;
          289  +  i64 i, x, v;
   284    290     const unsigned char *pData;
   285    291     const unsigned char *pLimit;
   286    292     char sep = ' ';
   287    293   
   288    294     pLimit = &a[nLocal];
   289    295     n = decodeVarint(a, &x);
   290    296     pData = &a[x];
................................................................................
   316    322         sprintf(zDesc, "real");
   317    323         pData += 8;
   318    324       }else if( x==8 ){
   319    325         sprintf(zDesc, "0");
   320    326       }else if( x==9 ){
   321    327         sprintf(zDesc, "1");
   322    328       }else if( x>=12 ){
   323         -      int size = (x-12)/2;
          329  +      i64 size = (x-12)/2;
   324    330         if( (x&1)==0 ){
   325         -        sprintf(zDesc, "blob(%d)", size);
          331  +        sprintf(zDesc, "blob(%lld)", size);
   326    332         }else{
   327         -        sprintf(zDesc, "txt(%d)", size);
          333  +        sprintf(zDesc, "txt(%lld)", size);
   328    334         }
   329    335         pData += size;
   330    336       }
   331         -    j = strlen(zDesc);
          337  +    j = (int)strlen(zDesc);
   332    338       zDesc += j;
   333    339       nDesc += j;
   334    340     }
   335    341     return nDesc;
   336    342   }
   337    343   
   338    344   /*
   339    345   ** Compute the local payload size given the total payload size and
   340    346   ** the page size.
   341    347   */
   342         -static int localPayload(i64 nPayload, char cType){
   343         -  int maxLocal;
   344         -  int minLocal;
   345         -  int surplus;
   346         -  int nLocal;
          348  +static i64 localPayload(i64 nPayload, char cType){
          349  +  i64 maxLocal;
          350  +  i64 minLocal;
          351  +  i64 surplus;
          352  +  i64 nLocal;
   347    353     if( cType==13 ){
   348    354       /* Table leaf */
   349    355       maxLocal = pagesize-35;
   350    356       minLocal = (pagesize-12)*32/255-23;
   351    357     }else{
   352    358       maxLocal = (pagesize-12)*64/255-23;
   353    359       minLocal = (pagesize-12)*32/255-23;
................................................................................
   366    372   }
   367    373   
   368    374   /*
   369    375   ** Create a description for a single cell.
   370    376   **
   371    377   ** The return value is the local cell size.
   372    378   */
   373         -static int describeCell(
          379  +static i64 describeCell(
   374    380     unsigned char cType,    /* Page type */
   375    381     unsigned char *a,       /* Cell content */
   376    382     int showCellContent,    /* Show cell content if true */
   377    383     char **pzDesc           /* Store description here */
   378    384   ){
   379    385     int i;
   380         -  int nDesc = 0;
          386  +  i64 nDesc = 0;
   381    387     int n = 0;
   382    388     int leftChild;
   383    389     i64 nPayload;
   384    390     i64 rowid;
   385         -  int nLocal;
          391  +  i64 nLocal;
   386    392     static char zDesc[1000];
   387    393     i = 0;
   388    394     if( cType<=5 ){
   389    395       leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
   390    396       a += 4;
   391    397       n += 4;
   392    398       sprintf(zDesc, "lx: %d ", leftChild);
................................................................................
   475    481       memset(zMap, '1', hdrSize);
   476    482       memset(&zMap[hdrSize], 'H', iCellPtr);
   477    483       memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
   478    484     }
   479    485     for(i=0; i<nCell; i++){
   480    486       int cofst = iCellPtr + i*2;
   481    487       char *zDesc;
   482         -    int n;
          488  +    i64 n;
   483    489   
   484    490       cofst = a[cofst]*256 + a[cofst+1];
   485    491       n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
   486    492       if( showMap ){
   487    493         char zBuf[30];
   488         -      memset(&zMap[cofst], '*', n);
          494  +      memset(&zMap[cofst], '*', (size_t)n);
   489    495         zMap[cofst] = '[';
   490    496         zMap[cofst+n-1] = ']';
   491    497         sprintf(zBuf, "%d", i);
   492         -      j = strlen(zBuf);
          498  +      j = (int)strlen(zBuf);
   493    499         if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
   494    500       }
   495    501       printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
   496    502     }
   497    503     if( showMap ){
   498    504       for(i=0; i<pagesize; i+=64){
   499    505         printf(" %03x: %.64s\n", i, &zMap[i]);