/ Check-in [69a312ad]
Login

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

Overview
Comment:Update this branch with latest trunk changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | ota-update
Files: files | file ages | folders
SHA1: 69a312ad3fe5b39bc394b9ce958cb63d734518c7
User & Date: dan 2014-12-08 07:50:31
Context
2015-01-28
12:00
Merge in all changes from trunk. check-in: 17c69be8 user: drh tags: ota-update
2014-12-08
07:50
Update this branch with latest trunk changes. check-in: 69a312ad user: dan tags: ota-update
07:28
Update comments in sqlite3ota.h to remove the "must have PRIMARY KEY" restriction. check-in: 088a41eb user: dan tags: ota-update
2014-12-06
14:56
Avoid accessing a single uninitialized byte when moving a rare 3-byte cell from an internal page to a leaf. This was not actually causing a problem, just a valgrind warning. check-in: 6aeece19 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   355    355     $(TOP)/src/test6.c \
   356    356     $(TOP)/src/test7.c \
   357    357     $(TOP)/src/test8.c \
   358    358     $(TOP)/src/test9.c \
   359    359     $(TOP)/src/test_autoext.c \
   360    360     $(TOP)/src/test_async.c \
   361    361     $(TOP)/src/test_backup.c \
          362  +  $(TOP)/src/test_blob.c \
   362    363     $(TOP)/src/test_btree.c \
   363    364     $(TOP)/src/test_config.c \
   364    365     $(TOP)/src/test_demovfs.c \
   365    366     $(TOP)/src/test_devsym.c \
   366    367     $(TOP)/src/test_fs.c \
   367    368     $(TOP)/src/test_func.c \
   368    369     $(TOP)/src/test_hexio.c \

Changes to Makefile.msc.

   824    824     $(TOP)\src\test6.c \
   825    825     $(TOP)\src\test7.c \
   826    826     $(TOP)\src\test8.c \
   827    827     $(TOP)\src\test9.c \
   828    828     $(TOP)\src\test_autoext.c \
   829    829     $(TOP)\src\test_async.c \
   830    830     $(TOP)\src\test_backup.c \
          831  +  $(TOP)\src\test_blob.c \
   831    832     $(TOP)\src\test_btree.c \
   832    833     $(TOP)\src\test_config.c \
   833    834     $(TOP)\src\test_demovfs.c \
   834    835     $(TOP)\src\test_devsym.c \
   835    836     $(TOP)\src\test_fs.c \
   836    837     $(TOP)\src\test_func.c \
   837    838     $(TOP)\src\test_hexio.c \

Changes to ext/fts3/fts3_porter.c.

   179    179   ** of m for the first i bytes of a word.
   180    180   **
   181    181   ** Return true if the m-value for z is 1 or more.  In other words,
   182    182   ** return true if z contains at least one vowel that is followed
   183    183   ** by a consonant.
   184    184   **
   185    185   ** In this routine z[] is in reverse order.  So we are really looking
   186         -** for an instance of of a consonant followed by a vowel.
          186  +** for an instance of a consonant followed by a vowel.
   187    187   */
   188    188   static int m_gt_0(const char *z){
   189    189     while( isVowel(z) ){ z++; }
   190    190     if( *z==0 ) return 0;
   191    191     while( isConsonant(z) ){ z++; }
   192    192     return *z!=0;
   193    193   }

Changes to ext/misc/eval.c.

    17     17   SQLITE_EXTENSION_INIT1
    18     18   #include <string.h>
    19     19   
    20     20   /*
    21     21   ** Structure used to accumulate the output
    22     22   */
    23     23   struct EvalResult {
    24         -  char *z;            /* Accumulated output */
    25         -  const char *zSep;   /* Separator */
    26         -  int szSep;          /* Size of the separator string */
    27         -  int nAlloc;         /* Number of bytes allocated for z[] */
    28         -  int nUsed;          /* Number of bytes of z[] actually used */
           24  +  char *z;               /* Accumulated output */
           25  +  const char *zSep;      /* Separator */
           26  +  int szSep;             /* Size of the separator string */
           27  +  sqlite3_int64 nAlloc;  /* Number of bytes allocated for z[] */
           28  +  sqlite3_int64 nUsed;   /* Number of bytes of z[] actually used */
    29     29   };
    30     30   
    31     31   /*
    32     32   ** Callback from sqlite_exec() for the eval() function.
    33     33   */
    34     34   static int callback(void *pCtx, int argc, char **argv, char **colnames){
    35     35     struct EvalResult *p = (struct EvalResult*)pCtx;
    36     36     int i; 
    37     37     for(i=0; i<argc; i++){
    38     38       const char *z = argv[i] ? argv[i] : "";
    39     39       size_t sz = strlen(z);
    40         -    if( sz+p->nUsed+p->szSep+1 > p->nAlloc ){
           40  +    if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){
    41     41         char *zNew;
    42     42         p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1;
    43         -      zNew = sqlite3_realloc(p->z, p->nAlloc);
           43  +      /* Using sqlite3_realloc64() would be better, but it is a recent
           44  +      ** addition and will cause a segfault if loaded by an older version
           45  +      ** of SQLite.  */
           46  +      zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0;
    44     47         if( zNew==0 ){
    45     48           sqlite3_free(p->z);
    46     49           memset(p, 0, sizeof(*p));
    47     50           return 1;
    48     51         }
    49     52         p->z = zNew;
    50     53       }
................................................................................
    89     92     if( rc!=SQLITE_OK ){
    90     93       sqlite3_result_error(context, zErr, -1);
    91     94       sqlite3_free(zErr);
    92     95     }else if( x.zSep==0 ){
    93     96       sqlite3_result_error_nomem(context);
    94     97       sqlite3_free(x.z);
    95     98     }else{
    96         -    sqlite3_result_text(context, x.z, x.nUsed, sqlite3_free);
           99  +    sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free);
    97    100     }
    98    101   }
    99    102   
   100    103   
   101    104   #ifdef _WIN32
   102    105   __declspec(dllexport)
   103    106   #endif

Changes to src/analyze.c.

  1434   1434     int i;
  1435   1435     tRowcnt v;
  1436   1436   
  1437   1437   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1438   1438     if( z==0 ) z = "";
  1439   1439   #else
  1440   1440     assert( z!=0 );
  1441         -  pIndex->bUnordered = 0;
  1442         -  pIndex->noSkipScan = 0;
  1443   1441   #endif
  1444   1442     for(i=0; *z && i<nOut; i++){
  1445   1443       v = 0;
  1446   1444       while( (c=z[0])>='0' && c<='9' ){
  1447   1445         v = v*10 + c - '0';
  1448   1446         z++;
  1449   1447       }
................................................................................
  1455   1453       UNUSED_PARAMETER(aOut);
  1456   1454       assert( aLog!=0 );
  1457   1455       aLog[i] = sqlite3LogEst(v);
  1458   1456   #endif
  1459   1457       if( *z==' ' ) z++;
  1460   1458     }
  1461   1459   #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
  1462         -  assert( pIndex!=0 );
         1460  +  assert( pIndex!=0 ); {
  1463   1461   #else
  1464         -  if( pIndex )
         1462  +  if( pIndex ){
  1465   1463   #endif
  1466         -  while( z[0] ){
  1467         -    if( sqlite3_strglob("unordered*", z)==0 ){
  1468         -      pIndex->bUnordered = 1;
  1469         -    }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
  1470         -      pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
  1471         -    }else if( sqlite3_strglob("noskipscan*", z)==0 ){
  1472         -      pIndex->noSkipScan = 1;
  1473         -    }
         1464  +    pIndex->bUnordered = 0;
         1465  +    pIndex->noSkipScan = 0;
         1466  +    while( z[0] ){
         1467  +      if( sqlite3_strglob("unordered*", z)==0 ){
         1468  +        pIndex->bUnordered = 1;
         1469  +      }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
         1470  +        pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
         1471  +      }else if( sqlite3_strglob("noskipscan*", z)==0 ){
         1472  +        pIndex->noSkipScan = 1;
         1473  +      }
  1474   1474   #ifdef SQLITE_ENABLE_COSTMULT
  1475         -    else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
  1476         -      pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
  1477         -    }
         1475  +      else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
         1476  +        pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
         1477  +      }
  1478   1478   #endif
  1479         -    while( z[0]!=0 && z[0]!=' ' ) z++;
  1480         -    while( z[0]==' ' ) z++;
         1479  +      while( z[0]!=0 && z[0]!=' ' ) z++;
         1480  +      while( z[0]==' ' ) z++;
         1481  +    }
  1481   1482     }
  1482   1483   }
  1483   1484   
  1484   1485   /*
  1485   1486   ** This callback is invoked once for each index when reading the
  1486   1487   ** sqlite_stat1 table.  
  1487   1488   **
................................................................................
  1591   1592         int i;                    /* Used to iterate through samples */
  1592   1593         tRowcnt sumEq = 0;        /* Sum of the nEq values */
  1593   1594         tRowcnt avgEq = 0;
  1594   1595         tRowcnt nRow;             /* Number of rows in index */
  1595   1596         i64 nSum100 = 0;          /* Number of terms contributing to sumEq */
  1596   1597         i64 nDist100;             /* Number of distinct values in index */
  1597   1598   
  1598         -      if( pIdx->aiRowEst==0 || pIdx->aiRowEst[iCol+1]==0 ){
         1599  +      if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
  1599   1600           nRow = pFinal->anLt[iCol];
  1600   1601           nDist100 = (i64)100 * pFinal->anDLt[iCol];
  1601   1602           nSample--;
  1602   1603         }else{
  1603   1604           nRow = pIdx->aiRowEst[0];
  1604   1605           nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
  1605   1606         }

Changes to src/btree.c.

  1353   1353   
  1354   1354     /* The request could not be fulfilled using a freelist slot.  Check
  1355   1355     ** to see if defragmentation is necessary.
  1356   1356     */
  1357   1357     testcase( gap+2+nByte==top );
  1358   1358     if( gap+2+nByte>top ){
  1359   1359    defragment_page:
  1360         -    testcase( pPage->nCell==0 );
         1360  +    assert( pPage->nCell>0 || CORRUPT_DB );
  1361   1361       rc = defragmentPage(pPage);
  1362   1362       if( rc ) return rc;
  1363   1363       top = get2byteNotZero(&data[hdr+5]);
  1364   1364       assert( gap+nByte<=top );
  1365   1365     }
  1366   1366   
  1367   1367   
................................................................................
  6254   6254       assert( pFree>aData && (pFree - aData)<65536 );
  6255   6255       freeSpace(pPg, (u16)(pFree - aData), szFree);
  6256   6256     }
  6257   6257     return nRet;
  6258   6258   }
  6259   6259   
  6260   6260   /*
         6261  +** apCell[] and szCell[] contains pointers to and sizes of all cells in the
         6262  +** pages being balanced.  The current page, pPg, has pPg->nCell cells starting
         6263  +** with apCell[iOld].  After balancing, this page should hold nNew cells
         6264  +** starting at apCell[iNew].
         6265  +**
         6266  +** This routine makes the necessary adjustments to pPg so that it contains
         6267  +** the correct cells after being balanced.
         6268  +**
  6261   6269   ** The pPg->nFree field is invalid when this function returns. It is the
  6262   6270   ** responsibility of the caller to set it correctly.
  6263   6271   */
  6264   6272   static void editPage(
  6265   6273     MemPage *pPg,                   /* Edit this page */
  6266   6274     int iOld,                       /* Index of first cell currently on page */
  6267   6275     int iNew,                       /* Index of new first cell on page */
................................................................................
  6294   6302     }
  6295   6303     if( iNewEnd < iOldEnd ){
  6296   6304       nCell -= pageFreeArray(
  6297   6305           pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd]
  6298   6306       );
  6299   6307     }
  6300   6308   
  6301         -  pData = &aData[get2byte(&aData[hdr+5])];
         6309  +  pData = &aData[get2byteNotZero(&aData[hdr+5])];
  6302   6310     if( pData<pBegin ) goto editpage_fail;
  6303   6311   
  6304   6312     /* Add cells to the start of the page */
  6305   6313     if( iNew<iOld ){
  6306         -    int nAdd = iOld-iNew;
         6314  +    int nAdd = MIN(nNew,iOld-iNew);
         6315  +    assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
  6307   6316       pCellptr = pPg->aCellIdx;
  6308   6317       memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
  6309   6318       if( pageInsertArray(
  6310   6319             pPg, pBegin, &pData, pCellptr,
  6311   6320             nAdd, &apCell[iNew], &szCell[iNew]
  6312   6321       ) ) goto editpage_fail;
  6313   6322       nCell += nAdd;
................................................................................
  6404   6413     Pgno pgnoNew;                        /* Page number of pNew */
  6405   6414   
  6406   6415     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  6407   6416     assert( sqlite3PagerIswriteable(pParent->pDbPage) );
  6408   6417     assert( pPage->nOverflow==1 );
  6409   6418   
  6410   6419     /* This error condition is now caught prior to reaching this function */
  6411         -  if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;
         6420  +  if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
  6412   6421   
  6413   6422     /* Allocate a new page. This page will become the right-sibling of 
  6414   6423     ** pPage. Make the parent page writable, so that the new divider cell
  6415   6424     ** may be inserted. If both these operations are successful, proceed.
  6416   6425     */
  6417   6426     rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
  6418   6427   
................................................................................
  6847   6856           assert( pOld->hdrOffset==0 );
  6848   6857           /* The right pointer of the child page pOld becomes the left
  6849   6858           ** pointer of the divider cell */
  6850   6859           memcpy(apCell[nCell], &pOld->aData[8], 4);
  6851   6860         }else{
  6852   6861           assert( leafCorrection==4 );
  6853   6862           if( szCell[nCell]<4 ){
  6854         -          /* Do not allow any cells smaller than 4 bytes. */
         6863  +          /* Do not allow any cells smaller than 4 bytes. If a smaller cell
         6864  +          ** does exist, pad it with 0x00 bytes. */
         6865  +          assert( szCell[nCell]==3 );
         6866  +          assert( apCell[nCell]==&pTemp[iSpace1-3] );
         6867  +          pTemp[iSpace1++] = 0x00;
  6855   6868             szCell[nCell] = 4;
  6856   6869           }
  6857   6870         }
  6858   6871         nCell++;
  6859   6872       }
  6860   6873     }
  6861   6874   
................................................................................
  7082   7095   
  7083   7096         /* Cell pCell is destined for new sibling page pNew. Originally, it
  7084   7097         ** was either part of sibling page iOld (possibly an overflow cell), 
  7085   7098         ** or else the divider cell to the left of sibling page iOld. So,
  7086   7099         ** if sibling page iOld had the same page number as pNew, and if
  7087   7100         ** pCell really was a part of sibling page iOld (not a divider or
  7088   7101         ** overflow cell), we can skip updating the pointer map entries.  */
  7089         -      if( pNew->pgno!=aPgno[iOld] || pCell<aOld || pCell>=&aOld[usableSize] ){
         7102  +      if( iOld>=nNew
         7103  +       || pNew->pgno!=aPgno[iOld]
         7104  +       || pCell<aOld
         7105  +       || pCell>=&aOld[usableSize]
         7106  +      ){
  7090   7107           if( !leafCorrection ){
  7091   7108             ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
  7092   7109           }
  7093   7110           if( szCell[i]>pNew->minLocal ){
  7094   7111             ptrmapPutOvflPtr(pNew, pCell, &rc);
  7095   7112           }
  7096   7113         }

Changes to src/expr.c.

  1410   1410       case TK_INTEGER:
  1411   1411       case TK_STRING:
  1412   1412       case TK_FLOAT:
  1413   1413       case TK_BLOB:
  1414   1414         return 0;
  1415   1415       case TK_COLUMN:
  1416   1416         assert( p->pTab!=0 );
  1417         -      return p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0;
         1417  +      return ExprHasProperty(p, EP_CanBeNull) ||
         1418  +             (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
  1418   1419       default:
  1419   1420         return 1;
  1420   1421     }
  1421   1422   }
  1422   1423   
  1423   1424   /*
  1424   1425   ** Return TRUE if the given expression is a constant which would be

Changes to src/main.c.

  1028   1028     /* Free any outstanding Savepoint structures. */
  1029   1029     sqlite3CloseSavepoints(db);
  1030   1030   
  1031   1031     /* Close all database connections */
  1032   1032     for(j=0; j<db->nDb; j++){
  1033   1033       struct Db *pDb = &db->aDb[j];
  1034   1034       if( pDb->pBt ){
         1035  +      if( pDb->pSchema ){
         1036  +        /* Must clear the KeyInfo cache.  See ticket [e4a18565a36884b00edf] */
         1037  +        for(i=sqliteHashFirst(&pDb->pSchema->idxHash); i; i=sqliteHashNext(i)){
         1038  +          Index *pIdx = sqliteHashData(i);
         1039  +          sqlite3KeyInfoUnref(pIdx->pKeyInfo);
         1040  +          pIdx->pKeyInfo = 0;
         1041  +        }
         1042  +      }
  1035   1043         sqlite3BtreeClose(pDb->pBt);
  1036   1044         pDb->pBt = 0;
  1037   1045         if( j!=1 ){
  1038   1046           pDb->pSchema = 0;
  1039   1047         }
  1040   1048       }
  1041   1049     }
