Index: Makefile.in ================================================================== --- Makefile.in +++ Makefile.in @@ -374,10 +374,11 @@ $(TOP)/src/test_pcache.c \ $(TOP)/src/test_quota.c \ $(TOP)/src/test_rtree.c \ $(TOP)/src/test_schema.c \ $(TOP)/src/test_server.c \ + $(TOP)/src/test_superlock.c \ $(TOP)/src/test_stat.c \ $(TOP)/src/test_tclvar.c \ $(TOP)/src/test_thread.c \ $(TOP)/src/test_vfs.c \ $(TOP)/src/test_wsd.c Index: ext/fts3/fts3.c ================================================================== --- ext/fts3/fts3.c +++ ext/fts3/fts3.c @@ -654,11 +654,11 @@ while( *zCsr!='=' ){ if( *zCsr=='\0' ) return 0; zCsr++; } - *pnKey = zCsr-z; + *pnKey = (int)(zCsr-z); zValue = sqlite3_mprintf("%s", &zCsr[1]); if( zValue ){ sqlite3Fts3Dequote(zValue); } *pzValue = zValue; @@ -709,11 +709,11 @@ nDb = (int)strlen(argv[1]) + 1; nName = (int)strlen(argv[2]) + 1; aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) ); if( !aCol ) return SQLITE_NOMEM; - memset(aCol, 0, sizeof(const char *) * (argc-2)); + memset((void *)aCol, 0, sizeof(const char *) * (argc-2)); /* Loop through all of the arguments passed by the user to the FTS3/4 ** module (i.e. all the column names and special arguments). This loop ** does the following: ** @@ -841,11 +841,11 @@ /* Declare the table schema to SQLite. */ fts3DeclareVtab(&rc, p); fts3_init_out: - sqlite3_free(aCol); + sqlite3_free((void *)aCol); if( rc!=SQLITE_OK ){ if( p ){ fts3DisconnectMethod((sqlite3_vtab *)p); }else if( pTokenizer ){ pTokenizer->pModule->xDestroy(pTokenizer); @@ -1020,11 +1020,10 @@ ** a prefix. ** ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK. */ static int fts3ScanInteriorNode( - Fts3Table *p, /* Virtual table handle */ const char *zTerm, /* Term to select leaves for */ int nTerm, /* Size of term zTerm in bytes */ const char *zNode, /* Buffer containing segment interior node */ int nNode, /* Size of buffer at zNode */ sqlite3_int64 *piFirst, /* OUT: Selected child node */ @@ -1051,11 +1050,11 @@ ** table, then there are always 20 bytes of zeroed padding following the ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). */ zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); - if( zCsr>=zEnd ){ + if( zCsr>zEnd ){ return SQLITE_CORRUPT; } while( zCsr1 ){ char *zBlob = 0; /* Blob read from %_segments table */ int nBlob; /* Size of zBlob in bytes */ @@ -1954,11 +1953,11 @@ */ static void fts3SegReaderArrayFree(Fts3SegReaderArray *pArray){ if( pArray ){ int i; for(i=0; inSegment; i++){ - sqlite3Fts3SegReaderFree(0, pArray->apSegment[i]); + sqlite3Fts3SegReaderFree(pArray->apSegment[i]); } sqlite3_free(pArray); } } @@ -1972,11 +1971,11 @@ int nNew = (pArray ? pArray->nAlloc+16 : 16); pArray = (Fts3SegReaderArray *)sqlite3_realloc(pArray, sizeof(Fts3SegReaderArray) + (nNew-1) * sizeof(Fts3SegReader*) ); if( !pArray ){ - sqlite3Fts3SegReaderFree(0, pNew); + sqlite3Fts3SegReaderFree(pNew); return SQLITE_NOMEM; } if( nNew==16 ){ pArray->nSegment = 0; pArray->nCost = 0; @@ -2025,18 +2024,18 @@ if( sqlite3_column_int64(pStmt, 1)==0 ){ /* The entire segment is stored on the root node (which must be a ** leaf). Do not bother inspecting any data in this case, just ** create a Fts3SegReader to scan the single leaf. */ - rc = sqlite3Fts3SegReaderNew(p, iAge, 0, 0, 0, zRoot, nRoot, &pNew); + rc = sqlite3Fts3SegReaderNew(iAge, 0, 0, 0, zRoot, nRoot, &pNew); }else{ sqlite3_int64 i1; /* First leaf that may contain zTerm */ sqlite3_int64 i2; /* Final leaf that may contain zTerm */ rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &i1, (isPrefix?&i2:0)); if( isPrefix==0 ) i2 = i1; if( rc==SQLITE_OK ){ - rc = sqlite3Fts3SegReaderNew(p, iAge, i1, i2, 0, 0, 0, &pNew); + rc = sqlite3Fts3SegReaderNew(iAge, i1, i2, 0, 0, 0, &pNew); } } assert( (pNew==0)==(rc!=SQLITE_OK) ); /* If a new Fts3SegReader was allocated, add it to the array. */ @@ -2198,11 +2197,11 @@ p += sqlite3Fts3GetVarint(p, &delta); fts3PoslistCopy(0, &p); pOut += sqlite3Fts3PutVarint(pOut, delta); } - *pnList = (pOut - aList); + *pnList = (int)(pOut - aList); } } /* ** Return a DocList corresponding to the phrase *pPhrase. @@ -2252,13 +2251,13 @@ } } for(ii=0; iinToken; ii++){ Fts3PhraseToken *pTok; /* Token to find doclist for */ - int iTok; /* The token being queried this iteration */ - char *pList; /* Pointer to token doclist */ - int nList; /* Size of buffer at pList */ + int iTok = 0; /* The token being queried this iteration */ + char *pList = 0; /* Pointer to token doclist */ + int nList = 0; /* Size of buffer at pList */ /* Select a token to process. If this is an xFilter() call, then tokens ** are processed in order from least to most costly. Otherwise, tokens ** are processed in the order in which they occur in the phrase. */ @@ -2296,12 +2295,13 @@ } if( pCsr->eEvalmode==FTS3_EVAL_NEXT && pTok->pDeferred ){ rc = fts3DeferredTermSelect(pTok->pDeferred, isTermPos, &nList, &pList); }else{ - assert( pTok->pArray ); - rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList); + if( pTok->pArray ){ + rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList); + } pTok->bFulltext = 1; } assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pArray==0 ); if( rc!=SQLITE_OK ) break; @@ -2525,11 +2525,14 @@ if( pExpr->eType==FTSQUERY_PHRASE ){ Fts3Phrase *pPhrase = pExpr->pPhrase; int ii; nCost = 0; for(ii=0; iinToken; ii++){ - nCost += pPhrase->aToken[ii].pArray->nCost; + Fts3SegReaderArray *pArray = pPhrase->aToken[ii].pArray; + if( pArray ){ + nCost += pPhrase->aToken[ii].pArray->nCost; + } } }else{ nCost = fts3ExprCost(pExpr->pLeft) + fts3ExprCost(pExpr->pRight); } return nCost; @@ -2553,11 +2556,11 @@ if( pExpr->eType==FTSQUERY_AND ){ fts3ExprAssignCosts(pExpr->pLeft, ppExprCost); fts3ExprAssignCosts(pExpr->pRight, ppExprCost); }else{ (*ppExprCost)->pExpr = pExpr; - (*ppExprCost)->nCost = fts3ExprCost(pExpr);; + (*ppExprCost)->nCost = fts3ExprCost(pExpr); (*ppExprCost)++; } } /* @@ -2678,12 +2681,17 @@ } } } } - *paOut = aRet; - *pnOut = nRet; + if( rc==SQLITE_OK ){ + *paOut = aRet; + *pnOut = nRet; + }else{ + assert( *paOut==0 ); + sqlite3_free(aRet); + } sqlite3_free(aExpr); fts3ExprFreeSegReaders(pExpr); }else{ char *aLeft; @@ -2752,10 +2760,11 @@ } sqlite3_free(aRight); } } + assert( rc==SQLITE_OK || *paOut==0 ); return rc; } /* ** This function is called from within xNext() for each row visited by @@ -3271,13 +3280,17 @@ sqlite3_context *pContext, /* SQLite function call context */ int nVal, /* Size of argument array */ sqlite3_value **apVal /* Array of arguments */ ){ Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */ - assert( nVal==1 ); + assert( nVal==1 || nVal==2 ); if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){ - sqlite3Fts3Matchinfo(pContext, pCsr); + const char *zArg = 0; + if( nVal>1 ){ + zArg = (const char *)sqlite3_value_text(apVal[1]); + } + sqlite3Fts3Matchinfo(pContext, pCsr, zArg); } } /* ** This routine implements the xFindFunction method for the FTS3 @@ -3462,10 +3475,11 @@ if( SQLITE_OK==rc && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1)) && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2)) && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1)) ){ rc = sqlite3_create_module_v2( db, "fts3", &fts3Module, (void *)pHash, hashDestroy ); Index: ext/fts3/fts3Int.h ================================================================== --- ext/fts3/fts3Int.h +++ ext/fts3/fts3Int.h @@ -160,19 +160,23 @@ i16 eSearch; /* Search strategy (see below) */ u8 isEof; /* True if at End Of Results */ u8 isRequireSeek; /* True if must seek pStmt to %_content row */ sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ Fts3Expr *pExpr; /* Parsed MATCH query string */ + int nPhrase; /* Number of matchable phrases in query */ Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */ sqlite3_int64 iPrevId; /* Previous id read from aDoclist */ char *pNextId; /* Pointer into the body of aDoclist */ char *aDoclist; /* List of docids for full-text queries */ int nDoclist; /* Size of buffer at aDoclist */ - int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */ - u32 *aMatchinfo; /* Information about most recent match */ int eEvalmode; /* An FTS3_EVAL_XX constant */ int nRowAvg; /* Average size of database rows, in pages */ + + int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */ + u32 *aMatchinfo; /* Information about most recent match */ + int nMatchinfo; /* Number of elements in aMatchinfo[] */ + char *zMatchinfo; /* Matchinfo specification */ }; #define FTS3_EVAL_FILTER 0 #define FTS3_EVAL_NEXT 1 #define FTS3_EVAL_MATCHINFO 2 @@ -275,25 +279,26 @@ /* fts3_write.c */ int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*); int sqlite3Fts3PendingTermsFlush(Fts3Table *); void sqlite3Fts3PendingTermsClear(Fts3Table *); int sqlite3Fts3Optimize(Fts3Table *); -int sqlite3Fts3SegReaderNew(Fts3Table *,int, sqlite3_int64, +int sqlite3Fts3SegReaderNew(int, sqlite3_int64, sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**); int sqlite3Fts3SegReaderPending(Fts3Table*,const char*,int,int,Fts3SegReader**); -void sqlite3Fts3SegReaderFree(Fts3Table *, Fts3SegReader *); +void sqlite3Fts3SegReaderFree(Fts3SegReader *); int sqlite3Fts3SegReaderIterate( Fts3Table *, Fts3SegReader **, int, Fts3SegFilter *, int (*)(Fts3Table *, void *, char *, int, char *, int), void * ); int sqlite3Fts3SegReaderCost(Fts3Cursor *, Fts3SegReader *, int *); int sqlite3Fts3AllSegdirs(Fts3Table*, sqlite3_stmt **); -int sqlite3Fts3MatchinfoDocsizeLocal(Fts3Cursor*, u32*); -int sqlite3Fts3MatchinfoDocsizeGlobal(Fts3Cursor*, u32*); int sqlite3Fts3ReadLock(Fts3Table *); int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*); +int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **); +int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **); + void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *); int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int); int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *); void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *); char *sqlite3Fts3DeferredDoclist(Fts3DeferredToken *, int *); @@ -337,11 +342,11 @@ /* fts3_snippet.c */ void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*); void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *, const char *, const char *, int, int ); -void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *); +void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *); /* fts3_expr.c */ int sqlite3Fts3ExprParse(sqlite3_tokenizer *, char **, int, int, const char *, int, Fts3Expr ** ); Index: ext/fts3/fts3_expr.c ================================================================== --- ext/fts3/fts3_expr.c +++ ext/fts3/fts3_expr.c @@ -403,11 +403,10 @@ /* Check for an open bracket. */ if( sqlite3_fts3_enable_parentheses ){ if( *zInput=='(' ){ int nConsumed; - int rc; pParse->nNest++; rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed); if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; } @@ -784,51 +783,57 @@ return sqlite3_finalize(pStmt); } /* -** This function is part of the test interface for the query parser. It -** writes a text representation of the query expression pExpr into the -** buffer pointed to by argument zBuf. It is assumed that zBuf is large -** enough to store the required text representation. +** Return a pointer to a buffer containing a text representation of the +** expression passed as the first argument. The buffer is obtained from +** sqlite3_malloc(). It is the responsibility of the caller to use +** sqlite3_free() to release the memory. If an OOM condition is encountered, +** NULL is returned. +** +** If the second argument is not NULL, then its contents are prepended to +** the returned expression text and then freed using sqlite3_free(). */ -static void exprToString(Fts3Expr *pExpr, char *zBuf){ +static char *exprToString(Fts3Expr *pExpr, char *zBuf){ switch( pExpr->eType ){ case FTSQUERY_PHRASE: { Fts3Phrase *pPhrase = pExpr->pPhrase; int i; - zBuf += sprintf(zBuf, "PHRASE %d %d", pPhrase->iColumn, pPhrase->isNot); - for(i=0; inToken; i++){ - zBuf += sprintf(zBuf," %.*s",pPhrase->aToken[i].n,pPhrase->aToken[i].z); - zBuf += sprintf(zBuf,"%s", (pPhrase->aToken[i].isPrefix?"+":"")); - } - return; - } - - case FTSQUERY_NEAR: - zBuf += sprintf(zBuf, "NEAR/%d ", pExpr->nNear); - break; - case FTSQUERY_NOT: - zBuf += sprintf(zBuf, "NOT "); - break; - case FTSQUERY_AND: - zBuf += sprintf(zBuf, "AND "); - break; - case FTSQUERY_OR: - zBuf += sprintf(zBuf, "OR "); - break; - } - - zBuf += sprintf(zBuf, "{"); - exprToString(pExpr->pLeft, zBuf); - zBuf += strlen(zBuf); - zBuf += sprintf(zBuf, "} "); - - zBuf += sprintf(zBuf, "{"); - exprToString(pExpr->pRight, zBuf); - zBuf += strlen(zBuf); - zBuf += sprintf(zBuf, "}"); + zBuf = sqlite3_mprintf( + "%zPHRASE %d %d", zBuf, pPhrase->iColumn, pPhrase->isNot); + for(i=0; zBuf && inToken; i++){ + zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, + pPhrase->aToken[i].n, pPhrase->aToken[i].z, + (pPhrase->aToken[i].isPrefix?"+":"") + ); + } + return zBuf; + } + + case FTSQUERY_NEAR: + zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear); + break; + case FTSQUERY_NOT: + zBuf = sqlite3_mprintf("%zNOT ", zBuf); + break; + case FTSQUERY_AND: + zBuf = sqlite3_mprintf("%zAND ", zBuf); + break; + case FTSQUERY_OR: + zBuf = sqlite3_mprintf("%zOR ", zBuf); + break; + } + + if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf); + if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf); + if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf); + + if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf); + if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf); + + return zBuf; } /* ** This is the implementation of a scalar SQL function used to test the ** expression parser. It should be called as follows: @@ -855,10 +860,11 @@ const char *zExpr; int nExpr; int nCol; int ii; Fts3Expr *pExpr; + char *zBuf = 0; sqlite3 *db = sqlite3_context_db_handle(context); if( argc<3 ){ sqlite3_result_error(context, "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1 @@ -897,21 +903,20 @@ } rc = sqlite3Fts3ExprParse( pTokenizer, azCol, nCol, nCol, zExpr, nExpr, &pExpr ); - if( rc==SQLITE_NOMEM ){ + if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){ + sqlite3_result_error(context, "Error parsing expression", -1); + }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){ sqlite3_result_error_nomem(context); - goto exprtest_out; - }else if( rc==SQLITE_OK ){ - char zBuf[4096]; - exprToString(pExpr, zBuf); + }else{ sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); - sqlite3Fts3ExprFree(pExpr); - }else{ - sqlite3_result_error(context, "Error parsing expression", -1); + sqlite3_free(zBuf); } + + sqlite3Fts3ExprFree(pExpr); exprtest_out: if( pModule && pTokenizer ){ rc = pModule->xDestroy(pTokenizer); } Index: ext/fts3/fts3_porter.c ================================================================== --- ext/fts3/fts3_porter.c +++ ext/fts3/fts3_porter.c @@ -341,11 +341,11 @@ */ static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){ int i, j; char zReverse[28]; char *z, *z2; - if( nIn<3 || nIn>=sizeof(zReverse)-7 ){ + if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){ /* The word is too big or too small for the porter stemmer. ** Fallback to the copy stemmer */ copy_stemmer(zIn, nIn, zOut, pnOut); return; } Index: ext/fts3/fts3_snippet.c ================================================================== --- ext/fts3/fts3_snippet.c +++ ext/fts3/fts3_snippet.c @@ -15,10 +15,26 @@ #include "fts3Int.h" #include #include +/* +** Characters that may appear in the second argument to matchinfo(). +*/ +#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */ +#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */ +#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */ +#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */ +#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */ +#define FTS3_MATCHINFO_LCS 's' /* nCol values */ +#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */ + +/* +** The default value for the second argument to matchinfo(). +*/ +#define FTS3_MATCHINFO_DEFAULT "pcx" + /* ** Used as an fts3ExprIterate() context when loading phrase doclists to ** Fts3Expr.aDoclist[]/nDoclist. */ @@ -68,10 +84,12 @@ */ typedef struct MatchInfo MatchInfo; struct MatchInfo { Fts3Cursor *pCursor; /* FTS3 Cursor */ int nCol; /* Number of columns in table */ + int nPhrase; /* Number of matchable phrases in query */ + sqlite3_int64 nDoc; /* Number of docs in database */ u32 *aMatchinfo; /* Pre-allocated buffer */ }; @@ -268,10 +286,22 @@ } if( pnPhrase ) *pnPhrase = sCtx.nPhrase; if( pnToken ) *pnToken = sCtx.nToken; return rc; } + +static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ + (*(int *)ctx)++; + UNUSED_PARAMETER(pExpr); + UNUSED_PARAMETER(iPhrase); + return SQLITE_OK; +} +static int fts3ExprPhraseCount(Fts3Expr *pExpr){ + int nPhrase = 0; + (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase); + return nPhrase; +} /* ** Advance the position list iterator specified by the first two ** arguments so that it points to the first element with a value greater ** than or equal to parameter iNext. @@ -781,24 +811,46 @@ *pp = pCsr; } /* ** fts3ExprIterate() callback used to collect the "global" matchinfo stats -** for a single query. The "global" stats are those elements of the matchinfo -** array that are constant for all rows returned by the current query. +** for a single query. +** +** fts3ExprIterate() callback to load the 'global' elements of a +** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements +** of the matchinfo array that are constant for all rows returned by the +** current query. +** +** Argument pCtx is actually a pointer to a struct of type MatchInfo. This +** function populates Matchinfo.aMatchinfo[] as follows: +** +** for(iCol=0; iColpCursor; char *pIter; char *pEnd; char *pFree = 0; - const int iStart = 2 + (iPhrase * p->nCol * 3) + 1; + u32 *aOut = &p->aMatchinfo[3*iPhrase*p->nCol]; assert( pExpr->isLoaded ); assert( pExpr->eType==FTSQUERY_PHRASE ); if( pCsr->pDeferred ){ @@ -812,14 +864,14 @@ int rc = sqlite3Fts3ExprLoadFtDoclist(pCsr, pExpr, &pFree, &nFree); if( rc!=SQLITE_OK ) return rc; pIter = pFree; pEnd = &pFree[nFree]; }else{ - int nDoc = p->aMatchinfo[2 + 3*p->nCol*p->aMatchinfo[0]]; - for(ii=0; iinCol; ii++){ - p->aMatchinfo[iStart + ii*3] = nDoc; - p->aMatchinfo[iStart + ii*3 + 1] = nDoc; + int iCol; /* Column index */ + for(iCol=0; iColnCol; iCol++){ + aOut[iCol*3 + 1] = (u32)p->nDoc; + aOut[iCol*3 + 2] = (u32)p->nDoc; } return SQLITE_OK; } }else{ pIter = pExpr->aDoclist; @@ -827,32 +879,32 @@ } /* Fill in the global hit count matrix row for this phrase. */ while( pIteraMatchinfo[iStart], 1); + fts3LoadColumnlistCounts(&pIter, &aOut[1], 1); } sqlite3_free(pFree); return SQLITE_OK; } /* -** fts3ExprIterate() callback used to collect the "local" matchinfo stats -** for a single query. The "local" stats are those elements of the matchinfo +** fts3ExprIterate() callback used to collect the "local" part of the +** FTS3_MATCHINFO_HITS array. The local stats are those elements of the ** array that are different for each row returned by the query. */ -static int fts3ExprLocalMatchinfoCb( +static int fts3ExprLocalHitsCb( Fts3Expr *pExpr, /* Phrase expression node */ int iPhrase, /* Phrase number */ void *pCtx /* Pointer to MatchInfo structure */ ){ MatchInfo *p = (MatchInfo *)pCtx; if( pExpr->aDoclist ){ char *pCsr; - int iStart = 2 + (iPhrase * p->nCol * 3); + int iStart = iPhrase * p->nCol * 3; int i; for(i=0; inCol; i++) p->aMatchinfo[iStart+i*3] = 0; pCsr = sqlite3Fts3FindPositions(pExpr, p->pCursor->iPrevId, -1); @@ -861,71 +913,404 @@ } } return SQLITE_OK; } + +static int fts3MatchinfoCheck( + Fts3Table *pTab, + char cArg, + char **pzErr +){ + if( (cArg==FTS3_MATCHINFO_NPHRASE) + || (cArg==FTS3_MATCHINFO_NCOL) + || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat) + || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat) + || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize) + || (cArg==FTS3_MATCHINFO_LCS) + || (cArg==FTS3_MATCHINFO_HITS) + ){ + return SQLITE_OK; + } + *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg); + return SQLITE_ERROR; +} + +static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ + int nVal; /* Number of integers output by cArg */ + + switch( cArg ){ + case FTS3_MATCHINFO_NDOC: + case FTS3_MATCHINFO_NPHRASE: + case FTS3_MATCHINFO_NCOL: + nVal = 1; + break; + + case FTS3_MATCHINFO_AVGLENGTH: + case FTS3_MATCHINFO_LENGTH: + case FTS3_MATCHINFO_LCS: + nVal = pInfo->nCol; + break; + + default: + assert( cArg==FTS3_MATCHINFO_HITS ); + nVal = pInfo->nCol * pInfo->nPhrase * 3; + break; + } + + return nVal; +} + +static int fts3MatchinfoSelectDoctotal( + Fts3Table *pTab, + sqlite3_stmt **ppStmt, + sqlite3_int64 *pnDoc, + const char **paLen +){ + sqlite3_stmt *pStmt; + const char *a; + sqlite3_int64 nDoc; + + if( !*ppStmt ){ + int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt); + if( rc!=SQLITE_OK ) return rc; + } + pStmt = *ppStmt; + + a = sqlite3_column_blob(pStmt, 0); + a += sqlite3Fts3GetVarint(a, &nDoc); + *pnDoc = (u32)nDoc; + + if( paLen ) *paLen = a; + return SQLITE_OK; +} + +/* +** An instance of the following structure is used to store state while +** iterating through a multi-column position-list corresponding to the +** hits for a single phrase on a single row in order to calculate the +** values for a matchinfo() FTS3_MATCHINFO_LCS request. +*/ +typedef struct LcsIterator LcsIterator; +struct LcsIterator { + Fts3Expr *pExpr; /* Pointer to phrase expression */ + char *pRead; /* Cursor used to iterate through aDoclist */ + int iPosOffset; /* Tokens count up to end of this phrase */ + int iCol; /* Current column number */ + int iPos; /* Current position */ +}; + +/* +** If LcsIterator.iCol is set to the following value, the iterator has +** finished iterating through all offsets for all columns. +*/ +#define LCS_ITERATOR_FINISHED 0x7FFFFFFF; + +static int fts3MatchinfoLcsCb( + Fts3Expr *pExpr, /* Phrase expression node */ + int iPhrase, /* Phrase number (numbered from zero) */ + void *pCtx /* Pointer to MatchInfo structure */ +){ + LcsIterator *aIter = (LcsIterator *)pCtx; + aIter[iPhrase].pExpr = pExpr; + return SQLITE_OK; +} + +/* +** Advance the iterator passed as an argument to the next position. Return +** 1 if the iterator is at EOF or if it now points to the start of the +** position list for the next column. +*/ +static int fts3LcsIteratorAdvance(LcsIterator *pIter){ + char *pRead = pIter->pRead; + sqlite3_int64 iRead; + int rc = 0; + + pRead += sqlite3Fts3GetVarint(pRead, &iRead); + if( iRead==0 ){ + pIter->iCol = LCS_ITERATOR_FINISHED; + rc = 1; + }else{ + if( iRead==1 ){ + pRead += sqlite3Fts3GetVarint(pRead, &iRead); + pIter->iCol = (int)iRead; + pIter->iPos = pIter->iPosOffset; + pRead += sqlite3Fts3GetVarint(pRead, &iRead); + rc = 1; + } + pIter->iPos += (int)(iRead-2); + } + + pIter->pRead = pRead; + return rc; +} + +/* +** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag. +** +** If the call is successful, the longest-common-substring lengths for each +** column are written into the first nCol elements of the pInfo->aMatchinfo[] +** array before returning. SQLITE_OK is returned in this case. +** +** Otherwise, if an error occurs, an SQLite error code is returned and the +** data written to the first nCol elements of pInfo->aMatchinfo[] is +** undefined. +*/ +static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){ + LcsIterator *aIter; + int i; + int iCol; + int nToken = 0; + + /* Allocate and populate the array of LcsIterator objects. The array + ** contains one element for each matchable phrase in the query. + **/ + aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase); + if( !aIter ) return SQLITE_NOMEM; + memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase); + (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter); + for(i=0; inPhrase; i++){ + LcsIterator *pIter = &aIter[i]; + nToken -= pIter->pExpr->pPhrase->nToken; + pIter->iPosOffset = nToken; + pIter->pRead = sqlite3Fts3FindPositions(pIter->pExpr, pCsr->iPrevId, -1); + if( pIter->pRead ){ + pIter->iPos = pIter->iPosOffset; + fts3LcsIteratorAdvance(&aIter[i]); + }else{ + pIter->iCol = LCS_ITERATOR_FINISHED; + } + } + + for(iCol=0; iColnCol; iCol++){ + int nLcs = 0; /* LCS value for this column */ + int nLive = 0; /* Number of iterators in aIter not at EOF */ + + /* Loop through the iterators in aIter[]. Set nLive to the number of + ** iterators that point to a position-list corresponding to column iCol. + */ + for(i=0; inPhrase; i++){ + assert( aIter[i].iCol>=iCol ); + if( aIter[i].iCol==iCol ) nLive++; + } + + /* The following loop runs until all iterators in aIter[] have finished + ** iterating through positions in column iCol. Exactly one of the + ** iterators is advanced each time the body of the loop is run. + */ + while( nLive>0 ){ + LcsIterator *pAdv = 0; /* The iterator to advance by one position */ + int nThisLcs = 0; /* LCS for the current iterator positions */ + + for(i=0; inPhrase; i++){ + LcsIterator *pIter = &aIter[i]; + if( iCol!=pIter->iCol ){ + /* This iterator is already at EOF for this column. */ + nThisLcs = 0; + }else{ + if( pAdv==0 || pIter->iPosiPos ){ + pAdv = pIter; + } + if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){ + nThisLcs++; + }else{ + nThisLcs = 1; + } + if( nThisLcs>nLcs ) nLcs = nThisLcs; + } + } + if( fts3LcsIteratorAdvance(pAdv) ) nLive--; + } + + pInfo->aMatchinfo[iCol] = nLcs; + } + + sqlite3_free(aIter); + return SQLITE_OK; +} + +/* +** Populate the buffer pInfo->aMatchinfo[] with an array of integers to +** be returned by the matchinfo() function. Argument zArg contains the +** format string passed as the second argument to matchinfo (or the +** default value "pcx" if no second argument was specified). The format +** string has already been validated and the pInfo->aMatchinfo[] array +** is guaranteed to be large enough for the output. +** +** If bGlobal is true, then populate all fields of the matchinfo() output. +** If it is false, then assume that those fields that do not change between +** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS) +** have already been populated. +** +** Return SQLITE_OK if successful, or an SQLite error code if an error +** occurs. If a value other than SQLITE_OK is returned, the state the +** pInfo->aMatchinfo[] buffer is left in is undefined. +*/ +static int fts3MatchinfoValues( + Fts3Cursor *pCsr, /* FTS3 cursor object */ + int bGlobal, /* True to grab the global stats */ + MatchInfo *pInfo, /* Matchinfo context object */ + const char *zArg /* Matchinfo format string */ +){ + int rc = SQLITE_OK; + int i; + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + sqlite3_stmt *pSelect = 0; + + for(i=0; rc==SQLITE_OK && zArg[i]; i++){ + + switch( zArg[i] ){ + case FTS3_MATCHINFO_NPHRASE: + if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase; + break; + + case FTS3_MATCHINFO_NCOL: + if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol; + break; + + case FTS3_MATCHINFO_NDOC: + if( bGlobal ){ + sqlite3_int64 nDoc; + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0); + pInfo->aMatchinfo[0] = (u32)nDoc; + } + break; + + case FTS3_MATCHINFO_AVGLENGTH: + if( bGlobal ){ + sqlite3_int64 nDoc; /* Number of rows in table */ + const char *a; /* Aggregate column length array */ + + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a); + if( rc==SQLITE_OK ){ + int iCol; + for(iCol=0; iColnCol; iCol++){ + sqlite3_int64 nToken; + a += sqlite3Fts3GetVarint(a, &nToken); + pInfo->aMatchinfo[iCol] = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc); + } + } + } + break; + + case FTS3_MATCHINFO_LENGTH: { + sqlite3_stmt *pSelectDocsize = 0; + rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize); + if( rc==SQLITE_OK ){ + int iCol; + const char *a = sqlite3_column_blob(pSelectDocsize, 0); + for(iCol=0; iColnCol; iCol++){ + sqlite3_int64 nToken; + a += sqlite3Fts3GetVarint(a, &nToken); + pInfo->aMatchinfo[iCol] = (u32)nToken; + } + } + sqlite3_reset(pSelectDocsize); + break; + } + + case FTS3_MATCHINFO_LCS: + rc = fts3ExprLoadDoclists(pCsr, 0, 0); + if( rc==SQLITE_OK ){ + rc = fts3MatchinfoLcs(pCsr, pInfo); + } + break; + + default: { + Fts3Expr *pExpr; + assert( zArg[i]==FTS3_MATCHINFO_HITS ); + pExpr = pCsr->pExpr; + rc = fts3ExprLoadDoclists(pCsr, 0, 0); + if( rc!=SQLITE_OK ) break; + if( bGlobal ){ + if( pCsr->pDeferred ){ + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0); + if( rc!=SQLITE_OK ) break; + } + rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo); + if( rc!=SQLITE_OK ) break; + } + (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo); + break; + } + } + + pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]); + } + + sqlite3_reset(pSelect); + return rc; +} + /* ** Populate pCsr->aMatchinfo[] with data for the current row. The ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32). */ -static int fts3GetMatchinfo(Fts3Cursor *pCsr){ +static int fts3GetMatchinfo( + Fts3Cursor *pCsr, /* FTS3 Cursor object */ + const char *zArg /* Second argument to matchinfo() function */ +){ MatchInfo sInfo; Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; int rc = SQLITE_OK; + int bGlobal = 0; /* Collect 'global' stats as well as local */ + memset(&sInfo, 0, sizeof(MatchInfo)); sInfo.pCursor = pCsr; sInfo.nCol = pTab->nColumn; + /* If there is cached matchinfo() data, but the format string for the + ** cache does not match the format string for this request, discard + ** the cached data. */ + if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){ + assert( pCsr->aMatchinfo ); + sqlite3_free(pCsr->aMatchinfo); + pCsr->zMatchinfo = 0; + pCsr->aMatchinfo = 0; + } + + /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the + ** matchinfo function has been called for this query. In this case + ** allocate the array used to accumulate the matchinfo data and + ** initialize those elements that are constant for every row. + */ if( pCsr->aMatchinfo==0 ){ - /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the - ** matchinfo function has been called for this query. In this case - ** allocate the array used to accumulate the matchinfo data and - ** initialize those elements that are constant for every row. - */ - int nPhrase; /* Number of phrases */ - int nMatchinfo; /* Number of u32 elements in match-info */ - - /* Load doclists for each phrase in the query. */ - rc = fts3ExprLoadDoclists(pCsr, &nPhrase, 0); - if( rc!=SQLITE_OK ){ - return rc; - } - nMatchinfo = 2 + 3*sInfo.nCol*nPhrase; - if( pTab->bHasDocsize ){ - nMatchinfo += 1 + 2*pTab->nColumn; - } - - sInfo.aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo); - if( !sInfo.aMatchinfo ){ - return SQLITE_NOMEM; - } - memset(sInfo.aMatchinfo, 0, sizeof(u32)*nMatchinfo); - - /* First element of match-info is the number of phrases in the query */ - sInfo.aMatchinfo[0] = nPhrase; - sInfo.aMatchinfo[1] = sInfo.nCol; - if( pTab->bHasDocsize ){ - int ofst = 2 + 3*sInfo.aMatchinfo[0]*sInfo.aMatchinfo[1]; - rc = sqlite3Fts3MatchinfoDocsizeGlobal(pCsr, &sInfo.aMatchinfo[ofst]); - } - (void)fts3ExprIterate(pCsr->pExpr, fts3ExprGlobalMatchinfoCb,(void*)&sInfo); - pCsr->aMatchinfo = sInfo.aMatchinfo; + int nMatchinfo = 0; /* Number of u32 elements in match-info */ + int nArg; /* Bytes in zArg */ + int i; /* Used to iterate through zArg */ + + /* Determine the number of phrases in the query */ + pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr); + sInfo.nPhrase = pCsr->nPhrase; + + /* Determine the number of integers in the buffer returned by this call. */ + for(i=0; zArg[i]; i++){ + nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]); + } + + /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */ + nArg = (int)strlen(zArg); + pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1); + if( !pCsr->aMatchinfo ) return SQLITE_NOMEM; + + pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo]; + pCsr->nMatchinfo = nMatchinfo; + memcpy(pCsr->zMatchinfo, zArg, nArg+1); + memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo); pCsr->isMatchinfoNeeded = 1; + bGlobal = 1; } sInfo.aMatchinfo = pCsr->aMatchinfo; - if( rc==SQLITE_OK && pCsr->isMatchinfoNeeded ){ - (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLocalMatchinfoCb, (void*)&sInfo); - if( pTab->bHasDocsize ){ - int ofst = 2 + 3*sInfo.aMatchinfo[0]*sInfo.aMatchinfo[1]; - rc = sqlite3Fts3MatchinfoDocsizeLocal(pCsr, &sInfo.aMatchinfo[ofst]); - } + sInfo.nPhrase = pCsr->nPhrase; + if( pCsr->isMatchinfoNeeded ){ + rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg); pCsr->isMatchinfoNeeded = 0; } - return SQLITE_OK; + return rc; } /* ** Implementation of snippet() function. */ @@ -982,11 +1367,11 @@ /* Loop through all columns of the table being considered for snippets. ** If the iCol argument to this function was negative, this means all ** columns of the FTS3 table. Otherwise, only column iCol is considered. */ for(iRead=0; iReadnColumn; iRead++){ - SnippetFragment sF; + SnippetFragment sF = {0, 0, 0, 0}; int iS; if( iCol>=0 && iRead!=iCol ) continue; /* Find the best snippet of nFToken tokens in column iRead. */ rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS); @@ -1209,26 +1594,47 @@ } /* ** Implementation of matchinfo() function. */ -void sqlite3Fts3Matchinfo(sqlite3_context *pContext, Fts3Cursor *pCsr){ +void sqlite3Fts3Matchinfo( + sqlite3_context *pContext, /* Function call context */ + Fts3Cursor *pCsr, /* FTS3 table cursor */ + const char *zArg /* Second arg to matchinfo() function */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; int rc; + int i; + const char *zFormat; + + if( zArg ){ + for(i=0; zArg[i]; i++){ + char *zErr = 0; + if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){ + sqlite3_result_error(pContext, zErr, -1); + sqlite3_free(zErr); + return; + } + } + zFormat = zArg; + }else{ + zFormat = FTS3_MATCHINFO_DEFAULT; + } + if( !pCsr->pExpr ){ sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC); return; } - rc = fts3GetMatchinfo(pCsr); - sqlite3Fts3SegmentsClose((Fts3Table *)pCsr->base.pVtab ); + + /* Retrieve matchinfo() data. */ + rc = fts3GetMatchinfo(pCsr, zFormat); + sqlite3Fts3SegmentsClose(pTab); + if( rc!=SQLITE_OK ){ sqlite3_result_error_code(pContext, rc); }else{ - Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab; - int n = sizeof(u32)*(2+pCsr->aMatchinfo[0]*pCsr->aMatchinfo[1]*3); - if( pTab->bHasDocsize ){ - n += sizeof(u32)*(1 + 2*pTab->nColumn); - } + int n = pCsr->nMatchinfo * sizeof(u32); sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT); } } #endif Index: ext/fts3/fts3_tokenizer.c ================================================================== --- ext/fts3/fts3_tokenizer.c +++ ext/fts3/fts3_tokenizer.c @@ -463,19 +463,27 @@ if( !zTest || !zTest2 ){ rc = SQLITE_NOMEM; } #endif - if( SQLITE_OK!=rc - || SQLITE_OK!=(rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0)) - || SQLITE_OK!=(rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0)) + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0); + } + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0); + } #ifdef SQLITE_TEST - || SQLITE_OK!=(rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0)) - || SQLITE_OK!=(rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0)) - || SQLITE_OK!=(rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0)) + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0); + } + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0); + } + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0); + } #endif - ); #ifdef SQLITE_TEST sqlite3_free(zTest); sqlite3_free(zTest2); #endif Index: ext/fts3/fts3_write.c ================================================================== --- ext/fts3/fts3_write.c +++ ext/fts3/fts3_write.c @@ -280,10 +280,55 @@ } } *pp = pStmt; return rc; } + +static int fts3SelectDocsize( + Fts3Table *pTab, /* FTS3 table handle */ + int eStmt, /* Either SQL_SELECT_DOCSIZE or DOCTOTAL */ + sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */ + sqlite3_stmt **ppStmt /* OUT: Statement handle */ +){ + sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */ + int rc; /* Return code */ + + assert( eStmt==SQL_SELECT_DOCSIZE || eStmt==SQL_SELECT_DOCTOTAL ); + + rc = fts3SqlStmt(pTab, eStmt, &pStmt, 0); + if( rc==SQLITE_OK ){ + if( eStmt==SQL_SELECT_DOCSIZE ){ + sqlite3_bind_int64(pStmt, 1, iDocid); + } + rc = sqlite3_step(pStmt); + if( rc!=SQLITE_ROW ){ + rc = sqlite3_reset(pStmt); + if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT; + pStmt = 0; + }else{ + rc = SQLITE_OK; + } + } + + *ppStmt = pStmt; + return rc; +} + +int sqlite3Fts3SelectDoctotal( + Fts3Table *pTab, /* Fts3 table handle */ + sqlite3_stmt **ppStmt /* OUT: Statement handle */ +){ + return fts3SelectDocsize(pTab, SQL_SELECT_DOCTOTAL, 0, ppStmt); +} + +int sqlite3Fts3SelectDocsize( + Fts3Table *pTab, /* Fts3 table handle */ + sqlite3_int64 iDocid, /* Docid to read size data for */ + sqlite3_stmt **ppStmt /* OUT: Statement handle */ +){ + return fts3SelectDocsize(pTab, SQL_SELECT_DOCSIZE, iDocid, ppStmt); +} /* ** Similar to fts3SqlStmt(). Except, after binding the parameters in ** array apVal[] to the SQL statement identified by eStmt, the statement ** is executed. @@ -1069,11 +1114,11 @@ while( anRowAvg = (((nByte / nDoc) + pgsz - 1) / pgsz); + pCsr->nRowAvg = (int)(((nByte / nDoc) + pgsz - 1) / pgsz); } rc = sqlite3_reset(pStmt); if( rc!=SQLITE_OK || pCsr->nRowAvg==0 ) return rc; } @@ -1097,11 +1142,11 @@ /* ** Free all allocations associated with the iterator passed as the ** second argument. */ -void sqlite3Fts3SegReaderFree(Fts3Table *p, Fts3SegReader *pReader){ +void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){ if( pReader && !fts3SegReaderIsPending(pReader) ){ sqlite3_free(pReader->zTerm); if( !fts3SegReaderIsRootOnly(pReader) ){ sqlite3_free(pReader->aNode); } @@ -1111,11 +1156,10 @@ /* ** Allocate a new SegReader object. */ int sqlite3Fts3SegReaderNew( - Fts3Table *p, /* Virtual table handle */ int iAge, /* Segment "age". */ sqlite3_int64 iStartLeaf, /* First leaf to traverse */ sqlite3_int64 iEndLeaf, /* Final leaf to traverse */ sqlite3_int64 iEndBlock, /* Final block of segment */ const char *zRoot, /* Buffer containing root node */ @@ -1152,11 +1196,11 @@ } if( rc==SQLITE_OK ){ *ppReader = pReader; }else{ - sqlite3Fts3SegReaderFree(p, pReader); + sqlite3Fts3SegReaderFree(pReader); } return rc; } /* @@ -1275,16 +1319,15 @@ ** If successful, the Fts3SegReader is left pointing to the first term ** in the segment and SQLITE_OK is returned. Otherwise, an SQLite error ** code is returned. */ static int fts3SegReaderNew( - Fts3Table *p, /* Virtual table handle */ sqlite3_stmt *pStmt, /* See above */ int iAge, /* Segment "age". */ Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */ ){ - return sqlite3Fts3SegReaderNew(p, iAge, + return sqlite3Fts3SegReaderNew(iAge, sqlite3_column_int64(pStmt, 1), sqlite3_column_int64(pStmt, 2), sqlite3_column_int64(pStmt, 3), sqlite3_column_blob(pStmt, 4), sqlite3_column_bytes(pStmt, 4), @@ -2311,11 +2354,11 @@ assert( SQL_SELECT_LEVEL+1==SQL_SELECT_ALL_LEVEL); rc = fts3SqlStmt(p, SQL_SELECT_LEVEL+(iLevel<0), &pStmt, 0); if( rc!=SQLITE_OK ) goto finished; sqlite3_bind_int(pStmt, 1, iLevel); for(i=0; SQLITE_ROW==(sqlite3_step(pStmt)); i++){ - rc = fts3SegReaderNew(p, pStmt, i, &apSegment[i]); + rc = fts3SegReaderNew(pStmt, i, &apSegment[i]); if( rc!=SQLITE_OK ){ goto finished; } } rc = sqlite3_reset(pStmt); @@ -2341,15 +2384,15 @@ finished: fts3SegWriterFree(pWriter); if( apSegment ){ for(i=0; ibase.pVtab; - rc = fts3SqlStmt(p, SQL_SELECT_DOCSIZE, &pStmt, 0); - if( rc ){ - return rc; - } - sqlite3_bind_int64(pStmt, 1, pCur->iPrevId); - if( sqlite3_step(pStmt)==SQLITE_ROW ){ - nBlob = sqlite3_column_bytes(pStmt, 0); - pBlob = (const char*)sqlite3_column_blob(pStmt, 0); - for(i=j=0; inColumn && jbase.pVtab; - rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0); - if( rc ){ - return rc; - } - if( sqlite3_step(pStmt)==SQLITE_ROW ){ - nBlob = sqlite3_column_bytes(pStmt, 0); - pBlob = (const char*)sqlite3_column_blob(pStmt, 0); - j = sqlite3Fts3GetVarint(pBlob, &x); - a[0] = nDoc = (u32)(x & 0xffffffff); - for(i=0; inColumn && jiPrevDocid. The sizes are encoded as ** a blob of varints. */ Index: ext/rtree/rtree.c ================================================================== --- ext/rtree/rtree.c +++ ext/rtree/rtree.c @@ -111,10 +111,16 @@ #include "sqlite3rtree.h" typedef sqlite3_int64 i64; typedef unsigned char u8; typedef unsigned int u32; #endif + +/* The following macro is used to suppress compiler warnings. +*/ +#ifndef UNUSED_PARAMETER +# define UNUSED_PARAMETER(x) (void)(x) +#endif typedef struct Rtree Rtree; typedef struct RtreeCursor RtreeCursor; typedef struct RtreeNode RtreeNode; typedef struct RtreeCell RtreeCell; @@ -188,11 +194,11 @@ */ #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) #define RTREE_REINSERT(p) RTREE_MINCELLS(p) #define RTREE_MAXCELLS 51 -/* +/* ** The smallest possible node-size is (512-64)==448 bytes. And the largest ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates). ** Therefore all non-root nodes must contain at least 3 entries. Since ** 2^40 is greater than 2^64, an r-tree structure always has a depth of ** 40 or less. @@ -850,11 +856,11 @@ static int rtreeEof(sqlite3_vtab_cursor *cur){ RtreeCursor *pCsr = (RtreeCursor *)cur; return (pCsr->pNode==0); } -/* +/* ** The r-tree constraint passed as the second argument to this function is ** guaranteed to be a MATCH constraint. */ static int testRtreeGeom( Rtree *pRtree, /* R-Tree object */ @@ -886,10 +892,11 @@ */ static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){ RtreeCell cell; int ii; int bRes = 0; + int rc = SQLITE_OK; nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); for(ii=0; bRes==0 && iinConstraint; ii++){ RtreeConstraint *p = &pCursor->aConstraint[ii]; double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]); @@ -906,29 +913,25 @@ case RTREE_GE: case RTREE_GT: bRes = p->rValue>cell_max; break; - case RTREE_EQ: + case RTREE_EQ: bRes = (p->rValue>cell_max || p->rValueop==RTREE_MATCH ); rc = testRtreeGeom(pRtree, p, &cell, &bRes); - if( rc!=SQLITE_OK ){ - return rc; - } bRes = !bRes; break; } } } *pbEof = bRes; - return SQLITE_OK; + return rc; } /* ** Test if the cell that cursor pCursor currently points to ** would be filtered (excluded) by the constraints in the @@ -1007,28 +1010,27 @@ rc = testRtreeEntry(pRtree, pCursor, &isEof); }else{ rc = testRtreeCell(pRtree, pCursor, &isEof); } if( rc!=SQLITE_OK || isEof || iHeight==0 ){ - *pEof = isEof; - return rc; + goto descend_to_cell_out; } iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell); rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild); if( rc!=SQLITE_OK ){ - return rc; + goto descend_to_cell_out; } nodeRelease(pRtree, pCursor->pNode); pCursor->pNode = pChild; isEof = 1; for(ii=0; isEof && iiiCell = ii; rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof); if( rc!=SQLITE_OK ){ - return rc; + goto descend_to_cell_out; } } if( isEof ){ assert( pCursor->pNode==pChild ); @@ -1036,12 +1038,13 @@ nodeRelease(pRtree, pChild); pCursor->pNode = pSavedNode; pCursor->iCell = iSavedCell; } +descend_to_cell_out: *pEof = isEof; - return SQLITE_OK; + return rc; } /* ** One of the cells in node pNode is guaranteed to have a 64-bit ** integer value equal to iRowid. Return the index of this cell. @@ -1193,11 +1196,11 @@ /* Check that value is actually a blob. */ if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR; /* Check that the blob is roughly the right size. */ nBlob = sqlite3_value_bytes(pValue); - if( nBlobmagic!=RTREE_GEOMETRY_MAGIC - || nBlob!=(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double)) + || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double)) ){ sqlite3_free(pGeom); return SQLITE_ERROR; } @@ -1354,10 +1357,11 @@ int ii, cCol; int iIdx = 0; char zIdxStr[RTREE_MAX_DIMENSIONS*8+1]; memset(zIdxStr, 0, sizeof(zIdxStr)); + UNUSED_PARAMETER(tab); assert( pIdxInfo->idxStr==0 ); for(ii=0; iinConstraint; ii++){ struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii]; @@ -1527,10 +1531,11 @@ for(ii=0; iinDim*2); jj+=2){ @@ -3113,10 +3118,11 @@ char *zText = 0; RtreeNode node; Rtree tree; int ii; + UNUSED_PARAMETER(nArg); memset(&node, 0, sizeof(RtreeNode)); memset(&tree, 0, sizeof(Rtree)); tree.nDim = sqlite3_value_int(apArg[0]); tree.nBytesPerCell = 8 + 8 * tree.nDim; node.zData = (u8 *)sqlite3_value_blob(apArg[1]); @@ -3146,10 +3152,11 @@ sqlite3_result_text(ctx, zText, -1, sqlite3_free); } static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){ + UNUSED_PARAMETER(nArg); if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB || sqlite3_value_bytes(apArg[0])<2 ){ sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); }else{ @@ -3167,11 +3174,10 @@ const int utf8 = SQLITE_UTF8; int rc; rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0); if( rc==SQLITE_OK ){ - int utf8 = SQLITE_UTF8; rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0); } if( rc==SQLITE_OK ){ void *c = (void *)RTREE_COORD_REAL32; rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0); Index: src/btree.c ================================================================== --- src/btree.c +++ src/btree.c @@ -1709,21 +1709,17 @@ /* True if opening an ephemeral, temporary database */ const int isTempDb = zFilename==0 || zFilename[0]==0; /* Set the variable isMemdb to true for an in-memory database, or - ** false for a file-based database. This symbol is only required if - ** either of the shared-data or autovacuum features are compiled - ** into the library. + ** false for a file-based database. */ -#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) - #ifdef SQLITE_OMIT_MEMORYDB - const int isMemdb = 0; - #else - const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) - || (isTempDb && sqlite3TempInMemory(db)); - #endif +#ifdef SQLITE_OMIT_MEMORYDB + const int isMemdb = 0; +#else + const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) + || (isTempDb && sqlite3TempInMemory(db)); #endif assert( db!=0 ); assert( sqlite3_mutex_held(db->mutex) ); assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ Index: src/expr.c ================================================================== --- src/expr.c +++ src/expr.c @@ -1653,10 +1653,11 @@ assert( !isRowid ); sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); dest.affinity = (u8)affinity; assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); + pExpr->x.pSelect->iLimit = 0; if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){ return 0; } pEList = pExpr->x.pSelect->pEList; if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ @@ -1753,10 +1754,11 @@ VdbeComment((v, "Init EXISTS result")); } sqlite3ExprDelete(pParse->db, pSel->pLimit); pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[1]); + pSel->iLimit = 0; if( sqlite3Select(pParse, pSel, &dest) ){ return 0; } rReg = dest.iParm; ExprSetIrreducible(pExpr); @@ -3036,13 +3038,26 @@ /* ** Preevaluate constant subexpressions within pExpr and store the ** results in registers. Modify pExpr so that the constant subexpresions ** are TK_REGISTER opcodes that refer to the precomputed values. +** +** This routine is a no-op if the jump to the cookie-check code has +** already occur. Since the cookie-check jump is generated prior to +** any other serious processing, this check ensures that there is no +** way to accidently bypass the constant initializations. +** +** This routine is also a no-op if the SQLITE_FactorOutConst optimization +** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS) +** interface. This allows test logic to verify that the same answer is +** obtained for queries regardless of whether or not constants are +** precomputed into registers or if they are inserted in-line. */ void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ Walker w; + if( pParse->cookieGoto ) return; + if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return; w.xExprCallback = evalConstExpr; w.xSelectCallback = 0; w.pParse = pParse; sqlite3WalkExpr(&w, pExpr); } Index: src/os_unix.c ================================================================== --- src/os_unix.c +++ src/os_unix.c @@ -5104,11 +5104,11 @@ ** message is available, it is written to zBufOut. If no error message ** is available, zBufOut is left unmodified and SQLite uses a default ** error message. */ static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ - char *zErr; + const char *zErr; UNUSED_PARAMETER(NotUsed); unixEnterMutex(); zErr = dlerror(); if( zErr ){ sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); @@ -5241,11 +5241,11 @@ static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; #if defined(NO_GETTOD) time_t t; time(&t); - *piNow = ((sqlite3_int64)i)*1000 + unixEpoch; + *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; #elif OS_VXWORKS struct timespec sNow; clock_gettime(CLOCK_REALTIME, &sNow); *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; #else @@ -5721,31 +5721,31 @@ /* create a new path by replace the trailing '-conch' with '-break' */ pathLen = strlcpy(tPath, cPath, MAXPATHLEN); if( pathLen>MAXPATHLEN || pathLen<6 || (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ - sprintf(errmsg, "path error (len %d)", (int)pathLen); + sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); goto end_breaklock; } /* read the conch content */ readLen = pread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); if( readLenh); Index: src/pager.c ================================================================== --- src/pager.c +++ src/pager.c @@ -3481,13 +3481,12 @@ */ int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ if( mxPage>0 ){ pPager->mxPgno = mxPage; } - if( pPager->eState!=PAGER_OPEN && pPager->mxPgnodbSize ){ - pPager->mxPgno = pPager->dbSize; - } + assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */ + assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */ return pPager->mxPgno; } /* ** The following set of routines are used to disable the simulated Index: src/pragma.c ================================================================== --- src/pragma.c +++ src/pragma.c @@ -33,11 +33,11 @@ static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16}; static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4}; static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2}; int i, n; if( sqlite3Isdigit(*z) ){ - return (u8)atoi(z); + return (u8)sqlite3Atoi(z); } n = sqlite3Strlen30(z); for(i=0; i=0&&i<=2)?i:0); } #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ #ifndef SQLITE_OMIT_PAGER_PRAGMAS @@ -383,11 +383,11 @@ addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); sqlite3VdbeChangeP1(v, addr, iDb); sqlite3VdbeChangeP1(v, addr+1, iDb); sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE); }else{ - int size = atoi(zRight); + int size = sqlite3Atoi(zRight); if( size<0 ) size = -size; sqlite3BeginWriteOperation(pParse, 0, iDb); sqlite3VdbeAddOp2(v, OP_Integer, size, 1); sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1); pDb->pSchema->cache_size = size; @@ -412,39 +412,17 @@ returnSingleInt(pParse, "page_size", &size); }else{ /* Malloc may fail when setting the page-size, as there is an internal ** buffer that the pager module resizes using sqlite3_realloc(). */ - db->nextPagesize = atoi(zRight); + db->nextPagesize = sqlite3Atoi(zRight); if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ db->mallocFailed = 1; } } }else - /* - ** PRAGMA [database.]max_page_count - ** PRAGMA [database.]max_page_count=N - ** - ** The first form reports the current setting for the - ** maximum number of pages in the database file. The - ** second form attempts to change this setting. Both - ** forms return the current setting. - */ - if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){ - Btree *pBt = pDb->pBt; - i64 newMax = 0; - assert( pBt!=0 ); - if( zRight ){ - newMax = atoi(zRight); - } - if( ALWAYS(pBt) ){ - newMax = sqlite3BtreeMaxPageCount(pBt, (int)newMax); - } - returnSingleInt(pParse, "max_page_count", &newMax); - }else - /* ** PRAGMA [database.]secure_delete ** PRAGMA [database.]secure_delete=ON/OFF ** ** The first form reports the current setting for the @@ -467,23 +445,37 @@ b = sqlite3BtreeSecureDelete(pBt, b); returnSingleInt(pParse, "secure_delete", &b); }else /* + ** PRAGMA [database.]max_page_count + ** PRAGMA [database.]max_page_count=N + ** + ** The first form reports the current setting for the + ** maximum number of pages in the database file. The + ** second form attempts to change this setting. Both + ** forms return the current setting. + ** ** PRAGMA [database.]page_count ** ** Return the number of pages in the specified database. */ - if( sqlite3StrICmp(zLeft,"page_count")==0 ){ + if( sqlite3StrICmp(zLeft,"page_count")==0 + || sqlite3StrICmp(zLeft,"max_page_count")==0 + ){ int iReg; if( sqlite3ReadSchema(pParse) ) goto pragma_out; sqlite3CodeVerifySchema(pParse, iDb); iReg = ++pParse->nMem; - sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); + if( zLeft[0]=='p' ){ + sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); + }else{ + sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight)); + } sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); }else /* ** PRAGMA [database.]locking_mode ** PRAGMA [database.]locking_mode = (normal|exclusive) @@ -702,11 +694,11 @@ if( sqlite3ReadSchema(pParse) ) goto pragma_out; if( !zRight ){ i64 cacheSize = pDb->pSchema->cache_size; returnSingleInt(pParse, "cache_size", &cacheSize); }else{ - int size = atoi(zRight); + int size = sqlite3Atoi(zRight); if( size<0 ) size = -size; pDb->pSchema->cache_size = size; sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); } }else @@ -1097,11 +1089,11 @@ sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC); /* Set the maximum error count */ mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; if( zRight ){ - mxErr = atoi(zRight); + sqlite3GetInt32(zRight, &mxErr); if( mxErr<=0 ){ mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; } } sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ @@ -1354,11 +1346,11 @@ { OP_Integer, 0, 1, 0}, /* 1 */ { OP_SetCookie, 0, 0, 1}, /* 2 */ }; int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie); sqlite3VdbeChangeP1(v, addr, iDb); - sqlite3VdbeChangeP1(v, addr+1, atoi(zRight)); + sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight)); sqlite3VdbeChangeP1(v, addr+2, iDb); sqlite3VdbeChangeP2(v, addr+2, iCookie); }else{ /* Read the specified cookie value */ static const VdbeOpList readCookie[] = { @@ -1416,12 +1408,11 @@ ** of N. */ if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){ i64 walArg = 0; if( zRight ){ - int nAuto = atoi(zRight); - sqlite3_wal_autocheckpoint(db, nAuto); + sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight)); } if( db->xWalCallback==sqlite3WalDefaultHook ){ walArg = SQLITE_PTR_TO_INT(db->pWalArg); } returnSingleInt(pParse, "wal_autocheckpoint", &walArg); Index: src/prepare.c ================================================================== --- src/prepare.c +++ src/prepare.c @@ -80,11 +80,11 @@ sqlite3_stmt *pStmt; TESTONLY(int rcp); /* Return code from sqlite3_prepare() */ assert( db->init.busy ); db->init.iDb = iDb; - db->init.newTnum = atoi(argv[1]); + db->init.newTnum = sqlite3Atoi(argv[1]); db->init.orphanTrigger = 0; TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0); rc = db->errCode; assert( (rc&0xFF)==(rcp&0xFF) ); db->init.iDb = 0; Index: src/select.c ================================================================== --- src/select.c +++ src/select.c @@ -3763,11 +3763,11 @@ } i = -1; }else{ sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); assert( pItem->isPopulated==0 ); - explainSetInteger(pItem->iSelectId, pParse->iNextSelectId); + explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); sqlite3Select(pParse, pSub, &dest); pItem->isPopulated = 1; pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow; } if( /*pParse->nErr ||*/ db->mallocFailed ){ Index: src/sqlite.h.in ================================================================== --- src/sqlite.h.in +++ src/sqlite.h.in @@ -4833,11 +4833,12 @@ ** the nominated column.)^ ^If the new row is not present in the table, or if ** it does not contain a blob or text value, or if another error occurs, an ** SQLite error code is returned and the blob handle is considered aborted. ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return -** SQLITE_ABORT. +** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle +** always returns zero. ** ** ^This function sets the database handle error code and message. */ SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -932,10 +932,11 @@ #define SQLITE_ColumnCache 0x02 /* Disable the column cache */ #define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */ #define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */ #define SQLITE_IndexCover 0x10 /* Disable index covering table */ #define SQLITE_GroupByOrder 0x20 /* Disable GROUPBY cover of ORDERBY */ +#define SQLITE_FactorOutConst 0x40 /* Disable factoring out constants */ #define SQLITE_OptMask 0xff /* Mask of all disablable opts */ /* ** Possible values for the sqlite.magic field. ** The numbers are obtained at random and have no special meaning, other @@ -1820,19 +1821,19 @@ Table *pTab; /* An SQL table corresponding to zName */ Select *pSelect; /* A SELECT statement used in place of a table name */ u8 isPopulated; /* Temporary table associated with SELECT is populated */ u8 jointype; /* Type of join between this able and the previous */ u8 notIndexed; /* True if there is a NOT INDEXED clause */ +#ifndef SQLITE_OMIT_EXPLAIN + u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */ +#endif int iCursor; /* The VDBE cursor number used to access this table */ Expr *pOn; /* The ON clause of a join */ IdList *pUsing; /* The USING clause of a join */ Bitmask colUsed; /* Bit N (1<" clause */ Index *pIndex; /* Index structure corresponding to zIndex, if any */ -#ifndef SQLITE_OMIT_EXPLAIN - int iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */ -#endif } a[1]; /* One entry for each identifier on the list */ }; /* ** Permitted values of the SrcList.a.jointype field @@ -2838,10 +2839,11 @@ int sqlite3FixExpr(DbFixer*, Expr*); int sqlite3FixExprList(DbFixer*, ExprList*); int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); int sqlite3AtoF(const char *z, double*, int, u8); int sqlite3GetInt32(const char *, int*); +int sqlite3Atoi(const char*); int sqlite3Utf16ByteLen(const void *pData, int nChar); int sqlite3Utf8CharLen(const char *pData, int nByte); int sqlite3Utf8Read(const u8*, const u8**); /* Index: src/test1.c ================================================================== --- src/test1.c +++ src/test1.c @@ -5423,10 +5423,68 @@ rc = printExplainQueryPlan(pStmt); Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); return TCL_OK; } #endif /* SQLITE_OMIT_EXPLAIN */ + +/* +** optimization_control DB OPT BOOLEAN +** +** Enable or disable query optimizations using the sqlite3_test_control() +** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. +** OPT is the name of the optimization to be disabled. +*/ +static int optimization_control( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int i; + sqlite3 *db; + const char *zOpt; + int onoff; + int mask; + static const struct { + const char *zOptName; + int mask; + } aOpt[] = { + { "all", SQLITE_OptMask }, + { "query-flattener", SQLITE_QueryFlattener }, + { "column-cache", SQLITE_ColumnCache }, + { "index-sort", SQLITE_IndexSort }, + { "index-search", SQLITE_IndexSearch }, + { "index-cover", SQLITE_IndexCover }, + { "groupby-order", SQLITE_GroupByOrder }, + { "factor-constants", SQLITE_FactorOutConst }, + }; + + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; + zOpt = Tcl_GetString(objv[2]); + for(i=0; i=sizeof(aOpt)/sizeof(aOpt[0]) ){ + Tcl_AppendResult(interp, "unknown optimization - should be one of:", + (char*)0); + for(i=0; ipMethods->xFileSize(pSubOpen, &sz); if( rc2!=SQLITE_OK ){ rc = rc2; }else{ + if( sz>gMultiplex.nChunkSize ){ + rc = SQLITE_IOERR_FSTAT; + } *pSize += sz; - assert(sz<=gMultiplex.nChunkSize); } }else{ break; } } Index: src/test_superlock.c ================================================================== --- src/test_superlock.c +++ src/test_superlock.c @@ -100,11 +100,10 @@ SuperlockBusy *pBusy /* Busy handler wrapper object */ ){ int rc; /* Return code */ sqlite3_file *fd = 0; /* Main database file handle */ void volatile *p = 0; /* Pointer to first page of shared memory */ - int nBusy = 0; /* Number of calls already made to xBusy */ /* Obtain a pointer to the sqlite3_file object open on the main db file. */ rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void *)&fd); if( rc!=SQLITE_OK ) return rc; @@ -309,10 +308,11 @@ rc = sqlite3demo_superlock(zPath, zVfs, xBusy, &busy, &pLock); assert( rc==SQLITE_OK || pLock==0 ); assert( rc!=SQLITE_OK || pLock!=0 ); if( rc!=SQLITE_OK ){ + extern const char *sqlite3ErrStr(int); Tcl_ResetResult(interp); Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); return TCL_ERROR; } Index: src/util.c ================================================================== --- src/util.c +++ src/util.c @@ -538,10 +538,20 @@ v = -v; } *pValue = (int)v; return 1; } + +/* +** Return a 32-bit integer value extracted from a string. If the +** string is not an integer, just return 0. +*/ +int sqlite3Atoi(const char *z){ + int x = 0; + if( z ) sqlite3GetInt32(z, &x); + return x; +} /* ** The variable-length integer encoding is as follows: ** ** KEY: Index: src/vdbe.c ================================================================== --- src/vdbe.c +++ src/vdbe.c @@ -1512,11 +1512,10 @@ ** without data loss, then jump immediately to P2, or if P2==0 ** raise an SQLITE_MISMATCH exception. */ case OP_MustBeInt: { /* jump, in1 */ pIn1 = &aMem[pOp->p1]; - memAboutToChange(p, pIn1); applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); if( (pIn1->flags & MEM_Int)==0 ){ if( pOp->p2==0 ){ rc = SQLITE_MISMATCH; goto abort_due_to_error; @@ -5785,10 +5784,36 @@ case OP_Pagecount: { /* out2-prerelease */ pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); break; } #endif + + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* Opcode: MaxPgcnt P1 P2 P3 * * +** +** Try to set the maximum page count for database P1 to the value in P3. +** Do not let the maximum page count fall below the current page count and +** do not change the maximum page count value if P3==0. +** +** Store the maximum page count after the change in register P2. +*/ +case OP_MaxPgcnt: { /* out2-prerelease */ + unsigned int newMax; + Btree *pBt; + + pBt = db->aDb[pOp->p1].pBt; + newMax = 0; + if( pOp->p3 ){ + newMax = sqlite3BtreeLastPage(pBt); + if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; + } + pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); + break; +} +#endif + #ifndef SQLITE_OMIT_TRACE /* Opcode: Trace * * * P4 * ** ** If tracing is enabled (by the sqlite3_trace()) interface, then Index: src/vdbeInt.h ================================================================== --- src/vdbeInt.h +++ src/vdbeInt.h @@ -95,30 +95,38 @@ ** values stored in the Vdbe struct. When the sub-program is finished, ** these values are copied back to the Vdbe from the VdbeFrame structure, ** restoring the state of the VM to as it was before the sub-program ** began executing. ** -** Frames are stored in a linked list headed at Vdbe.pParent. Vdbe.pParent -** is the parent of the current frame, or zero if the current frame -** is the main Vdbe program. +** The memory for a VdbeFrame object is allocated and managed by a memory +** cell in the parent (calling) frame. When the memory cell is deleted or +** overwritten, the VdbeFrame object is not freed immediately. Instead, it +** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame +** list is deleted when the VM is reset in VdbeHalt(). The reason for doing +** this instead of deleting the VdbeFrame immediately is to avoid recursive +** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the +** child frame are released. +** +** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is +** set to NULL if the currently executing frame is the main program. */ typedef struct VdbeFrame VdbeFrame; struct VdbeFrame { Vdbe *v; /* VM this frame belongs to */ - int pc; /* Program Counter */ - Op *aOp; /* Program instructions */ + int pc; /* Program Counter in parent (calling) frame */ + Op *aOp; /* Program instructions for parent frame */ int nOp; /* Size of aOp array */ - Mem *aMem; /* Array of memory cells */ + Mem *aMem; /* Array of memory cells for parent frame */ int nMem; /* Number of entries in aMem */ - VdbeCursor **apCsr; /* Element of Vdbe cursors */ + VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ u16 nCursor; /* Number of entries in apCsr */ void *token; /* Copy of SubProgram.token */ int nChildMem; /* Number of memory cells for child frame */ int nChildCsr; /* Number of cursors for child frame */ i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ int nChange; /* Statement changes (Vdbe.nChanges) */ - VdbeFrame *pParent; /* Parent of this frame */ + VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ }; #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) /* @@ -331,10 +339,11 @@ int iStatement; /* Statement number (or 0 if has not opened stmt) */ #ifdef SQLITE_DEBUG FILE *trace; /* Write an execution trace here, if not NULL */ #endif VdbeFrame *pFrame; /* Parent frame */ + VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */ int nFrame; /* Number of frames in pFrame list */ u32 expmask; /* Binding to these vars invalidates VM */ SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ }; Index: src/vdbeaux.c ================================================================== --- src/vdbeaux.c +++ src/vdbeaux.c @@ -1535,10 +1535,15 @@ } } if( p->aMem ){ releaseMemArray(&p->aMem[1], p->nMem); } + while( p->pDelFrame ){ + VdbeFrame *pDel = p->pDelFrame; + p->pDelFrame = pDel->pParent; + sqlite3VdbeFrameDelete(pDel); + } } /* ** Clean up the VM after execution. ** Index: src/vdbeblob.c ================================================================== --- src/vdbeblob.c +++ src/vdbeblob.c @@ -417,11 +417,11 @@ ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob ** so no mutex is required for access. */ int sqlite3_blob_bytes(sqlite3_blob *pBlob){ Incrblob *p = (Incrblob *)pBlob; - return p ? p->nByte : 0; + return (p && p->pStmt) ? p->nByte : 0; } /* ** Move an existing blob handle to point to a different row of the same ** database table. @@ -455,10 +455,11 @@ } assert( rc!=SQLITE_SCHEMA ); } rc = sqlite3ApiExit(db, rc); + assert( rc==SQLITE_OK || p->pStmt==0 ); sqlite3_mutex_leave(db->mutex); return rc; } #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ Index: src/vdbemem.c ================================================================== --- src/vdbemem.c +++ src/vdbemem.c @@ -485,11 +485,13 @@ /* ** Delete any previous value and set the value stored in *pMem to NULL. */ void sqlite3VdbeMemSetNull(Mem *pMem){ if( pMem->flags & MEM_Frame ){ - sqlite3VdbeFrameDelete(pMem->u.pFrame); + VdbeFrame *pFrame = pMem->u.pFrame; + pFrame->pParent = pFrame->v->pDelFrame; + pFrame->v->pDelFrame = pFrame; } if( pMem->flags & MEM_RowSet ){ sqlite3RowSetClear(pMem->u.pRowSet); } MemSetTypeFlag(pMem, MEM_Null); Index: test/all.test ================================================================== --- test/all.test +++ test/all.test @@ -14,10 +14,11 @@ set testdir [file dirname $argv0] source $testdir/permutations.test run_test_suite full +run_test_suite no_optimization run_test_suite memsubsys1 run_test_suite memsubsys2 run_test_suite singlethread run_test_suite multithread run_test_suite onefile Index: test/backcompat.test ================================================================== --- test/backcompat.test +++ test/backcompat.test @@ -268,7 +268,105 @@ do_test backcompat-2.1.4 { sql1 { SELECT * FROM t1; } } {I 1 II 2 III 3} } } + +#------------------------------------------------------------------------- +# Test that FTS3 tables may be read/written by different versions of +# SQLite. +# +set contents { + CREATE VIRTUAL TABLE t1 USING fts3(a, b); +} +foreach {num doc} { + one "jk zm jk eczkjblu urvysbnykk sk gnl jk ttvgf hmjf" + two "jk bnhc jjrxpjkb mjpavjuhw fibokdry igju jk zm zm xh" + three "wxe ogttbykvt uhzq xr iaf zf urvysbnykk aayxpmve oacaxgjoo mjpavjuhw" + four "gazrt jk ephknonq myjp uenvbm wuvajhwqz jk zm xnxhf nvfasfh" + five "zm aayxpmve csjqxhgj xnxhf xr jk aayxpmve xnxhf zm zm" + six "sokcyf zm ogyavjvv jk zm fibokdry zm jk igju igju" + seven "vgsld bvgimjik xuprtlyle jk akmikrqyt jk aayxpmve hkfoudzftq ddjj" + eight "zm uhzq ovkyevlgv zk uenvbm csjqxhgj jk vgsld pgybs jk" + nine "zm agmckuiu zexh fibokdry jk uhzq bu tugflixoex xnxhf sk" +} { + append contents "INSERT INTO t1 VALUES('$num', '$doc');" +} +do_allbackcompat_test { + if {[code1 {set ::sqlite_options(fts3)}] + && [code2 {set ::sqlite_options(fts3)}] + } { + + do_test backcompat-3.1 { sql1 $contents } {} + + foreach {n q} { + 1 "SELECT * FROM t1 ORDER BY a, b" + 2 "SELECT rowid FROM t1 WHERE a MATCH 'five'" + 3 "SELECT * FROM t1 WHERE a MATCH 'five'" + 4 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'jk'" + 5 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'tug* OR eight'" + } { + do_test backcompat-3.2 [list sql1 $q] [sql2 $q] + } + + do_test backcompat-3.3 { sql1 { + INSERT INTO t1 SELECT * FROM t1; + INSERT INTO t1 SELECT * FROM t1; + INSERT INTO t1 SELECT * FROM t1; + INSERT INTO t1 SELECT * FROM t1; + INSERT INTO t1 SELECT * FROM t1; + INSERT INTO t1 SELECT * FROM t1; + INSERT INTO t1 SELECT * FROM t1; + INSERT INTO t1 SELECT * FROM t1; + } } {} + + foreach {n q} { + 1 "SELECT * FROM t1 ORDER BY a, b" + 2 "SELECT rowid FROM t1 WHERE a MATCH 'five'" + 3 "SELECT * FROM t1 WHERE a MATCH 'five'" + 4 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'jk'" + 5 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'tug* OR eight'" + } { + do_test backcompat-3.4 [list sql1 $q] [sql2 $q] + } + + set alphabet "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4" + for {set i 0} {$i < 900} {incr i} { + set term "[lindex $alphabet [expr $i/30]][lindex $alphabet [expr $i%30]] " + sql1 "INSERT INTO t1 VALUES($i, '[string repeat $term 14]')" + } + + foreach {n q} { + 1 "SELECT * FROM t1 ORDER BY a, b" + 2 "SELECT rowid FROM t1 WHERE a MATCH 'five'" + 3 "SELECT * FROM t1 WHERE a MATCH 'five'" + 4 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'jk'" + 5 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'tug* OR eight'" + + 6 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'aa'" + 7 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH '44'" + 8 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'a*'" + } { + do_test backcompat-3.5 [list sql1 $q] [sql2 $q] + } + + do_test backcompat-3.6 { + sql1 "SELECT optimize(t1) FROM t1 LIMIT 1" + } {{Index optimized}} + + foreach {n q} { + 1 "SELECT * FROM t1 ORDER BY a, b" + 2 "SELECT rowid FROM t1 WHERE a MATCH 'five'" + 3 "SELECT * FROM t1 WHERE a MATCH 'five'" + 4 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'jk'" + 5 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'tug* OR eight'" + + 6 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'aa'" + 7 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH '44'" + 8 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'a*'" + } { + do_test backcompat-3.7 [list sql1 $q] [sql2 $q] + } + } +} finish_test Index: test/cache.test ================================================================== --- test/cache.test +++ test/cache.test @@ -49,15 +49,92 @@ # This tests that once the pager-cache is initialised, it can be locked # and unlocked repeatedly without internally allocating any new pages. # set cache_size [pager_cache_size db] for {set ii 0} {$ii < 10} {incr ii} { - do_test cache-1.3.$ii { execsql {SELECT * FROM abc} pager_cache_size db } $::cache_size +} + +#------------------------------------------------------------------------- +# This block of tests checks that it is possible to set the cache_size of a +# database to a small (< 10) value. More specifically: +# +# cache-2.1.*: Test that "PRAGMA cache_size" appears to work with small +# values. +# cache-2.2.*: Test that "PRAGMA main.cache_size" appears to work with +# small values. +# cache-2.3.*: Test cache_size=1 correctly spills/flushes the cache. +# cache-2.4.*: Test cache_size=0 correctly spills/flushes the cache. +# +# +db_delete_and_reopen +do_execsql_test cache-2.0 { + PRAGMA auto_vacuum=OFF; + PRAGMA journal_mode=DELETE; + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + INSERT INTO t1 VALUES('x', 'y'); + INSERT INTO t2 VALUES('i', 'j'); +} {delete} +for {set i 0} {$i < 20} {incr i} { + do_execsql_test cache-2.1.$i.1 "PRAGMA cache_size = $i" + do_execsql_test cache-2.1.$i.2 "PRAGMA cache_size" $i + do_execsql_test cache-2.1.$i.3 "SELECT * FROM t1" {x y} + do_execsql_test cache-2.1.$i.4 "PRAGMA cache_size" $i +} +for {set i 0} {$i < 20} {incr i} { + do_execsql_test cache-2.2.$i.1 "PRAGMA main.cache_size = $i" + do_execsql_test cache-2.2.$i.2 "PRAGMA main.cache_size" $i + do_execsql_test cache-2.2.$i.3 "SELECT * FROM t1" {x y} + do_execsql_test cache-2.2.$i.4 "PRAGMA main.cache_size" $i } -sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) + +# Tests for cache_size = 1. +# +do_execsql_test cache-2.3.1 { + PRAGMA cache_size = 1; + BEGIN; + INSERT INTO t1 VALUES(1, 2); + PRAGMA lock_status; +} {main reserved temp closed} +do_test cache-2.3.2 { pager_cache_size db } 2 +do_execsql_test cache-2.3.3 { + INSERT INTO t2 VALUES(1, 2); + PRAGMA lock_status; +} {main exclusive temp closed} +do_test cache-2.3.4 { pager_cache_size db } 2 +do_execsql_test cache-2.3.5 COMMIT +do_test cache-2.3.6 { pager_cache_size db } 1 + +do_execsql_test cache-2.3.7 { + SELECT * FROM t1 UNION SELECT * FROM t2; +} {1 2 i j x y} +do_test cache-2.3.8 { pager_cache_size db } 1 + +# Tests for cache_size = 0. +# +do_execsql_test cache-2.4.1 { + PRAGMA cache_size = 0; + BEGIN; + INSERT INTO t1 VALUES(1, 2); + PRAGMA lock_status; +} {main reserved temp closed} +do_test cache-2.4.2 { pager_cache_size db } 2 +do_execsql_test cache-2.4.3 { + INSERT INTO t2 VALUES(1, 2); + PRAGMA lock_status; +} {main exclusive temp closed} +do_test cache-2.4.4 { pager_cache_size db } 2 +do_execsql_test cache-2.4.5 COMMIT + +do_test cache-2.4.6 { pager_cache_size db } 0 +do_execsql_test cache-2.4.7 { + SELECT * FROM t1 UNION SELECT * FROM t2; +} {1 2 i j x y} +do_test cache-2.4.8 { pager_cache_size db } 0 +sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) finish_test ADDED test/e_droptrigger.test Index: test/e_droptrigger.test ================================================================== --- /dev/null +++ test/e_droptrigger.test @@ -0,0 +1,218 @@ +# 2010 November 29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests to verify that the "testable statements" in +# the lang_droptrigger.html document are correct. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix e_droptrigger + +ifcapable !trigger { finish_test ; return } + +proc do_droptrigger_tests {nm args} { + uplevel do_select_tests [list e_createtable-$nm] $args +} + +proc list_all_triggers {{db db}} { + set res [list] + $db eval { PRAGMA database_list } { + if {$name == "temp"} { + set tbl sqlite_temp_master + } else { + set tbl "$name.sqlite_master" + } + lappend res {*}[ + db eval "SELECT '$name.' || name FROM $tbl WHERE type = 'trigger'" + ] + } + set res +} + + +proc droptrigger_reopen_db {{event INSERT}} { + db close + forcedelete test.db test.db2 + sqlite3 db test.db + + set ::triggers_fired [list] + proc r {x} { lappend ::triggers_fired $x } + db func r r + + db eval " + ATTACH 'test.db2' AS aux; + + CREATE TEMP TABLE t1(a, b); + INSERT INTO t1 VALUES('a', 'b'); + CREATE TRIGGER tr1 AFTER $event ON t1 BEGIN SELECT r('temp.tr1') ; END; + + CREATE TABLE t2(a, b); + INSERT INTO t2 VALUES('a', 'b'); + CREATE TRIGGER tr1 BEFORE $event ON t2 BEGIN SELECT r('main.tr1') ; END; + CREATE TRIGGER tr2 AFTER $event ON t2 BEGIN SELECT r('main.tr2') ; END; + + CREATE TABLE aux.t3(a, b); + INSERT INTO t3 VALUES('a', 'b'); + CREATE TRIGGER aux.tr1 BEFORE $event ON t3 BEGIN SELECT r('aux.tr1') ; END; + CREATE TRIGGER aux.tr2 AFTER $event ON t3 BEGIN SELECT r('aux.tr2') ; END; + CREATE TRIGGER aux.tr3 AFTER $event ON t3 BEGIN SELECT r('aux.tr3') ; END; + " +} + + +# EVIDENCE-OF: R-52650-16855 -- syntax diagram drop-trigger-stmt +# +do_droptrigger_tests 1.1 -repair { + droptrigger_reopen_db +} -tclquery { + list_all_triggers +} { + 1 "DROP TRIGGER main.tr1" + {main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3} + 2 "DROP TRIGGER IF EXISTS main.tr1" + {main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3} + 3 "DROP TRIGGER tr1" + {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3} + 4 "DROP TRIGGER IF EXISTS tr1" + {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3} + + 5 "DROP TRIGGER aux.tr1" + {main.tr1 main.tr2 temp.tr1 aux.tr2 aux.tr3} + 6 "DROP TRIGGER IF EXISTS aux.tr1" + {main.tr1 main.tr2 temp.tr1 aux.tr2 aux.tr3} + + 7 "DROP TRIGGER IF EXISTS aux.xxx" + {main.tr1 main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3} + 8 "DROP TRIGGER IF EXISTS aux.xxx" + {main.tr1 main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3} +} + +# EVIDENCE-OF: R-61172-15671 The DROP TRIGGER statement removes a +# trigger created by the CREATE TRIGGER statement. +# +foreach {tn tbl droptrigger before after} { + 1 t1 "DROP TRIGGER tr1" {temp.tr1} {} + 2 t2 "DROP TRIGGER tr1" {main.tr1 main.tr2} {main.tr1 main.tr2} + 3 t3 "DROP TRIGGER tr1" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2} + + 4 t1 "DROP TRIGGER tr2" {temp.tr1} {temp.tr1} + 5 t2 "DROP TRIGGER tr2" {main.tr1 main.tr2} {main.tr1} + 6 t3 "DROP TRIGGER tr2" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2} + + 7 t1 "DROP TRIGGER tr3" {temp.tr1} {temp.tr1} + 8 t2 "DROP TRIGGER tr3" {main.tr1 main.tr2} {main.tr1 main.tr2} + 9 t3 "DROP TRIGGER tr3" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr2} +} { + + do_test 2.$tn.1 { + droptrigger_reopen_db + execsql " INSERT INTO $tbl VALUES('1', '2') " + set ::triggers_fired + } $before + + do_test 2.$tn.2 { + droptrigger_reopen_db + execsql $droptrigger + execsql " INSERT INTO $tbl VALUES('1', '2') " + set ::triggers_fired + } $after +} + +# EVIDENCE-OF: R-50239-29811 Once removed, the trigger definition is no +# longer present in the sqlite_master (or sqlite_temp_master) table and +# is not fired by any subsequent INSERT, UPDATE or DELETE statements. +# +# Test cases e_droptrigger-1.* test the first part of this statement +# (that dropped triggers do not appear in the schema table), and tests +# droptrigger-2.* test that dropped triggers are not fired by INSERT +# statements. The following tests verify that they are not fired by +# UPDATE or DELETE statements. +# +foreach {tn tbl droptrigger before after} { + 1 t1 "DROP TRIGGER tr1" {temp.tr1} {} + 2 t2 "DROP TRIGGER tr1" {main.tr1 main.tr2} {main.tr1 main.tr2} + 3 t3 "DROP TRIGGER tr1" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2} + + 4 t1 "DROP TRIGGER tr2" {temp.tr1} {temp.tr1} + 5 t2 "DROP TRIGGER tr2" {main.tr1 main.tr2} {main.tr1} + 6 t3 "DROP TRIGGER tr2" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2} + + 7 t1 "DROP TRIGGER tr3" {temp.tr1} {temp.tr1} + 8 t2 "DROP TRIGGER tr3" {main.tr1 main.tr2} {main.tr1 main.tr2} + 9 t3 "DROP TRIGGER tr3" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr2} +} { + + do_test 3.1.$tn.1 { + droptrigger_reopen_db UPDATE + execsql "UPDATE $tbl SET a = 'abc'" + set ::triggers_fired + } $before + + do_test 3.1.$tn.2 { + droptrigger_reopen_db UPDATE + execsql $droptrigger + execsql "UPDATE $tbl SET a = 'abc'" + set ::triggers_fired + } $after +} +foreach {tn tbl droptrigger before after} { + 1 t1 "DROP TRIGGER tr1" {temp.tr1} {} + 2 t2 "DROP TRIGGER tr1" {main.tr1 main.tr2} {main.tr1 main.tr2} + 3 t3 "DROP TRIGGER tr1" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2} + + 4 t1 "DROP TRIGGER tr2" {temp.tr1} {temp.tr1} + 5 t2 "DROP TRIGGER tr2" {main.tr1 main.tr2} {main.tr1} + 6 t3 "DROP TRIGGER tr2" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2} + + 7 t1 "DROP TRIGGER tr3" {temp.tr1} {temp.tr1} + 8 t2 "DROP TRIGGER tr3" {main.tr1 main.tr2} {main.tr1 main.tr2} + 9 t3 "DROP TRIGGER tr3" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr2} +} { + + do_test 3.2.$tn.1 { + droptrigger_reopen_db DELETE + execsql "DELETE FROM $tbl" + set ::triggers_fired + } $before + + do_test 3.2.$tn.2 { + droptrigger_reopen_db DELETE + execsql $droptrigger + execsql "DELETE FROM $tbl" + set ::triggers_fired + } $after +} + +# EVIDENCE-OF: R-37808-62273 Note that triggers are automatically +# dropped when the associated table is dropped. +# +do_test 4.1 { + droptrigger_reopen_db + list_all_triggers +} {main.tr1 main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3} +do_test 4.2 { + droptrigger_reopen_db + execsql "DROP TABLE t1" + list_all_triggers +} {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3} +do_test 4.3 { + droptrigger_reopen_db + execsql "DROP TABLE t1" + list_all_triggers +} {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3} +do_test 4.4 { + droptrigger_reopen_db + execsql "DROP TABLE t1" + list_all_triggers +} {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3} + +finish_test ADDED test/e_dropview.test Index: test/e_dropview.test ================================================================== --- /dev/null +++ test/e_dropview.test @@ -0,0 +1,192 @@ +# 2010 November 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests to verify that the "testable statements" in +# the lang_dropview.html document are correct. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix e_dropview + +proc dropview_reopen_db {} { + db close + forcedelete test.db test.db2 + sqlite3 db test.db + + db eval { + ATTACH 'test.db2' AS aux; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES('a main', 'b main'); + CREATE VIEW v1 AS SELECT * FROM t1; + CREATE VIEW v2 AS SELECT * FROM t1; + + CREATE TEMP TABLE t1(a, b); + INSERT INTO temp.t1 VALUES('a temp', 'b temp'); + CREATE VIEW temp.v1 AS SELECT * FROM t1; + + CREATE TABLE aux.t1(a, b); + INSERT INTO aux.t1 VALUES('a aux', 'b aux'); + CREATE VIEW aux.v1 AS SELECT * FROM t1; + CREATE VIEW aux.v2 AS SELECT * FROM t1; + CREATE VIEW aux.v3 AS SELECT * FROM t1; + } +} + +proc list_all_views {{db db}} { + set res [list] + $db eval { PRAGMA database_list } { + set tbl "$name.sqlite_master" + if {$name == "temp"} { set tbl sqlite_temp_master } + + set sql "SELECT '$name.' || name FROM $tbl WHERE type = 'view'" + lappend res {*}[$db eval $sql] + } + set res +} + +proc list_all_data {{db db}} { + set res [list] + $db eval { PRAGMA database_list } { + set tbl "$name.sqlite_master" + if {$name == "temp"} { set tbl sqlite_temp_master } + + db eval "SELECT '$name.' || name AS x FROM $tbl WHERE type = 'table'" { + lappend res [list $x [db eval "SELECT * FROM $x"]] + } + } + set res +} + +proc do_dropview_tests {nm args} { + uplevel do_select_tests $nm $args +} + +# EVIDENCE-OF: R-21739-51207 -- syntax diagram drop-view-stmt +# +# All paths in the syntax diagram for DROP VIEW are tested by tests 1.*. +# +do_dropview_tests 1 -repair { + dropview_reopen_db +} -tclquery { + list_all_views +} { + 1 "DROP VIEW v1" {main.v1 main.v2 aux.v1 aux.v2 aux.v3} + 2 "DROP VIEW v2" {main.v1 temp.v1 aux.v1 aux.v2 aux.v3} + 3 "DROP VIEW main.v1" {main.v2 temp.v1 aux.v1 aux.v2 aux.v3} + 4 "DROP VIEW main.v2" {main.v1 temp.v1 aux.v1 aux.v2 aux.v3} + 5 "DROP VIEW IF EXISTS v1" {main.v1 main.v2 aux.v1 aux.v2 aux.v3} + 6 "DROP VIEW IF EXISTS v2" {main.v1 temp.v1 aux.v1 aux.v2 aux.v3} + 7 "DROP VIEW IF EXISTS main.v1" {main.v2 temp.v1 aux.v1 aux.v2 aux.v3} + 8 "DROP VIEW IF EXISTS main.v2" {main.v1 temp.v1 aux.v1 aux.v2 aux.v3} +} + +# EVIDENCE-OF: R-27002-52307 The DROP VIEW statement removes a view +# created by the CREATE VIEW statement. +# +dropview_reopen_db +do_execsql_test 2.1 { + CREATE VIEW "new view" AS SELECT * FROM t1 AS x, t1 AS y; + SELECT * FROM "new view"; +} {{a main} {b main} {a main} {b main}} +do_execsql_test 2.2 {; + SELECT * FROM sqlite_master WHERE name = 'new view'; +} { + view {new view} {new view} 0 + {CREATE VIEW "new view" AS SELECT * FROM t1 AS x, t1 AS y} +} +do_execsql_test 2.3 { + DROP VIEW "new view"; + SELECT * FROM sqlite_master WHERE name = 'new view'; +} {} +do_catchsql_test 2.4 { + SELECT * FROM "new view" +} {1 {no such table: new view}} + +# EVIDENCE-OF: R-00359-41639 The view definition is removed from the +# database schema, but no actual data in the underlying base tables is +# modified. +# +# For each view in the database, check that it can be queried. Then drop +# it. Check that it can no longer be queried and is no longer listed +# in any schema table. Then check that the contents of the db tables have +# not changed +# +set databasedata [list_all_data] + +do_execsql_test 3.1.0 { SELECT * FROM temp.v1 } {{a temp} {b temp}} +do_execsql_test 3.1.1 { DROP VIEW temp.v1 } {} +do_catchsql_test 3.1.2 { SELECT * FROM temp.v1 } {1 {no such table: temp.v1}} +do_test 3.1.3 { list_all_views } {main.v1 main.v2 aux.v1 aux.v2 aux.v3} +do_test 3.1.4 { list_all_data } $databasedata + +do_execsql_test 3.2.0 { SELECT * FROM v1 } {{a main} {b main}} +do_execsql_test 3.2.1 { DROP VIEW v1 } {} +do_catchsql_test 3.2.2 { SELECT * FROM main.v1 } {1 {no such table: main.v1}} +do_test 3.2.3 { list_all_views } {main.v2 aux.v1 aux.v2 aux.v3} +do_test 3.2.4 { list_all_data } $databasedata + +do_execsql_test 3.3.0 { SELECT * FROM v2 } {{a main} {b main}} +do_execsql_test 3.3.1 { DROP VIEW v2 } {} +do_catchsql_test 3.3.2 { SELECT * FROM main.v2 } {1 {no such table: main.v2}} +do_test 3.3.3 { list_all_views } {aux.v1 aux.v2 aux.v3} +do_test 3.3.4 { list_all_data } $databasedata + +do_execsql_test 3.4.0 { SELECT * FROM v1 } {{a aux} {b aux}} +do_execsql_test 3.4.1 { DROP VIEW v1 } {} +do_catchsql_test 3.4.2 { SELECT * FROM v1 } {1 {no such table: v1}} +do_test 3.4.3 { list_all_views } {aux.v2 aux.v3} +do_test 3.4.4 { list_all_data } $databasedata + +do_execsql_test 3.4.0 { SELECT * FROM aux.v2 } {{a aux} {b aux}} +do_execsql_test 3.4.1 { DROP VIEW aux.v2 } {} +do_catchsql_test 3.4.2 { SELECT * FROM aux.v2 } {1 {no such table: aux.v2}} +do_test 3.4.3 { list_all_views } {aux.v3} +do_test 3.4.4 { list_all_data } $databasedata + +do_execsql_test 3.5.0 { SELECT * FROM v3 } {{a aux} {b aux}} +do_execsql_test 3.5.1 { DROP VIEW v3 } {} +do_catchsql_test 3.5.2 { SELECT * FROM v3 } {1 {no such table: v3}} +do_test 3.5.3 { list_all_views } {} +do_test 3.5.4 { list_all_data } $databasedata + +# EVIDENCE-OF: R-25558-37487 If the specified view cannot be found and +# the IF EXISTS clause is not present, it is an error. +# +do_dropview_tests 4 -repair { + dropview_reopen_db +} -errorformat { + no such view: %s +} { + 1 "DROP VIEW xx" xx + 2 "DROP VIEW main.xx" main.xx + 3 "DROP VIEW temp.v2" temp.v2 +} + +# EVIDENCE-OF: R-07490-32536 If the specified view cannot be found and +# an IF EXISTS clause is present in the DROP VIEW statement, then the +# statement is a no-op. +# +do_dropview_tests 5 -repair { + dropview_reopen_db +} -tclquery { + list_all_views + expr {[list_all_views] == "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3"} +} { + 1 "DROP VIEW IF EXISTS xx" 1 + 2 "DROP VIEW IF EXISTS main.xx" 1 + 3 "DROP VIEW IF EXISTS temp.v2" 1 +} + + + + +finish_test ADDED test/e_resolve.test Index: test/e_resolve.test ================================================================== --- /dev/null +++ test/e_resolve.test @@ -0,0 +1,135 @@ +# 2010 November 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests to verify that the "testable statements" in +# the lang_naming.html document are correct. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix e_resolve + +# An example database schema for testing name resolution: +# +set schema { + ATTACH 'test.db2' AS at1; + ATTACH 'test.db3' AS at2; + + CREATE TABLE temp.n1(x, y); INSERT INTO temp.n1 VALUES('temp', 'n1'); + CREATE TRIGGER temp.n3 AFTER INSERT ON n1 BEGIN SELECT 1; END; + CREATE INDEX temp.n4 ON n1(x, y); + + CREATE TABLE main.n1(x, y); INSERT INTO main.n1 VALUES('main', 'n1'); + CREATE TABLE main.n2(x, y); INSERT INTO main.n2 VALUES('main', 'n2'); + CREATE INDEX main.n3 ON n2(y, x); + CREATE TRIGGER main.n4 BEFORE INSERT ON n2 BEGIN SELECT 1; END; + + CREATE TABLE at1.n1(x, y); INSERT INTO at1.n1 VALUES('at1', 'n1'); + CREATE TABLE at1.n2(x, y); INSERT INTO at1.n2 VALUES('at1', 'n2'); + CREATE TABLE at1.n3(x, y); INSERT INTO at1.n3 VALUES('at1', 'n3'); + + CREATE TABLE at2.n1(x, y); INSERT INTO at2.n1 VALUES('at2', 'n1'); + CREATE TABLE at2.n2(x, y); INSERT INTO at2.n2 VALUES('at2', 'n2'); + CREATE TABLE at2.n3(x, y); INSERT INTO at2.n3 VALUES('at2', 'n3'); + CREATE TABLE at2.n4(x, y); INSERT INTO at2.n4 VALUES('at2', 'n4'); + CREATE TRIGGER at2.n4 BEFORE INSERT ON n4 BEGIN SELECT 1; END; +} + +proc resolve_reopen_db {} { + db close + forcedelete test.db test.db2 test.db3 + sqlite3 db test.db + db eval $::schema +} + + + +# EVIDENCE-OF: R-33528-20612 If no database is specified as part of the +# object reference, then SQLite searches the main, temp and all attached +# databases for an object with a matching name. The temp database is +# searched first, followed by the main database, followed all attached +# databases in the order that they were attached. The reference resolves +# to the first match found. +# +resolve_reopen_db +do_execsql_test 1.1 { SELECT * FROM n1 } {temp n1} +do_execsql_test 1.2 { SELECT * FROM n2 } {main n2} +do_execsql_test 1.3 { SELECT * FROM n3 } {at1 n3} +do_execsql_test 1.4 { SELECT * FROM n4 } {at2 n4} + +# EVIDENCE-OF: R-54577-28142 If a database name is specified as part of +# an object reference, it must be either "main", or "temp" or the name +# of an attached database. +# +# Or else it is a "no such table: xxx" error. +# +resolve_reopen_db +do_execsql_test 2.1.1 { SELECT * FROM main.n1 } {main n1} +do_execsql_test 2.1.2 { SELECT * FROM temp.n1 } {temp n1} +do_execsql_test 2.1.3 { SELECT * FROM at1.n1 } {at1 n1} +do_execsql_test 2.1.4 { SELECT * FROM at2.n1 } {at2 n1} + +do_catchsql_test 2.2 { SELECT * FROM xxx.n1 } {1 {no such table: xxx.n1}} + +# EVIDENCE-OF: R-26223-47623 Like other SQL identifiers, database names +# are case-insensitive. +# +resolve_reopen_db +do_execsql_test 3.1 { SELECT * FROM MAIN.n1 } {main n1} +do_execsql_test 3.2 { SELECT * FROM tEmP.n1 } {temp n1} +do_execsql_test 3.3 { SELECT * FROM aT1.n1 } {at1 n1} +do_execsql_test 3.4 { SELECT * FROM At2.n1 } {at2 n1} + +# EVIDENCE-OF: R-15639-28392 If a database name is specified, then only +# the named database is searched for the named object. +# +do_catchsql_test 4.1 { SELECT * FROM temp.n2 } {1 {no such table: temp.n2}} +do_catchsql_test 4.2 { SELECT * FROM main.n2 } {0 {main n2}} +do_catchsql_test 4.3 { SELECT * FROM at1.n2 } {0 {at1 n2}} +do_catchsql_test 4.4 { SELECT * FROM at2.n2 } {0 {at2 n2}} + +# EVIDENCE-OF: R-08951-19801 When searching database schemas for a named +# object, objects of types that cannot be used in the context of the +# reference are always ignored. +# +# In this case, "types that cannot be used" are triggers and indexes. +# The temp and main databases both contain triggers and indexes named +# "n3" and "n4". Database "at2" contains a trigger called "n4". And yet: +# +do_execsql_test 5.1 { SELECT * FROM n3 } {at1 n3} +do_execsql_test 5.2 { SELECT * FROM n4 } {at2 n4} + +#------------------------------------------------------------------------- +# EVIDENCE-OF: R-37286-42536 +# +db close +forcedelete test.db file.db +sqlite3 db test.db +do_execsql_test 6.1 { + ATTACH 'file.db' AS aux; + CREATE TABLE t1(x, y); + CREATE TEMP TABLE t1(x, y); + CREATE TABLE aux.t1(x, y); +} + +do_execsql_test 6.2.0 { DROP TABLE t1 } +do_catchsql_test 6.2.1 { SELECT * FROM temp.t1 } {1 {no such table: temp.t1}} +do_catchsql_test 6.2.2 { SELECT * FROM main.t1 } {0 {}} +do_catchsql_test 6.2.3 { SELECT * FROM aux.t1 } {0 {}} + +do_execsql_test 6.3.0 { DROP TABLE t1 } +do_catchsql_test 6.3.1 { SELECT * FROM main.t1 } {1 {no such table: main.t1}} +do_catchsql_test 6.3.3 { SELECT * FROM aux.t1 } {0 {}} + +do_execsql_test 6.4.0 { DROP TABLE t1 } +do_catchsql_test 6.4.1 { SELECT * FROM aux.t1 } {1 {no such table: aux.t1}} + +finish_test Index: test/fts3aa.test ================================================================== --- test/fts3aa.test +++ test/fts3aa.test @@ -194,11 +194,34 @@ execsql {INSERT INTO t1(rowid, content) VALUES(-1, 'three four')} } {} do_test fts3aa-6.3 { execsql {SELECT content FROM t1 WHERE rowid = -1} } {{three four}} -breakpoint do_test fts3aa-6.4 { execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'four'} } {-1 0 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31} + +# Test creation of FTS3 and FTS4 tables with columns that contain +# an "=" character. +# +do_execsql_test fts3aa-7.1 { + CREATE VIRTUAL TABLE t2 USING fts3(xyz=abc); + SELECT xyz FROM t2; +} {} +do_catchsql_test fts3aa-7.2 { + CREATE VIRTUAL TABLE t3 USING fts4(xyz=abc); +} {1 {unrecognized parameter: xyz=abc}} +do_catchsql_test fts3aa-7.3 { + CREATE VIRTUAL TABLE t3 USING fts4(xyz = abc); +} {1 {unrecognized parameter: xyz = abc}} + +do_execsql_test fts3aa-7.4 { + CREATE VIRTUAL TABLE t3 USING fts3(tokenize=simple, tokenize=simple); + SELECT tokenize FROM t3; +} {} +do_catchsql_test fts3aa-7.5 { + CREATE VIRTUAL TABLE t4 USING fts4(tokenize=simple, tokenize=simple); +} {1 {unrecognized parameter: tokenize=simple}} + finish_test + Index: test/fts3defer2.test ================================================================== --- test/fts3defer2.test +++ test/fts3defer2.test @@ -51,20 +51,20 @@ do_execsql_test 1.2.1 { SELECT content FROM t1 WHERE t1 MATCH 'f (e NEAR/2 a)'; } {{a b c d e f a x y}} do_execsql_test 1.2.2 { - SELECT snippet(t1, '[', ']'), offsets(t1), mit(matchinfo(t1)) + SELECT snippet(t1, '[', ']'), offsets(t1), mit(matchinfo(t1, 'pcxnal')) FROM t1 WHERE t1 MATCH 'f (e NEAR/2 a)'; } [list \ {a b c d [e] [f] [a] x y} \ {0 1 8 1 0 0 10 1 0 2 12 1} \ [list 3 1 1 1 1 1 8 8 1 8 8 8 5001 9] ] do_execsql_test 1.2.3 { - SELECT snippet(t1, '[', ']'), offsets(t1), mit(matchinfo(t1)) + SELECT snippet(t1, '[', ']'), offsets(t1), mit(matchinfo(t1, 'pcxnal')) FROM t1 WHERE t1 MATCH 'f (e NEAR/3 a)'; } [list \ {[a] b c d [e] [f] [a] x y} \ {0 2 0 1 0 1 8 1 0 0 10 1 0 2 12 1} \ [list 3 1 1 1 1 1 8 8 2 8 8 8 5001 9] @@ -89,12 +89,13 @@ 3 { UPDATE t2_segments SET block = zeroblob(length(block)) WHERE length(block)>10000; } } { execsql $sql + do_execsql_test 2.2.$tn { - SELECT mit(matchinfo(t2)) FROM t2 WHERE t2 MATCH 'a b'; + SELECT mit(matchinfo(t2, 'pcxnal')) FROM t2 WHERE t2 MATCH 'a b'; } [list \ [list 2 1 1 54 54 1 3 3 54 372 7] \ [list 2 1 1 54 54 1 3 3 54 372 7] \ ] } @@ -122,12 +123,12 @@ WHERE length(block)>10000; } } { execsql $sql do_execsql_test 2.4.$tn { - SELECT docid, mit(matchinfo(t3)) FROM t3 WHERE t3 MATCH '"a b c"'; + SELECT docid, mit(matchinfo(t3, 'pcxnal')) FROM t3 WHERE t3 MATCH '"a b c"'; } {1 {1 1 1 4 4 11 912 6} 3 {1 1 1 4 4 11 912 6}} } finish_test Index: test/fts3fault.test ================================================================== --- test/fts3fault.test +++ test/fts3fault.test @@ -15,10 +15,12 @@ set ::testprefix fts3fault # If SQLITE_ENABLE_FTS3 is not defined, omit this file. ifcapable !fts3 { finish_test ; return } + +if 1 { # Test error handling in the sqlite3Fts3Init() function. This is the # function that registers the FTS3 module and various support functions # with SQLite. # @@ -152,7 +154,80 @@ execsql { CREATE VIRTUAL TABLE t1 USING fts4(a, b, matchnfo=fts3) } } -test { faultsim_test_result {1 {unrecognized parameter: matchnfo=fts3}} \ {1 {vtable constructor failed: t1}} } + +} + +proc mit {blob} { + set scan(littleEndian) i* + set scan(bigEndian) I* + binary scan $blob $scan($::tcl_platform(byteOrder)) r + return $r +} + +do_test 8.0 { + faultsim_delete_and_reopen + execsql { CREATE VIRTUAL TABLE t8 USING fts4 } + execsql "INSERT INTO t8 VALUES('a b c')" + execsql "INSERT INTO t8 VALUES('b b b')" + execsql "INSERT INTO t8 VALUES('[string repeat {c } 50000]')" + execsql "INSERT INTO t8 VALUES('d d d')" + execsql "INSERT INTO t8 VALUES('e e e')" + execsql "INSERT INTO t8(t8) VALUES('optimize')" + faultsim_save_and_close +} {} + +do_faultsim_test 8.1 -prep { + faultsim_restore_and_reopen + db func mit mit +} -body { + execsql { SELECT mit(matchinfo(t8, 'x')) FROM t8 WHERE t8 MATCH 'a b c' } +} -test { + faultsim_test_result {0 {{1 1 1 1 4 2 1 5 5}}} +} +do_faultsim_test 8.2 -faults oom-t* -prep { + faultsim_restore_and_reopen + db func mit mit +} -body { + execsql { SELECT mit(matchinfo(t8, 's')) FROM t8 WHERE t8 MATCH 'a b c' } +} -test { + faultsim_test_result {0 3} +} +do_faultsim_test 8.3 -prep { + faultsim_restore_and_reopen + db func mit mit +} -body { + execsql { SELECT mit(matchinfo(t8, 'a')) FROM t8 WHERE t8 MATCH 'a b c' } +} -test { + faultsim_test_result {0 10002} +} +do_faultsim_test 8.4 -prep { + faultsim_restore_and_reopen + db func mit mit +} -body { + execsql { SELECT mit(matchinfo(t8, 'l')) FROM t8 WHERE t8 MATCH 'a b c' } +} -test { + faultsim_test_result {0 3} +} + +do_test 9.0 { + faultsim_delete_and_reopen + execsql { + CREATE VIRTUAL TABLE t9 USING fts4(tokenize=porter); + INSERT INTO t9 VALUES( + 'this record is used toooooooooooooooooooooooooooooooooooooo try to' + ); + SELECT offsets(t9) FROM t9 WHERE t9 MATCH 'to*'; + } + faultsim_save_and_close +} {} +do_faultsim_test 9.1 -prep { + faultsim_restore_and_reopen +} -body { + execsql { SELECT offsets(t9) FROM t9 WHERE t9 MATCH 'to*' } +} -test { + faultsim_test_result {0 {{0 0 20 39 0 0 64 2}}} +} finish_test Index: test/fts3matchinfo.test ================================================================== --- test/fts3matchinfo.test +++ test/fts3matchinfo.test @@ -39,19 +39,17 @@ INSERT INTO t1(content) VALUES('When all at once I saw a crowd,'); INSERT INTO t1(content) VALUES('A host, of golden daffodils,'); SELECT mit(matchinfo(t1)) FROM t1 WHERE t1 MATCH 'I'; } {{1 1 1 2 2} {1 1 1 2 2}} -# Now create an FTS4 table that does not specify matchinfo=fts3. The -# %_docsize table is created in this case and the array of integers returned -# by matchinfo() includes the extra data. +# Now create an FTS4 table that does not specify matchinfo=fts3. # do_execsql_test 1.2 { CREATE VIRTUAL TABLE t2 USING fts4; INSERT INTO t2 SELECT * FROM t1; SELECT mit(matchinfo(t2)) FROM t2 WHERE t2 MATCH 'I'; -} {{1 1 1 2 2 4 7 6} {1 1 1 2 2 4 7 8}} +} {{1 1 1 2 2} {1 1 1 2 2}} # Test some syntax-error handling. # do_catchsql_test 2.0 { CREATE VIRTUAL TABLE x1 USING fts4(matchinfo=fs3); @@ -65,7 +63,280 @@ do_execsql_test 3.1 { CREATE VIRTUAL TABLE t3 USING fts3(mtchinfo=fts3); INSERT INTO t3(mtchinfo) VALUES('Beside the lake, beneath the trees'); SELECT mtchinfo FROM t3; } {{Beside the lake, beneath the trees}} + +do_execsql_test 3.2 { + CREATE VIRTUAL TABLE xx USING FTS4; + SELECT * FROM xx WHERE xx MATCH 'abc'; + SELECT * FROM xx WHERE xx MATCH 'a b c'; +} + + +#-------------------------------------------------------------------------- +# Proc [do_matchinfo_test] is used to test the FTSX matchinfo() function. +# +# The first argument - $tn - is a test identifier. This may be either a +# full identifier (i.e. "fts3matchinfo-1.1") or, if global var $testprefix +# is set, just the numeric component (i.e. "1.1"). +# +# The second argument is the name of an FTSX table. The third is the +# full text of a WHERE/MATCH expression to query the table for +# (i.e. "t1 MATCH 'abc'"). The final argument - $results - should be a +# key-value list (serialized array) with matchinfo() format specifiers +# as keys, and the results of executing the statement: +# +# SELECT matchinfo($tbl, '$key') FROM $tbl WHERE $expr +# +# For example: +# +# CREATE VIRTUAL TABLE t1 USING fts4; +# INSERT INTO t1 VALUES('abc'); +# INSERT INTO t1 VALUES('def'); +# INSERT INTO t1 VALUES('abc abc'); +# +# do_matchinfo_test 1.1 t1 "t1 MATCH 'abc'" { +# n {3 3} +# p {1 1} +# c {1 1} +# x {{1 3 2} {2 3 2}} +# } +# +# If the $results list contains keys mapped to "-" instead of a matchinfo() +# result, then this command computes the expected results based on other +# mappings to test the matchinfo() function. For example, the command above +# could be changed to: +# +# do_matchinfo_test 1.1 t1 "t1 MATCH 'abc'" { +# n {3 3} p {1 1} c {1 1} x {{1 3 2} {2 3 2}} +# pcx - +# } +# +# And this command would compute the expected results for matchinfo(t1, 'pcx') +# based on the results of matchinfo(t1, 'p'), matchinfo(t1, 'c') and +# matchinfo(t1, 'x') in order to test 'pcx'. +# +proc do_matchinfo_test {tn tbl expr results} { + + foreach {fmt res} $results { + if {$res == "-"} continue + set resarray($fmt) $res + } + + set nRow 0 + foreach {fmt res} [array get resarray] { + if {[llength $res]>$nRow} { set nRow [llength $res] } + } + + # Construct expected results for any formats for which the caller + # supplied result is "-". + # + foreach {fmt res} $results { + if {$res == "-"} { + set res [list] + for {set iRow 0} {$iRow<$nRow} {incr iRow} { + set rowres [list] + foreach c [split $fmt ""] { + set rowres [concat $rowres [lindex $resarray($c) $iRow]] + } + lappend res $rowres + } + set resarray($fmt) $res + } + } + + # Test each matchinfo() request individually. + # + foreach {fmt res} [array get resarray] { + set sql "SELECT mit(matchinfo($tbl, '$fmt')) FROM $tbl WHERE $expr" + do_execsql_test $tn.$fmt $sql [normalize2 $res] + } + + # Test them all executed together (multiple invocations of matchinfo()). + # + set exprlist [list] + foreach {format res} [array get resarray] { + lappend exprlist "mit(matchinfo($tbl, '$format'))" + } + set allres [list] + for {set iRow 0} {$iRow<$nRow} {incr iRow} { + foreach {format res} [array get resarray] { + lappend allres [lindex $res $iRow] + } + } + set sql "SELECT [join $exprlist ,] FROM $tbl WHERE $expr" + do_execsql_test $tn.multi $sql [normalize2 $allres] +} +proc normalize2 {list_of_lists} { + set res [list] + foreach elem $list_of_lists { + lappend res [list {*}$elem] + } + return $res +} + + +do_execsql_test 4.1.0 { + CREATE VIRTUAL TABLE t4 USING fts4(x, y); + INSERT INTO t4 VALUES('a b c d e', 'f g h i j'); + INSERT INTO t4 VALUES('f g h i j', 'a b c d e'); +} + +do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} { + p {3 3} + c {2 2} + x { + {1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1} + {0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1} + } + n {2 2} + l {{5 5} {5 5}} + a {{5 5} {5 5}} + + s {{3 0} {0 3}} + + xxxxxxxxxxxxxxxxxx - pcx - xpc - ccc - pppxpcpcx - laxnpc - + xpxsscplax - +} + +do_matchinfo_test 4.1.2 t4 {t4 MATCH '"g h i"'} { + p {1 1} + c {2 2} + x { + {0 1 1 1 1 1} + {1 1 1 0 1 1} + } + n {2 2} + l {{5 5} {5 5}} + a {{5 5} {5 5}} + + s {{0 1} {1 0}} + + xxxxxxxxxxxxxxxxxx - pcx - xpc - ccc - pppxpcpcx - laxnpc - + sxsxs - +} + +do_matchinfo_test 4.1.3 t4 {t4 MATCH 'a b'} { s {{2 0} {0 2}} } +do_matchinfo_test 4.1.4 t4 {t4 MATCH '"a b" c'} { s {{2 0} {0 2}} } +do_matchinfo_test 4.1.5 t4 {t4 MATCH 'a "b c"'} { s {{2 0} {0 2}} } +do_matchinfo_test 4.1.6 t4 {t4 MATCH 'd d'} { s {{1 0} {0 1}} } + +do_execsql_test 4.2.0 { + CREATE VIRTUAL TABLE t5 USING fts4; + INSERT INTO t5 VALUES('a a a a a'); + INSERT INTO t5 VALUES('a b a b a'); + INSERT INTO t5 VALUES('c b c b c'); + INSERT INTO t5 VALUES('x x x x x'); +} +do_matchinfo_test 4.2.1 t5 {t5 MATCH 'a a'} { + x {{5 8 2 5 8 2} {3 8 2 3 8 2}} + s {2 1} +} +do_matchinfo_test 4.2.2 t5 {t5 MATCH 'a b'} { s {2} } +do_matchinfo_test 4.2.3 t5 {t5 MATCH 'a b a'} { s {3} } +do_matchinfo_test 4.2.4 t5 {t5 MATCH 'a a a'} { s {3 1} } +do_matchinfo_test 4.2.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} } +do_matchinfo_test 4.2.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1} } + +do_execsql_test 4.3.0 "INSERT INTO t5 VALUES('x y [string repeat {b } 50000]')"; + +do_matchinfo_test 4.3.1 t5 {t5 MATCH 'a a'} { + x {{5 8 2 5 5 5} {3 8 2 3 5 5}} + s {2 1} +} + +do_matchinfo_test 4.3.2 t5 {t5 MATCH 'a b'} { s {2} } +do_matchinfo_test 4.3.3 t5 {t5 MATCH 'a b a'} { s {3} } +do_matchinfo_test 4.3.4 t5 {t5 MATCH 'a a a'} { s {3 1} } +do_matchinfo_test 4.3.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} } +do_matchinfo_test 4.3.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1 1} } + +do_execsql_test 4.4.0 { + INSERT INTO t5(t5) VALUES('optimize'); + UPDATE t5_segments + SET block = zeroblob(length(block)) + WHERE length(block)>10000; +} + +do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'} { s {2} } +do_matchinfo_test 4.4.1 t5 {t5 MATCH 'a a'} { s {2 1} } +do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'} { s {2} } +do_matchinfo_test 4.4.3 t5 {t5 MATCH 'a b a'} { s {3} } +do_matchinfo_test 4.4.4 t5 {t5 MATCH 'a a a'} { s {3 1} } +do_matchinfo_test 4.4.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} } + +do_execsql_test 4.5.0 { + CREATE VIRTUAL TABLE t6 USING fts4(a, b, c); + INSERT INTO t6 VALUES('a', 'b', 'c'); +} +do_matchinfo_test 4.5.1 t6 {t6 MATCH 'a b c'} { s {{1 1 1}} } + + +#------------------------------------------------------------------------- +# Check the following restrictions: +# +# + Matchinfo flags 'a', 'l' and 'n' can only be used with fts4, not fts3. +# + Matchinfo flag 'l' cannot be used with matchinfo=fts3. +# +do_execsql_test 5.1 { + CREATE VIRTUAL TABLE t7 USING fts3(a, b); + INSERT INTO t7 VALUES('u v w', 'x y z'); + + CREATE VIRTUAL TABLE t8 USING fts4(a, b, matchinfo=fts3); + INSERT INTO t8 VALUES('u v w', 'x y z'); +} + +do_catchsql_test 5.2.1 { + SELECT matchinfo(t7, 'a') FROM t7 WHERE t7 MATCH 'x y' +} {1 {unrecognized matchinfo request: a}} +do_catchsql_test 5.2.2 { + SELECT matchinfo(t7, 'l') FROM t7 WHERE t7 MATCH 'x y' +} {1 {unrecognized matchinfo request: l}} +do_catchsql_test 5.2.3 { + SELECT matchinfo(t7, 'n') FROM t7 WHERE t7 MATCH 'x y' +} {1 {unrecognized matchinfo request: n}} + +do_catchsql_test 5.3.1 { + SELECT matchinfo(t8, 'l') FROM t8 WHERE t8 MATCH 'x y' +} {1 {unrecognized matchinfo request: l}} + +#------------------------------------------------------------------------- +# Test that the offsets() function handles corruption in the %_content +# table correctly. +# +do_execsql_test 6.1 { + CREATE VIRTUAL TABLE t9 USING fts4; + INSERT INTO t9 VALUES( + 'this record is used to try to dectect corruption' + ); + SELECT offsets(t9) FROM t9 WHERE t9 MATCH 'to'; +} {{0 0 20 2 0 0 27 2}} + +do_catchsql_test 6.2 { + UPDATE t9_content SET c0content = 'this record is used to'; + SELECT offsets(t9) FROM t9 WHERE t9 MATCH 'to'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# Test the outcome of matchinfo() when used within a query that does not +# use the full-text index (i.e. lookup by rowid or full-table scan). +# +do_execsql_test 7.1 { + CREATE VIRTUAL TABLE t10 USING fts4; + INSERT INTO t10 VALUES('first record'); + INSERT INTO t10 VALUES('second record'); +} +do_execsql_test 7.2 { + SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10; +} {blob 0 blob 0} +do_execsql_test 7.3 { + SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10 WHERE docid=1; +} {blob 0} +do_execsql_test 7.4 { + SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) + FROM t10 WHERE t10 MATCH 'record' +} {blob 20 blob 20} + finish_test + Index: test/fts3query.test ================================================================== --- test/fts3query.test +++ test/fts3query.test @@ -150,12 +150,10 @@ } do_select_tests 5.2 -errorformat { wrong number of arguments to function %s() } { 1 "SELECT matchinfo() FROM t2 WHERE t2 MATCH 'history'" matchinfo - 2 "SELECT matchinfo(t2, t2) FROM t2 WHERE t2 MATCH 'history'" matchinfo - 3 "SELECT snippet(t2, 1, 2, 3, 4, 5, 6) FROM t2 WHERE t2 MATCH 'history'" snippet } do_select_tests 5.3 -errorformat { illegal first argument to %s @@ -172,11 +170,16 @@ 1 "SELECT matchinfo(content) FROM t2 WHERE t2 MATCH 'history'" matchinfo 2 "SELECT offsets(content) FROM t2 WHERE t2 MATCH 'history'" offsets 3 "SELECT snippet(content) FROM t2 WHERE t2 MATCH 'history'" snippet 4 "SELECT optimize(content) FROM t2 WHERE t2 MATCH 'history'" optimize } +do_catchsql_test 5.5.1 { + SELECT matchinfo(t2, 'abc') FROM t2 WHERE t2 MATCH 'history' +} {1 {unrecognized matchinfo request: b}} + do_execsql_test 5.5 { DROP TABLE t2 } + # Test the snippet() function with 1 to 6 arguments. # do_execsql_test 6.1 { CREATE VIRTUAL TABLE t3 USING FTS4(a, b); Index: test/fts3rnd.test ================================================================== --- test/fts3rnd.test +++ test/fts3rnd.test @@ -303,10 +303,14 @@ execsql BEGIN insert_row $iInsert update_row $iUpdate delete_row $iDelete if {0==($iTest%2)} { execsql COMMIT } + + if {0==($iTest%2)} { + do_test fts3rnd-1.$nodesize.$iTest.0 { fts3_integrity_check t1 } ok + } # Pick 10 terms from the vocabulary. Check that the results of querying # the database for the set of documents containing each of these terms # is the same as the result obtained by scanning the contents of the Tcl # array for each term. @@ -377,11 +381,10 @@ # for {set i 0} {$i < $nRep} {incr i} { set terms [list [random_term] [random_term] [random_term]] set nNear 11 set match [join $terms " NEAR/$nNear "] - set fts3 [execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }] do_select_test fts3rnd-1.$nodesize.$iTest.7.$i { SELECT docid FROM t1 WHERE t1 MATCH $match } [simple_near $terms $nNear] } Index: test/incrblob3.test ================================================================== --- test/incrblob3.test +++ test/incrblob3.test @@ -90,12 +90,15 @@ list [catch {sqlite3_blob_read $::blob 0 10} msg] $msg } {1 SQLITE_ABORT} do_test incrblob3-2.2.$tn.5 { list [catch {sqlite3_blob_write $::blob 0 "abcd"} msg] $msg } {1 SQLITE_ABORT} + do_test incrblob3-2.2.$tn.6 { + sqlite3_blob_bytes $::blob + } {0} - do_test incrblob3-2.2.$tn.4 { close $::blob } {} + do_test incrblob3-2.2.$tn.7 { close $::blob } {} } # Test that passing NULL to sqlite3_blob_XXX() APIs returns SQLITE_MISUSE. # # incrblob3-3.1: sqlite3_blob_reopen() Index: test/lock_common.tcl ================================================================== --- test/lock_common.tcl +++ test/lock_common.tcl @@ -14,10 +14,11 @@ # proc do_multiclient_test {varname script} { foreach code [list { + if {[info exists ::G(valgrind)]} { db close ; continue } set ::code2_chan [launch_testfixture] set ::code3_chan [launch_testfixture] proc code2 {tcl} { testfixture $::code2_chan $tcl } proc code3 {tcl} { testfixture $::code3_chan $tcl } set tn 1 Index: test/malloc_common.tcl ================================================================== --- test/malloc_common.tcl +++ test/malloc_common.tcl @@ -132,10 +132,11 @@ set testspec [list -prep $O(-prep) -body $O(-body) -test $O(-test)] foreach f [lsort -unique $faultlist] { eval do_one_faultsim_test "$name-$f" $FAULTSIM($f) $testspec } } + #------------------------------------------------------------------------- # Procedures to save and restore the current file-system state: # # faultsim_save @@ -142,48 +143,29 @@ # faultsim_restore # faultsim_save_and_close # faultsim_restore_and_reopen # faultsim_delete_and_reopen # -proc faultsim_save {} { - foreach f [glob -nocomplain sv_test.db*] { forcedelete $f } - foreach f [glob -nocomplain test.db*] { - set f2 "sv_$f" - file copy -force $f $f2 - } -} -proc faultsim_save_and_close {} { - faultsim_save - catch { db close } - return "" -} -proc faultsim_restore {} { - foreach f [glob -nocomplain test.db*] { forcedelete $f } - foreach f2 [glob -nocomplain sv_test.db*] { - set f [string range $f2 3 end] - file copy -force $f2 $f - } -} -proc faultsim_restore_and_reopen {{dbfile test.db}} { - catch { db close } - faultsim_restore - sqlite3 db $dbfile +proc faultsim_save {args} { uplevel db_save $args } +proc faultsim_save_and_close {args} { uplevel db_save_and_close $args } +proc faultsim_restore {args} { uplevel db_restore $args } +proc faultsim_restore_and_reopen {args} { + uplevel db_restore_and_reopen $args + sqlite3_extended_result_codes db 1 + sqlite3_db_config_lookaside db 0 0 0 +} +proc faultsim_delete_and_reopen {args} { + uplevel db_delete_and_reopen $args sqlite3_extended_result_codes db 1 sqlite3_db_config_lookaside db 0 0 0 } proc faultsim_integrity_check {{db db}} { set ic [$db eval { PRAGMA integrity_check }] if {$ic != "ok"} { error "Integrity check: $ic" } } -proc faultsim_delete_and_reopen {{file test.db}} { - catch { db close } - foreach f [glob -nocomplain test.db*] { file delete -force $f } - sqlite3 db $file -} - # The following procs are used as [do_one_faultsim_test] callbacks when # injecting OOM faults into test cases. # proc oom_injectstart {nRepeat iFail} { Index: test/misc7.test ================================================================== --- test/misc7.test +++ test/misc7.test @@ -257,28 +257,26 @@ file delete -force test.db file delete -force test.db-journal sqlite3 db test.db ifcapable explain { - do_test misc7-14.1 { - execsql { - CREATE TABLE abc(a PRIMARY KEY, b, c); - } - execsql { - EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE rowid = 1; - } - } {0 0 {TABLE abc AS t2 USING PRIMARY KEY}} - do_test misc7-14.2 { - execsql { - EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE a = 1; - } - } {0 0 {TABLE abc AS t2 WITH INDEX sqlite_autoindex_abc_1}} - do_test misc7-14.3 { - execsql { - EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 ORDER BY a; - } - } {0 0 {TABLE abc AS t2 WITH INDEX sqlite_autoindex_abc_1 ORDER BY}} + do_execsql_test misc7-14.1 { + CREATE TABLE abc(a PRIMARY KEY, b, c); + EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE rowid = 1; + } { + 0 0 0 {SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)} + } + do_execsql_test misc7-14.2 { + EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE a = 1; + } {0 0 0 + {SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?) (~1 rows)} + } + do_execsql_test misc7-14.3 { + EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 ORDER BY a; + } {0 0 0 + {SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (~1000000 rows)} + } } db close file delete -force test.db file delete -force test.db-journal Index: test/multiplex.test ================================================================== --- test/multiplex.test +++ test/multiplex.test @@ -24,11 +24,11 @@ # file name with the chunk number. proc multiplex_name {name chunk} { if {$chunk==0} { return $name } set num [format "%02d" $chunk] ifcapable {multiplex_ext_overwrite} { - set name [string range $name 0 [expr [string length $name]-2-1]] + set name [string range $name 0 [expr [string length $name]-2-1]] } return $name$num } # This saves off the parameters and calls the @@ -52,10 +52,13 @@ } } db close +multiplex_delete test.db +multiplex_delete test2.db + #------------------------------------------------------------------------- # multiplex-1.1.*: Test initialize and shutdown. do_test multiplex-1.1 { sqlite3_multiplex_initialize nosuchvfs 1 } {SQLITE_ERROR} do_test multiplex-1.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} @@ -121,11 +124,11 @@ execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } } {} do_test multiplex-2.2.3 { file size [multiplex_name test.db 0] } {6144} do_test multiplex-2.3.1 { - sqlite3 db2 bak.db + sqlite3 db2 test2.db db2 close } {} do_test multiplex-2.4.1 { sqlite3_multiplex_shutdown @@ -445,8 +448,52 @@ catch { sqlite3_multiplex_shutdown } } -body { sqlite3_multiplex_initialize "" 1 multiplex_set 32768 16 } + +# test that mismatch filesize is detected +# +# Do not run this test if $::G(perm:presql) is set. If it is set, then the +# expected IO error will occur within the Tcl [sqlite3] wrapper, not within +# the first SQL statement executed below. This breaks the test case. +# +if {0==[info exists ::G(perm:presql)] || $::G(perm:presql) == ""} { + set all_journal_modes {delete persist truncate memory off} + foreach jmode $all_journal_modes { + do_test multiplex-5.6.1.$jmode { + sqlite3_multiplex_shutdown + multiplex_delete test.db + sqlite3 db test.db + db eval { + PRAGMA page_size = 1024; + PRAGMA auto_vacuum = off; + } + db eval "PRAGMA journal_mode = $jmode;" + } $jmode + do_test multiplex-5.6.2.$jmode { + execsql { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, randomblob(1100)); + INSERT INTO t1 VALUES(2, randomblob(1100)); + INSERT INTO t1 VALUES(3, randomblob(1100)); + INSERT INTO t1 VALUES(4, randomblob(1100)); + INSERT INTO t1 VALUES(5, randomblob(1100)); + } + db close + sqlite3_multiplex_initialize "" 1 + multiplex_set 4096 16 + sqlite3 db test.db + } {} + do_test multiplex-5.6.3.$jmode { + catchsql { + INSERT INTO t1 VALUES(6, randomblob(1100)); + } + } {1 {disk I/O error}} + do_test multiplex-5.6.4.$jmode { + db close + } {} + } +} catch { sqlite3_multiplex_shutdown } finish_test Index: test/pager1.test ================================================================== --- test/pager1.test +++ test/pager1.test @@ -1060,11 +1060,11 @@ INSERT INTO t11 VALUES(3, 4); PRAGMA max_page_count = 10; } {11} do_execsql_test pager1-6.9 { COMMIT } {} -do_execsql_test pager1-6.10 { PRAGMA max_page_count = 10 } {10} +do_execsql_test pager1-6.10 { PRAGMA max_page_count = 10 } {11} do_execsql_test pager1-6.11 { SELECT * FROM t11 } {1 2 3 4} do_execsql_test pager1-6.12 { PRAGMA max_page_count } {11} #------------------------------------------------------------------------- Index: test/permutations.test ================================================================== --- test/permutations.test +++ test/permutations.test @@ -23,19 +23,21 @@ # -initialize SCRIPT (default "") # -shutdown SCRIPT (default "") # -presql SQL (default "") # -files LIST-OF-FILES (default $::ALLTESTS) # -prefix NAME (default "$::NAME.") +# -dbconfig SCRIPT (default "") # proc test_suite {name args} { set default(-shutdown) "" set default(-initialize) "" set default(-presql) "" set default(-description) "no description supplied (fixme)" set default(-files) "" set default(-prefix) "${name}." + set default(-dbconfig) "" array set options [array get default] if {[llength $args]%2} { error "uneven number of options/switches passed to test_suite" } @@ -46,11 +48,10 @@ set options([lindex $o 0]) $v } set ::testspec($name) [array get options] lappend ::testsuitelist $name - } #------------------------------------------------------------------------- # test_set ARGS... # @@ -134,10 +135,21 @@ This test suite is the same as the "quick" tests, except that some files that test malloc and IO errors are omitted. } -files [ test_set $allquicktests -exclude *malloc* *ioerr* *fault* ] + +test_suite "valgrind" -prefix "" -description { + Run the "veryquick" test suite with a couple of multi-process tests (that + fail under valgrind) omitted. +} -files [ + test_set $allquicktests -exclude *malloc* *ioerr* *fault* +] -initialize { + set ::G(valgrind) 1 +} -shutdown { + unset -nocomplain ::G(valgrind) +} test_suite "quick" -prefix "" -description { Quick test suite. Runs in around 10 minutes on a workstation. } -files [ test_set $allquicktests @@ -744,10 +756,23 @@ } test_suite "rtree" -description { All R-tree related tests. Provides coverage of source file rtree.c. } -files [glob -nocomplain $::testdir/../ext/rtree/*.test] + +test_suite "no_optimization" -description { + Run test scripts with optimizations disabled using the + sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS) interface. +} -files { + where.test where2.test where3.test where4.test where5.test + where6.test where7.test where8.test where9.test + whereA.test whereB.test wherelimit.test + select1.test select2.test select3.test select4.test select5.test + select7.test select8.test selectA.test selectC.test +} -dbconfig { + optimization_control $::dbhandle all 0 +} # End of tests ############################################################################# # run_tests NAME OPTIONS @@ -766,10 +791,11 @@ set ::G(perm:name) $name set ::G(perm:prefix) $options(-prefix) set ::G(perm:presql) $options(-presql) set ::G(isquick) 1 + set ::G(perm:dbconfig) $options(-dbconfig) uplevel $options(-initialize) foreach file [lsort $options(-files)] { if {[file tail $file] == $file} { set file [file join $::testdir $file] } @@ -779,10 +805,11 @@ uplevel $options(-shutdown) unset ::G(perm:name) unset ::G(perm:prefix) unset ::G(perm:presql) + unset ::G(perm:dbconfig) } proc run_test_suite {name} { if {[info exists ::testspec($name)]==0} { error "No such test suite: $name" Index: test/superlock.test ================================================================== --- test/superlock.test +++ test/superlock.test @@ -13,10 +13,36 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl source $testdir/lock_common.tcl set testprefix superlock + +# Test organization: +# +# 1.*: Test superlock on a rollback database. Test that once the db is +# superlocked, it is not possible for a second client to read from +# it. +# +# 2.*: Test superlock on a WAL database with zero frames in the WAL file. +# Test that once the db is superlocked, it is not possible to read, +# write or checkpoint the db. +# +# 3.*: As 2.*, for WAL databases with one or more frames in the WAL. +# +# 4.*: As 2.*, for WAL databases with one or more checkpointed frames +# in the WAL. +# +# 5.*: Test that a call to sqlite3demo_superlock() uses the busy handler +# correctly to wait for existing clients to clear on a WAL database. +# And returns SQLITE_BUSY if no busy handler is defined or the busy +# handler returns 0 before said clients relinquish their locks. +# +# 6.*: Test that if a superlocked WAL database is overwritten, existing +# clients run the recovery to build the new wal-index after the +# superlock is released. +# +# do_execsql_test 1.1 { CREATE TABLE t1(a, b); INSERT INTO t1 VALUES(1, 2); PRAGMA journal_mode = DELETE; @@ -52,10 +78,11 @@ do_catchsql_test 4.4 { INSERT INTO t1 VALUES(5, 6)} {1 {database is locked}} do_catchsql_test 4.5 { PRAGMA wal_checkpoint } {1 {database is locked}} do_test 4.6 { unlock } {} do_multiclient_test tn { + proc busyhandler {x} { switch -- $x { 1 { sql1 "COMMIT" } 2 { sql2 "COMMIT" } 3 { sql3 "COMMIT" } @@ -90,9 +117,124 @@ csql3 { INSERT INTO t1 VALUES(5, 6) } } {1 {database is locked}} do_test 5.$tn.6 { csql1 "PRAGMA wal_checkpoint" } {1 {database is locked}} do_test 5.$tn.7 { unlock } {} + + + do_test 5.$tn.8 { + sql1 { BEGIN ; SELECT * FROM t1 } + sql2 { BEGIN ; INSERT INTO t1 VALUES(5, 6) } + sql3 { BEGIN ; SELECT * FROM t1 } + } {1 2 3 4} + + do_test 5.$tn.9 { + list [catch {sqlite3demo_superlock unlock test.db} msg] $msg + } {1 {database is locked}} + do_test 5.$tn.10 { + sql1 COMMIT + list [catch {sqlite3demo_superlock unlock test.db} msg] $msg + } {1 {database is locked}} + do_test 5.$tn.11 { + sql2 COMMIT + list [catch {sqlite3demo_superlock unlock test.db} msg] $msg + } {1 {database is locked}} + do_test 5.$tn.12 { + sql3 COMMIT + list [catch {sqlite3demo_superlock unlock test.db} msg] $msg + } {0 unlock} + unlock +} + +proc read_content {file} { + if {[file exists $file]==0} {return ""} + set fd [open $file] + fconfigure $fd -encoding binary -translation binary + set content [read $fd] + close $fd + return $content +} + +proc write_content {file content} { + set fd [open $file w+] + fconfigure $fd -encoding binary -translation binary + puts -nonewline $fd $content + close $fd +} + +# Both $file1 and $file2 are database files. This function takes a +# superlock on each, then exchanges the content of the two files (i.e. +# overwrites $file1 with the initial contents of $file2, and overwrites +# $file2 with the initial contents of $file1). The contents of any WAL +# file is also exchanged. +# +proc db_swap {file1 file2} { + sqlite3demo_superlock unlock1 $file1 + sqlite3demo_superlock unlock2 $file2 + + set db1 [read_content $file1] + set db2 [read_content $file2] + write_content $file1 $db2 + write_content $file2 $db1 + + set wal1 [read_content ${file1}-wal] + set wal2 [read_content ${file2}-wal] + write_content ${file1}-wal $wal2 + write_content ${file2}-wal $wal1 + + unlock1 + unlock2 } +forcedelete test.db +sqlite3 db test.db +do_execsql_test 6.1 { + ATTACH 'test.db2' AS aux; + PRAGMA aux.journal_mode = wal; + CREATE TABLE aux.t2(x, y); + INSERT INTO aux.t2 VALUES('a', 'b'); + PRAGMA schema_version = 450; + DETACH aux; + + PRAGMA main.journal_mode = wal; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + SELECT * FROM t1; +} {wal wal 1 2 3 4} + + +db_swap test.db2 test.db +do_catchsql_test 6.2 { SELECT * FROM t1 } {1 {no such table: t1}} +do_catchsql_test 6.3 { SELECT * FROM t2 } {0 {a b}} + +db_swap test.db2 test.db +do_catchsql_test 6.4 { SELECT * FROM t1 } {0 {1 2 3 4}} +do_catchsql_test 6.5 { SELECT * FROM t2 } {1 {no such table: t2}} + +do_execsql_test 6.6 { PRAGMA wal_checkpoint } + +db_swap test.db2 test.db +do_catchsql_test 6.7 { SELECT * FROM t1 } {1 {no such table: t1}} +do_catchsql_test 6.8 { SELECT * FROM t2 } {0 {a b}} + +db_swap test.db2 test.db +do_catchsql_test 6.9 { SELECT * FROM t1 } {0 {1 2 3 4}} +do_catchsql_test 6.10 { SELECT * FROM t2 } {1 {no such table: t2}} + +do_execsql_test 6.11 { + PRAGMA journal_mode = delete; + PRAGMA page_size = 512; + VACUUM; + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(5, 6); +} {delete wal} + +db_swap test.db2 test.db +do_catchsql_test 6.12 { SELECT * FROM t1 } {1 {no such table: t1}} +do_catchsql_test 6.13 { SELECT * FROM t2 } {0 {a b}} + +db_swap test.db2 test.db +do_catchsql_test 6.14 { SELECT * FROM t1 } {0 {1 2 3 4 5 6}} +do_catchsql_test 6.15 { SELECT * FROM t2 } {1 {no such table: t2}} finish_test Index: test/tester.tcl ================================================================== --- test/tester.tcl +++ test/tester.tcl @@ -107,10 +107,14 @@ set res [uplevel 1 sqlite_orig $args] if {[info exists ::G(perm:presql)]} { [lindex $args 0] eval $::G(perm:presql) } + if {[info exists ::G(perm:dbconfig)]} { + set ::dbhandle [lindex $args 0] + uplevel #0 $::G(perm:dbconfig) + } set res } else { # This command is not opening a new database connection. Pass the # arguments through to the C implemenation as the are. # @@ -335,11 +339,11 @@ } else { puts " Ok" } flush stdout } - + proc fix_testname {varname} { upvar $varname testname if {[info exists ::testprefix] && [string is digit [string range $testname 0 0]] } { @@ -1405,12 +1409,42 @@ db36231 close hexio_write test.db 28 $A hexio_write test.db 92 $B return "" } + +proc db_save {} { + foreach f [glob -nocomplain sv_test.db*] { forcedelete $f } + foreach f [glob -nocomplain test.db*] { + set f2 "sv_$f" + file copy -force $f $f2 + } +} +proc db_save_and_close {} { + db_save + catch { db close } + return "" +} +proc db_restore {} { + foreach f [glob -nocomplain test.db*] { forcedelete $f } + foreach f2 [glob -nocomplain sv_test.db*] { + set f [string range $f2 3 end] + file copy -force $f2 $f + } +} +proc db_restore_and_reopen {{dbfile test.db}} { + catch { db close } + db_restore + sqlite3 db $dbfile +} +proc db_delete_and_reopen {{file test.db}} { + catch { db close } + foreach f [glob -nocomplain test.db*] { file delete -force $f } + sqlite3 db $file +} # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set # to non-zero, then set the global variable $AUTOVACUUM to 1. set AUTOVACUUM $sqlite_options(default_autovacuum) source $testdir/thread_common.tcl source $testdir/malloc_common.tcl ADDED test/tkt-80ba201079.test Index: test/tkt-80ba201079.test ================================================================== --- /dev/null +++ test/tkt-80ba201079.test @@ -0,0 +1,191 @@ +# 2010 December 6 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. Specifically, +# it tests that ticket [80ba201079ea608071d22a57856b940ea3ac53ce] is +# resolved. That ticket is about an incorrect result that appears when +# an index is added. The root cause is that a constant is being used +# without initialization when the OR optimization applies in the WHERE clause. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix tkt-80ba2 + +do_test tkt-80ba2-100 { + db eval { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES('A'); + CREATE TABLE t2(b); + INSERT INTO t2 VALUES('B'); + CREATE TABLE t3(c); + INSERT INTO t3 VALUES('C'); + SELECT * FROM t1, t2 + WHERE (a='A' AND b='X') + OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C')); + } +} {A B} +do_test tkt-80ba2-101 { + db eval { + CREATE INDEX i1 ON t1(a); + SELECT * FROM t1, t2 + WHERE (a='A' AND b='X') + OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C')); + } +} {A B} +do_test tkt-80ba2-102 { + optimization_control db factor-constants 0 + db cache flush + db eval { + SELECT * FROM t1, t2 + WHERE (a='A' AND b='X') + OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C')); + } +} {A B} +optimization_control db all 1 + +# Verify that the optimization_control command is actually working +# +do_test tkt-80ba2-150 { + optimization_control db factor-constants 1 + db cache flush + set x1 [db eval {EXPLAIN + SELECT * FROM t1, t2 + WHERE (a='A' AND b='X') + OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));}] + optimization_control db factor-constants 0 + db cache flush + set x2 [db eval {EXPLAIN + SELECT * FROM t1, t2 + WHERE (a='A' AND b='X') + OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));}] + + expr {$x1==$x2} +} {0} + +do_test tkt-80ba2-200 { + db eval { + CREATE TABLE entry_types ( + id integer primary key, + name text + ); + INSERT INTO "entry_types" VALUES(100,'cli_command'); + INSERT INTO "entry_types" VALUES(300,'object_change'); + CREATE TABLE object_changes ( + change_id integer primary key, + system_id int, + obj_id int, + obj_context text, + change_type int, + command_id int + ); + INSERT INTO "object_changes" VALUES(1551,1,114608,'exported_pools',1,2114); + INSERT INTO "object_changes" VALUES(2048,1,114608,'exported_pools',2,2319); + CREATE TABLE timeline ( + rowid integer primary key, + timestamp text, + system_id int, + entry_type int, + entry_id int + ); + INSERT INTO "timeline" VALUES(6735,'2010-11-21 17:08:27.000',1,300,2048); + INSERT INTO "timeline" VALUES(6825,'2010-11-21 17:09:21.000',1,300,2114); + SELECT entry_type, + entry_types.name, + entry_id + FROM timeline JOIN entry_types ON entry_type = entry_types.id + WHERE (entry_types.name = 'cli_command' AND entry_id=2114) + OR (entry_types.name = 'object_change' + AND entry_id IN (SELECT change_id + FROM object_changes + WHERE obj_context = 'exported_pools')); + } +} {300 object_change 2048} +do_test tkt-80ba2-201 { + db eval { + CREATE INDEX timeline_entry_id_idx on timeline(entry_id); + SELECT entry_type, + entry_types.name, + entry_id + FROM timeline JOIN entry_types ON entry_type = entry_types.id + WHERE (entry_types.name = 'cli_command' AND entry_id=2114) + OR (entry_types.name = 'object_change' + AND entry_id IN (SELECT change_id + FROM object_changes + WHERE obj_context = 'exported_pools')); + } +} {300 object_change 2048} +do_test tkt-80ba2-202 { + optimization_control db factor-constants 0 + db cache flush + db eval { + SELECT entry_type, + entry_types.name, + entry_id + FROM timeline JOIN entry_types ON entry_type = entry_types.id + WHERE (entry_types.name = 'cli_command' AND entry_id=2114) + OR (entry_types.name = 'object_change' + AND entry_id IN (SELECT change_id + FROM object_changes + WHERE obj_context = 'exported_pools')); + } +} {300 object_change 2048} + +#------------------------------------------------------------------------- +# + +drop_all_tables +do_execsql_test 301 { + CREATE TABLE t1(a, b, c); + CREATE INDEX i1 ON t1(a); + CREATE INDEX i2 ON t1(b); + CREATE TABLE t2(d, e); + + INSERT INTO t1 VALUES('A', 'B', 'C'); + INSERT INTO t2 VALUES('D', 'E'); +} + +do_execsql_test 302 { + SELECT * FROM t1, t2 WHERE + (a='A' AND d='E') OR + (b='B' AND c IN ('C', 'D', 'E')) +} {A B C D E} + +do_execsql_test 303 { + SELECT * FROM t1, t2 WHERE + (a='A' AND d='E') OR + (b='B' AND c IN (SELECT c FROM t1)) +} {A B C D E} + +do_execsql_test 304 { + SELECT * FROM t1, t2 WHERE + (a='A' AND d='E') OR + (b='B' AND c IN (SELECT 'B' UNION SELECT 'C' UNION SELECT 'D')) +} {A B C D E} + +do_execsql_test 305 { + SELECT * FROM t1, t2 WHERE + (b='B' AND c IN ('C', 'D', 'E')) OR + (a='A' AND d='E') +} {A B C D E} + +do_execsql_test 306 { + SELECT * FROM t1, t2 WHERE + (b='B' AND c IN (SELECT c FROM t1)) OR + (a='A' AND d='E') +} {A B C D E} + +do_execsql_test 307 { + SELECT * FROM t1, t2 WHERE + (b='B' AND c IN (SELECT 'B' UNION SELECT 'C' UNION SELECT 'D')) OR + (a='A' AND d='E') +} {A B C D E} + +finish_test Index: test/triggerC.test ================================================================== --- test/triggerC.test +++ test/triggerC.test @@ -935,8 +935,20 @@ if {$a == 3} { execsql { DROP TRIGGER tr1 } } } execsql { SELECT count(*) FROM sqlite_master } } {1} +do_execsql_test triggerC-13.1 { + PRAGMA recursive_triggers = ON; + CREATE TABLE t12(a, b); + INSERT INTO t12 VALUES(1, 2); + CREATE TRIGGER tr12 AFTER UPDATE ON t12 BEGIN + UPDATE t12 SET a=new.a+1, b=new.b+1; + END; +} {} +do_catchsql_test triggerC-13.2 { + UPDATE t12 SET a=a+1, b=b+1; +} {1 {too many levels of trigger recursion}} + finish_test Index: test/wal2.test ================================================================== --- test/wal2.test +++ test/wal2.test @@ -16,10 +16,12 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl source $testdir/lock_common.tcl source $testdir/malloc_common.tcl source $testdir/wal_common.tcl + +set testprefix wal2 ifcapable !wal {finish_test ; return } if { ![wal_is_ok] || [path_is_dos "."]} { finish_test return @@ -1154,11 +1156,11 @@ catchsql { INSERT INTO t1 DEFAULT VALUES } } $b($can_read,$can_write) } catch { db close } } -} +} #------------------------------------------------------------------------- # Test that "PRAGMA checkpoint_fullsync" appears to be working. # foreach {tn sql reslist} { @@ -1208,9 +1210,75 @@ execsql { INSERT INTO t1 VALUES(13, 14) } db close list $sqlite_sync_count $sqlite_fullsync_count } [lrange $reslist 4 5] } + +catch { db close } + +# PRAGMA checkpoint_fullsync +# PRAGMA fullfsync +# PRAGMA synchronous +# +foreach {tn settings commit_sync ckpt_sync} { + 1 {0 0 off} {0 0} {0 0} + 2 {0 0 normal} {0 0} {2 0} + 3 {0 0 full} {1 0} {2 0} + + 4 {0 1 off} {0 0} {0 0} + 5 {0 1 normal} {0 0} {0 2} + 6 {0 1 full} {0 1} {0 2} + + 7 {1 0 off} {0 0} {0 0} + 8 {1 0 normal} {0 0} {0 2} + 9 {1 0 full} {1 0} {0 2} + + 10 {1 1 off} {0 0} {0 0} + 11 {1 1 normal} {0 0} {0 2} + 12 {1 1 full} {0 1} {0 2} +} { + forcedelete test.db + + testvfs tvfs -default 1 + tvfs filter xSync + tvfs script xSyncCb + proc xSyncCb {method file fileid flags} { + incr ::sync($flags) + } + + sqlite3 db test.db + do_execsql_test 15.$tn.1 " + CREATE TABLE t1(x); + PRAGMA journal_mode = WAL; + PRAGMA checkpoint_fullfsync = [lindex $settings 0]; + PRAGMA fullfsync = [lindex $settings 1]; + PRAGMA synchronous = [lindex $settings 2]; + " {wal} + + do_test 15.$tn.2 { + set sync(normal) 0 + set sync(full) 0 + execsql { INSERT INTO t1 VALUES('abc') } + list $::sync(normal) $::sync(full) + } $commit_sync + + do_test 15.$tn.3 { + set sync(normal) 0 + set sync(full) 0 + execsql { INSERT INTO t1 VALUES('def') } + list $::sync(normal) $::sync(full) + } $commit_sync + + do_test 15.$tn.4 { + set sync(normal) 0 + set sync(full) 0 + execsql { PRAGMA wal_checkpoint } + list $::sync(normal) $::sync(full) + } $ckpt_sync + + db close + tvfs delete +} finish_test Index: test/wal3.test ================================================================== --- test/wal3.test +++ test/wal3.test @@ -705,10 +705,12 @@ # This test case verifies that if an exclusive lock cannot be obtained # on any aReadMark[] slot (because there are already several readers), # the client takes a shared-lock on a slot without modifying the value # and continues. # +set nConn 50 +if { [string match *BSD $tcl_platform(os)] } { set nConn 35 } do_test wal3-9.0 { file delete -force test.db test.db-journal test.db wal sqlite3 db test.db execsql { PRAGMA page_size = 1024; @@ -715,11 +717,11 @@ PRAGMA journal_mode = WAL; CREATE TABLE whoami(x); INSERT INTO whoami VALUES('nobody'); } } {wal} -for {set i 0} {$i < 50} {incr i} { +for {set i 0} {$i < $nConn} {incr i} { set c db$i do_test wal3-9.1.$i { sqlite3 $c test.db execsql { UPDATE whoami SET x = $c } execsql { @@ -726,25 +728,25 @@ BEGIN; SELECT * FROM whoami } $c } $c } -for {set i 0} {$i < 50} {incr i} { +for {set i 0} {$i < $nConn} {incr i} { set c db$i do_test wal3-9.2.$i { execsql { SELECT * FROM whoami } $c } $c } set sz [expr 1024 * (2+$AUTOVACUUM)] do_test wal3-9.3 { - for {set i 0} {$i < 49} {incr i} { db$i close } + for {set i 0} {$i < ($nConn-1)} {incr i} { db$i close } execsql { PRAGMA wal_checkpoint } byte_is_zero test.db [expr $sz-1024] } {1} do_test wal3-9.4 { - db49 close + db[expr $nConn-1] close execsql { PRAGMA wal_checkpoint } set sz2 [file size test.db] byte_is_zero test.db [expr $sz-1024] } {0} ADDED test/wal6.test Index: test/wal6.test ================================================================== --- /dev/null +++ test/wal6.test @@ -0,0 +1,90 @@ +# 2010 December 1 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/wal_common.tcl +source $testdir/malloc_common.tcl +ifcapable !wal {finish_test ; return } + +#------------------------------------------------------------------------- +# Changing to WAL mode in one connection forces the change in others. +# +db close +forcedelete test.db + +set all_journal_modes {delete persist truncate memory off} +foreach jmode $all_journal_modes { + + do_test wal6-1.0.$jmode { + sqlite3 db test.db + execsql "PRAGMA journal_mode = $jmode;" + } $jmode + + do_test wal6-1.1.$jmode { + execsql { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1,2); + SELECT * FROM t1; + } + } {1 2} + +# Under Windows, you'll get an error trying to delete +# a file this is already opened. For now, make sure +# we get that error, then close the first connection +# so the other tests work. +if {$tcl_platform(platform)=="windows"} { + if {$jmode=="persist" || $jmode=="truncate"} { + do_test wal6-1.2.$jmode.win { + sqlite3 db2 test.db + catchsql { + PRAGMA journal_mode=WAL; + } db2 + } {1 {disk I/O error}} + db2 close + db close + } +} + + do_test wal6-1.2.$jmode { + sqlite3 db2 test.db + execsql { + PRAGMA journal_mode=WAL; + INSERT INTO t1 VALUES(3,4); + SELECT * FROM t1 ORDER BY a; + } db2 + } {wal 1 2 3 4} + +if {$tcl_platform(platform)=="windows"} { + if {$jmode=="persist" || $jmode=="truncate"} { + sqlite3 db test.db + } +} + + do_test wal6-1.3.$jmode { + execsql { + SELECT * FROM t1 ORDER BY a; + } + } {1 2 3 4} + + db close + db2 close + forcedelete test.db + +} + +finish_test + Index: tool/lemon.c ================================================================== --- tool/lemon.c +++ tool/lemon.c @@ -18,11 +18,17 @@ # define __WIN32__ # endif #endif #ifdef __WIN32__ -extern int access(); +#ifdef __cplusplus +extern "C" { +#endif +extern int access(const char *path, int mode); +#ifdef __cplusplus +} +#endif #else #include #endif /* #define PRIVATE static */ @@ -3261,11 +3267,11 @@ used += n; assert( used>=0 ); } n = lemonStrlen(zText); } - if( n+sizeof(zInt)*2+used >= alloced ){ + if( (int) (n+sizeof(zInt)*2+used) >= alloced ){ alloced = n + sizeof(zInt)*2 + used + 200; z = (char *) realloc(z, alloced); } if( z==0 ) return empty; while( n-- > 0 ){