|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_FTS5) |
|
|
|
|
|
#include "fts5.h" |
|
|
#include <string.h> |
|
|
#include <assert.h> |
|
|
|
|
|
typedef struct Fts5tokTable Fts5tokTable; |
|
|
typedef struct Fts5tokCursor Fts5tokCursor; |
|
|
typedef struct Fts5tokRow Fts5tokRow; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Fts5tokTable { |
|
|
sqlite3_vtab base; |
|
|
fts5_tokenizer tok; |
|
|
Fts5Tokenizer *pTok; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Fts5tokRow { |
|
|
char *zToken; |
|
|
int iStart; |
|
|
int iEnd; |
|
|
int iPos; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Fts5tokCursor { |
|
|
sqlite3_vtab_cursor base; |
|
|
int iRowid; |
|
|
char *zInput; |
|
|
int nRow; |
|
|
Fts5tokRow *aRow; |
|
|
}; |
|
|
|
|
|
static void fts5tokDequote(char *z){ |
|
|
char q = z[0]; |
|
|
|
|
|
if( q=='[' || q=='\'' || q=='"' || q=='`' ){ |
|
|
int iIn = 1; |
|
|
int iOut = 0; |
|
|
if( q=='[' ) q = ']'; |
|
|
|
|
|
while( z[iIn] ){ |
|
|
if( z[iIn]==q ){ |
|
|
if( z[iIn+1]!=q ){ |
|
|
|
|
|
iIn++; |
|
|
break; |
|
|
}else{ |
|
|
|
|
|
|
|
|
|
|
|
iIn += 2; |
|
|
z[iOut++] = q; |
|
|
} |
|
|
}else{ |
|
|
z[iOut++] = z[iIn++]; |
|
|
} |
|
|
} |
|
|
|
|
|
z[iOut] = '\0'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokDequoteArray( |
|
|
int argc, |
|
|
const char * const *argv, |
|
|
char ***pazDequote |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
if( argc==0 ){ |
|
|
*pazDequote = 0; |
|
|
}else{ |
|
|
int i; |
|
|
int nByte = 0; |
|
|
char **azDequote; |
|
|
|
|
|
for(i=0; i<argc; i++){ |
|
|
nByte += (int)(strlen(argv[i]) + 1); |
|
|
} |
|
|
|
|
|
*pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte); |
|
|
if( azDequote==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
char *pSpace = (char *)&azDequote[argc]; |
|
|
for(i=0; i<argc; i++){ |
|
|
int n = (int)strlen(argv[i]); |
|
|
azDequote[i] = pSpace; |
|
|
memcpy(pSpace, argv[i], n+1); |
|
|
fts5tokDequote(pSpace); |
|
|
pSpace += (n+1); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define FTS3_TOK_SCHEMA "CREATE TABLE x(input HIDDEN, token, start, end, position)" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokConnectMethod( |
|
|
sqlite3 *db, |
|
|
void *pCtx, |
|
|
int argc, |
|
|
const char * const *argv, |
|
|
sqlite3_vtab **ppVtab, |
|
|
char **pzErr |
|
|
){ |
|
|
fts5_api *pApi = (fts5_api*)pCtx; |
|
|
Fts5tokTable *pTab = 0; |
|
|
int rc; |
|
|
char **azDequote = 0; |
|
|
int nDequote = 0; |
|
|
|
|
|
rc = sqlite3_declare_vtab(db, |
|
|
"CREATE TABLE x(input HIDDEN, token, start, end, position)" |
|
|
); |
|
|
|
|
|
if( rc==SQLITE_OK ){ |
|
|
nDequote = argc-3; |
|
|
rc = fts5tokDequoteArray(nDequote, &argv[3], &azDequote); |
|
|
} |
|
|
|
|
|
if( rc==SQLITE_OK ){ |
|
|
pTab = (Fts5tokTable*)sqlite3_malloc(sizeof(Fts5tokTable)); |
|
|
if( pTab==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
memset(pTab, 0, sizeof(Fts5tokTable)); |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc==SQLITE_OK ){ |
|
|
void *pTokCtx = 0; |
|
|
const char *zModule = 0; |
|
|
if( nDequote>0 ){ |
|
|
zModule = azDequote[0]; |
|
|
} |
|
|
|
|
|
rc = pApi->xFindTokenizer(pApi, zModule, &pTokCtx, &pTab->tok); |
|
|
if( rc==SQLITE_OK ){ |
|
|
const char **azArg = (nDequote>1 ? (const char **)&azDequote[1] : 0); |
|
|
int nArg = nDequote>0 ? nDequote-1 : 0; |
|
|
rc = pTab->tok.xCreate(pTokCtx, azArg, nArg, &pTab->pTok); |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc!=SQLITE_OK ){ |
|
|
sqlite3_free(pTab); |
|
|
pTab = 0; |
|
|
} |
|
|
|
|
|
*ppVtab = (sqlite3_vtab*)pTab; |
|
|
sqlite3_free(azDequote); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokDisconnectMethod(sqlite3_vtab *pVtab){ |
|
|
Fts5tokTable *pTab = (Fts5tokTable *)pVtab; |
|
|
if( pTab->pTok ){ |
|
|
pTab->tok.xDelete(pTab->pTok); |
|
|
} |
|
|
sqlite3_free(pTab); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokBestIndexMethod( |
|
|
sqlite3_vtab *pVTab, |
|
|
sqlite3_index_info *pInfo |
|
|
){ |
|
|
int i; |
|
|
|
|
|
for(i=0; i<pInfo->nConstraint; i++){ |
|
|
if( pInfo->aConstraint[i].usable |
|
|
&& pInfo->aConstraint[i].iColumn==0 |
|
|
&& pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ |
|
|
){ |
|
|
pInfo->idxNum = 1; |
|
|
pInfo->aConstraintUsage[i].argvIndex = 1; |
|
|
pInfo->aConstraintUsage[i].omit = 1; |
|
|
pInfo->estimatedCost = 1; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
} |
|
|
|
|
|
pInfo->idxNum = 0; |
|
|
assert( pInfo->estimatedCost>1000000.0 ); |
|
|
|
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ |
|
|
Fts5tokCursor *pCsr; |
|
|
|
|
|
pCsr = (Fts5tokCursor *)sqlite3_malloc(sizeof(Fts5tokCursor)); |
|
|
if( pCsr==0 ){ |
|
|
return SQLITE_NOMEM; |
|
|
} |
|
|
memset(pCsr, 0, sizeof(Fts5tokCursor)); |
|
|
|
|
|
*ppCsr = (sqlite3_vtab_cursor *)pCsr; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void fts5tokResetCursor(Fts5tokCursor *pCsr){ |
|
|
int i; |
|
|
for(i=0; i<pCsr->nRow; i++){ |
|
|
sqlite3_free(pCsr->aRow[i].zToken); |
|
|
} |
|
|
sqlite3_free(pCsr->zInput); |
|
|
sqlite3_free(pCsr->aRow); |
|
|
pCsr->zInput = 0; |
|
|
pCsr->aRow = 0; |
|
|
pCsr->nRow = 0; |
|
|
pCsr->iRowid = 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokCloseMethod(sqlite3_vtab_cursor *pCursor){ |
|
|
Fts5tokCursor *pCsr = (Fts5tokCursor *)pCursor; |
|
|
fts5tokResetCursor(pCsr); |
|
|
sqlite3_free(pCsr); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokNextMethod(sqlite3_vtab_cursor *pCursor){ |
|
|
Fts5tokCursor *pCsr = (Fts5tokCursor *)pCursor; |
|
|
pCsr->iRowid++; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
static int fts5tokCb( |
|
|
void *pCtx, |
|
|
int tflags, |
|
|
const char *pToken, |
|
|
int nToken, |
|
|
int iStart, |
|
|
int iEnd |
|
|
){ |
|
|
Fts5tokCursor *pCsr = (Fts5tokCursor*)pCtx; |
|
|
Fts5tokRow *pRow; |
|
|
|
|
|
if( (pCsr->nRow & (pCsr->nRow-1))==0 ){ |
|
|
int nNew = pCsr->nRow ? pCsr->nRow*2 : 32; |
|
|
Fts5tokRow *aNew; |
|
|
aNew = (Fts5tokRow*)sqlite3_realloc64(pCsr->aRow, nNew*sizeof(Fts5tokRow)); |
|
|
if( aNew==0 ) return SQLITE_NOMEM; |
|
|
memset(&aNew[pCsr->nRow], 0, sizeof(Fts5tokRow)*(nNew-pCsr->nRow)); |
|
|
pCsr->aRow = aNew; |
|
|
} |
|
|
|
|
|
pRow = &pCsr->aRow[pCsr->nRow]; |
|
|
pRow->iStart = iStart; |
|
|
pRow->iEnd = iEnd; |
|
|
if( pCsr->nRow ){ |
|
|
pRow->iPos = pRow[-1].iPos + ((tflags & FTS5_TOKEN_COLOCATED) ? 0 : 1); |
|
|
} |
|
|
pRow->zToken = sqlite3_malloc(nToken+1); |
|
|
if( pRow->zToken==0 ) return SQLITE_NOMEM; |
|
|
memcpy(pRow->zToken, pToken, nToken); |
|
|
pRow->zToken[nToken] = 0; |
|
|
pCsr->nRow++; |
|
|
|
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokFilterMethod( |
|
|
sqlite3_vtab_cursor *pCursor, |
|
|
int idxNum, |
|
|
const char *idxStr, |
|
|
int nVal, |
|
|
sqlite3_value **apVal |
|
|
){ |
|
|
int rc = SQLITE_ERROR; |
|
|
Fts5tokCursor *pCsr = (Fts5tokCursor *)pCursor; |
|
|
Fts5tokTable *pTab = (Fts5tokTable *)(pCursor->pVtab); |
|
|
|
|
|
fts5tokResetCursor(pCsr); |
|
|
if( idxNum==1 ){ |
|
|
const char *zByte = (const char *)sqlite3_value_text(apVal[0]); |
|
|
int nByte = sqlite3_value_bytes(apVal[0]); |
|
|
pCsr->zInput = sqlite3_malloc(nByte+1); |
|
|
if( pCsr->zInput==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte); |
|
|
pCsr->zInput[nByte] = 0; |
|
|
rc = pTab->tok.xTokenize( |
|
|
pTab->pTok, (void*)pCsr, 0, zByte, nByte, fts5tokCb |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc!=SQLITE_OK ) return rc; |
|
|
return fts5tokNextMethod(pCursor); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokEofMethod(sqlite3_vtab_cursor *pCursor){ |
|
|
Fts5tokCursor *pCsr = (Fts5tokCursor *)pCursor; |
|
|
return (pCsr->iRowid>pCsr->nRow); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokColumnMethod( |
|
|
sqlite3_vtab_cursor *pCursor, |
|
|
sqlite3_context *pCtx, |
|
|
int iCol |
|
|
){ |
|
|
Fts5tokCursor *pCsr = (Fts5tokCursor *)pCursor; |
|
|
Fts5tokRow *pRow = &pCsr->aRow[pCsr->iRowid-1]; |
|
|
|
|
|
|
|
|
switch( iCol ){ |
|
|
case 0: |
|
|
sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT); |
|
|
break; |
|
|
case 1: |
|
|
sqlite3_result_text(pCtx, pRow->zToken, -1, SQLITE_TRANSIENT); |
|
|
break; |
|
|
case 2: |
|
|
sqlite3_result_int(pCtx, pRow->iStart); |
|
|
break; |
|
|
case 3: |
|
|
sqlite3_result_int(pCtx, pRow->iEnd); |
|
|
break; |
|
|
default: |
|
|
assert( iCol==4 ); |
|
|
sqlite3_result_int(pCtx, pRow->iPos); |
|
|
break; |
|
|
} |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5tokRowidMethod( |
|
|
sqlite3_vtab_cursor *pCursor, |
|
|
sqlite_int64 *pRowid |
|
|
){ |
|
|
Fts5tokCursor *pCsr = (Fts5tokCursor *)pCursor; |
|
|
*pRowid = (sqlite3_int64)pCsr->iRowid; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5TestRegisterTok(sqlite3 *db, fts5_api *pApi){ |
|
|
static const sqlite3_module fts5tok_module = { |
|
|
0, |
|
|
fts5tokConnectMethod, |
|
|
fts5tokConnectMethod, |
|
|
fts5tokBestIndexMethod, |
|
|
fts5tokDisconnectMethod, |
|
|
fts5tokDisconnectMethod, |
|
|
fts5tokOpenMethod, |
|
|
fts5tokCloseMethod, |
|
|
fts5tokFilterMethod, |
|
|
fts5tokNextMethod, |
|
|
fts5tokEofMethod, |
|
|
fts5tokColumnMethod, |
|
|
fts5tokRowidMethod, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0 |
|
|
}; |
|
|
int rc; |
|
|
|
|
|
rc = sqlite3_create_module(db, "fts5tokenize", &fts5tok_module, (void*)pApi); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
#endif |
|
|
|