................................................................................
  1932   1940     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
  1933   1941   #endif
  1934   1942   
  1935   1943     /* Initialize the output variables to -1 in case an error occurs. */
  1936   1944     if( pnLog ) *pnLog = -1;
  1937   1945     if( pnCkpt ) *pnCkpt = -1;
  1938   1946   
  1939         -  assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
  1940         -  assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
  1941         -  assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
  1942         -  if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
         1947  +  assert( SQLITE_CHECKPOINT_PASSIVE==0 );
         1948  +  assert( SQLITE_CHECKPOINT_FULL==1 );
         1949  +  assert( SQLITE_CHECKPOINT_RESTART==2 );
         1950  +  assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
         1951  +  if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
         1952  +    /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
         1953  +    ** mode: */
  1943   1954       return SQLITE_MISUSE;
  1944   1955     }
  1945   1956   
  1946   1957     sqlite3_mutex_enter(db->mutex);
  1947   1958     if( zDb && zDb[0] ){
  1948   1959       iDb = sqlite3FindDbName(db, zDb);
  1949   1960     }
................................................................................
  1990   2001   
  1991   2002   /*
  1992   2003   ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
  1993   2004   ** to contains a zero-length string, all attached databases are 
  1994   2005   ** checkpointed.
  1995   2006   */
  1996   2007   int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
  1997         -  return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
         2008  +  /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
         2009  +  ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
         2010  +  return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
  1998   2011   }
  1999   2012   
  2000   2013   #ifndef SQLITE_OMIT_WAL
  2001   2014   /*
  2002   2015   ** Run a checkpoint on database iDb. This is a no-op if database iDb is
  2003   2016   ** not currently open in WAL mode.
  2004   2017   **

Changes to src/pager.c.

  7117   7117   **
  7118   7118   ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
  7119   7119   */
  7120   7120   int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
  7121   7121     int rc = SQLITE_OK;
  7122   7122     if( pPager->pWal && PagerOtaMode(pPager)==0 ){
  7123   7123       rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
  7124         -        pPager->xBusyHandler, pPager->pBusyHandlerArg,
         7124  +        (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
         7125  +        pPager->pBusyHandlerArg,
  7125   7126           pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
  7126   7127           pnLog, pnCkpt
  7127   7128       );
  7128   7129     }
  7129   7130     return rc;
  7130   7131   }
  7131   7132   

Changes to src/pragma.c.

  2253   2253       }
  2254   2254     }
  2255   2255     break;
  2256   2256   #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  2257   2257   
  2258   2258   #ifndef SQLITE_OMIT_WAL
  2259   2259     /*
  2260         -  **   PRAGMA [database.]wal_checkpoint = passive|full|restart
         2260  +  **   PRAGMA [database.]wal_checkpoint = passive|full|restart|truncate
  2261   2261     **
  2262   2262     ** Checkpoint the database.
  2263   2263     */
  2264   2264     case PragTyp_WAL_CHECKPOINT: {
  2265   2265       int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
  2266   2266       int eMode = SQLITE_CHECKPOINT_PASSIVE;
  2267   2267       if( zRight ){
  2268   2268         if( sqlite3StrICmp(zRight, "full")==0 ){
  2269   2269           eMode = SQLITE_CHECKPOINT_FULL;
  2270   2270         }else if( sqlite3StrICmp(zRight, "restart")==0 ){
  2271   2271           eMode = SQLITE_CHECKPOINT_RESTART;
         2272  +      }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
         2273  +        eMode = SQLITE_CHECKPOINT_TRUNCATE;
  2272   2274         }
  2273   2275       }
  2274   2276       sqlite3VdbeSetNumCols(v, 3);
  2275   2277       pParse->nMem = 3;
  2276   2278       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
  2277   2279       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
  2278   2280       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);

Changes to src/resolve.c.

   316    316               break;
   317    317             }
   318    318           }
   319    319         }
   320    320         if( pMatch ){
   321    321           pExpr->iTable = pMatch->iCursor;
   322    322           pExpr->pTab = pMatch->pTab;
          323  +        assert( (pMatch->jointype & JT_RIGHT)==0 ); /* RIGHT JOIN not (yet) supported */
          324  +        if( (pMatch->jointype & JT_LEFT)!=0 ){
          325  +          ExprSetProperty(pExpr, EP_CanBeNull);
          326  +        }
   323    327           pSchema = pExpr->pTab->pSchema;
   324    328         }
   325    329       } /* if( pSrcList ) */
   326    330   
   327    331   #ifndef SQLITE_OMIT_TRIGGER
   328    332       /* If we have not already resolved the name, then maybe 
   329    333       ** it is a new.* or old.* trigger argument reference

Changes to src/select.c.

  4825   4825     ** if the select-list is the same as the ORDER BY list, then this query
  4826   4826     ** can be rewritten as a GROUP BY. In other words, this:
  4827   4827     **
  4828   4828     **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
  4829   4829     **
  4830   4830     ** is transformed to:
  4831   4831     **
  4832         -  **     SELECT xyz FROM ... GROUP BY xyz
         4832  +  **     SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
  4833   4833     **
  4834   4834     ** The second form is preferred as a single index (or temp-table) may be 
  4835   4835     ** used for both the ORDER BY and DISTINCT processing. As originally 
  4836   4836     ** written the query must use a temp-table for at least one of the ORDER 
  4837   4837     ** BY and DISTINCT, and an index or separate temp-table for the other.
  4838   4838     */
  4839   4839     if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
  4840   4840      && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0
  4841   4841     ){
  4842   4842       p->selFlags &= ~SF_Distinct;
  4843   4843       p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
  4844   4844       pGroupBy = p->pGroupBy;
  4845         -    sSort.pOrderBy = 0;
  4846   4845       /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
  4847   4846       ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
  4848   4847       ** original setting of the SF_Distinct flag, not the current setting */
  4849   4848       assert( sDistinct.isTnct );
  4850   4849     }
  4851   4850   
  4852   4851     /* If there is an ORDER BY clause, then this sorting

Changes to src/shell.c.

  4034   4034     return argv[i];
  4035   4035   }
  4036   4036   
  4037   4037   int main(int argc, char **argv){
  4038   4038     char *zErrMsg = 0;
  4039   4039     ShellState data;
  4040   4040     const char *zInitFile = 0;
  4041         -  char *zFirstCmd = 0;
  4042   4041     int i;
  4043   4042     int rc = 0;
  4044   4043     int warnInmemoryDb = 0;
         4044  +  int readStdin = 1;
         4045  +  int nCmd = 0;
         4046  +  char **azCmd = 0;
  4045   4047   
  4046   4048   #if USE_SYSTEM_SQLITE+0!=1
  4047   4049     if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
  4048   4050       fprintf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
  4049   4051               sqlite3_sourceid(), SQLITE_SOURCE_ID);
  4050   4052       exit(1);
  4051   4053     }
................................................................................
  4056   4058   
  4057   4059     /* Make sure we have a valid signal handler early, before anything
  4058   4060     ** else is done.
  4059   4061     */
  4060   4062   #ifdef SIGINT
  4061   4063     signal(SIGINT, interrupt_handler);
  4062   4064   #endif
         4065  +
         4066  +#ifdef SQLITE_SHELL_DBNAME_PROC
         4067  +  {
         4068  +    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
         4069  +    ** of a C-function that will provide the name of the database file.  Use
         4070  +    ** this compile-time option to embed this shell program in larger
         4071  +    ** applications. */
         4072  +    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
         4073  +    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
         4074  +    warnInmemoryDb = 0;
         4075  +  }
         4076  +#endif
  4063   4077   
  4064   4078     /* Do an initial pass through the command-line argument to locate
  4065   4079     ** the name of the database file, the name of the initialization file,
  4066   4080     ** the size of the alternative malloc heap,
  4067   4081     ** and the first command to execute.
  4068   4082     */
  4069   4083     for(i=1; i<argc; i++){
  4070   4084       char *z;
  4071   4085       z = argv[i];
  4072   4086       if( z[0]!='-' ){
  4073   4087         if( data.zDbFilename==0 ){
  4074   4088           data.zDbFilename = z;
  4075         -        continue;
         4089  +      }else{
         4090  +        /* Excesss arguments are interpreted as SQL (or dot-commands) and
         4091  +        ** mean that nothing is read from stdin */
         4092  +        readStdin = 0;
         4093  +        nCmd++;
         4094  +        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
         4095  +        if( azCmd==0 ){
         4096  +          fprintf(stderr, "out of memory\n");
         4097  +          exit(1);
         4098  +        }
         4099  +        azCmd[nCmd-1] = z;
  4076   4100         }
  4077         -      if( zFirstCmd==0 ){
  4078         -        zFirstCmd = z;
  4079         -        continue;
  4080         -      }
  4081         -      fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
  4082         -      fprintf(stderr,"Use -help for a list of options.\n");
  4083         -      return 1;
  4084   4101       }
  4085   4102       if( z[1]=='-' ) z++;
  4086   4103       if( strcmp(z,"-separator")==0
  4087   4104        || strcmp(z,"-nullvalue")==0
  4088   4105        || strcmp(z,"-newline")==0
  4089   4106        || strcmp(z,"-cmd")==0
  4090   4107       ){
................................................................................
  4167   4184   #ifndef SQLITE_OMIT_MEMORYDB
  4168   4185       data.zDbFilename = ":memory:";
  4169   4186       warnInmemoryDb = argc==1;
  4170   4187   #else
  4171   4188       fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
  4172   4189       return 1;
  4173   4190   #endif
  4174         -#ifdef SQLITE_SHELL_DBNAME_PROC
  4175         -    { extern void SQLITE_SHELL_DBNAME_PROC(const char**);
  4176         -      SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
  4177         -      warnInmemoryDb = 0; }
  4178         -#endif
  4179   4191     }
  4180   4192     data.out = stdout;
  4181   4193   
  4182   4194     /* Go ahead and open the database file if it already exists.  If the
  4183   4195     ** file does not exist, delay opening it.  This prevents empty database
  4184   4196     ** files from being created if a user mistypes the database name argument
  4185   4197     ** to the sqlite command-line tool.
................................................................................
  4268   4280   #ifdef SQLITE_ENABLE_MULTIPLEX
  4269   4281       }else if( strcmp(z,"-multiplex")==0 ){
  4270   4282         i++;
  4271   4283   #endif
  4272   4284       }else if( strcmp(z,"-help")==0 ){
  4273   4285         usage(1);
  4274   4286       }else if( strcmp(z,"-cmd")==0 ){
         4287  +      /* Run commands that follow -cmd first and separately from commands
         4288  +      ** that simply appear on the command-line.  This seems goofy.  It would
         4289  +      ** be better if all commands ran in the order that they appear.  But
         4290  +      ** we retain the goofy behavior for historical compatibility. */
  4275   4291         if( i==argc-1 ) break;
  4276   4292         z = cmdline_option_value(argc,argv,++i);
  4277   4293         if( z[0]=='.' ){
  4278   4294           rc = do_meta_command(z, &data);
  4279   4295           if( rc && bail_on_error ) return rc==2 ? 0 : rc;
  4280   4296         }else{
  4281   4297           open_db(&data, 0);
................................................................................
  4291   4307       }else{
  4292   4308         fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
  4293   4309         fprintf(stderr,"Use -help for a list of options.\n");
  4294   4310         return 1;
  4295   4311       }
  4296   4312     }
  4297   4313   
  4298         -  if( zFirstCmd ){
  4299         -    /* Run just the command that follows the database name
         4314  +  if( !readStdin ){
         4315  +    /* Run all arguments that do not begin with '-' as if they were separate
         4316  +    ** command-line inputs, except for the argToSkip argument which contains
         4317  +    ** the database filename.
  4300   4318       */
  4301         -    if( zFirstCmd[0]=='.' ){
  4302         -      rc = do_meta_command(zFirstCmd, &data);
  4303         -      if( rc==2 ) rc = 0;
  4304         -    }else{
  4305         -      open_db(&data, 0);
  4306         -      rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
  4307         -      if( zErrMsg!=0 ){
  4308         -        fprintf(stderr,"Error: %s\n", zErrMsg);
  4309         -        return rc!=0 ? rc : 1;
  4310         -      }else if( rc!=0 ){
  4311         -        fprintf(stderr,"Error: unable to process SQL \"%s\"\n", zFirstCmd);
  4312         -        return rc;
         4319  +    for(i=0; i<nCmd; i++){
         4320  +      if( azCmd[i][0]=='.' ){
         4321  +        rc = do_meta_command(azCmd[i], &data);
         4322  +        if( rc ) return rc==2 ? 0 : rc;
         4323  +      }else{
         4324  +        open_db(&data, 0);
         4325  +        rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
         4326  +        if( zErrMsg!=0 ){
         4327  +          fprintf(stderr,"Error: %s\n", zErrMsg);
         4328  +          return rc!=0 ? rc : 1;
         4329  +        }else if( rc!=0 ){
         4330  +          fprintf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
         4331  +          return rc;
         4332  +        }
  4313   4333         }
  4314   4334       }
         4335  +    free(azCmd);
  4315   4336     }else{
  4316   4337       /* Run commands received from standard input
  4317   4338       */
  4318   4339       if( stdin_is_interactive ){
  4319   4340         char *zHome;
  4320   4341         char *zHistory = 0;
  4321   4342         int nHistory;

Changes to src/sqlite.h.in.

  1217   1217   ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
  1218   1218   ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
  1219   1219   ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
  1220   1220   ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
  1221   1221   ** </ul>
  1222   1222   **
  1223   1223   ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
  1224         -** was given no the corresponding lock.  
         1224  +** was given on the corresponding lock.  
  1225   1225   **
  1226   1226   ** The xShmLock method can transition between unlocked and SHARED or
  1227   1227   ** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
  1228   1228   ** and EXCLUSIVE.
  1229   1229   */
  1230   1230   #define SQLITE_SHM_UNLOCK       1
  1231   1231   #define SQLITE_SHM_LOCK         2
................................................................................
  1520   1520   ** This option can be used to overload the default memory allocation
  1521   1521   ** routines with a wrapper that simulations memory allocation failure or
  1522   1522   ** tracks memory usage, for example. </dd>
  1523   1523   **
  1524   1524   ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
  1525   1525   ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
  1526   1526   ** interpreted as a boolean, which enables or disables the collection of
  1527         -** memory allocation statistics. ^(When memory allocation statistics are disabled, the 
  1528         -** following SQLite interfaces become non-operational:
         1527  +** memory allocation statistics. ^(When memory allocation statistics are
         1528  +** disabled, the following SQLite interfaces become non-operational:
  1529   1529   **   <ul>
  1530   1530   **   <li> [sqlite3_memory_used()]
  1531   1531   **   <li> [sqlite3_memory_highwater()]
  1532   1532   **   <li> [sqlite3_soft_heap_limit64()]
  1533   1533   **   <li> [sqlite3_status()]
  1534   1534   **   </ul>)^
  1535   1535   ** ^Memory allocation statistics are enabled by default unless SQLite is
................................................................................
  1562   1562   ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
  1563   1563   ** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer
  1564   1564   ** that SQLite can use for the database page cache with the default page
  1565   1565   ** cache implementation.  
  1566   1566   ** This configuration should not be used if an application-define page
  1567   1567   ** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]
  1568   1568   ** configuration option.
  1569         -** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned
         1569  +** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
         1570  +** 8-byte aligned
  1570   1571   ** memory, the size of each page buffer (sz), and the number of pages (N).
  1571   1572   ** The sz argument should be the size of the largest database page
  1572   1573   ** (a power of two between 512 and 32768) plus some extra bytes for each
  1573   1574   ** page header.  ^The number of extra bytes needed by the page header
  1574   1575   ** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option 
  1575   1576   ** to [sqlite3_config()].
  1576   1577   ** ^It is harmless, apart from the wasted memory,
................................................................................
  1582   1583   ** memory needs for the first N pages that it adds to cache.  ^If additional
  1583   1584   ** page cache memory is needed beyond what is provided by this option, then
  1584   1585   ** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
  1585   1586   **
  1586   1587   ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
  1587   1588   ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer 
  1588   1589   ** that SQLite will use for all of its dynamic memory allocation needs
  1589         -** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
         1590  +** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and
         1591  +** [SQLITE_CONFIG_PAGECACHE].
  1590   1592   ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
  1591   1593   ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
  1592   1594   ** [SQLITE_ERROR] if invoked otherwise.
  1593   1595   ** ^There are three arguments to SQLITE_CONFIG_HEAP:
  1594   1596   ** An 8-byte aligned pointer to the memory,
  1595   1597   ** the number of bytes in the memory buffer, and the minimum allocation size.
  1596   1598   ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
................................................................................
  1602   1604   ** boundary or subsequent behavior of SQLite will be undefined.
  1603   1605   ** The minimum allocation size is capped at 2**12. Reasonable values
  1604   1606   ** for the minimum allocation size are 2**5 through 2**8.</dd>
  1605   1607   **
  1606   1608   ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
  1607   1609   ** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
  1608   1610   ** pointer to an instance of the [sqlite3_mutex_methods] structure.
  1609         -** The argument specifies alternative low-level mutex routines to be used in place
  1610         -** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
  1611         -** content of the [sqlite3_mutex_methods] structure before the call to
         1611  +** The argument specifies alternative low-level mutex routines to be used
         1612  +** in place the mutex routines built into SQLite.)^  ^SQLite makes a copy of
         1613  +** the content of the [sqlite3_mutex_methods] structure before the call to
  1612   1614   ** [sqlite3_config()] returns. ^If SQLite is compiled with
  1613   1615   ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1614   1616   ** the entire mutexing subsystem is omitted from the build and hence calls to
  1615   1617   ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
  1616   1618   ** return [SQLITE_ERROR].</dd>
  1617   1619   **
  1618   1620   ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
................................................................................
  1642   1644   ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is 
  1643   1645   ** a pointer to an [sqlite3_pcache_methods2] object.  This object specifies
  1644   1646   ** the interface to a custom page cache implementation.)^
  1645   1647   ** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
  1646   1648   **
  1647   1649   ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
  1648   1650   ** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
  1649         -** is a pointer to an [sqlite3_pcache_methods2] object.  SQLite copies of the current
  1650         -** page cache implementation into that object.)^ </dd>
         1651  +** is a pointer to an [sqlite3_pcache_methods2] object.  SQLite copies of
         1652  +** the current page cache implementation into that object.)^ </dd>
  1651   1653   **
  1652   1654   ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
  1653   1655   ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
  1654   1656   ** global [error log].
  1655   1657   ** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
  1656   1658   ** function with a call signature of void(*)(void*,int,const char*), 
  1657   1659   ** and a pointer to void. ^If the function pointer is not NULL, it is
