2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** 2014 May 31
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fts5Int.h"
|
|
|
|
#include "fts5parse.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
** All token types in the generated fts5parse.h file are greater than 0.
|
|
|
|
*/
|
|
|
|
#define FTS5_EOF 0
|
|
|
|
|
|
|
|
typedef struct Fts5ExprTerm Fts5ExprTerm;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Functions generated by lemon from fts5parse.y.
|
|
|
|
*/
|
|
|
|
void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(size_t));
|
|
|
|
void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
|
|
|
|
void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
|
|
|
|
|
2014-06-26 00:28:38 +04:00
|
|
|
struct Fts5Expr {
|
|
|
|
Fts5Index *pIndex;
|
|
|
|
Fts5ExprNode *pRoot;
|
|
|
|
int bAsc;
|
|
|
|
};
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** eType:
|
|
|
|
** Expression node type. Always one of:
|
|
|
|
**
|
|
|
|
** FTS5_AND (pLeft, pRight valid)
|
|
|
|
** FTS5_OR (pLeft, pRight valid)
|
|
|
|
** FTS5_NOT (pLeft, pRight valid)
|
|
|
|
** FTS5_STRING (pNear valid)
|
|
|
|
*/
|
2014-06-26 00:28:38 +04:00
|
|
|
struct Fts5ExprNode {
|
2014-06-23 15:33:22 +04:00
|
|
|
int eType; /* Node type */
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5ExprNode *pLeft; /* Left hand child node */
|
|
|
|
Fts5ExprNode *pRight; /* Right hand child node */
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
|
2014-06-26 00:28:38 +04:00
|
|
|
int bEof; /* True at EOF */
|
|
|
|
i64 iRowid;
|
2014-06-23 15:33:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** An instance of the following structure represents a single search term
|
|
|
|
** or term prefix.
|
|
|
|
*/
|
|
|
|
struct Fts5ExprTerm {
|
|
|
|
int bPrefix; /* True for a prefix term */
|
|
|
|
char *zTerm; /* nul-terminated term */
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5IndexIter *pIter; /* Iterator for this term */
|
2014-06-23 15:33:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** A phrase. One or more terms that must appear in a contiguous sequence
|
|
|
|
** within a document for it to match.
|
|
|
|
*/
|
|
|
|
struct Fts5ExprPhrase {
|
2014-07-03 00:18:49 +04:00
|
|
|
Fts5Buffer poslist; /* Current position list */
|
|
|
|
i64 iRowid; /* Current rowid */
|
2014-06-23 15:33:22 +04:00
|
|
|
int nTerm; /* Number of entries in aTerm[] */
|
|
|
|
Fts5ExprTerm aTerm[0]; /* Terms that make up this phrase */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** One or more phrases that must appear within a certain token distance of
|
|
|
|
** each other within each matching document.
|
|
|
|
*/
|
|
|
|
struct Fts5ExprNearset {
|
|
|
|
int nNear; /* NEAR parameter */
|
|
|
|
int iCol; /* Column to search (-1 -> all columns) */
|
|
|
|
int nPhrase; /* Number of entries in aPhrase[] array */
|
|
|
|
Fts5ExprPhrase *apPhrase[0]; /* Array of phrase pointers */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Parse context.
|
|
|
|
*/
|
|
|
|
struct Fts5Parse {
|
|
|
|
Fts5Config *pConfig;
|
|
|
|
char *zErr;
|
|
|
|
int rc;
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5ExprNode *pExpr; /* Result of a successful parse */
|
2014-06-23 15:33:22 +04:00
|
|
|
};
|
|
|
|
|
2014-07-03 00:18:49 +04:00
|
|
|
/*************************************************************************
|
|
|
|
*/
|
|
|
|
typedef struct Fts5PoslistIter Fts5PoslistIter;
|
|
|
|
struct Fts5PoslistIter {
|
|
|
|
const u8 *a; /* Position list to iterate through */
|
|
|
|
int n; /* Size of buffer at a[] in bytes */
|
|
|
|
int i; /* Current offset in a[] */
|
|
|
|
|
|
|
|
/* Output variables */
|
|
|
|
int bEof; /* Set to true at EOF */
|
|
|
|
i64 iPos; /* (iCol<<32) + iPos */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void fts5PoslistIterNext(Fts5PoslistIter *pIter){
|
|
|
|
if( pIter->i>=pIter->n ){
|
|
|
|
pIter->bEof = 1;
|
|
|
|
}else{
|
|
|
|
int iVal;
|
|
|
|
pIter->i += getVarint32(&pIter->a[pIter->i], iVal);
|
|
|
|
if( iVal==1 ){
|
|
|
|
pIter->i += getVarint32(&pIter->a[pIter->i], iVal);
|
|
|
|
pIter->iPos = ((u64)iVal << 32);
|
|
|
|
pIter->i += getVarint32(&pIter->a[pIter->i], iVal);
|
|
|
|
}
|
|
|
|
pIter->iPos += (iVal-2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fts5PoslistIterInit(const u8 *a, int n, Fts5PoslistIter *pIter){
|
|
|
|
memset(pIter, 0, sizeof(*pIter));
|
|
|
|
pIter->a = a;
|
|
|
|
pIter->n = n;
|
|
|
|
fts5PoslistIterNext(pIter);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
*************************************************************************/
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
|
|
|
|
if( pParse->rc==SQLITE_OK ){
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, zFmt);
|
|
|
|
pParse->zErr = sqlite3_vmprintf(zFmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
pParse->rc = SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5ExprIsspace(char t){
|
|
|
|
return t==' ' || t=='\t' || t=='\n' || t=='\r';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5ExprIstoken(char t){
|
|
|
|
return fts5ExprIsspace(t)==0 && t!='\0'
|
|
|
|
&& t!=':' && t!='(' && t!=')'
|
|
|
|
&& t!=',' && t!='+' && t!='*';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Read the first token from the nul-terminated string at *pz.
|
|
|
|
*/
|
|
|
|
static int fts5ExprGetToken(
|
|
|
|
Fts5Parse *pParse,
|
|
|
|
const char **pz, /* IN/OUT: Pointer into buffer */
|
|
|
|
Fts5Token *pToken
|
|
|
|
){
|
|
|
|
const char *z = *pz;
|
|
|
|
int tok;
|
|
|
|
|
|
|
|
/* Skip past any whitespace */
|
|
|
|
while( fts5ExprIsspace(*z) ) z++;
|
|
|
|
|
|
|
|
pToken->p = z;
|
|
|
|
pToken->n = 1;
|
|
|
|
switch( *z ){
|
|
|
|
case '(': tok = FTS5_LP; break;
|
|
|
|
case ')': tok = FTS5_RP; break;
|
|
|
|
case ':': tok = FTS5_COLON; break;
|
|
|
|
case ',': tok = FTS5_COMMA; break;
|
|
|
|
case '+': tok = FTS5_PLUS; break;
|
|
|
|
case '*': tok = FTS5_STAR; break;
|
|
|
|
case '\0': tok = FTS5_EOF; break;
|
|
|
|
|
|
|
|
case '"': {
|
|
|
|
const char *z2;
|
|
|
|
tok = FTS5_STRING;
|
|
|
|
|
|
|
|
for(z2=&z[1]; 1; z2++){
|
|
|
|
if( z2[0]=='"' ){
|
|
|
|
z2++;
|
|
|
|
if( z2[0]!='"' ) break;
|
|
|
|
}
|
|
|
|
if( z2[0]=='\0' ){
|
|
|
|
sqlite3Fts5ParseError(pParse, "unterminated string");
|
|
|
|
return FTS5_EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pToken->n = (z2 - z);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
const char *z2;
|
|
|
|
tok = FTS5_STRING;
|
|
|
|
for(z2=&z[1]; fts5ExprIstoken(*z2); z2++);
|
|
|
|
pToken->n = (z2 - z);
|
|
|
|
if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR;
|
|
|
|
if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
|
|
|
|
if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*pz = &pToken->p[pToken->n];
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *fts5ParseAlloc(size_t t){ return sqlite3_malloc((int)t); }
|
|
|
|
static void fts5ParseFree(void *p){ sqlite3_free(p); }
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprNew(
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5Config *pConfig, /* FTS5 Configuration */
|
2014-06-23 15:33:22 +04:00
|
|
|
const char *zExpr, /* Expression text */
|
|
|
|
Fts5Expr **ppNew,
|
|
|
|
char **pzErr
|
|
|
|
){
|
|
|
|
Fts5Parse sParse;
|
|
|
|
Fts5Token token;
|
|
|
|
const char *z = zExpr;
|
|
|
|
int t; /* Next token type */
|
|
|
|
void *pEngine;
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5Expr *pNew;
|
2014-06-23 15:33:22 +04:00
|
|
|
|
|
|
|
*ppNew = 0;
|
|
|
|
*pzErr = 0;
|
|
|
|
memset(&sParse, 0, sizeof(sParse));
|
|
|
|
pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
|
2014-06-26 00:28:38 +04:00
|
|
|
if( pEngine==0 ){ return SQLITE_NOMEM; }
|
2014-06-23 15:33:22 +04:00
|
|
|
sParse.pConfig = pConfig;
|
|
|
|
|
|
|
|
do {
|
|
|
|
t = fts5ExprGetToken(&sParse, &z, &token);
|
|
|
|
sqlite3Fts5Parser(pEngine, t, token, &sParse);
|
|
|
|
}while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
|
|
|
|
sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
|
|
|
|
|
|
|
|
assert( sParse.pExpr==0 || (sParse.rc==SQLITE_OK && sParse.zErr==0) );
|
2014-06-26 00:28:38 +04:00
|
|
|
if( sParse.rc==SQLITE_OK ){
|
|
|
|
*ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
|
|
|
|
if( pNew==0 ){
|
|
|
|
sParse.rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
pNew->pRoot = sParse.pExpr;
|
|
|
|
pNew->pIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
*pzErr = sParse.zErr;
|
|
|
|
return sParse.rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-06-26 00:28:38 +04:00
|
|
|
** Free the expression node object passed as the only argument.
|
2014-06-23 15:33:22 +04:00
|
|
|
*/
|
2014-06-26 00:28:38 +04:00
|
|
|
void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
|
2014-06-23 15:33:22 +04:00
|
|
|
if( p ){
|
2014-06-26 00:28:38 +04:00
|
|
|
sqlite3Fts5ParseNodeFree(p->pLeft);
|
|
|
|
sqlite3Fts5ParseNodeFree(p->pRight);
|
2014-06-23 15:33:22 +04:00
|
|
|
sqlite3Fts5ParseNearsetFree(p->pNear);
|
|
|
|
sqlite3_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 00:28:38 +04:00
|
|
|
/*
|
|
|
|
** Free the expression object passed as the only argument.
|
|
|
|
*/
|
|
|
|
void sqlite3Fts5ExprFree(Fts5Expr *p){
|
|
|
|
if( p ){
|
|
|
|
sqlite3Fts5ParseNodeFree(p->pRoot);
|
|
|
|
sqlite3_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
static int fts5ExprNodeTest(Fts5Expr *pExpr, Fts5ExprNode *pNode){
|
|
|
|
assert( 0 );
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2014-07-03 00:18:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** All individual term iterators in pPhrase are guaranteed to be valid and
|
|
|
|
** pointing to the same rowid when this function is called. This function
|
|
|
|
** checks if the current rowid really is a match, and if so populates
|
|
|
|
** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
|
|
|
|
** is set to true if this is really a match, or false otherwise.
|
|
|
|
**
|
|
|
|
** SQLITE_OK is returned if an error occurs, or an SQLite error code
|
|
|
|
** otherwise. It is not considered an error code if the current rowid is
|
|
|
|
** not a match.
|
|
|
|
*/
|
|
|
|
static int fts5ExprPhraseIsMatch(
|
|
|
|
Fts5Expr *pExpr, /* Expression pPhrase belongs to */
|
|
|
|
Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
|
|
|
|
int *pbMatch /* OUT: Set to true if really a match */
|
|
|
|
){
|
|
|
|
Fts5PoslistIter aStatic[4];
|
|
|
|
Fts5PoslistIter *aIter = aStatic;
|
|
|
|
int i;
|
2014-06-26 00:28:38 +04:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
2014-07-03 00:18:49 +04:00
|
|
|
if( pPhrase->nTerm>(sizeof(aStatic) / sizeof(aStatic[0])) ){
|
|
|
|
int nByte = sizeof(Fts5PoslistIter) * pPhrase->nTerm;
|
|
|
|
aIter = (Fts5PoslistIter*)sqlite3_malloc(nByte);
|
|
|
|
if( !aIter ) return SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a term iterator for each term in the phrase */
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){
|
|
|
|
int n;
|
|
|
|
const u8 *a = sqlite3Fts5IterPoslist(pPhrase->aTerm[i].pIter, &n);
|
|
|
|
fts5PoslistIterInit(a, n, &aIter[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pbMatch = 0;
|
|
|
|
while( 1 ){
|
|
|
|
|
|
|
|
int bMatch = 1;
|
|
|
|
i64 iPos = aIter[0].iPos;
|
|
|
|
for(i=1; i<pPhrase->nTerm; i++){
|
|
|
|
Fts5PoslistIter *pPos = &aIter[i];
|
|
|
|
i64 iAdj = pPos->iPos-i;
|
|
|
|
if( (pPos->iPos-i)!=iPos ){
|
|
|
|
bMatch = 0;
|
|
|
|
if( iAdj>iPos ) iPos = iAdj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( bMatch ){
|
|
|
|
*pbMatch = 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-06-26 00:28:38 +04:00
|
|
|
|
2014-07-03 00:18:49 +04:00
|
|
|
for(i=0; i<pPhrase->nTerm; i++){
|
|
|
|
Fts5PoslistIter *pPos = &aIter[i];
|
|
|
|
while( (pPos->iPos-i) < iPos ){
|
|
|
|
fts5PoslistIterNext(pPos);
|
|
|
|
if( pPos->bEof ) goto ismatch_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ismatch_out:
|
|
|
|
if( aIter!=aStatic ) sqlite3_free(aIter);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** All individual term iterators in pPhrase are guaranteed to be valid when
|
|
|
|
** this function is called. This function checks if all term iterators
|
|
|
|
** point to the same rowid, and if not, advances them until they do.
|
|
|
|
** If an EOF is reached before this happens, *pbEof is set to true before
|
|
|
|
** returning.
|
|
|
|
**
|
|
|
|
** SQLITE_OK is returned if an error occurs, or an SQLite error code
|
|
|
|
** otherwise. It is not considered an error code if an iterator reaches
|
|
|
|
** EOF.
|
|
|
|
*/
|
|
|
|
static int fts5ExprPhraseNextRowidMatch(
|
|
|
|
Fts5Expr *pExpr, /* Expression pPhrase belongs to */
|
|
|
|
Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
|
|
|
|
int *pbEof /* OUT: Set to true if phrase at EOF */
|
|
|
|
){
|
|
|
|
assert( *pbEof==0 );
|
|
|
|
while( 1 ){
|
|
|
|
int i;
|
|
|
|
int bMatch = 1;
|
|
|
|
i64 iMin = sqlite3Fts5IterRowid(pPhrase->aTerm[0].pIter);
|
|
|
|
for(i=1; i<pPhrase->nTerm; i++){
|
|
|
|
i64 iRowid = sqlite3Fts5IterRowid(pPhrase->aTerm[i].pIter);
|
|
|
|
if( iRowid!=iMin ){
|
|
|
|
bMatch = 0;
|
|
|
|
if( iRowid<iMin ) iMin = iRowid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( bMatch ) break;
|
|
|
|
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){
|
|
|
|
Fts5IndexIter *pIter = pPhrase->aTerm[i].pIter;
|
|
|
|
while( sqlite3Fts5IterRowid(pIter)>iMin ){
|
|
|
|
sqlite3Fts5IterNext(pIter, 0);
|
|
|
|
if( sqlite3Fts5IterEof(pIter) ){
|
|
|
|
*pbEof = 1;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5ExprPhraseAdvanceAll(
|
|
|
|
Fts5Expr *pExpr, /* Expression pPhrase belongs to */
|
|
|
|
Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
|
|
|
|
int *pbEof /* OUT: Set to true if phrase at EOF */
|
|
|
|
){
|
|
|
|
int i;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){
|
|
|
|
Fts5IndexIter *pIter = pPhrase->aTerm[i].pIter;
|
|
|
|
sqlite3Fts5IterNext(pIter, 0);
|
|
|
|
if( sqlite3Fts5IterEof(pIter) ){
|
|
|
|
*pbEof = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Argument pPhrase points to a multi-term phrase object. All individual
|
|
|
|
** term iterators point to valid entries (not EOF).
|
|
|
|
*
|
|
|
|
** This function tests if the term iterators currently all point to the
|
|
|
|
** same rowid, and if so, if the rowid matches the phrase constraint. If
|
|
|
|
** so, the pPhrase->poslist buffer is populated and the pPhrase->iRowid
|
|
|
|
** variable set before returning. Or, if the current combination of
|
|
|
|
** iterators is not a match, they are advanced until they are. If one of
|
|
|
|
** the iterators reaches EOF before a match is found, *pbEof is set to
|
|
|
|
** true before returning. The final values of the pPhrase->poslist and
|
|
|
|
** iRowid fields are undefined in this case.
|
|
|
|
**
|
|
|
|
** SQLITE_OK is returned if an error occurs, or an SQLite error code
|
|
|
|
** otherwise. It is not considered an error code if an iterator reaches
|
|
|
|
** EOF.
|
|
|
|
*/
|
|
|
|
static int fts5ExprPhraseNextMatch(
|
|
|
|
Fts5Expr *pExpr, /* Expression pPhrase belongs to */
|
|
|
|
Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
|
|
|
|
int *pbEof /* OUT: Set to true if phrase at EOF */
|
|
|
|
){
|
|
|
|
int i; /* Used to iterate through terms */
|
|
|
|
int rc = SQLITE_OK; /* Return code */
|
|
|
|
int bMatch = 0;
|
|
|
|
|
|
|
|
assert( *pbEof==0 );
|
|
|
|
|
|
|
|
while( 1 ){
|
|
|
|
rc = fts5ExprPhraseNextRowidMatch(pExpr, pPhrase, pbEof);
|
|
|
|
if( rc!=SQLITE_OK || *pbEof ) break;
|
|
|
|
|
|
|
|
/* At this point, all term iterators are valid and point to the same rowid.
|
|
|
|
** The following assert() statements verify this. */
|
|
|
|
#ifdef SQLITE_DEBUG
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){
|
|
|
|
Fts5IndexIter *pIter = pPhrase->aTerm[i].pIter;
|
|
|
|
Fts5IndexIter *pOne = pPhrase->aTerm[0].pIter;
|
|
|
|
assert( 0==sqlite3Fts5IterEof(pIter) );
|
|
|
|
assert( sqlite3Fts5IterRowid(pOne)==sqlite3Fts5IterRowid(pIter) );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rc = fts5ExprPhraseIsMatch(pExpr, pPhrase, &bMatch);
|
|
|
|
if( rc!=SQLITE_OK || bMatch ) break;
|
|
|
|
rc = fts5ExprPhraseAdvanceAll(pExpr, pPhrase, pbEof);
|
|
|
|
if( rc!=SQLITE_OK || *pbEof ) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pPhrase->iRowid = sqlite3Fts5IterRowid(pPhrase->aTerm[0].pIter);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Advance the phrase iterator pPhrase to the next match.
|
|
|
|
*/
|
|
|
|
static int fts5ExprPhraseNext(
|
|
|
|
Fts5Expr *pExpr, /* Expression pPhrase belongs to */
|
|
|
|
Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
|
|
|
|
int *pbEof /* OUT: Set to true if phrase at EOF */
|
|
|
|
){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){
|
|
|
|
Fts5IndexIter *pIter = pPhrase->aTerm[i].pIter;
|
|
|
|
sqlite3Fts5IterNext(pIter, 0);
|
|
|
|
if( sqlite3Fts5IterEof(pIter) ){
|
|
|
|
*pbEof = 1;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pPhrase->nTerm==1 ){
|
|
|
|
pPhrase->iRowid = sqlite3Fts5IterRowid(pPhrase->aTerm[0].pIter);
|
|
|
|
}else{
|
|
|
|
fts5ExprPhraseNextMatch(pExpr, pPhrase, pbEof);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Point phrase object pPhrase at the first matching document. Or, if there
|
|
|
|
** are no matching documents at all, move pPhrase to EOF and set *pbEof to
|
|
|
|
** true before returning.
|
|
|
|
**
|
|
|
|
** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
|
|
|
|
** code.
|
|
|
|
*/
|
|
|
|
static int fts5ExprPhraseFirst(
|
|
|
|
Fts5Expr *pExpr, /* Expression pPhrase belongs to */
|
|
|
|
Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
|
|
|
|
int *pbEof /* OUT: Set to true if phrase at EOF */
|
|
|
|
){
|
|
|
|
int i; /* Used to iterate through terms */
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){
|
|
|
|
Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
|
2014-06-26 00:28:38 +04:00
|
|
|
pTerm->pIter = sqlite3Fts5IndexQuery(
|
|
|
|
pExpr->pIndex, pTerm->zTerm, strlen(pTerm->zTerm),
|
|
|
|
(pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
|
|
|
|
(pExpr->bAsc ? FTS5INDEX_QUERY_ASC : 0)
|
|
|
|
);
|
|
|
|
if( sqlite3Fts5IterEof(pTerm->pIter) ){
|
2014-07-03 00:18:49 +04:00
|
|
|
*pbEof = 1;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pPhrase->nTerm==1 ){
|
|
|
|
const u8 *a; int n;
|
|
|
|
Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
|
|
|
|
pPhrase->iRowid = sqlite3Fts5IterRowid(pIter);
|
|
|
|
a = sqlite3Fts5IterPoslist(pIter, &n);
|
|
|
|
if( a ){
|
|
|
|
sqlite3Fts5BufferSet(&rc, &pPhrase->poslist, n, a);
|
2014-06-26 00:28:38 +04:00
|
|
|
}
|
2014-07-03 00:18:49 +04:00
|
|
|
}else{
|
|
|
|
rc = fts5ExprPhraseNextMatch(pExpr, pPhrase, pbEof);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
|
|
|
|
int rc = SQLITE_OK;
|
2014-06-26 00:28:38 +04:00
|
|
|
|
2014-07-03 00:18:49 +04:00
|
|
|
pNode->bEof = 0;
|
|
|
|
if( pNode->eType==FTS5_STRING ){
|
|
|
|
Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
|
|
|
|
assert( pNode->pNear->nPhrase==1 );
|
|
|
|
assert( pNode->bEof==0 );
|
|
|
|
rc = fts5ExprPhraseFirst(pExpr, pPhrase, &pNode->bEof);
|
|
|
|
pNode->iRowid = pPhrase->iRowid;
|
2014-06-26 00:28:38 +04:00
|
|
|
}else{
|
|
|
|
rc = fts5ExprNodeFirst(pExpr, pNode->pLeft);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5ExprNodeFirst(pExpr, pNode->pRight);
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5ExprNodeTest(pExpr, pNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5ExprNodeNext(Fts5Expr *pExpr, Fts5ExprNode *pNode){
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
|
|
|
if( pNode->eType==FTS5_STRING ){
|
|
|
|
Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
|
2014-07-03 00:18:49 +04:00
|
|
|
assert( pNode->pNear->nPhrase==1 );
|
|
|
|
rc = fts5ExprPhraseNext(pExpr, pPhrase, &pNode->bEof);
|
|
|
|
pNode->iRowid = pPhrase->iRowid;
|
2014-06-26 00:28:38 +04:00
|
|
|
}else{
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Begin iterating through the set of documents in index pIdx matched by
|
|
|
|
** the MATCH expression passed as the first argument. If the "bAsc" parameter
|
|
|
|
** is passed a non-zero value, iteration is in ascending rowid order. Or,
|
|
|
|
** if it is zero, in descending order.
|
|
|
|
**
|
|
|
|
** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
|
|
|
|
** is not considered an error if the query does not match any documents.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, int bAsc){
|
|
|
|
int rc;
|
|
|
|
p->pIndex = pIdx;
|
|
|
|
p->bAsc = bAsc;
|
|
|
|
rc = fts5ExprNodeFirst(p, p->pRoot);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Move to the next document
|
|
|
|
**
|
|
|
|
** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
|
|
|
|
** is not considered an error if the query does not match any documents.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5ExprNext(Fts5Expr *p){
|
|
|
|
int rc;
|
|
|
|
rc = fts5ExprNodeNext(p, p->pRoot);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprEof(Fts5Expr *p){
|
|
|
|
return p->pRoot->bEof;
|
|
|
|
}
|
|
|
|
|
|
|
|
i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
|
|
|
|
return p->pRoot->iRowid;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** Argument pIn points to a buffer of nIn bytes. This function allocates
|
|
|
|
** and returns a new buffer populated with a copy of (pIn/nIn) with a
|
|
|
|
** nul-terminator byte appended to it.
|
|
|
|
**
|
|
|
|
** It is the responsibility of the caller to eventually free the returned
|
|
|
|
** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
|
|
|
|
*/
|
|
|
|
static char *fts5Strdup(const char *pIn, int nIn){
|
|
|
|
char *zRet = (char*)sqlite3_malloc(nIn+1);
|
|
|
|
if( zRet ){
|
|
|
|
memcpy(zRet, pIn, nIn);
|
|
|
|
zRet[nIn] = '\0';
|
|
|
|
}
|
|
|
|
return zRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
|
2014-06-26 00:28:38 +04:00
|
|
|
*pz = fts5Strdup(pToken->p, pToken->n);
|
2014-06-23 15:33:22 +04:00
|
|
|
if( *pz==0 ) return SQLITE_NOMEM;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Free the phrase object passed as the only argument.
|
|
|
|
*/
|
|
|
|
static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
|
|
|
|
if( pPhrase ){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
|
|
|
|
sqlite3_free(pTerm->zTerm);
|
|
|
|
if( pTerm->pIter ){
|
|
|
|
sqlite3Fts5IterClose(pTerm->pIter);
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2014-07-03 00:18:49 +04:00
|
|
|
fts5BufferFree(&pPhrase->poslist);
|
2014-06-23 15:33:22 +04:00
|
|
|
sqlite3_free(pPhrase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
|
|
|
|
** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
|
|
|
|
** appended to it and the results returned.
|
|
|
|
**
|
|
|
|
** If an OOM error occurs, both the pNear and pPhrase objects are freed and
|
|
|
|
** NULL returned.
|
|
|
|
*/
|
|
|
|
Fts5ExprNearset *sqlite3Fts5ParseNearset(
|
|
|
|
Fts5Parse *pParse, /* Parse context */
|
|
|
|
Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
|
|
|
|
Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
|
|
|
|
){
|
|
|
|
const int SZALLOC = 8;
|
|
|
|
Fts5ExprNearset *pRet = 0;
|
|
|
|
|
|
|
|
if( pParse->rc==SQLITE_OK ){
|
|
|
|
if( pNear==0 ){
|
|
|
|
int nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
|
|
|
|
pRet = sqlite3_malloc(nByte);
|
|
|
|
if( pRet==0 ){
|
|
|
|
pParse->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
memset(pRet, 0, nByte);
|
|
|
|
pRet->iCol = -1;
|
|
|
|
}
|
|
|
|
}else if( (pNear->nPhrase % SZALLOC)==0 ){
|
|
|
|
int nNew = pRet->nPhrase + SZALLOC;
|
|
|
|
int nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
|
|
|
|
|
|
|
|
pRet = (Fts5ExprNearset*)sqlite3_realloc(pNear, nByte);
|
|
|
|
if( pRet==0 ){
|
|
|
|
pParse->rc = SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
pRet = pNear;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pRet==0 ){
|
|
|
|
assert( pParse->rc!=SQLITE_OK );
|
|
|
|
sqlite3Fts5ParseNearsetFree(pNear);
|
|
|
|
sqlite3Fts5ParsePhraseFree(pPhrase);
|
|
|
|
}else{
|
|
|
|
pRet->apPhrase[pRet->nPhrase++] = pPhrase;
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct TokenCtx TokenCtx;
|
|
|
|
struct TokenCtx {
|
|
|
|
Fts5ExprPhrase *pPhrase;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Callback for tokenizing terms used by ParseTerm().
|
|
|
|
*/
|
|
|
|
static int fts5ParseTokenize(
|
|
|
|
void *pContext, /* Pointer to Fts5InsertCtx object */
|
|
|
|
const char *pToken, /* Buffer containing token */
|
|
|
|
int nToken, /* Size of token in bytes */
|
|
|
|
int iStart, /* Start offset of token */
|
|
|
|
int iEnd, /* End offset of token */
|
|
|
|
int iPos /* Position offset of token */
|
|
|
|
){
|
|
|
|
const int SZALLOC = 8;
|
|
|
|
TokenCtx *pCtx = (TokenCtx*)pContext;
|
|
|
|
Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
|
|
|
|
Fts5ExprTerm *pTerm;
|
|
|
|
|
|
|
|
if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
|
|
|
|
Fts5ExprPhrase *pNew;
|
|
|
|
int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
|
|
|
|
|
|
|
|
pNew = (Fts5ExprPhrase*)sqlite3_realloc(pPhrase,
|
|
|
|
sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
|
|
|
|
);
|
|
|
|
if( pNew==0 ) return SQLITE_NOMEM;
|
2014-07-03 00:18:49 +04:00
|
|
|
if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
|
2014-06-23 15:33:22 +04:00
|
|
|
pCtx->pPhrase = pPhrase = pNew;
|
|
|
|
pNew->nTerm = nNew - SZALLOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
|
2014-07-03 00:18:49 +04:00
|
|
|
memset(pTerm, 0, sizeof(Fts5ExprTerm));
|
2014-06-23 15:33:22 +04:00
|
|
|
pTerm->zTerm = fts5Strdup(pToken, nToken);
|
2014-06-26 16:31:41 +04:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
return pTerm->zTerm ? SQLITE_OK : SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Free the phrase object passed as the only argument.
|
|
|
|
*/
|
|
|
|
void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
|
|
|
|
fts5ExprPhraseFree(pPhrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Free the phrase object passed as the second argument.
|
|
|
|
*/
|
|
|
|
void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
|
|
|
|
if( pNear ){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pNear->nPhrase; i++){
|
|
|
|
fts5ExprPhraseFree(pNear->apPhrase[i]);
|
|
|
|
}
|
|
|
|
sqlite3_free(pNear);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 00:28:38 +04:00
|
|
|
void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
|
2014-06-23 15:33:22 +04:00
|
|
|
assert( pParse->pExpr==0 );
|
|
|
|
pParse->pExpr = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This function is called by the parser to process a string token. The
|
|
|
|
** string may or may not be quoted. In any case it is tokenized and a
|
|
|
|
** phrase object consisting of all tokens returned.
|
|
|
|
*/
|
|
|
|
Fts5ExprPhrase *sqlite3Fts5ParseTerm(
|
|
|
|
Fts5Parse *pParse, /* Parse context */
|
|
|
|
Fts5ExprPhrase *pPhrase, /* Phrase to append to */
|
|
|
|
Fts5Token *pToken, /* String to tokenize */
|
|
|
|
int bPrefix /* True if there is a trailing "*" */
|
|
|
|
){
|
|
|
|
Fts5Config *pConfig = pParse->pConfig;
|
|
|
|
TokenCtx sCtx; /* Context object passed to callback */
|
|
|
|
int rc; /* Tokenize return code */
|
|
|
|
char *z = 0;
|
|
|
|
|
|
|
|
pParse->rc = fts5ParseStringFromToken(pToken, &z);
|
|
|
|
if( z==0 ) return 0;
|
|
|
|
sqlite3Fts5Dequote(z);
|
|
|
|
|
|
|
|
memset(&sCtx, 0, sizeof(TokenCtx));
|
|
|
|
sCtx.pPhrase = pPhrase;
|
|
|
|
rc = sqlite3Fts5Tokenize(pConfig, z, strlen(z), &sCtx, fts5ParseTokenize);
|
|
|
|
if( rc ){
|
|
|
|
pParse->rc = rc;
|
|
|
|
fts5ExprPhraseFree(sCtx.pPhrase);
|
|
|
|
sCtx.pPhrase = 0;
|
|
|
|
}else if( sCtx.pPhrase->nTerm>0 ){
|
|
|
|
sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_free(z);
|
|
|
|
return sCtx.pPhrase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
|
|
|
|
if( pParse->rc==SQLITE_OK ){
|
|
|
|
if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
|
|
|
|
sqlite3Fts5ParseError(
|
2014-06-26 00:28:38 +04:00
|
|
|
pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
|
2014-06-23 15:33:22 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sqlite3Fts5ParseSetDistance(
|
|
|
|
Fts5Parse *pParse,
|
|
|
|
Fts5ExprNearset *pNear,
|
|
|
|
Fts5Token *p
|
|
|
|
){
|
|
|
|
int nNear = 0;
|
|
|
|
int i;
|
|
|
|
if( p->n ){
|
|
|
|
for(i=0; i<p->n; i++){
|
|
|
|
char c = (char)p->p[i];
|
|
|
|
if( c<'0' || c>'9' ){
|
|
|
|
sqlite3Fts5ParseError(
|
|
|
|
pParse, "expected integer, got \"%.*s\"", p->n, p->p
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nNear = nNear * 10 + (p->p[i] - '0');
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
nNear = FTS5_DEFAULT_NEARDIST;
|
|
|
|
}
|
|
|
|
pNear->nNear = nNear;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sqlite3Fts5ParseSetColumn(
|
|
|
|
Fts5Parse *pParse,
|
|
|
|
Fts5ExprNearset *pNear,
|
|
|
|
Fts5Token *p
|
|
|
|
){
|
|
|
|
char *z = 0;
|
|
|
|
int rc = fts5ParseStringFromToken(p, &z);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
Fts5Config *pConfig = pParse->pConfig;
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pConfig->nCol; i++){
|
|
|
|
if( 0==sqlite3_stricmp(pConfig->azCol[i], z) ){
|
|
|
|
pNear->iCol = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( i==pConfig->nCol ){
|
|
|
|
sqlite3Fts5ParseError(pParse, "no such column: %s", z);
|
|
|
|
}
|
|
|
|
sqlite3_free(z);
|
|
|
|
}else{
|
|
|
|
pParse->rc = rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Allocate and return a new expression object. If anything goes wrong (i.e.
|
|
|
|
** OOM error), leave an error code in pParse and return NULL.
|
|
|
|
*/
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5ExprNode *sqlite3Fts5ParseNode(
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5Parse *pParse, /* Parse context */
|
|
|
|
int eType, /* FTS5_STRING, AND, OR or NOT */
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5ExprNode *pLeft, /* Left hand child expression */
|
|
|
|
Fts5ExprNode *pRight, /* Right hand child expression */
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
|
|
|
|
){
|
2014-06-26 00:28:38 +04:00
|
|
|
Fts5ExprNode *pRet = 0;
|
2014-06-23 15:33:22 +04:00
|
|
|
|
|
|
|
if( pParse->rc==SQLITE_OK ){
|
|
|
|
assert( (eType!=FTS5_STRING && pLeft && pRight && !pNear)
|
|
|
|
|| (eType==FTS5_STRING && !pLeft && !pRight && pNear)
|
|
|
|
);
|
2014-06-26 00:28:38 +04:00
|
|
|
pRet = (Fts5ExprNode*)sqlite3_malloc(sizeof(Fts5ExprNode));
|
2014-06-23 15:33:22 +04:00
|
|
|
if( pRet==0 ){
|
|
|
|
pParse->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
memset(pRet, 0, sizeof(*pRet));
|
|
|
|
pRet->eType = eType;
|
|
|
|
pRet->pLeft = pLeft;
|
|
|
|
pRet->pRight = pRight;
|
|
|
|
pRet->pNear = pNear;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pRet==0 ){
|
|
|
|
assert( pParse->rc!=SQLITE_OK );
|
2014-06-26 00:28:38 +04:00
|
|
|
sqlite3Fts5ParseNodeFree(pLeft);
|
|
|
|
sqlite3Fts5ParseNodeFree(pRight);
|
2014-06-23 15:33:22 +04:00
|
|
|
sqlite3Fts5ParseNearsetFree(pNear);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
|
|
|
|
char *zQuoted = sqlite3_malloc(strlen(pTerm->zTerm) * 2 + 3 + 2);
|
|
|
|
if( zQuoted ){
|
|
|
|
int i = 0;
|
|
|
|
char *zIn = pTerm->zTerm;
|
|
|
|
zQuoted[i++] = '"';
|
|
|
|
while( *zIn ){
|
|
|
|
if( *zIn=='"' ) zQuoted[i++] = '"';
|
|
|
|
zQuoted[i++] = *zIn++;
|
|
|
|
}
|
|
|
|
zQuoted[i++] = '"';
|
|
|
|
if( pTerm->bPrefix ){
|
|
|
|
zQuoted[i++] = ' ';
|
|
|
|
zQuoted[i++] = '*';
|
|
|
|
}
|
|
|
|
zQuoted[i++] = '\0';
|
|
|
|
}
|
|
|
|
return zQuoted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
|
|
|
|
char *zNew;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, zFmt);
|
|
|
|
zNew = sqlite3_vmprintf(zFmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if( zApp ){
|
|
|
|
char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
|
|
|
|
sqlite3_free(zNew);
|
|
|
|
zNew = zNew2;
|
|
|
|
}
|
|
|
|
sqlite3_free(zApp);
|
|
|
|
return zNew;
|
|
|
|
}
|
|
|
|
|
2014-06-26 00:28:38 +04:00
|
|
|
static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
|
2014-06-23 15:33:22 +04:00
|
|
|
char *zRet = 0;
|
|
|
|
if( pExpr->eType==FTS5_STRING ){
|
|
|
|
Fts5ExprNearset *pNear = pExpr->pNear;
|
|
|
|
int i;
|
|
|
|
int iTerm;
|
|
|
|
|
|
|
|
if( pNear->iCol>=0 ){
|
|
|
|
zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[pNear->iCol]);
|
|
|
|
if( zRet==0 ) return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pNear->nPhrase>1 ){
|
|
|
|
zRet = fts5PrintfAppend(zRet, "NEAR(");
|
|
|
|
if( zRet==0 ) return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<pNear->nPhrase; i++){
|
|
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
|
|
|
|
if( i!=0 ){
|
|
|
|
zRet = fts5PrintfAppend(zRet, " ");
|
|
|
|
if( zRet==0 ) return 0;
|
|
|
|
}
|
|
|
|
for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
|
|
|
|
char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
|
|
|
|
if( zTerm ){
|
|
|
|
zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
|
|
|
|
sqlite3_free(zTerm);
|
|
|
|
}
|
|
|
|
if( zTerm==0 || zRet==0 ){
|
|
|
|
sqlite3_free(zRet);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pNear->nPhrase>1 ){
|
|
|
|
zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
|
|
|
|
if( zRet==0 ) return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
char *zOp = 0;
|
|
|
|
char *z1 = 0;
|
|
|
|
char *z2 = 0;
|
|
|
|
switch( pExpr->eType ){
|
|
|
|
case FTS5_AND: zOp = "AND"; break;
|
|
|
|
case FTS5_NOT: zOp = "NOT"; break;
|
|
|
|
case FTS5_OR: zOp = "OR"; break;
|
|
|
|
default: assert( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
z1 = fts5ExprPrint(pConfig, pExpr->pLeft);
|
|
|
|
z2 = fts5ExprPrint(pConfig, pExpr->pRight);
|
|
|
|
if( z1 && z2 ){
|
|
|
|
int b1 = pExpr->pLeft->eType!=FTS5_STRING;
|
|
|
|
int b2 = pExpr->pRight->eType!=FTS5_STRING;
|
|
|
|
zRet = sqlite3_mprintf("%s%s%s %s %s%s%s",
|
|
|
|
b1 ? "(" : "", z1, b1 ? ")" : "",
|
|
|
|
zOp,
|
|
|
|
b2 ? "(" : "", z2, b2 ? ")" : ""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
sqlite3_free(z1);
|
|
|
|
sqlite3_free(z2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return zRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The implementation of user-defined scalar function fts5_expr().
|
|
|
|
*/
|
|
|
|
static void fts5ExprFunction(
|
|
|
|
sqlite3_context *pCtx, /* Function call context */
|
|
|
|
int nArg, /* Number of args */
|
|
|
|
sqlite3_value **apVal /* Function arguments */
|
|
|
|
){
|
|
|
|
sqlite3 *db = sqlite3_context_db_handle(pCtx);
|
|
|
|
const char *zExpr = 0;
|
|
|
|
char *zErr = 0;
|
|
|
|
Fts5Expr *pExpr = 0;
|
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
const char **azConfig; /* Array of arguments for Fts5Config */
|
|
|
|
int nConfig; /* Size of azConfig[] */
|
|
|
|
Fts5Config *pConfig = 0;
|
|
|
|
|
|
|
|
nConfig = nArg + 2;
|
|
|
|
azConfig = (const char**)sqlite3_malloc(sizeof(char*) * nConfig);
|
|
|
|
if( azConfig==0 ){
|
|
|
|
sqlite3_result_error_nomem(pCtx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
azConfig[0] = 0;
|
|
|
|
azConfig[1] = "main";
|
|
|
|
azConfig[2] = "tbl";
|
|
|
|
for(i=1; i<nArg; i++){
|
|
|
|
azConfig[i+2] = (const char*)sqlite3_value_text(apVal[i]);
|
|
|
|
}
|
|
|
|
zExpr = (const char*)sqlite3_value_text(apVal[0]);
|
|
|
|
|
|
|
|
rc = sqlite3Fts5ConfigParse(db, nConfig, azConfig, &pConfig, &zErr);
|
|
|
|
if( rc==SQLITE_OK ){
|
2014-06-26 00:28:38 +04:00
|
|
|
rc = sqlite3Fts5ExprNew(pConfig, zExpr, &pExpr, &zErr);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
2014-06-26 00:28:38 +04:00
|
|
|
char *zText = fts5ExprPrint(pConfig, pExpr->pRoot);
|
2014-06-23 15:33:22 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
|
|
|
|
sqlite3_free(zText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
if( zErr ){
|
|
|
|
sqlite3_result_error(pCtx, zErr, -1);
|
|
|
|
sqlite3_free(zErr);
|
|
|
|
}else{
|
|
|
|
sqlite3_result_error_code(pCtx, rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_free(azConfig);
|
|
|
|
sqlite3Fts5ConfigFree(pConfig);
|
|
|
|
sqlite3Fts5ExprFree(pExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This is called during initialization to register the fts5_expr() scalar
|
|
|
|
** UDF with the SQLite handle passed as the only argument.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5ExprInit(sqlite3 *db){
|
|
|
|
int rc = sqlite3_create_function(
|
|
|
|
db, "fts5_expr", -1, SQLITE_UTF8, 0, fts5ExprFunction, 0, 0
|
|
|
|
);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|