................................................................................
  1668   1670   ** supplied by the application must not invoke any SQLite interface.
  1669   1671   ** In a multi-threaded application, the application-defined logger
  1670   1672   ** function must be threadsafe. </dd>
  1671   1673   **
  1672   1674   ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
  1673   1675   ** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
  1674   1676   ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
  1675         -** then URI handling is globally disabled.)^ ^If URI handling is globally enabled,
  1676         -** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
         1677  +** then URI handling is globally disabled.)^ ^If URI handling is globally
         1678  +** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
         1679  +** [sqlite3_open16()] or
  1677   1680   ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
  1678   1681   ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
  1679   1682   ** connection is opened. ^If it is globally disabled, filenames are
  1680   1683   ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  1681   1684   ** database connection is opened. ^(By default, URI handling is globally
  1682   1685   ** disabled. The default value may be changed by compiling with the
  1683   1686   ** [SQLITE_USE_URI] symbol defined.)^
................................................................................
  1731   1734   ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
  1732   1735   ** ^If either argument to this option is negative, then that argument is
  1733   1736   ** changed to its compile-time default.
  1734   1737   **
  1735   1738   ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
  1736   1739   ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
  1737   1740   ** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
  1738         -** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
  1739         -** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
         1741  +** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
         1742  +** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
  1740   1743   ** that specifies the maximum size of the created heap.
  1741   1744   ** </dl>
  1742   1745   **
  1743   1746   ** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
  1744   1747   ** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
  1745   1748   ** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
  1746   1749   ** is a pointer to an integer and writes into that integer the number of extra
  1747         -** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of
  1748         -** extra space required can change depending on the compiler,
         1750  +** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
         1751  +** The amount of extra space required can change depending on the compiler,
  1749   1752   ** target platform, and SQLite version.
  1750   1753   ** </dl>
  1751   1754   */
  1752   1755   #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  1753   1756   #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  1754   1757   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  1755   1758   #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
................................................................................
  2045   2048   ** UTF-16 string in native byte order.
  2046   2049   */
  2047   2050   int sqlite3_complete(const char *sql);
  2048   2051   int sqlite3_complete16(const void *sql);
  2049   2052   
  2050   2053   /*
  2051   2054   ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
         2055  +** KEYWORDS: {busy-handler callback} {busy handler}
  2052   2056   **
  2053   2057   ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
  2054   2058   ** that might be invoked with argument P whenever
  2055   2059   ** an attempt is made to access a database table associated with
  2056   2060   ** [database connection] D when another thread
  2057   2061   ** or process has the table locked.
  2058   2062   ** The sqlite3_busy_handler() interface is used to implement
................................................................................
  2061   2065   ** ^If the busy callback is NULL, then [SQLITE_BUSY]
  2062   2066   ** is returned immediately upon encountering the lock.  ^If the busy callback
  2063   2067   ** is not NULL, then the callback might be invoked with two arguments.
  2064   2068   **
  2065   2069   ** ^The first argument to the busy handler is a copy of the void* pointer which
  2066   2070   ** is the third argument to sqlite3_busy_handler().  ^The second argument to
  2067   2071   ** the busy handler callback is the number of times that the busy handler has
  2068         -** been invoked for the same locking event.  ^If the
         2072  +** been invoked previously for the same locking event.  ^If the
  2069   2073   ** busy callback returns 0, then no additional attempts are made to
  2070   2074   ** access the database and [SQLITE_BUSY] is returned
  2071   2075   ** to the application.
  2072   2076   ** ^If the callback returns non-zero, then another attempt
  2073   2077   ** is made to access the database and the cycle repeats.
  2074   2078   **
  2075   2079   ** The presence of a busy handler does not guarantee that it will be invoked
................................................................................
  4516   4520   ** kind of [sqlite3_value] object can be used with this interface.
  4517   4521   **
  4518   4522   ** If these routines are called from within the different thread
  4519   4523   ** than the one containing the application-defined function that received
  4520   4524   ** the [sqlite3_context] pointer, the results are undefined.
  4521   4525   */
  4522   4526   void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
  4523         -void sqlite3_result_blob64(sqlite3_context*,const void*,sqlite3_uint64,void(*)(void*));
         4527  +void sqlite3_result_blob64(sqlite3_context*,const void*,
         4528  +                           sqlite3_uint64,void(*)(void*));
  4524   4529   void sqlite3_result_double(sqlite3_context*, double);
  4525   4530   void sqlite3_result_error(sqlite3_context*, const char*, int);
  4526   4531   void sqlite3_result_error16(sqlite3_context*, const void*, int);
  4527   4532   void sqlite3_result_error_toobig(sqlite3_context*);
  4528   4533   void sqlite3_result_error_nomem(sqlite3_context*);
  4529   4534   void sqlite3_result_error_code(sqlite3_context*, int);
  4530   4535   void sqlite3_result_int(sqlite3_context*, int);
................................................................................
  7242   7247   ** for a particular application.
  7243   7248   */
  7244   7249   int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  7245   7250   
  7246   7251   /*
  7247   7252   ** CAPI3REF: Checkpoint a database
  7248   7253   **
  7249         -** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
  7250         -** on [database connection] D to be [checkpointed].  ^If X is NULL or an
  7251         -** empty string, then a checkpoint is run on all databases of
  7252         -** connection D.  ^If the database connection D is not in
  7253         -** [WAL | write-ahead log mode] then this interface is a harmless no-op.
  7254         -** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a
  7255         -** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint.
  7256         -** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL
  7257         -** or RESET checkpoint.
         7254  +** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
         7255  +** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
  7258   7256   **
  7259         -** ^The [wal_checkpoint pragma] can be used to invoke this interface
  7260         -** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
  7261         -** [wal_autocheckpoint pragma] can be used to cause this interface to be
  7262         -** run whenever the WAL reaches a certain size threshold.
         7257  +** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the 
         7258  +** [write-ahead log] for database X on [database connection] D to be
         7259  +** transferred into the database file and for the write-ahead log to
         7260  +** be reset.  See the [checkpointing] documentation for addition
         7261  +** information.
  7263   7262   **
  7264         -** See also: [sqlite3_wal_checkpoint_v2()]
         7263  +** This interface used to be the only way to cause a checkpoint to
         7264  +** occur.  But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
         7265  +** interface was added.  This interface is retained for backwards
         7266  +** compatibility and as a convenience for applications that need to manually
         7267  +** start a callback but which do not need the full power (and corresponding
         7268  +** complication) of [sqlite3_wal_checkpoint_v2()].
  7265   7269   */
  7266   7270   int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
  7267   7271   
  7268   7272   /*
  7269   7273   ** CAPI3REF: Checkpoint a database
  7270   7274   **
  7271         -** Run a checkpoint operation on WAL database zDb attached to database 
  7272         -** handle db. The specific operation is determined by the value of the 
  7273         -** eMode parameter:
         7275  +** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
         7276  +** operation on database X of [database connection] D in mode M.  Status
         7277  +** information is written back into integers pointed to by L and C.)^
         7278  +** ^(The M parameter must be a valid [checkpoint mode]:)^
  7274   7279   **
  7275   7280   ** <dl>
  7276   7281   ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
  7277         -**   Checkpoint as many frames as possible without waiting for any database 
  7278         -**   readers or writers to finish. Sync the db file if all frames in the log
  7279         -**   are checkpointed. This mode is the same as calling 
  7280         -**   sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback]
  7281         -**   is never invoked.
         7282  +**   ^Checkpoint as many frames as possible without waiting for any database 
         7283  +**   readers or writers to finish, then sync the database file if all frames 
         7284  +**   in the log were checkpointed. ^The [busy-handler callback]
         7285  +**   is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.  
         7286  +**   ^On the other hand, passive mode might leave the checkpoint unfinished
         7287  +**   if there are concurrent readers or writers.
  7282   7288   **
  7283   7289   ** <dt>SQLITE_CHECKPOINT_FULL<dd>
  7284         -**   This mode blocks (it invokes the
         7290  +**   ^This mode blocks (it invokes the
  7285   7291   **   [sqlite3_busy_handler|busy-handler callback]) until there is no
  7286   7292   **   database writer and all readers are reading from the most recent database
  7287         -**   snapshot. It then checkpoints all frames in the log file and syncs the
  7288         -**   database file. This call blocks database writers while it is running,
  7289         -**   but not database readers.
         7293  +**   snapshot. ^It then checkpoints all frames in the log file and syncs the
         7294  +**   database file. ^This mode blocks new database writers while it is pending,
         7295  +**   but new database readers are allowed to continue unimpeded.
  7290   7296   **
  7291   7297   ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
  7292         -**   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
  7293         -**   checkpointing the log file it blocks (calls the 
  7294         -**   [sqlite3_busy_handler|busy-handler callback])
  7295         -**   until all readers are reading from the database file only. This ensures 
  7296         -**   that the next client to write to the database file restarts the log file 
  7297         -**   from the beginning. This call blocks database writers while it is running,
  7298         -**   but not database readers.
         7298  +**   ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
         7299  +**   that after checkpointing the log file it blocks (calls the 
         7300  +**   [busy-handler callback])
         7301  +**   until all readers are reading from the database file only. ^This ensures 
         7302  +**   that the next writer will restart the log file from the beginning.
         7303  +**   ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
         7304  +**   database writer attempts while it is pending, but does not impede readers.
         7305  +**
         7306  +** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
         7307  +**   ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
         7308  +**   addition that it also truncates the log file to zero bytes just prior
         7309  +**   to a successful return.
  7299   7310   ** </dl>
  7300   7311   **
  7301         -** If pnLog is not NULL, then *pnLog is set to the total number of frames in
  7302         -** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
  7303         -** the total number of checkpointed frames (including any that were already
  7304         -** checkpointed when this function is called). *pnLog and *pnCkpt may be
  7305         -** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
  7306         -** If no values are available because of an error, they are both set to -1
  7307         -** before returning to communicate this to the caller.
         7312  +** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
         7313  +** the log file or to -1 if the checkpoint could not run because
         7314  +** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
         7315  +** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
         7316  +** log file (including any that were already checkpointed before the function
         7317  +** was called) or to -1 if the checkpoint could not run due to an error or
         7318  +** because the database is not in WAL mode. ^Note that upon successful
         7319  +** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
         7320  +** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
  7308   7321   **
  7309         -** All calls obtain an exclusive "checkpoint" lock on the database file. If
         7322  +** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
  7310   7323   ** any other process is running a checkpoint operation at the same time, the 
  7311         -** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a 
         7324  +** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a 
  7312   7325   ** busy-handler configured, it will not be invoked in this case.
  7313   7326   **
  7314         -** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive 
  7315         -** "writer" lock on the database file. If the writer lock cannot be obtained
  7316         -** immediately, and a busy-handler is configured, it is invoked and the writer
  7317         -** lock retried until either the busy-handler returns 0 or the lock is
  7318         -** successfully obtained. The busy-handler is also invoked while waiting for
  7319         -** database readers as described above. If the busy-handler returns 0 before
         7327  +** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the 
         7328  +** exclusive "writer" lock on the database file. ^If the writer lock cannot be
         7329  +** obtained immediately, and a busy-handler is configured, it is invoked and
         7330  +** the writer lock retried until either the busy-handler returns 0 or the lock
         7331  +** is successfully obtained. ^The busy-handler is also invoked while waiting for
         7332  +** database readers as described above. ^If the busy-handler returns 0 before
  7320   7333   ** the writer lock is obtained or while waiting for database readers, the
  7321   7334   ** checkpoint operation proceeds from that point in the same way as 
  7322   7335   ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible 
  7323         -** without blocking any further. SQLITE_BUSY is returned in this case.
         7336  +** without blocking any further. ^SQLITE_BUSY is returned in this case.
  7324   7337   **
  7325         -** If parameter zDb is NULL or points to a zero length string, then the
  7326         -** specified operation is attempted on all WAL databases. In this case the
  7327         -** values written to output parameters *pnLog and *pnCkpt are undefined. If 
         7338  +** ^If parameter zDb is NULL or points to a zero length string, then the
         7339  +** specified operation is attempted on all WAL databases [attached] to 
         7340  +** [database connection] db.  In this case the
         7341  +** values written to output parameters *pnLog and *pnCkpt are undefined. ^If 
  7328   7342   ** an SQLITE_BUSY error is encountered when processing one or more of the 
  7329   7343   ** attached WAL databases, the operation is still attempted on any remaining 
  7330         -** attached databases and SQLITE_BUSY is returned to the caller. If any other 
         7344  +** attached databases and SQLITE_BUSY is returned at the end. ^If any other 
  7331   7345   ** error occurs while processing an attached database, processing is abandoned 
  7332         -** and the error code returned to the caller immediately. If no error 
         7346  +** and the error code is returned to the caller immediately. ^If no error 
  7333   7347   ** (SQLITE_BUSY or otherwise) is encountered while processing the attached 
  7334   7348   ** databases, SQLITE_OK is returned.
  7335   7349   **
  7336         -** If database zDb is the name of an attached database that is not in WAL
  7337         -** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
         7350  +** ^If database zDb is the name of an attached database that is not in WAL
         7351  +** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
  7338   7352   ** zDb is not NULL (or a zero length string) and is not the name of any
  7339   7353   ** attached database, SQLITE_ERROR is returned to the caller.
         7354  +**
         7355  +** ^Unless it returns SQLITE_MISUSE,
         7356  +** the sqlite3_wal_checkpoint_v2() interface
         7357  +** sets the error information that is queried by
         7358  +** [sqlite3_errcode()] and [sqlite3_errmsg()].
         7359  +**
         7360  +** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
         7361  +** from SQL.
  7340   7362   */
  7341   7363   int sqlite3_wal_checkpoint_v2(
  7342   7364     sqlite3 *db,                    /* Database handle */
  7343   7365     const char *zDb,                /* Name of attached database (or NULL) */
  7344   7366     int eMode,                      /* SQLITE_CHECKPOINT_* value */
  7345   7367     int *pnLog,                     /* OUT: Size of WAL log in frames */
  7346   7368     int *pnCkpt                     /* OUT: Total number of frames checkpointed */
  7347   7369   );
  7348   7370   
  7349   7371   /*
  7350         -** CAPI3REF: Checkpoint operation parameters
         7372  +** CAPI3REF: Checkpoint Mode Values
         7373  +** KEYWORDS: {checkpoint mode}
  7351   7374   **
  7352         -** These constants can be used as the 3rd parameter to
  7353         -** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
  7354         -** documentation for additional information about the meaning and use of
  7355         -** each of these values.
         7375  +** These constants define all valid values for the "checkpoint mode" passed
         7376  +** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
         7377  +** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
         7378  +** meaning of each of these checkpoint modes.
  7356   7379   */
  7357         -#define SQLITE_CHECKPOINT_PASSIVE 0
  7358         -#define SQLITE_CHECKPOINT_FULL    1
  7359         -#define SQLITE_CHECKPOINT_RESTART 2
         7380  +#define SQLITE_CHECKPOINT_PASSIVE  0  /* Do as much as possible w/o blocking */
         7381  +#define SQLITE_CHECKPOINT_FULL     1  /* Wait for writers, then checkpoint */
         7382  +#define SQLITE_CHECKPOINT_RESTART  2  /* Like FULL but wait for for readers */
         7383  +#define SQLITE_CHECKPOINT_TRUNCATE 3  /* Like RESTART but also truncate WAL */
  7360   7384   
  7361   7385   /*
  7362   7386   ** CAPI3REF: Virtual Table Interface Configuration
  7363   7387   **
  7364   7388   ** This function may be called by either the [xConnect] or [xCreate] method
  7365   7389   ** of a [virtual table] implementation to configure
  7366   7390   ** various facets of the virtual table interface.
................................................................................
  7451   7475   **
  7452   7476   ** The following constants can be used for the T parameter to the
  7453   7477   ** [sqlite3_stmt_scanstatus(S,X,T,V)] interface.  Each constant designates a
  7454   7478   ** different metric for sqlite3_stmt_scanstatus() to return.
  7455   7479   **
  7456   7480   ** <dl>
  7457   7481   ** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
  7458         -** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
  7459         -** total number of times that the X-th loop has run.</dd>
         7482  +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be
         7483  +** set to the total number of times that the X-th loop has run.</dd>
  7460   7484   **
  7461   7485   ** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
  7462         -** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
  7463         -** total number of rows examined by all iterations of the X-th loop.</dd>
         7486  +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set
         7487  +** to the total number of rows examined by all iterations of the X-th loop.</dd>
  7464   7488   **
  7465   7489   ** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
  7466   7490   ** <dd>^The "double" variable pointed to by the T parameter will be set to the
  7467   7491   ** query planner's estimate for the average number of rows output from each
  7468   7492   ** iteration of the X-th loop.  If the query planner's estimates was accurate,
  7469   7493   ** then this value will approximate the quotient NVISIT/NLOOP and the
  7470   7494   ** product of this value for all prior loops with the same SELECTID will
  7471   7495   ** be the NLOOP value for the current loop.
  7472   7496   **
  7473   7497   ** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
  7474         -** <dd>^The "const char *" variable pointed to by the T parameter will be set to 
  7475         -** a zero-terminated UTF-8 string containing the name of the index or table used
  7476         -** for the X-th loop.
         7498  +** <dd>^The "const char *" variable pointed to by the T parameter will be set
         7499  +** to a zero-terminated UTF-8 string containing the name of the index or table
         7500  +** used for the X-th loop.
  7477   7501   **
  7478   7502   ** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
  7479         -** <dd>^The "const char *" variable pointed to by the T parameter will be set to 
  7480         -** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description
  7481         -** for the X-th loop.
         7503  +** <dd>^The "const char *" variable pointed to by the T parameter will be set
         7504  +** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
         7505  +** description for the X-th loop.
  7482   7506   **
  7483   7507   ** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
  7484   7508   ** <dd>^The "int" variable pointed to by the T parameter will be set to the
  7485   7509   ** "select-id" for the X-th loop.  The select-id identifies which query or
  7486   7510   ** subquery the loop is part of.  The main query has a select-id of zero.
  7487   7511   ** The select-id is the same value as is output in the first column
  7488   7512   ** of an [EXPLAIN QUERY PLAN] query.
................................................................................
  7497   7521   
  7498   7522   /*
  7499   7523   ** CAPI3REF: Prepared Statement Scan Status
  7500   7524   **
  7501   7525   ** Return status data for a single loop within query pStmt.
  7502   7526   **
  7503   7527   ** The "iScanStatusOp" parameter determines which status information to return.
  7504         -** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of
  7505         -** this interface is undefined.
         7528  +** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
         7529  +** of this interface is undefined.
  7506   7530   ** ^The requested measurement is written into a variable pointed to by
  7507   7531   ** the "pOut" parameter.
  7508   7532   ** Parameter "idx" identifies the specific loop to retrieve statistics for.
  7509   7533   ** Loops are numbered starting from zero. ^If idx is out of range - less than
  7510   7534   ** zero or greater than or equal to the total number of loops used to implement
  7511   7535   ** the statement - a non-zero value is returned and the variable that pOut
  7512   7536   ** points to is unchanged.

Changes to src/sqliteInt.h.

  2016   2016     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  2017   2017     Table *pTab;           /* Table for TK_COLUMN expressions. */
  2018   2018   };
  2019   2019   
  2020   2020   /*
  2021   2021   ** The following are the meanings of bits in the Expr.flags field.
  2022   2022   */
  2023         -#define EP_FromJoin  0x000001 /* Originated in ON or USING clause of a join */
         2023  +#define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
  2024   2024   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
  2025   2025   #define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
  2026   2026   #define EP_Error     0x000008 /* Expression contains one or more errors */
  2027   2027   #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
  2028   2028   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
  2029   2029   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
  2030   2030   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
................................................................................
  2036   2036   #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
  2037   2037   #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
  2038   2038   #define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
  2039   2039   #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
  2040   2040   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
  2041   2041   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
  2042   2042   #define EP_Constant  0x080000 /* Node is a constant */
         2043  +#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
  2043   2044   
  2044   2045   /*
  2045   2046   ** These macros can be used to test, set, or clear bits in the 
  2046   2047   ** Expr.flags field.
  2047   2048   */
  2048   2049   #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
  2049   2050   #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))

Changes to src/test1.c.

  3634   3634     void * clientData,
  3635   3635     Tcl_Interp *interp,
  3636   3636     int objc,
  3637   3637     Tcl_Obj *CONST objv[]
  3638   3638   ){
  3639   3639     sqlite3 *db;
  3640   3640     const char *zSql;
         3641  +  char *zCopy = 0;                /* malloc() copy of zSql */
  3641   3642     int bytes;
  3642   3643     const char *zTail = 0;
  3643   3644     sqlite3_stmt *pStmt = 0;
  3644   3645     char zBuf[50];
  3645   3646     int rc;
  3646   3647   
  3647   3648     if( objc!=5 && objc!=4 ){
................................................................................
  3649   3650          Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  3650   3651       return TCL_ERROR;
  3651   3652     }
  3652   3653     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3653   3654     zSql = Tcl_GetString(objv[2]);
  3654   3655     if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  3655   3656   
  3656         -  rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
         3657  +  /* Instead of using zSql directly, make a copy into a buffer obtained
         3658  +  ** directly from malloc(). The idea is to make it easier for valgrind
         3659  +  ** to spot buffer overreads.  */
         3660  +  if( bytes>=0 ){
         3661  +    zCopy = malloc(bytes);
         3662  +    memcpy(zCopy, zSql, bytes);
         3663  +  }else{
         3664  +    int n = (int)strlen(zSql) + 1;
         3665  +    zCopy = malloc(n);
         3666  +    memcpy(zCopy, zSql, n);
         3667  +  }
         3668  +  rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0);
         3669  +  free(zCopy);
         3670  +  zTail = &zSql[(zTail - zCopy)];
         3671  +
  3657   3672     assert(rc==SQLITE_OK || pStmt==0);
  3658   3673     Tcl_ResetResult(interp);
  3659   3674     if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  3660   3675     if( zTail && objc>=5 ){
  3661   3676       if( bytes>=0 ){
  3662   3677         bytes = bytes - (int)(zTail-zSql);
  3663   3678       }
................................................................................
  5671   5686     int rc;
  5672   5687   
  5673   5688     int eMode;
  5674   5689     int nLog = -555;
  5675   5690     int nCkpt = -555;
  5676   5691     Tcl_Obj *pRet;
  5677   5692   
  5678         -  const char * aMode[] = { "passive", "full", "restart", 0 };
         5693  +  const char * aMode[] = { "passive", "full", "restart", "truncate", 0 };
  5679   5694     assert( SQLITE_CHECKPOINT_PASSIVE==0 );
  5680   5695     assert( SQLITE_CHECKPOINT_FULL==1 );
  5681   5696     assert( SQLITE_CHECKPOINT_RESTART==2 );
         5697  +  assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
  5682   5698   
  5683   5699     if( objc!=3 && objc!=4 ){
  5684   5700       Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?");
  5685   5701       return TCL_ERROR;
  5686   5702     }
  5687   5703   
  5688   5704     if( objc==4 ){
  5689   5705       zDb = Tcl_GetString(objv[3]);
  5690   5706     }
  5691         -  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
  5692         -   || Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode) 
  5693         -  ){
         5707  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || (
         5708  +      TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode)
         5709  +   && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode) 
         5710  +  )){
  5694   5711       return TCL_ERROR;
  5695   5712     }
  5696   5713   
  5697   5714     rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt);
  5698   5715     if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
  5699         -    Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
         5716  +    const char *zErrCode = sqlite3ErrName(rc);
         5717  +    Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0);
  5700   5718       return TCL_ERROR;
  5701   5719     }
  5702   5720   
  5703   5721     pRet = Tcl_NewObj();
  5704   5722     Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0));
  5705   5723     Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog));
  5706   5724     Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt));

Changes to src/vdbe.c.

  5701   5701     break;
  5702   5702   }
  5703   5703   
  5704   5704   #ifndef SQLITE_OMIT_WAL
  5705   5705   /* Opcode: Checkpoint P1 P2 P3 * *
  5706   5706   **
  5707   5707   ** Checkpoint database P1. This is a no-op if P1 is not currently in
  5708         -** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
  5709         -** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
         5708  +** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
         5709  +** RESTART, or TRUNCATE.  Write 1 or 0 into mem[P3] if the checkpoint returns
  5710   5710   ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
  5711   5711   ** WAL after the checkpoint into mem[P3+1] and the number of pages
  5712   5712   ** in the WAL that have been checkpointed after the checkpoint
  5713   5713   ** completes into mem[P3+2].  However on an error, mem[P3+1] and
  5714   5714   ** mem[P3+2] are initialized to -1.
  5715   5715   */
  5716   5716   case OP_Checkpoint: {
................................................................................
  5720   5720   
  5721   5721     assert( p->readOnly==0 );
  5722   5722     aRes[0] = 0;
  5723   5723     aRes[1] = aRes[2] = -1;
  5724   5724     assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
  5725   5725          || pOp->p2==SQLITE_CHECKPOINT_FULL
  5726   5726          || pOp->p2==SQLITE_CHECKPOINT_RESTART
         5727  +       || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
  5727   5728     );
  5728   5729     rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
  5729   5730     if( rc==SQLITE_BUSY ){
  5730   5731       rc = SQLITE_OK;
  5731   5732       aRes[0] = 1;
  5732   5733     }
  5733   5734     for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){

Changes to src/vdbeaux.c.

  1738   1738     if( p->aVar ){
  1739   1739       p->nVar = (ynVar)nVar;
  1740   1740       for(n=0; n<nVar; n++){
  1741   1741         p->aVar[n].flags = MEM_Null;
  1742   1742         p->aVar[n].db = db;
  1743   1743       }
  1744   1744     }
  1745         -  if( p->azVar ){
         1745  +  if( p->azVar && pParse->nzVar>0 ){
  1746   1746       p->nzVar = pParse->nzVar;
  1747   1747       memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
  1748   1748       memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
  1749   1749     }
  1750   1750     if( p->aMem ){
  1751   1751       p->aMem--;                      /* aMem[] goes from 1..nMem */
  1752   1752       p->nMem = nMem;                 /*       not from 0..nMem-1 */

Changes to src/vdbesort.c.

   143    143   ** messages to stderr that may be helpful in understanding the performance
   144    144   ** characteristics of the sorter in multi-threaded mode.
   145    145   */
   146    146   #if 0
   147    147   # define SQLITE_DEBUG_SORTER_THREADS 1
   148    148   #endif
   149    149   
          150  +/*
          151  +** Hard-coded maximum amount of data to accumulate in memory before flushing
          152  +** to a level 0 PMA. The purpose of this limit is to prevent various integer
          153  +** overflows. 512MiB.
          154  +*/
          155  +#define SQLITE_MAX_MXPMASIZE    (1<<29)
          156  +
   150    157   /*
   151    158   ** Private objects used by the sorter
   152    159   */
   153    160   typedef struct MergeEngine MergeEngine;     /* Merge PMAs together */
   154    161   typedef struct PmaReader PmaReader;         /* Incrementally read one PMA */
   155    162   typedef struct PmaWriter PmaWriter;         /* Incrementally write one PMA */
   156    163   typedef struct SorterRecord SorterRecord;   /* A record being sorted */
................................................................................
   841    848         pTask->pSorter = pSorter;
   842    849       }
   843    850   
   844    851       if( !sqlite3TempInMemory(db) ){
   845    852         pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
   846    853         mxCache = db->aDb[0].pSchema->cache_size;
   847    854         if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
   848         -      pSorter->mxPmaSize = mxCache * pgsz;
          855  +      pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_MXPMASIZE);
   849    856   
   850    857         /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
   851    858         ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
   852    859         ** large heap allocations.
   853    860         */
   854    861         if( sqlite3GlobalConfig.pScratch==0 ){
   855    862           assert( pSorter->iMemory==0 );

Changes to src/vtab.c.

   328    328     assert( iDb>=0 );
   329    329   
   330    330     pTable->tabFlags |= TF_Virtual;
   331    331     pTable->nModuleArg = 0;
   332    332     addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
   333    333     addModuleArgument(db, pTable, 0);
   334    334     addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
   335         -  pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
          335  +  assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
          336  +       || (pParse->sNameToken.z==pName1->z && pName2->z==0)
          337  +  );
          338  +  pParse->sNameToken.n = (int)(
          339  +      &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
          340  +  );
   336    341   
   337    342   #ifndef SQLITE_OMIT_AUTHORIZATION
   338    343     /* Creating a virtual table invokes the authorization callback twice.
   339    344     ** The first invocation, to obtain permission to INSERT a row into the
   340    345     ** sqlite_master table, has already been made by sqlite3StartTable().
   341    346     ** The second call, to obtain permission to create the table, is made now.
   342    347     */

Changes to src/wal.c.

  1824   1824       walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
  1825   1825       walIteratorFree(p->pIter);
  1826   1826       p->pIter = 0;
  1827   1827     }
  1828   1828   
  1829   1829     return p->rc;
  1830   1830   }
         1831  +
         1832  +/*
         1833  +** The following is guaranteed when this function is called:
         1834  +**
         1835  +**   a) the WRITER lock is held,
         1836  +**   b) the entire log file has been checkpointed, and
         1837  +**   c) any existing readers are reading exclusively from the database
         1838  +**      file - there are no readers that may attempt to read a frame from
         1839  +**      the log file.
         1840  +**
         1841  +** This function updates the shared-memory structures so that the next
         1842  +** client to write to the database (which may be this one) does so by
         1843  +** writing frames into the start of the log file.
         1844  +**
         1845  +** The value of parameter salt1 is used as the aSalt[1] value in the 
         1846  +** new wal-index header. It should be passed a pseudo-random value (i.e. 
         1847  +** one obtained from sqlite3_randomness()).
         1848  +*/
         1849  +static void walRestartHdr(Wal *pWal, u32 salt1){
         1850  +  volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
         1851  +  int i;                          /* Loop counter */
         1852  +  u32 *aSalt = pWal->hdr.aSalt;   /* Big-endian salt values */
         1853  +  pWal->nCkpt++;
         1854  +  pWal->hdr.mxFrame = 0;
         1855  +  sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
         1856  +  memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
         1857  +  walIndexWriteHdr(pWal);
         1858  +  pInfo->nBackfill = 0;
         1859  +  pInfo->aReadMark[1] = 0;
         1860  +  for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
         1861  +  assert( pInfo->aReadMark[0]==0 );
         1862  +}
  1831   1863   
  1832   1864   /*
  1833   1865   ** Copy as much content as we can from the WAL back into the database file
  1834   1866   ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
  1835   1867   **
  1836   1868   ** The amount of information copies from WAL to database might be limited
  1837   1869   ** by active readers.  This routine will never overwrite a database page
................................................................................
  1859   1891   ** The caller must be holding sufficient locks to ensure that no other
  1860   1892   ** checkpoint is running (in any other thread or process) at the same
  1861   1893   ** time.
  1862   1894   */
  1863   1895   static int walCheckpoint(
  1864   1896     Wal *pWal,                      /* Wal connection */
  1865   1897     int eMode,                      /* One of PASSIVE, FULL or RESTART */
  1866         -  int (*xBusyCall)(void*),        /* Function to call when busy */
         1898  +  int (*xBusy)(void*),            /* Function to call when busy */
  1867   1899     void *pBusyArg,                 /* Context argument for xBusyHandler */
  1868   1900     int sync_flags,                 /* Flags for OsSync() (or 0) */
  1869   1901     u8 *zBuf,                       /* Temporary buffer to use */
  1870   1902     int nBuf                        /* Size of zBuf in bytes */
  1871   1903   ){
  1872   1904     int rc;                         /* Return code */
  1873         -  int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
  1874   1905     WalCkpt sC;
  1875   1906   
  1876         -  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
         1907  +  /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
         1908  +  ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
         1909  +  assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
         1910  +
  1877   1911     rc = walCheckpointStart(pWal, zBuf, nBuf, xBusy, pBusyArg, sync_flags, &sC);
  1878   1912     if( sC.pIter==0 ) goto walcheckpoint_out;
  1879   1913     assert( rc==SQLITE_OK );
  1880   1914   
  1881   1915     /* Step the checkpoint object until it reports something other than 
  1882   1916     ** SQLITE_OK.  */
  1883   1917     while( SQLITE_OK==(rc = walCheckpointStep(&sC)) );
  1884   1918     rc = walCheckpointFinalize(&sC);
  1885   1919   
  1886         -  /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
  1887         -  ** file has been copied into the database file, then block until all
  1888         -  ** readers have finished using the wal file. This ensures that the next
  1889         -  ** process to write to the database restarts the wal file.
         1920  +  /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
         1921  +  ** entire wal file has been copied into the database file, then block 
         1922  +  ** until all readers have finished using the wal file. This ensures that 
         1923  +  ** the next process to write to the database restarts the wal file.
  1890   1924     */
  1891   1925     if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
  1892   1926       assert( pWal->writeLock );
  1893   1927       if( sC.pInfo->nBackfill<pWal->hdr.mxFrame ){
  1894   1928         rc = SQLITE_BUSY;
  1895         -    }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
         1929  +    }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
         1930  +      u32 salt1;
         1931  +      sqlite3_randomness(4, &salt1);
  1896   1932         assert( sC.mxSafeFrame==pWal->hdr.mxFrame );
  1897   1933         rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
  1898   1934         if( rc==SQLITE_OK ){
         1935  +        if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
         1936  +          /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
         1937  +          ** SQLITE_CHECKPOINT_RESTART with the addition that it also
         1938  +          ** truncates the log file to zero bytes just prior to a
         1939  +          ** successful return.
         1940  +          **
         1941  +          ** In theory, it might be safe to do this without updating the
         1942  +          ** wal-index header in shared memory, as all subsequent reader or
         1943  +          ** writer clients should see that the entire log file has been
         1944  +          ** checkpointed and behave accordingly. This seems unsafe though,
         1945  +          ** as it would leave the system in a state where the contents of
         1946  +          ** the wal-index header do not match the contents of the 
         1947  +          ** file-system. To avoid this, update the wal-index header to
         1948  +          ** indicate that the log file contains zero valid frames.  */
         1949  +          walRestartHdr(pWal, salt1);
         1950  +          rc = sqlite3OsTruncate(pWal->pWalFd, 0);
         1951  +        }
  1899   1952           walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
  1900   1953         }
  1901   1954       }
  1902   1955     }
  1903   1956   
  1904   1957    walcheckpoint_out:
  1905   1958     walIteratorFree(sC.pIter);
................................................................................
  2685   2738       pWal->hdr.aFrameCksum[1] = aWalData[2];
  2686   2739       walCleanupHash(pWal);
  2687   2740     }
  2688   2741   
  2689   2742     return rc;
  2690   2743   }
  2691   2744   
  2692         -
  2693   2745   /*
  2694   2746   ** This function is called just before writing a set of frames to the log
  2695   2747   ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
  2696   2748   ** to the current log file, it is possible to overwrite the start of the
  2697   2749   ** existing log file with the new frames (i.e. "reset" the log). If so,
  2698   2750   ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
  2699   2751   ** unchanged.
................................................................................
  2718   2770           ** readers are currently using the WAL), then the transactions
  2719   2771           ** frames will overwrite the start of the existing log. Update the
  2720   2772           ** wal-index header to reflect this.
  2721   2773           **
  2722   2774           ** In theory it would be Ok to update the cache of the header only
  2723   2775           ** at this point. But updating the actual wal-index header is also
  2724   2776           ** safe and means there is no special case for sqlite3WalUndo()
  2725         -        ** to handle if this transaction is rolled back.
  2726         -        */
  2727         -        int i;                    /* Loop counter */
  2728         -        u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
  2729         -
  2730         -        pWal->nCkpt++;
  2731         -        pWal->hdr.mxFrame = 0;
  2732         -        sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
  2733         -        aSalt[1] = salt1;
  2734         -        walIndexWriteHdr(pWal);
  2735         -        pInfo->nBackfill = 0;
  2736         -        pInfo->aReadMark[1] = 0;
  2737         -        for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
  2738         -        assert( pInfo->aReadMark[0]==0 );
         2777  +        ** to handle if this transaction is rolled back.  */
         2778  +        walRestartHdr(pWal, salt1);
  2739   2779           walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
  2740   2780         }else if( rc!=SQLITE_BUSY ){
  2741   2781           return rc;
  2742   2782         }
  2743   2783       }
  2744   2784       walUnlockShared(pWal, WAL_READ_LOCK(0));
  2745   2785       pWal->readLock = -1;
................................................................................
  3021   3061   ** we can from WAL into the database.
  3022   3062   **
  3023   3063   ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
  3024   3064   ** callback. In this case this function runs a blocking checkpoint.
  3025   3065   */
  3026   3066   int sqlite3WalCheckpoint(
  3027   3067     Wal *pWal,                      /* Wal connection */
  3028         -  int eMode,                      /* PASSIVE, FULL or RESTART */
         3068  +  int eMode,                      /* PASSIVE, FULL, RESTART, or TRUNCATE */
  3029   3069     int (*xBusy)(void*),            /* Function to call when busy */
  3030   3070     void *pBusyArg,                 /* Context argument for xBusyHandler */
  3031   3071     int sync_flags,                 /* Flags to sync db file with (or 0) */
  3032   3072     int nBuf,                       /* Size of temporary buffer */
  3033   3073     u8 *zBuf,                       /* Temporary buffer to use */
  3034   3074     int *pnLog,                     /* OUT: Number of frames in WAL */
  3035   3075     int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
  3036   3076   ){
  3037   3077     int rc;                         /* Return code */
  3038   3078     int isChanged = 0;              /* True if a new wal-index header is loaded */
  3039   3079     int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
         3080  +  int (*xBusy2)(void*) = xBusy;   /* Busy handler for eMode2 */
  3040   3081   
  3041   3082     assert( pWal->ckptLock==0 );
  3042   3083     assert( pWal->writeLock==0 );
  3043   3084   
         3085  +  /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
         3086  +  ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
         3087  +  assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
         3088  +
  3044   3089     if( pWal->readOnly ) return SQLITE_READONLY;
  3045   3090     WALTRACE(("WAL%p: checkpoint begins\n", pWal));
         3091  +
         3092  +  /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive 
         3093  +  ** "checkpoint" lock on the database file. */
  3046   3094     rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
  3047   3095     if( rc ){
  3048         -    /* Usually this is SQLITE_BUSY meaning that another thread or process
  3049         -    ** is already running a checkpoint, or maybe a recovery.  But it might
  3050         -    ** also be SQLITE_IOERR. */
         3096  +    /* EVIDENCE-OF: R-10421-19736 If any other process is running a
         3097  +    ** checkpoint operation at the same time, the lock cannot be obtained and
         3098  +    ** SQLITE_BUSY is returned.
         3099  +    ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
         3100  +    ** it will not be invoked in this case.
         3101  +    */
         3102  +    testcase( rc==SQLITE_BUSY );
         3103  +    testcase( xBusy!=0 );
  3051   3104       return rc;
  3052   3105     }
  3053   3106     pWal->ckptLock = 1;
  3054   3107   
  3055         -  /* If this is a blocking-checkpoint, then obtain the write-lock as well
  3056         -  ** to prevent any writers from running while the checkpoint is underway.
  3057         -  ** This has to be done before the call to walIndexReadHdr() below.
         3108  +  /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
         3109  +  ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
         3110  +  ** file.
  3058   3111     **
  3059         -  ** If the writer lock cannot be obtained, then a passive checkpoint is
  3060         -  ** run instead. Since the checkpointer is not holding the writer lock,
  3061         -  ** there is no point in blocking waiting for any readers. Assuming no 
  3062         -  ** other error occurs, this function will return SQLITE_BUSY to the caller.
         3112  +  ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
         3113  +  ** immediately, and a busy-handler is configured, it is invoked and the
         3114  +  ** writer lock retried until either the busy-handler returns 0 or the
         3115  +  ** lock is successfully obtained.
  3063   3116     */
  3064   3117     if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
  3065   3118       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
  3066   3119       if( rc==SQLITE_OK ){
  3067   3120         pWal->writeLock = 1;
  3068   3121       }else if( rc==SQLITE_BUSY ){
  3069   3122         eMode2 = SQLITE_CHECKPOINT_PASSIVE;
         3123  +      xBusy2 = 0;
  3070   3124         rc = SQLITE_OK;
  3071   3125       }
  3072   3126     }
  3073   3127   
  3074   3128     /* Read the wal-index header. */
  3075   3129     if( rc==SQLITE_OK ){
  3076   3130       rc = walIndexReadHdr(pWal, &isChanged);
................................................................................
  3077   3131       if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
  3078   3132         sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
  3079   3133       }
  3080   3134     }
  3081   3135   
  3082   3136     /* Copy data from the log to the database file. */
  3083   3137     if( rc==SQLITE_OK ){
  3084         -    rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf, nBuf);
         3138  +    rc = walCheckpoint(pWal, eMode2, xBusy2, pBusyArg, sync_flags, zBuf, nBuf);
  3085   3139   
  3086   3140       /* If no error occurred, set the output variables. */
  3087   3141       if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
  3088   3142         if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
  3089   3143         if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
  3090   3144       }
  3091   3145     }

Changes to src/where.c.

   218    218         return 0;
   219    219       }
   220    220       memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
   221    221       if( pOld!=pWC->aStatic ){
   222    222         sqlite3DbFree(db, pOld);
   223    223       }
   224    224       pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
          225  +    memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
   225    226     }
   226    227     pTerm = &pWC->a[idx = pWC->nTerm++];
   227    228     if( p && ExprHasProperty(p, EP_Unlikely) ){
   228    229       pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
   229    230     }else{
   230    231       pTerm->truthProb = 1;
   231    232     }
................................................................................
  2943   2944     Vdbe *v,                        /* Vdbe to add scanstatus entry to */
  2944   2945     SrcList *pSrclist,              /* FROM clause pLvl reads data from */
  2945   2946     WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
  2946   2947     int addrExplain                 /* Address of OP_Explain (or 0) */
  2947   2948   ){
  2948   2949     const char *zObj = 0;
  2949   2950     WhereLoop *pLoop = pLvl->pWLoop;
  2950         -  if( (pLoop->wsFlags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
         2951  +  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
  2951   2952       zObj = pLoop->u.btree.pIndex->zName;
  2952   2953     }else{
  2953   2954       zObj = pSrclist->a[pLvl->iFrom].zName;
  2954   2955     }
  2955   2956     sqlite3VdbeScanStatus(
  2956   2957         v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
  2957   2958     );
................................................................................
  3587   3588       */
  3588   3589       if( pWC->nTerm>1 ){
  3589   3590         int iTerm;
  3590   3591         for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
  3591   3592           Expr *pExpr = pWC->a[iTerm].pExpr;
  3592   3593           if( &pWC->a[iTerm] == pTerm ) continue;
  3593   3594           if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
  3594         -        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
  3595         -        testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
  3596         -        if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue;
         3595  +        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
  3597   3596           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
         3597  +        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
  3598   3598           pExpr = sqlite3ExprDup(db, pExpr, 0);
  3599   3599           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
  3600   3600         }
  3601   3601         if( pAndExpr ){
  3602   3602           pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
  3603   3603         }
  3604   3604       }
................................................................................
  4287   4287     return SQLITE_OK;
  4288   4288   }
  4289   4289   
  4290   4290   /*
  4291   4291   ** Adjust the WhereLoop.nOut value downward to account for terms of the
  4292   4292   ** WHERE clause that reference the loop but which are not used by an
  4293   4293   ** index.
         4294  +*
         4295  +** For every WHERE clause term that is not used by the index
         4296  +** and which has a truth probability assigned by one of the likelihood(),
         4297  +** likely(), or unlikely() SQL functions, reduce the estimated number
         4298  +** of output rows by the probability specified.
  4294   4299   **
  4295         -** In the current implementation, the first extra WHERE clause term reduces
  4296         -** the number of output rows by a factor of 10 and each additional term
  4297         -** reduces the number of output rows by sqrt(2).
         4300  +** TUNING:  For every WHERE clause term that is not used by the index
         4301  +** and which does not have an assigned truth probability, heuristics
         4302  +** described below are used to try to estimate the truth probability.
         4303  +** TODO --> Perhaps this is something that could be improved by better
         4304  +** table statistics.
         4305  +**
         4306  +** Heuristic 1:  Estimate the truth probability as 93.75%.  The 93.75%
         4307  +** value corresponds to -1 in LogEst notation, so this means decrement
         4308  +** the WhereLoop.nOut field for every such WHERE clause term.
         4309  +**
         4310  +** Heuristic 2:  If there exists one or more WHERE clause terms of the
         4311  +** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
         4312  +** final output row estimate is no greater than 1/4 of the total number
         4313  +** of rows in the table.  In other words, assume that x==EXPR will filter
         4314  +** out at least 3 out of 4 rows.  If EXPR is -1 or 0 or 1, then maybe the
         4315  +** "x" column is boolean or else -1 or 0 or 1 is a common default value
         4316  +** on the "x" column and so in that case only cap the output row estimate
         4317  +** at 1/2 instead of 1/4.
  4298   4318   */
  4299   4319   static void whereLoopOutputAdjust(
  4300   4320     WhereClause *pWC,      /* The WHERE clause */
  4301   4321     WhereLoop *pLoop,      /* The loop to adjust downward */
  4302   4322     LogEst nRow            /* Number of rows in the entire table */
  4303   4323   ){
  4304   4324     WhereTerm *pTerm, *pX;
  4305   4325     Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
  4306         -  int i, j;
  4307         -  int nEq = 0;    /* Number of = constraints not within likely()/unlikely() */
         4326  +  int i, j, k;
         4327  +  LogEst iReduce = 0;    /* pLoop->nOut should not exceed nRow-iReduce */
  4308   4328   
         4329  +  assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
  4309   4330     for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
  4310   4331       if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
  4311   4332       if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
  4312   4333       if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
  4313   4334       for(j=pLoop->nLTerm-1; j>=0; j--){
  4314   4335         pX = pLoop->aLTerm[j];
  4315   4336         if( pX==0 ) continue;
  4316   4337         if( pX==pTerm ) break;
  4317   4338         if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
  4318   4339       }
  4319   4340       if( j<0 ){
  4320   4341         if( pTerm->truthProb<=0 ){
         4342  +        /* If a truth probability is specified using the likelihood() hints,
         4343  +        ** then use the probability provided by the application. */
  4321   4344           pLoop->nOut += pTerm->truthProb;
  4322   4345         }else{
         4346  +        /* In the absence of explicit truth probabilities, use heuristics to
         4347  +        ** guess a reasonable truth probability. */
  4323   4348           pLoop->nOut--;
  4324         -        if( pTerm->eOperator&WO_EQ ) nEq++;
         4349  +        if( pTerm->eOperator&WO_EQ ){
         4350  +          Expr *pRight = pTerm->pExpr->pRight;
         4351  +          if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
         4352  +            k = 10;
         4353  +          }else{
         4354  +            k = 20;
         4355  +          }
         4356  +          if( iReduce<k ) iReduce = k;
         4357  +        }
  4325   4358         }
  4326   4359       }
  4327   4360     }
  4328         -  /* TUNING:  If there is at least one equality constraint in the WHERE
  4329         -  ** clause that does not have a likelihood() explicitly assigned to it
  4330         -  ** then do not let the estimated number of output rows exceed half 
  4331         -  ** the number of rows in the table. */
  4332         -  if( nEq && pLoop->nOut>nRow-10 ){
  4333         -    pLoop->nOut = nRow - 10;
  4334         -  }
         4361  +  if( pLoop->nOut > nRow-iReduce )  pLoop->nOut = nRow - iReduce;
  4335   4362   }
  4336   4363   
  4337   4364   /*
  4338   4365   ** Adjust the cost C by the costMult facter T.  This only occurs if
  4339   4366   ** compiled with -DSQLITE_ENABLE_COSTMULT
  4340   4367   */
  4341   4368   #ifdef SQLITE_ENABLE_COSTMULT

Changes to test/autoindex2.test.

   220    220     ORDER BY t1.ptime desc LIMIT 500;
   221    221   } {0 0 0 {SEARCH TABLE t1 USING INDEX t1x1 (ptime>?)} 0 1 1 {SEARCH TABLE t2 USING INDEX t2x0 (did=?)} 0 2 2 {SEARCH TABLE t3 USING INDEX t3x0 (uid=?)}}
   222    222   #
   223    223   # ^^^--- Before being fixed, the above was using an automatic covering
   224    224   # on t3 and reordering the tables so that t3 was in the outer loop and
   225    225   # implementing the ORDER BY clause using a B-Tree.
   226    226   
   227         -do_execsql_test autoindex2-120 {
   228         -  EXPLAIN QUERY PLAN
   229         -  SELECT
   230         -     t1_id,
   231         -     t1.did,
   232         -     param2,
   233         -     param3,
   234         -     t1.ptime,
   235         -     t1.trange,
   236         -     t1.exmass,
   237         -     t1.mass,
   238         -     t1.vstatus,
   239         -     type,
   240         -     subtype,
   241         -     t1.deviation,
   242         -     t1.formula,
   243         -     dparam1,
   244         -     reserve1,
   245         -     reserve2,
   246         -     param4,
   247         -     t1.last_operation,
   248         -     t1.admin_uuid,
   249         -     t1.previous_value,
   250         -     t1.job_id,
   251         -     client_did, 
   252         -     t1.last_t1,
   253         -     t1.data_t1,
   254         -     t1.previous_date,
   255         -     param5,
   256         -     param6,
   257         -     mgr_uuid
   258         -  FROM
   259         -     t3,
   260         -     t2,
   261         -     t1
   262         -  WHERE
   263         -     t1.ptime > 1393520400
   264         -     AND param3<>9001
   265         -     AND t3.flg7 = 1
   266         -     AND t1.did = t2.did
   267         -     AND t2.uid = t3.uid
   268         -  ORDER BY t1.ptime desc LIMIT 500;
   269         -} {0 0 2 {SEARCH TABLE t1 USING INDEX t1x1 (ptime>?)} 0 1 1 {SEARCH TABLE t2 USING INDEX t2x0 (did=?)} 0 2 0 {SEARCH TABLE t3 USING INDEX t3x0 (uid=?)}}
   270         -
   271    227   finish_test

Added test/bigsort.test.

            1  +# 2014 November 26
            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  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +set testprefix bigsort
           16  +
           17  +#--------------------------------------------------------------------
           18  +# At one point there was an overflow problem if the product of the 
           19  +# cache-size and page-size was larger than 2^31. Causing an infinite 
           20  +# loop if the product was also an integer multiple of 2^32, or 
           21  +# inefficiency otherwise.
           22  +#
           23  +do_execsql_test 1.0 {
           24  +  PRAGMA page_size = 1024;
           25  +  CREATE TABLE t1(a, b);
           26  +  BEGIN;
           27  +  WITH data(x,y) AS (
           28  +    SELECT 1, zeroblob(10000)
           29  +    UNION ALL
           30  +    SELECT x+1, y FROM data WHERE x < 300000
           31  +  )
           32  +  INSERT INTO t1 SELECT * FROM data;
           33  +  COMMIT;
           34  +}
           35  +do_execsql_test 1.1 {
           36  +  PRAGMA cache_size = 4194304;
           37  +  CREATE INDEX i1 ON t1(a, b);
           38  +}
           39  +
           40  +
           41  +finish_test
           42  +
           43  +

Added test/btree01.test.

            1  +# 2014-11-27
            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 test cases for b-tree logic.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set testprefix btree01
           18  +
           19  +# The refactoring on the b-tree balance() routine in check-in
           20  +# http://www.sqlite.org/src/info/face33bea1ba3a (2014-10-27)
           21  +# caused the integrity_check on the following SQL to fail.
           22  +#
           23  +do_execsql_test btree01-1.1 {
           24  +  PRAGMA page_size=65536;
           25  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);
           26  +  WITH RECURSIVE
           27  +     c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<30)
           28  +  INSERT INTO t1(a,b) SELECT i, zeroblob(6500) FROM c;
           29  +  UPDATE t1 SET b=zeroblob(3000);
           30  +  UPDATE t1 SET b=zeroblob(64000) WHERE a=2;
           31  +  PRAGMA integrity_check;
           32  +} {ok}
           33  +
           34  +# The previous test is sufficient to prevent a regression.  But we
           35  +# add a number of additional tests to stress the balancer in similar
           36  +# ways, looking for related problems.
           37  +#
           38  +for {set i 1} {$i<=30} {incr i} {
           39  +  do_test btree01-1.2.$i {
           40  +    db eval {
           41  +      DELETE FROM t1;
           42  +      WITH RECURSIVE
           43  +        c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<30)
           44  +      INSERT INTO t1(a,b) SELECT i, zeroblob(6500) FROM c;
           45  +      UPDATE t1 SET b=zeroblob(3000);
           46  +      UPDATE t1 SET b=zeroblob(64000) WHERE a=$::i;
           47  +      PRAGMA integrity_check;
           48  +    }
           49  +  } {ok}
           50  +}
           51  +for {set i 1} {$i<=30} {incr i} {
           52  +  do_test btree01-1.3.$i {
           53  +    db eval {
           54  +      DELETE FROM t1;
           55  +      WITH RECURSIVE
           56  +        c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<30)
           57  +      INSERT INTO t1(a,b) SELECT i, zeroblob(6500) FROM c;
           58  +      UPDATE t1 SET b=zeroblob(2000);
           59  +      UPDATE t1 SET b=zeroblob(64000) WHERE a=$::i;
           60  +      PRAGMA integrity_check;
           61  +    }
           62  +  } {ok}
           63  +}
           64  +for {set i 1} {$i<=30} {incr i} {
           65  +  do_test btree01-1.4.$i {
           66  +    db eval {
           67  +      DELETE FROM t1;
           68  +      WITH RECURSIVE
           69  +        c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<30)
           70  +      INSERT INTO t1(a,b) SELECT i, zeroblob(6500) FROM c;
           71  +      UPDATE t1 SET b=zeroblob(6499) WHERE (a%3)==0;
           72  +      UPDATE t1 SET b=zeroblob(6499) WHERE (a%3)==1;
           73  +      UPDATE t1 SET b=zeroblob(6499) WHERE (a%3)==2;
           74  +      UPDATE t1 SET b=zeroblob(64000) WHERE a=$::i;
           75  +      PRAGMA integrity_check;
           76  +    }
           77  +  } {ok}
           78  +}
           79  +for {set i 1} {$i<=30} {incr i} {
           80  +  do_test btree01-1.5.$i {
           81  +    db eval {
           82  +      DELETE FROM t1;
           83  +      WITH RECURSIVE
           84  +        c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<30)
           85  +      INSERT INTO t1(a,b) SELECT i, zeroblob(6542) FROM c;
           86  +      UPDATE t1 SET b=zeroblob(2331);
           87  +      UPDATE t1 SET b=zeroblob(65496) WHERE a=$::i;
           88  +      PRAGMA integrity_check;
           89  +    }
           90  +  } {ok}
           91  +}
           92  +for {set i 1} {$i<=30} {incr i} {
           93  +  do_test btree01-1.6.$i {
           94  +    db eval {
           95  +      DELETE FROM t1;
           96  +      WITH RECURSIVE
           97  +        c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<30)
           98  +      INSERT INTO t1(a,b) SELECT i, zeroblob(6542) FROM c;
           99  +      UPDATE t1 SET b=zeroblob(2332);
          100  +      UPDATE t1 SET b=zeroblob(65496) WHERE a=$::i;
          101  +      PRAGMA integrity_check;
          102  +    }
          103  +  } {ok}
          104  +}
          105  +for {set i 1} {$i<=30} {incr i} {
          106  +  do_test btree01-1.7.$i {
          107  +    db eval {
          108  +      DELETE FROM t1;
          109  +      WITH RECURSIVE
          110  +        c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<30)
          111  +      INSERT INTO t1(a,b) SELECT i, zeroblob(6500) FROM c;
          112  +      UPDATE t1 SET b=zeroblob(1);
          113  +      UPDATE t1 SET b=zeroblob(65000) WHERE a=$::i;
          114  +      PRAGMA integrity_check;
          115  +    }
          116  +  } {ok}
          117  +}
          118  +for {set i 1} {$i<=31} {incr i} {
          119  +  do_test btree01-1.8.$i {
          120  +    db eval {
          121  +      DELETE FROM t1;
          122  +      WITH RECURSIVE
          123  +        c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<31)
          124  +      INSERT INTO t1(a,b) SELECT i, zeroblob(6500) FROM c;
          125  +      UPDATE t1 SET b=zeroblob(4000);
          126  +      UPDATE t1 SET b=zeroblob(65000) WHERE a=$::i;
          127  +      PRAGMA integrity_check;
          128  +    }
          129  +  } {ok}
          130  +}
          131  +
          132  +finish_test

Changes to test/distinct.test.

   217    217       SELECT DISTINCT
   218    218         CASE a WHEN 1 THEN x'0000000000'
   219    219                WHEN 2 THEN zeroblob(5)
   220    220                ELSE 'xyzzy' END
   221    221         FROM t1;
   222    222     SELECT quote(x) FROM t2 ORDER BY 1;
   223    223   } {'xyzzy' X'0000000000'}
          224  +
          225  +#----------------------------------------------------------------------------
          226  +# Ticket [c5ea805691bfc4204b1cb9e9aa0103bd48bc7d34] (2014-12-04)
          227  +# Make sure that DISTINCT works together with ORDER BY and descending
          228  +# indexes.
          229  +#
          230  +do_execsql_test 5.1 {
          231  +  DROP TABLE IF EXISTS t1;
          232  +  CREATE TABLE t1(x);
          233  +  INSERT INTO t1(x) VALUES(3),(1),(5),(2),(6),(4),(5),(1),(3);
          234  +  CREATE INDEX t1x ON t1(x DESC);
          235  +  SELECT DISTINCT x FROM t1 ORDER BY x ASC;
          236  +} {1 2 3 4 5 6}
          237  +do_execsql_test 5.2 {
          238  +  SELECT DISTINCT x FROM t1 ORDER BY x DESC;
          239  +} {6 5 4 3 2 1}
          240  +do_execsql_test 5.3 {
          241  +  SELECT DISTINCT x FROM t1 ORDER BY x;
          242  +} {1 2 3 4 5 6}
          243  +do_execsql_test 5.4 {
          244  +  DROP INDEX t1x;
          245  +  CREATE INDEX t1x ON t1(x ASC);
          246  +  SELECT DISTINCT x FROM t1 ORDER BY x ASC;
          247  +} {1 2 3 4 5 6}
          248  +do_execsql_test 5.5 {
          249  +  SELECT DISTINCT x FROM t1 ORDER BY x DESC;
          250  +} {6 5 4 3 2 1}
          251  +do_execsql_test 5.6 {
          252  +  SELECT DISTINCT x FROM t1 ORDER BY x;
          253  +} {1 2 3 4 5 6}
   224    254   
   225    255   finish_test

Added test/e_walckpt.test.

            1  +# 2014 December 04
            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  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +source $testdir/lock_common.tcl
           16  +source $testdir/wal_common.tcl
           17  +set testprefix e_walckpt
           18  +
           19  +# The following two commands are used to determine if any of the files
           20  +# "test.db", "test.db2" and "test.db3" are modified by a test case.
           21  +#
           22  +# The [save_db_hashes] command saves a hash of the current contents of
           23  +# all three files in global variables. The [compare_db_hashes] compares
           24  +# the current contents with the saved hashes and returns a list of the
           25  +# files that have changed.
           26  +#
           27  +proc save_db_hashes {} {
           28  +  global H
           29  +  foreach f {test.db test.db2 test.db3} {
           30  +    set H($f) 0
           31  +    catch { set H($f) [md5file $f] }
           32  +  }
           33  +}
           34  +proc compare_db_hashes {} {
           35  +  global H
           36  +  set ret [list]
           37  +  foreach f {test.db test.db2 test.db3} {
           38  +    set expect 0
           39  +    catch { set expect [md5file $f] }
           40  +    if {$H($f) != $expect} { lappend ret $f }
           41  +  }
           42  +  set ret
           43  +}
           44  +
           45  +
           46  +# The following tests are run 3 times, each using a different method of 
           47  +# invoking a checkpoint:
           48  +#
           49  +#   1) Using sqlite3_wal_checkpoint_v2()
           50  +#   2) Using "PRAGMA wal_checkpoint"
           51  +#   3) Using sqlite3_wal_checkpoint() in place of checkpoint_v2(PASSIVE)
           52  +#
           53  +# Cases (2) and (3) are to show that the following statements are 
           54  +# correct, respectively:
           55  +#
           56  +# EVIDENCE-OF: R-36706-10507 The PRAGMA wal_checkpoint command can be
           57  +# used to invoke this interface from SQL.
           58  +#
           59  +# EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is
           60  +# equivalent to
           61  +# sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0).
           62  +# 
           63  +foreach {tn script} {
           64  +  1 {
           65  +    proc checkpoint {db mode args} {
           66  +      eval sqlite3_wal_checkpoint_v2 [list $db] [list $mode] $args
           67  +    }
           68  +  }
           69  +
           70  +  2 {
           71  +    proc checkpoint {db mode args} {
           72  +      set sql "PRAGMA wal_checkpoint"
           73  +      if {[llength $args] && [lindex $args 0]!=""} {
           74  +        set sql "PRAGMA [lindex $args 0].wal_checkpoint"
           75  +      }
           76  +      set rc [catch { $db eval $sql } msg]
           77  +      if {$rc} {
           78  +        regsub {database} $msg {database:} msg
           79  +        error "[sqlite3_errcode $db] - $msg"
           80  +      }
           81  +      set msg
           82  +    }
           83  +  }
           84  +
           85  +  3 {
           86  +    proc checkpoint {db mode args} {
           87  +      if {$mode == "passive"} {
           88  +        set rc [eval sqlite3_wal_checkpoint [list $db] $args]
           89  +        if {$rc != "SQLITE_OK"} {
           90  +          error "$rc - [sqlite3_errmsg $db]"
           91  +        }
           92  +      } else {
           93  +        eval sqlite3_wal_checkpoint_v2 [list $db] [list $mode] $args
           94  +      }
           95  +    }
           96  +  }
           97  +
           98  +} {
           99  +
          100  +  eval $script
          101  +
          102  +  reset_db
          103  +  forcedelete test.db2 test.db3 test.db4
          104  +  execsql {
          105  +    ATTACH 'test.db2' AS aux;
          106  +    ATTACH 'test.db3' AS aux2;
          107  +    ATTACH 'test.db4' AS aux3;
          108  +    CREATE TABLE t1(x);
          109  +    CREATE TABLE aux.t2(x);
          110  +    CREATE TABLE aux2.t3(x);
          111  +    CREATE TABLE aux3.t4(x);
          112  +    PRAGMA main.journal_mode = WAL;
          113  +    PRAGMA aux.journal_mode = WAL;
          114  +    PRAGMA aux2.journal_mode = WAL;
          115  +    /* Leave aux4 in rollback mode */
          116  +  }
          117  +
          118  +  # EVIDENCE-OF: R-49787-09095 The sqlite3_wal_checkpoint_v2(D,X,M,L,C)
          119  +  # interface runs a checkpoint operation on database X of database
          120  +  # connection D in mode M. Status information is written back into
          121  +  # integers pointed to by L and C.
          122  +  #
          123  +  #     Tests 1, 2 and 3 below verify the "on database X" part of the
          124  +  #     above. Other parts of this requirement are tested below.
          125  +  #
          126  +  # EVIDENCE-OF: R-00653-06026 If parameter zDb is NULL or points to a
          127  +  # zero length string, then the specified operation is attempted on all
          128  +  # WAL databases attached to database connection db.
          129  +  #
          130  +  #     Tests 4 and 5 below test this.
          131  +  #
          132  +  foreach {tn2 zDb dblist} {
          133  +    1 main  test.db
          134  +    2 aux   test.db2
          135  +    3 aux2  test.db3
          136  +    4 ""    {test.db test.db2 test.db3}
          137  +    5 -     {test.db test.db2 test.db3}
          138  +    6 temp  {}
          139  +  } {
          140  +    do_test $tn.1.$tn2 {
          141  +      execsql {
          142  +        INSERT INTO t1 VALUES(1);
          143  +        INSERT INTO t2 VALUES(2);
          144  +        INSERT INTO t3 VALUES(3);
          145  +      }
          146  +      save_db_hashes
          147  +
          148  +      if {$zDb == "-"} {
          149  +        checkpoint db passive
          150  +      } else {
          151  +        checkpoint db passive $zDb
          152  +      }
          153  +
          154  +      compare_db_hashes
          155  +    } $dblist
          156  +  }
          157  +
          158  +  # EVIDENCE-OF: R-38207-48996 If zDb is not NULL (or a zero length
          159  +  # string) and is not the name of any attached database, SQLITE_ERROR is
          160  +  # returned to the caller.
          161  +  do_test $tn.2.1 {
          162  +    list [catch { checkpoint db passive notadb } msg] $msg
          163  +  } {1 {SQLITE_ERROR - unknown database: notadb}}
          164  +
          165  +  # EVIDENCE-OF: R-14303-42483 If database zDb is the name of an attached
          166  +  # database that is not in WAL mode, SQLITE_OK is returned and both
          167  +  # *pnLog and *pnCkpt set to -1.
          168  +  #
          169  +  if {$tn==3} {
          170  +    # With sqlite3_wal_checkpoint() the two output variables cannot be 
          171  +    # tested. So just test that no error is returned when attempting to
          172  +    # checkpoint a db in rollback mode.
          173  +    do_test $tn.2.2.a { checkpoint db passive aux3 } {}
          174  +  } else {
          175  +    do_test $tn.2.2.b { checkpoint db passive aux3 } {0 -1 -1}
          176  +  }
          177  +
          178  +  # EVIDENCE-OF: R-62028-47212 All calls obtain an exclusive "checkpoint"
          179  +  # lock on the database file.
          180  +  db close
          181  +  testvfs tvfs
          182  +  tvfs filter xShmLock
          183  +  tvfs script filelock
          184  +  proc filelock {method file handle details} {
          185  +    # Test for an exclusive checkpoint lock. A checkpoint lock locks a
          186  +    # single byte starting at offset 1.
          187  +    if {$details == "1 1 lock exclusive"} { set ::seen_checkpoint_lock 1 }
          188  +  }
          189  +  sqlite3 db test.db -vfs tvfs
          190  +  do_test $tn.3.1 {
          191  +    execsql { INSERT INTO t1 VALUES('xyz') }
          192  +    unset -nocomplain ::seen_checkpoint_lock
          193  +    checkpoint db passive
          194  +    set ::seen_checkpoint_lock
          195  +  } {1}
          196  +  db close
          197  +  tvfs delete
          198  +  reset_db
          199  +
          200  +
          201  + 
          202  +
          203  +  #-----------------------------------------------------------------------
          204  +  # EVIDENCE-OF: R-10421-19736 If any other process is running a
          205  +  # checkpoint operation at the same time, the lock cannot be obtained and
          206  +  # SQLITE_BUSY is returned.
          207  +  #
          208  +  # EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
          209  +  # it will not be invoked in this case.
          210  +  #
          211  +  testvfs tvfs
          212  +  tvfs filter xWrite
          213  +  sqlite3 db test.db -vfs tvfs
          214  +  sqlite3 db2 test.db -vfs tvfs
          215  +
          216  +  do_test $tn.3.2.1 {
          217  +    db2 eval {
          218  +      PRAGMA journal_mode = WAL;
          219  +      CREATE TABLE t1(x, y);
          220  +      INSERT INTO t1 VALUES(1,2);
          221  +      INSERT INTO t1 VALUES(3,4);
          222  +      INSERT INTO t1 VALUES(5,6);
          223  +    }
          224  +    file size test.db-wal
          225  +  } [wal_file_size 5 1024]
          226  +
          227  +
          228  +  # Connection [db] runs a checkpoint. During this checkpoint, each
          229  +  # time it calls xWrite() to write a page into the database file, we
          230  +  # attempt to start a checkpoint using [db2]. According to the 
          231  +  # first requirement being tested, this should return SQLITE_BUSY. According
          232  +  # to the second, the busy-handler belonging to [db2] should not be
          233  +  # invoked.
          234  +  #
          235  +  set ::write_count 0
          236  +  set ::write_errors [list]
          237  +  proc busy_callback {args} {
          238  +    lappend ::write_errors "busy handler called!"
          239  +  }
          240  +  proc write_callback {args} {
          241  +    set rc [catch {checkpoint db2 passive} msg]
          242  +    if {0==[regexp "database is locked" $msg] && $msg!="1 -1 -1"} {
          243  +      lappend ::write_errors "$rc $msg"
          244  +    } 
          245  +    incr ::write_count
          246  +  }
          247  +  db2 busy busy_callback
          248  +  tvfs script write_callback
          249  +
          250  +  do_test $tn.3.2.2 {
          251  +    db eval {SELECT * FROM sqlite_master}
          252  +    checkpoint db full
          253  +    set ::write_count
          254  +  } {2}
          255  +
          256  +  do_test $tn.3.2.3 {
          257  +    set ::write_errors
          258  +  } {}
          259  +
          260  +  db close
          261  +  db2 close
          262  +  tvfs delete
          263  +
          264  +}
          265  +
          266  +#-----------------------------------------------------------------------
          267  +# EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
          268  +# mode:
          269  +#
          270  +#   Valid checkpoint modes are 0, 1, 2 and 3.
          271  +#
          272  +sqlite3 db test.db
          273  +foreach {tn mode res} {
          274  +  0 -1001    {1 {SQLITE_MISUSE - not an error}}
          275  +  1 -1       {1 {SQLITE_MISUSE - not an error}}
          276  +  2  0       {0 {0 -1 -1}}
          277  +  3  1       {0 {0 -1 -1}}
          278  +  4  2       {0 {0 -1 -1}}
          279  +  5  3       {0 {0 -1 -1}}
          280  +  6  4       {1 {SQLITE_MISUSE - not an error}}
          281  +  7  114     {1 {SQLITE_MISUSE - not an error}}
          282  +  8  1000000 {1 {SQLITE_MISUSE - not an error}}
          283  +} {
          284  +  do_test 4.$tn {
          285  +    list [catch "sqlite3_wal_checkpoint_v2 db $mode" msg] $msg
          286  +  } $res
          287  +}
          288  +
          289  +
          290  +finish_test

Changes to test/join5.test.

   102    102   do_test join5-2.11 {
   103    103     execsql {SELECT * FROM xy LEFT JOIN ab ON 1 WHERE NULL}
   104    104   } {}
   105    105   do_test join5-2.12 {
   106    106     execsql {SELECT * FROM xy LEFT JOIN ab ON NULL WHERE NULL}
   107    107   } {}
   108    108   
          109  +# Ticket https://www.sqlite.org/src/tktview/6f2222d550f5b0ee7ed37601
          110  +# Incorrect output on a LEFT JOIN.
          111  +#
          112  +do_execsql_test join5-3.1 {
          113  +  DROP TABLE IF EXISTS t1;
          114  +  DROP TABLE IF EXISTS t2;
          115  +  DROP TABLE IF EXISTS t3;
          116  +  CREATE TABLE x1(a);
          117  +  INSERT INTO x1 VALUES(1);
          118  +  CREATE TABLE x2(b NOT NULL);
          119  +  CREATE TABLE x3(c, d);
          120  +  INSERT INTO x3 VALUES('a', NULL);
          121  +  INSERT INTO x3 VALUES('b', NULL);
          122  +  INSERT INTO x3 VALUES('c', NULL);
          123  +  SELECT * FROM x1 LEFT JOIN x2 LEFT JOIN x3 ON x3.d = x2.b;
          124  +} {1 {} {} {}}
          125  +do_execsql_test join5-3.2 {
          126  +  DROP TABLE IF EXISTS t1;
          127  +  DROP TABLE IF EXISTS t2;
          128  +  DROP TABLE IF EXISTS t3;
          129  +  DROP TABLE IF EXISTS t4;
          130  +  DROP TABLE IF EXISTS t5;
          131  +  CREATE TABLE t1(x text NOT NULL, y text);
          132  +  CREATE TABLE t2(u text NOT NULL, x text NOT NULL);
          133  +  CREATE TABLE t3(w text NOT NULL, v text);
          134  +  CREATE TABLE t4(w text NOT NULL, z text NOT NULL);
          135  +  CREATE TABLE t5(z text NOT NULL, m text);
          136  +  INSERT INTO t1 VALUES('f6d7661f-4efe-4c90-87b5-858e61cd178b',NULL);
          137  +  INSERT INTO t1 VALUES('f6ea82c3-2cad-45ce-ae8f-3ddca4fb2f48',NULL);
          138  +  INSERT INTO t1 VALUES('f6f47499-ecb4-474b-9a02-35be73c235e5',NULL);
          139  +  INSERT INTO t1 VALUES('56f47499-ecb4-474b-9a02-35be73c235e5',NULL);
          140  +  INSERT INTO t3 VALUES('007f2033-cb20-494c-b135-a1e4eb66130c',
          141  +                        'f6d7661f-4efe-4c90-87b5-858e61cd178b');
          142  +  SELECT *
          143  +    FROM t3
          144  +         INNER JOIN t1 ON t1.x= t3.v AND t1.y IS NULL
          145  +         LEFT JOIN t4  ON t4.w = t3.w
          146  +         LEFT JOIN t5  ON t5.z = t4.z
          147  +         LEFT JOIN t2  ON t2.u = t5.m
          148  +         LEFT JOIN t1 xyz ON xyz.y = t2.x;
          149  +} {007f2033-cb20-494c-b135-a1e4eb66130c f6d7661f-4efe-4c90-87b5-858e61cd178b f6d7661f-4efe-4c90-87b5-858e61cd178b {} {} {} {} {} {} {} {} {}}
          150  +do_execsql_test join5-3.3 {
          151  +  DROP TABLE IF EXISTS x1;
          152  +  DROP TABLE IF EXISTS x2;
          153  +  DROP TABLE IF EXISTS x3;
          154  +  CREATE TABLE x1(a);
          155  +  INSERT INTO x1 VALUES(1);
          156  +  CREATE TABLE x2(b NOT NULL);
          157  +  CREATE TABLE x3(c, d);
          158  +  INSERT INTO x3 VALUES('a', NULL);
          159  +  INSERT INTO x3 VALUES('b', NULL);
          160  +  INSERT INTO x3 VALUES('c', NULL);
          161  +  SELECT * FROM x1 LEFT JOIN x2 JOIN x3 WHERE x3.d = x2.b;
          162  +} {}
   109    163   
   110    164   finish_test

Changes to test/permutations.test.

   109    109     speed1.test speed1p.test speed2.test speed3.test speed4.test 
   110    110     speed4p.test sqllimits1.test tkt2686.test thread001.test thread002.test
   111    111     thread003.test thread004.test thread005.test trans2.test vacuum3.test 
   112    112     incrvacuum_ioerr.test autovacuum_crash.test btree8.test shared_err.test
   113    113     vtab_err.test walslow.test walcrash.test walcrash3.test
   114    114     walthread.test rtree3.test indexfault.test securedel2.test
   115    115     sort3.test sort4.test fts4growth.test fts4growth2.test
          116  +  bigsort.test
   116    117   }]
   117    118   if {[info exists ::env(QUICKTEST_INCLUDE)]} {
   118    119     set allquicktests [concat $allquicktests $::env(QUICKTEST_INCLUDE)]
   119    120   }
   120    121   
   121    122   #############################################################################
   122    123   # Start of tests

Changes to test/scanstatus.test.

   264    264     PRAGMA foreign_keys=on;
   265    265   }
   266    266   do_execsql_test    4.2.1 { DELETE FROM p1 WHERE x=4 }
   267    267   do_scanstatus_test 4.2.2 { 
   268    268     nLoop 1 nVisit 1 nEst 1.0 zName sqlite_autoindex_p1_1 
   269    269     zExplain {SEARCH TABLE p1 USING INDEX sqlite_autoindex_p1_1 (x=?)}
   270    270   
   271         -  nLoop 1 nVisit 3 nEst 524288.0 zName c1 zExplain {SCAN TABLE c1}
          271  +  nLoop 1 nVisit 3 nEst 262144.0 zName c1 zExplain {SCAN TABLE c1}
   272    272   }
   273    273   
   274    274   #-------------------------------------------------------------------------
   275    275   # Further tests of different scan types.
   276    276   #
   277    277   reset_db
   278    278   proc tochar {i} {

Added test/sharedB.test.

            1  +# 2014-12-05
            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  +# Open two database connections on the same database in shared cache
           13  +# mode.  Hold one open while repeatedly closing, reopening, and using
           14  +# the second.
           15  +#
           16  +# This test is designed to demostrate that the fix for ticket
           17  +# [e4a18565a36884b00edf66541f38c693827968ab] works.  
           18  +#
           19  +
           20  +
           21  +set testdir [file dirname $argv0]
           22  +source $testdir/tester.tcl
           23  +if {[run_thread_tests]==0} { finish_test ; return }
           24  +db close
           25  +set ::testprefix sharedB
           26  +
           27  +set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
           28  +
           29  +#-------------------------------------------------------------------------
           30  +#
           31  +do_test 1.1 {
           32  +  sqlite3 db1 test.db
           33  +  sqlite3 db2 test.db
           34  +
           35  +  db1 eval {
           36  +    CREATE TABLE t1(x,y TEXT COLLATE nocase);
           37  +    WITH RECURSIVE
           38  +      c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
           39  +    INSERT INTO t1(x,y) SELECT i, printf('x%03dy',i) FROM c;
           40  +    CREATE INDEX t1yx ON t1(y,x);
           41  +  }
           42  +  db2 eval {
           43  +    SELECT x FROM t1 WHERE y='X014Y';
           44  +  }
           45  +} {14}
           46  +
           47  +for {set j 1} {$j<=100} {incr j} {
           48  +  do_test 1.2.$j {
           49  +    db2 close
           50  +    sqlite3 db2 test.db
           51  +    db2 eval {
           52  +      SELECT x FROM t1 WHERE y='X014Y';
           53  +    }
           54  +  } {14}
           55  +}
           56  +
           57  +db1 close
           58  +db2 close
           59  +sqlite3_enable_shared_cache $::enable_shared_cache
           60  +finish_test

Changes to test/shell1.test.

    41     41   # invalid option
    42     42   do_test shell1-1.1.1 {
    43     43     set res [catchcmd "-bad test.db" ""]
    44     44     set rc [lindex $res 0]
    45     45     list $rc \
    46     46          [regexp {Error: unknown option: -bad} $res]
    47     47   } {1 1}
    48         -# error on extra options
    49         -do_test shell1-1.1.2 {
    50         -  set res [catchcmd "-bad test.db \"select 3\" \"select 4\"" ""]
           48  +do_test shell1-1.1.1b {
           49  +  set res [catchcmd "test.db -bad" ""]
    51     50     set rc [lindex $res 0]
    52     51     list $rc \
    53         -       [regexp {Error: too many options: "select 4"} $res]
           52  +       [regexp {Error: unknown option: -bad} $res]
    54     53   } {1 1}
           54  +# error on extra options
           55  +do_test shell1-1.1.2 {
           56  +  catchcmd "test.db \"select 3\" \"select 4\"" ""
           57  +} {0 {3
           58  +4}}
    55     59   # error on extra options
    56     60   do_test shell1-1.1.3 {
    57         -  set res [catchcmd "-bad FOO test.db BAD" ".quit"]
    58         -  set rc [lindex $res 0]
    59         -  list $rc \
    60         -       [regexp {Error: too many options: "BAD"} $res]
    61         -} {1 1}
           61  +  catchcmd "test.db FOO test.db BAD" ".quit"
           62  +} {1 {Error: near "FOO": syntax error}}
    62     63   
    63     64   # -help
    64     65   do_test shell1-1.2.1 {
    65     66     set res [catchcmd "-help test.db" ""]
    66     67     set rc [lindex $res 0]
    67     68     list $rc \
    68     69          [regexp {Usage} $res] \
................................................................................
    71     72   } {1 1 1 1}
    72     73   
    73     74   # -init filename       read/process named file
    74     75   do_test shell1-1.3.1 {
    75     76     catchcmd "-init FOO test.db" ""
    76     77   } {0 {}}
    77     78   do_test shell1-1.3.2 {
    78         -  set res [catchcmd "-init FOO test.db .quit BAD" ""]
    79         -  set rc [lindex $res 0]
    80         -  list $rc \
    81         -       [regexp {Error: too many options: "BAD"} $res]
    82         -} {1 1}
           79  +  catchcmd "-init FOO test.db .quit BAD" ""
           80  +} {0 {}}
           81  +do_test shell1-1.3.3 {
           82  +  catchcmd "-init FOO test.db BAD .quit" ""
           83  +} {1 {Error: near "BAD": syntax error}}
    83     84   
    84     85   # -echo                print commands before execution
    85     86   do_test shell1-1.4.1 {
    86     87     catchcmd "-echo test.db" "" 
    87     88   } {0 {}}
    88     89   
    89     90   # -[no]header          turn headers on or off

Changes to test/shell2.test.

    48     48     list $rc $fexist
    49     49   } {{0 {}} 1}
    50     50   
    51     51   # Shell silently ignores extra parameters.
    52     52   # Ticket [f5cb008a65].
    53     53   do_test shell2-1.2.1 {
    54     54     set rc [catch { eval exec $CLI \":memory:\" \"select 3\" \"select 4\" } msg]
    55         -  list $rc \
    56         -       [regexp {Error: too many options: "select 4"} $msg]
    57         -} {1 1}
           55  +  list $rc $msg
           56  +} {0 {3
           57  +4}}
    58     58   
    59     59   # Test a problem reported on the mailing list. The shell was at one point
    60     60   # returning the generic SQLITE_ERROR message ("SQL error or missing database")
    61     61   # instead of the "too many levels..." message in the test below.
    62     62   #
    63     63   do_test shell2-1.3 {
    64     64     catchcmd "-batch test.db" {

Changes to test/vtab1.test.

  1390   1390   do_execsql_test 21.2 {
  1391   1391     SELECT * FROM t9v WHERE a<b;
  1392   1392   } {1 2 3}
  1393   1393   
  1394   1394   do_execsql_test 21.3 {
  1395   1395     SELECT * FROM t9v WHERE a=b;
  1396   1396   } {2 2 2}
         1397  +
         1398  +#-------------------------------------------------------------------------
         1399  +# At one point executing a CREATE VIRTUAL TABLE statement that specified 
         1400  +# a database name but no virtual table arguments was causing an internal
         1401  +# buffer overread. Valgrind would report errors while running the following 
         1402  +# tests. Specifically:
         1403  +#
         1404  +#   CREATE VIRTUAL TABLE t1 USING fts4;          -- Ok - no db name.
         1405  +#   CREATE VIRTUAL TABLE main.t1 USING fts4(x);  -- Ok - has vtab arguments.
         1406  +#   CREATE VIRTUAL TABLE main.t1 USING fts4;     -- Had the problem. 
         1407  +#
         1408  +ifcapable fts3 {
         1409  +  forcedelete test.db2
         1410  +  set nm [string repeat abcdefghij 100]
         1411  +  do_execsql_test 22.1 {
         1412  +    ATTACH 'test.db2' AS $nm
         1413  +  }
         1414  +  
         1415  +  execsql "SELECT * FROM sqlite_master"
         1416  +  do_execsql_test 22.2 "CREATE VIRTUAL TABLE ${nm}.t1 USING fts4"
         1417  +  
         1418  +  do_test 22.3.1 {
         1419  +    set sql "CREATE VIRTUAL TABLE ${nm}.t2 USING fts4"
         1420  +    set stmt [sqlite3_prepare_v2 db $sql -1 dummy]
         1421  +    sqlite3_step $stmt
         1422  +  } {SQLITE_DONE}
         1423  +  
         1424  +  do_test 22.3.2 {
         1425  +    sqlite3_finalize $stmt
         1426  +  } {SQLITE_OK}
         1427  +  
         1428  +  do_test 22.4.1 {
         1429  +    set sql "CREATE VIRTUAL TABLE ${nm}.t3 USING fts4"
         1430  +    set n [string length $sql]
         1431  +    set stmt [sqlite3_prepare db "${sql}xyz" $n dummy]
         1432  +    sqlite3_step $stmt
         1433  +  } {SQLITE_DONE}
         1434  +  
         1435  +  do_test 22.4.2 {
         1436  +    sqlite3_finalize $stmt
         1437  +  } {SQLITE_OK}
         1438  +}
  1397   1439   
  1398   1440   finish_test

Changes to test/wal5.test.

    51     51       proc do_wal_checkpoint { dbhandle args } {
    52     52         set a(-mode) passive
    53     53         array set a $args
    54     54         foreach key [array names a] {
    55     55           if {[lsearch {-mode -db} $key]<0} { error "unknown switch: $key" }
    56     56         }
    57     57   
    58         -      if {$a(-mode)!="restart" && $a(-mode)!="full"} { set a(-mode) passive }
           58  +      set vals {restart full truncate}
           59  +      if {[lsearch -exact $vals $a(-mode)]<0} { set a(-mode) passive }
    59     60   
    60     61         set cmd [list sqlite3_wal_checkpoint_v2 $dbhandle $a(-mode)]
    61     62         if {[info exists a(-db)]} { lappend sql $a(-db) }
    62     63   
    63     64         uplevel $cmd
    64     65       }
    65     66     }
................................................................................
   274    275       6   FULL      3   {0 4 4}   2
   275    276   
   276    277       7   RESTART   -   {0 4 4}   3
   277    278       8   RESTART   1   {1 3 3}   1
   278    279       9   RESTART   2   {1 4 3}   2
   279    280       10  RESTART   3   {1 4 4}   3
   280    281   
          282  +    11  TRUNCATE  -   {0 0 0}   3
          283  +    12  TRUNCATE  1   {1 3 3}   1
          284  +    13  TRUNCATE  2   {1 4 3}   2
          285  +    14  TRUNCATE  3   {1 4 4}   3
          286  +
   281    287     } {
   282    288       do_multiclient_test tn {
   283    289         setup_and_attach_aux
   284    290   
   285    291         proc busyhandler {x} {
   286    292           set ::max_busyhandler $x
   287    293           if {$::busy_on!="-" && $x==$::busy_on} { return 1 }
................................................................................
   343    349       code1 {sqlite3 db  test.db}
   344    350       code2 {sqlite3 db2 test.db}
   345    351       code3 {sqlite3 db3 test.db}
   346    352   
   347    353       do_test 3.$tn.5 { sql3 { PRAGMA journal_mode } } {wal}
   348    354   
   349    355       do_test 3.$tn.6 { code3 { do_wal_checkpoint db3 } } {0 0 0}
          356  +  }
          357  +
          358  +  # Test SQLITE_CHECKPOINT_TRUNCATE.
          359  +  #
          360  +  do_multiclient_test tn {
          361  +
          362  +    code1 $do_wal_checkpoint
          363  +    code2 $do_wal_checkpoint
          364  +    code3 $do_wal_checkpoint
          365  +
          366  +    do_test 3.$tn.1 {
          367  +      sql1 {
          368  +        PRAGMA page_size = 1024;
          369  +        PRAGMA journal_mode = WAL;
          370  +        PRAGMA synchronous = normal;
          371  +        CREATE TABLE t1(x, y);
          372  +        CREATE INDEX i1 ON t1(x, y);
          373  +        INSERT INTO t1 VALUES(1, 2);
          374  +        INSERT INTO t1 VALUES(3, 4);
          375  +      }
          376  +      file size test.db-wal
          377  +    } [wal_file_size 8 1024]
          378  +
          379  +    do_test 3.$tn.2 { do_wal_checkpoint db -mode truncate } {0 0 0}
          380  +    do_test 3.$tn.3 { file size test.db-wal } 0
          381  +
          382  +    do_test 3.$tn.4 {
          383  +      sql2 { SELECT * FROM t1 }
          384  +    } {1 2 3 4}
          385  +
          386  +    do_test 3.$tn.5 {
          387  +      sql2 { INSERT INTO t1 VALUES('a', 'b') }
          388  +      file size test.db-wal
          389  +    } [wal_file_size 2 1024]
          390  +
   350    391     }
   351    392   }
   352    393   
   353    394   
   354    395   finish_test

Changes to test/whereJ.test.

   634    634     WHERE
   635    635        cx.code = '2990'
   636    636        AND cx.type=2
   637    637        AND px.cx_id = cx.cx_id
   638    638        AND px.px_tid = 0
   639    639        AND px.le_id = le.le_id;
   640    640   } {/.*SCAN TABLE cx.*SEARCH TABLE px.*SEARCH TABLE le.*/}
          641  +
          642  +
          643  +# The following test is derived from a performance problem reported from
          644  +# the field.  Notice the multiple indexes with the same initial tables,
          645  +# and the unusual WHERE clause terms.
          646  +#
          647  +do_test 5.1 {
          648  +  set res [db eval {
          649  +    DROP TABLE IF EXISTS t1;
          650  +    CREATE TABLE t1(a,b,c,d,e,f,g,h);
          651  +    CREATE INDEX t1abc ON t1(a,b,c);
          652  +    CREATE INDEX t1abe ON t1(a,b,e);
          653  +    CREATE INDEX t1abf ON t1(a,b,f);
          654  +    ANALYZE;
          655  +    DROP TABLE IF EXISTS sqlite_stat4;
          656  +    DROP TABLE IF EXISTS sqlite_stat3;
          657  +    DELETE FROM sqlite_stat1;
          658  +    INSERT INTO sqlite_stat1(tbl,idx,stat)
          659  +      VALUES('t1','t1abc','2000000 8000 1600 800'),
          660  +            ('t1','t1abe','2000000 8000 1600 150'),
          661  +            ('t1','t1abf','2000000 8000 1600 150');
          662  +    ANALYZE sqlite_master;
          663  +  
          664  +    EXPLAIN QUERY PLAN
          665  +    SELECT * FROM t1
          666  +     WHERE (a=1 OR a=2)
          667  +       AND (b=3 OR b=4)
          668  +       AND (d>=5 AND d<=5)
          669  +       AND ((e>=7 AND e<=7) OR (f>=8 AND f<=8))
          670  +       AND g>0;
          671  +  }]
          672  +} {~/ANY/}
          673  +do_test 5.2 {set res} {/USING INDEX t1abe/}
          674  +do_test 5.3 {set res} {/USING INDEX t1abf/}
          675  +
   641    676   
   642    677   
   643    678   finish_test