2011-02-02 07:26:46 +03:00
|
|
|
/*
|
|
|
|
** 2011 Jan 27
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
**
|
|
|
|
*/
|
2011-06-16 04:54:45 +04:00
|
|
|
#include "fts3Int.h"
|
2011-02-02 07:26:46 +03:00
|
|
|
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
typedef struct Fts3auxTable Fts3auxTable;
|
|
|
|
typedef struct Fts3auxCursor Fts3auxCursor;
|
|
|
|
|
|
|
|
struct Fts3auxTable {
|
|
|
|
sqlite3_vtab base; /* Base class used by SQLite core */
|
|
|
|
Fts3Table *pFts3Tab;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Fts3auxCursor {
|
|
|
|
sqlite3_vtab_cursor base; /* Base class used by SQLite core */
|
2011-06-02 23:57:24 +04:00
|
|
|
Fts3MultiSegReader csr; /* Must be right after "base" */
|
2011-02-02 07:26:46 +03:00
|
|
|
Fts3SegFilter filter;
|
2011-02-02 20:30:43 +03:00
|
|
|
char *zStop;
|
2011-02-04 21:56:25 +03:00
|
|
|
int nStop; /* Byte-length of string zStop */
|
2013-10-15 23:06:17 +04:00
|
|
|
int iLangid; /* Language id to query */
|
2011-02-04 21:56:25 +03:00
|
|
|
int isEof; /* True if cursor is at EOF */
|
|
|
|
sqlite3_int64 iRowid; /* Current rowid */
|
|
|
|
|
|
|
|
int iCol; /* Current value of 'col' column */
|
|
|
|
int nStat; /* Size of aStat[] array */
|
|
|
|
struct Fts3auxColstats {
|
|
|
|
sqlite3_int64 nDoc; /* 'documents' values for current csr row */
|
|
|
|
sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
|
|
|
|
} *aStat;
|
2011-02-02 07:26:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Schema of the terms table.
|
|
|
|
*/
|
2013-10-15 23:06:17 +04:00
|
|
|
#define FTS3_AUX_SCHEMA \
|
|
|
|
"CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
|
2011-02-02 07:26:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** This function does all the work for both the xConnect and xCreate methods.
|
|
|
|
** These tables have no persistent representation of their own, so xConnect
|
|
|
|
** and xCreate are identical operations.
|
|
|
|
*/
|
|
|
|
static int fts3auxConnectMethod(
|
|
|
|
sqlite3 *db, /* Database connection */
|
|
|
|
void *pUnused, /* Unused */
|
|
|
|
int argc, /* Number of elements in argv array */
|
|
|
|
const char * const *argv, /* xCreate/xConnect argument array */
|
|
|
|
sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
|
|
|
|
char **pzErr /* OUT: sqlite3_malloc'd error message */
|
|
|
|
){
|
|
|
|
char const *zDb; /* Name of database (e.g. "main") */
|
|
|
|
char const *zFts3; /* Name of fts3 table */
|
|
|
|
int nDb; /* Result of strlen(zDb) */
|
|
|
|
int nFts3; /* Result of strlen(zFts3) */
|
2019-01-08 23:02:48 +03:00
|
|
|
sqlite3_int64 nByte; /* Bytes of space to allocate here */
|
2011-02-02 07:26:46 +03:00
|
|
|
int rc; /* value returned by declare_vtab() */
|
|
|
|
Fts3auxTable *p; /* Virtual table object to return */
|
|
|
|
|
2011-04-06 02:08:24 +04:00
|
|
|
UNUSED_PARAMETER(pUnused);
|
|
|
|
|
2013-04-12 20:47:27 +04:00
|
|
|
/* The user should invoke this in one of two forms:
|
|
|
|
**
|
|
|
|
** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
|
|
|
|
** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
|
|
|
|
*/
|
|
|
|
if( argc!=4 && argc!=5 ) goto bad_args;
|
2011-02-02 07:26:46 +03:00
|
|
|
|
|
|
|
zDb = argv[1];
|
2012-03-19 18:51:19 +04:00
|
|
|
nDb = (int)strlen(zDb);
|
2013-04-12 20:47:27 +04:00
|
|
|
if( argc==5 ){
|
|
|
|
if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
|
|
|
|
zDb = argv[3];
|
|
|
|
nDb = (int)strlen(zDb);
|
|
|
|
zFts3 = argv[4];
|
|
|
|
}else{
|
|
|
|
goto bad_args;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
zFts3 = argv[3];
|
|
|
|
}
|
2012-03-19 18:51:19 +04:00
|
|
|
nFts3 = (int)strlen(zFts3);
|
2011-02-02 07:26:46 +03:00
|
|
|
|
2013-10-15 23:06:17 +04:00
|
|
|
rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
|
2011-02-02 07:26:46 +03:00
|
|
|
if( rc!=SQLITE_OK ) return rc;
|
|
|
|
|
|
|
|
nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
|
2019-01-08 23:02:48 +03:00
|
|
|
p = (Fts3auxTable *)sqlite3_malloc64(nByte);
|
2011-02-02 07:26:46 +03:00
|
|
|
if( !p ) return SQLITE_NOMEM;
|
|
|
|
memset(p, 0, nByte);
|
|
|
|
|
|
|
|
p->pFts3Tab = (Fts3Table *)&p[1];
|
|
|
|
p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
|
|
|
|
p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
|
|
|
|
p->pFts3Tab->db = db;
|
2011-05-25 22:34:53 +04:00
|
|
|
p->pFts3Tab->nIndex = 1;
|
2011-02-02 07:26:46 +03:00
|
|
|
|
|
|
|
memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
|
|
|
|
memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
|
2011-02-05 18:47:12 +03:00
|
|
|
sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
|
2011-02-02 07:26:46 +03:00
|
|
|
|
|
|
|
*ppVtab = (sqlite3_vtab *)p;
|
|
|
|
return SQLITE_OK;
|
2013-04-12 20:47:27 +04:00
|
|
|
|
|
|
|
bad_args:
|
2015-05-01 17:07:30 +03:00
|
|
|
sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
|
2013-04-12 20:47:27 +04:00
|
|
|
return SQLITE_ERROR;
|
2011-02-02 07:26:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This function does the work for both the xDisconnect and xDestroy methods.
|
|
|
|
** These tables have no persistent representation of their own, so xDisconnect
|
|
|
|
** and xDestroy are identical operations.
|
|
|
|
*/
|
|
|
|
static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
|
|
|
|
Fts3auxTable *p = (Fts3auxTable *)pVtab;
|
|
|
|
Fts3Table *pFts3 = p->pFts3Tab;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Free any prepared statements held */
|
|
|
|
for(i=0; i<SizeofArray(pFts3->aStmt); i++){
|
|
|
|
sqlite3_finalize(pFts3->aStmt[i]);
|
|
|
|
}
|
|
|
|
sqlite3_free(pFts3->zSegmentsTbl);
|
|
|
|
sqlite3_free(p);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-02 20:30:43 +03:00
|
|
|
#define FTS4AUX_EQ_CONSTRAINT 1
|
|
|
|
#define FTS4AUX_GE_CONSTRAINT 2
|
|
|
|
#define FTS4AUX_LE_CONSTRAINT 4
|
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
/*
|
|
|
|
** xBestIndex - Analyze a WHERE and ORDER BY clause.
|
|
|
|
*/
|
|
|
|
static int fts3auxBestIndexMethod(
|
|
|
|
sqlite3_vtab *pVTab,
|
|
|
|
sqlite3_index_info *pInfo
|
|
|
|
){
|
2011-02-02 20:30:43 +03:00
|
|
|
int i;
|
|
|
|
int iEq = -1;
|
|
|
|
int iGe = -1;
|
|
|
|
int iLe = -1;
|
2013-10-15 23:06:17 +04:00
|
|
|
int iLangid = -1;
|
|
|
|
int iNext = 1; /* Next free argvIndex value */
|
2011-02-02 07:26:46 +03:00
|
|
|
|
2011-04-06 02:08:24 +04:00
|
|
|
UNUSED_PARAMETER(pVTab);
|
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
/* This vtab delivers always results in "ORDER BY term ASC" order. */
|
|
|
|
if( pInfo->nOrderBy==1
|
|
|
|
&& pInfo->aOrderBy[0].iColumn==0
|
|
|
|
&& pInfo->aOrderBy[0].desc==0
|
|
|
|
){
|
|
|
|
pInfo->orderByConsumed = 1;
|
|
|
|
}
|
|
|
|
|
2013-10-15 23:06:17 +04:00
|
|
|
/* Search for equality and range constraints on the "term" column.
|
|
|
|
** And equality constraints on the hidden "languageid" column. */
|
2011-02-02 20:30:43 +03:00
|
|
|
for(i=0; i<pInfo->nConstraint; i++){
|
2013-10-15 23:06:17 +04:00
|
|
|
if( pInfo->aConstraint[i].usable ){
|
2011-02-02 20:30:43 +03:00
|
|
|
int op = pInfo->aConstraint[i].op;
|
2013-10-15 23:06:17 +04:00
|
|
|
int iCol = pInfo->aConstraint[i].iColumn;
|
|
|
|
|
|
|
|
if( iCol==0 ){
|
|
|
|
if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
|
|
|
|
if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
|
|
|
|
if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
|
|
|
|
if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
|
|
|
|
if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
|
|
|
|
}
|
|
|
|
if( iCol==4 ){
|
|
|
|
if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
|
|
|
|
}
|
2011-02-02 20:30:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( iEq>=0 ){
|
|
|
|
pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
|
2013-10-15 23:06:17 +04:00
|
|
|
pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
|
2011-02-03 13:56:00 +03:00
|
|
|
pInfo->estimatedCost = 5;
|
2011-02-02 20:30:43 +03:00
|
|
|
}else{
|
|
|
|
pInfo->idxNum = 0;
|
2011-02-03 13:56:00 +03:00
|
|
|
pInfo->estimatedCost = 20000;
|
2011-02-02 20:30:43 +03:00
|
|
|
if( iGe>=0 ){
|
|
|
|
pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
|
2013-10-15 23:06:17 +04:00
|
|
|
pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
|
2011-02-03 13:56:00 +03:00
|
|
|
pInfo->estimatedCost /= 2;
|
2011-02-02 20:30:43 +03:00
|
|
|
}
|
|
|
|
if( iLe>=0 ){
|
|
|
|
pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
|
2013-10-15 23:06:17 +04:00
|
|
|
pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
|
2011-02-03 13:56:00 +03:00
|
|
|
pInfo->estimatedCost /= 2;
|
2011-02-02 20:30:43 +03:00
|
|
|
}
|
|
|
|
}
|
2013-10-15 23:06:17 +04:00
|
|
|
if( iLangid>=0 ){
|
|
|
|
pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
|
|
|
|
pInfo->estimatedCost--;
|
|
|
|
}
|
2011-02-02 20:30:43 +03:00
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** xOpen - Open a cursor.
|
|
|
|
*/
|
|
|
|
static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
|
|
|
|
Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
|
|
|
|
|
2011-04-06 02:08:24 +04:00
|
|
|
UNUSED_PARAMETER(pVTab);
|
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
|
|
|
|
if( !pCsr ) return SQLITE_NOMEM;
|
|
|
|
memset(pCsr, 0, sizeof(Fts3auxCursor));
|
|
|
|
|
|
|
|
*ppCsr = (sqlite3_vtab_cursor *)pCsr;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** xClose - Close a cursor.
|
|
|
|
*/
|
|
|
|
static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
|
|
|
|
Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
|
|
|
|
Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
|
|
|
|
|
|
|
|
sqlite3Fts3SegmentsClose(pFts3);
|
|
|
|
sqlite3Fts3SegReaderFinish(&pCsr->csr);
|
2011-02-02 20:30:43 +03:00
|
|
|
sqlite3_free((void *)pCsr->filter.zTerm);
|
|
|
|
sqlite3_free(pCsr->zStop);
|
2011-02-04 21:56:25 +03:00
|
|
|
sqlite3_free(pCsr->aStat);
|
2011-02-02 07:26:46 +03:00
|
|
|
sqlite3_free(pCsr);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-04 21:56:25 +03:00
|
|
|
static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
|
|
|
|
if( nSize>pCsr->nStat ){
|
|
|
|
struct Fts3auxColstats *aNew;
|
2019-01-08 23:02:48 +03:00
|
|
|
aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat,
|
2011-02-04 21:56:25 +03:00
|
|
|
sizeof(struct Fts3auxColstats) * nSize
|
|
|
|
);
|
|
|
|
if( aNew==0 ) return SQLITE_NOMEM;
|
|
|
|
memset(&aNew[pCsr->nStat], 0,
|
|
|
|
sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
|
|
|
|
);
|
|
|
|
pCsr->aStat = aNew;
|
|
|
|
pCsr->nStat = nSize;
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
/*
|
|
|
|
** xNext - Advance the cursor to the next row, if any.
|
|
|
|
*/
|
|
|
|
static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
|
|
|
|
Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
|
|
|
|
Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
|
|
|
|
int rc;
|
|
|
|
|
2011-02-04 21:56:25 +03:00
|
|
|
/* Increment our pretend rowid value. */
|
|
|
|
pCsr->iRowid++;
|
|
|
|
|
|
|
|
for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
|
|
|
|
if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
|
|
|
|
if( rc==SQLITE_ROW ){
|
2011-02-04 21:56:25 +03:00
|
|
|
int i = 0;
|
2011-02-02 07:26:46 +03:00
|
|
|
int nDoclist = pCsr->csr.nDoclist;
|
|
|
|
char *aDoclist = pCsr->csr.aDoclist;
|
2011-02-04 21:56:25 +03:00
|
|
|
int iCol;
|
|
|
|
|
|
|
|
int eState = 0;
|
2011-02-02 07:26:46 +03:00
|
|
|
|
2011-02-02 20:30:43 +03:00
|
|
|
if( pCsr->zStop ){
|
|
|
|
int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
|
|
|
|
int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
|
|
|
|
if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
|
|
|
|
pCsr->isEof = 1;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 21:56:25 +03:00
|
|
|
if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
|
|
|
|
memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
|
|
|
|
iCol = 0;
|
2021-11-22 15:59:44 +03:00
|
|
|
rc = SQLITE_OK;
|
2011-02-04 21:56:25 +03:00
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
while( i<nDoclist ){
|
|
|
|
sqlite3_int64 v = 0;
|
2011-02-04 21:56:25 +03:00
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
|
2011-02-04 21:56:25 +03:00
|
|
|
switch( eState ){
|
|
|
|
/* State 0. In this state the integer just read was a docid. */
|
|
|
|
case 0:
|
|
|
|
pCsr->aStat[0].nDoc++;
|
|
|
|
eState = 1;
|
|
|
|
iCol = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* State 1. In this state we are expecting either a 1, indicating
|
|
|
|
** that the following integer will be a column number, or the
|
|
|
|
** start of a position list for column 0.
|
|
|
|
**
|
|
|
|
** The only difference between state 1 and state 2 is that if the
|
|
|
|
** integer encountered in state 1 is not 0 or 1, then we need to
|
|
|
|
** increment the column 0 "nDoc" count for this term.
|
|
|
|
*/
|
|
|
|
case 1:
|
|
|
|
assert( iCol==0 );
|
|
|
|
if( v>1 ){
|
|
|
|
pCsr->aStat[1].nDoc++;
|
|
|
|
}
|
|
|
|
eState = 2;
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if( v==0 ){ /* 0x00. Next integer will be a docid. */
|
|
|
|
eState = 0;
|
|
|
|
}else if( v==1 ){ /* 0x01. Next integer will be a column number. */
|
|
|
|
eState = 3;
|
|
|
|
}else{ /* 2 or greater. A position. */
|
|
|
|
pCsr->aStat[iCol+1].nOcc++;
|
|
|
|
pCsr->aStat[0].nOcc++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* State 3. The integer just read is a column number. */
|
2011-02-05 17:37:57 +03:00
|
|
|
default: assert( eState==3 );
|
2011-02-04 21:56:25 +03:00
|
|
|
iCol = (int)v;
|
2021-11-22 11:12:14 +03:00
|
|
|
if( iCol<1 ){
|
|
|
|
rc = SQLITE_CORRUPT_VTAB;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-04 21:56:25 +03:00
|
|
|
if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
|
|
|
|
pCsr->aStat[iCol+1].nDoc++;
|
|
|
|
eState = 2;
|
|
|
|
break;
|
2011-02-02 07:26:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 21:56:25 +03:00
|
|
|
pCsr->iCol = 0;
|
2011-02-02 07:26:46 +03:00
|
|
|
}else{
|
|
|
|
pCsr->isEof = 1;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** xFilter - Initialize a cursor to point at the start of its data.
|
|
|
|
*/
|
|
|
|
static int fts3auxFilterMethod(
|
|
|
|
sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
|
|
|
|
int idxNum, /* Strategy index */
|
|
|
|
const char *idxStr, /* Unused */
|
|
|
|
int nVal, /* Number of elements in apVal */
|
|
|
|
sqlite3_value **apVal /* Arguments for the indexing scheme */
|
|
|
|
){
|
|
|
|
Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
|
|
|
|
Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
|
|
|
|
int rc;
|
2013-10-15 23:06:17 +04:00
|
|
|
int isScan = 0;
|
|
|
|
int iLangVal = 0; /* Language id to query */
|
|
|
|
|
|
|
|
int iEq = -1; /* Index of term=? value in apVal */
|
|
|
|
int iGe = -1; /* Index of term>=? value in apVal */
|
|
|
|
int iLe = -1; /* Index of term<=? value in apVal */
|
|
|
|
int iLangid = -1; /* Index of languageid=? value in apVal */
|
|
|
|
int iNext = 0;
|
2011-02-02 07:26:46 +03:00
|
|
|
|
2011-04-06 02:08:24 +04:00
|
|
|
UNUSED_PARAMETER(nVal);
|
2011-05-26 03:18:02 +04:00
|
|
|
UNUSED_PARAMETER(idxStr);
|
2011-04-06 02:08:24 +04:00
|
|
|
|
2011-02-02 20:30:43 +03:00
|
|
|
assert( idxStr==0 );
|
|
|
|
assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
|
|
|
|
|| idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
|
|
|
|
|| idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
|
|
|
|
);
|
2013-10-15 23:06:17 +04:00
|
|
|
|
|
|
|
if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
|
|
|
|
iEq = iNext++;
|
|
|
|
}else{
|
|
|
|
isScan = 1;
|
|
|
|
if( idxNum & FTS4AUX_GE_CONSTRAINT ){
|
|
|
|
iGe = iNext++;
|
|
|
|
}
|
|
|
|
if( idxNum & FTS4AUX_LE_CONSTRAINT ){
|
|
|
|
iLe = iNext++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( iNext<nVal ){
|
|
|
|
iLangid = iNext++;
|
|
|
|
}
|
2011-02-02 20:30:43 +03:00
|
|
|
|
|
|
|
/* In case this cursor is being reused, close and zero it. */
|
|
|
|
testcase(pCsr->filter.zTerm);
|
2011-02-02 07:26:46 +03:00
|
|
|
sqlite3Fts3SegReaderFinish(&pCsr->csr);
|
2011-02-02 20:30:43 +03:00
|
|
|
sqlite3_free((void *)pCsr->filter.zTerm);
|
2011-02-04 21:56:25 +03:00
|
|
|
sqlite3_free(pCsr->aStat);
|
2021-04-08 16:41:20 +03:00
|
|
|
sqlite3_free(pCsr->zStop);
|
2011-02-02 20:30:43 +03:00
|
|
|
memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
|
|
|
|
|
2011-02-02 07:26:46 +03:00
|
|
|
pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
|
2011-02-02 20:30:43 +03:00
|
|
|
if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
|
|
|
|
|
2013-10-15 23:06:17 +04:00
|
|
|
if( iEq>=0 || iGe>=0 ){
|
2011-02-03 13:56:00 +03:00
|
|
|
const unsigned char *zStr = sqlite3_value_text(apVal[0]);
|
2013-10-15 23:06:17 +04:00
|
|
|
assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
|
2011-02-02 20:30:43 +03:00
|
|
|
if( zStr ){
|
|
|
|
pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
|
|
|
|
if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
|
2019-01-29 19:54:31 +03:00
|
|
|
pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
|
2011-02-02 20:30:43 +03:00
|
|
|
}
|
|
|
|
}
|
2013-10-15 23:06:17 +04:00
|
|
|
|
|
|
|
if( iLe>=0 ){
|
|
|
|
pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
|
2011-02-02 20:30:43 +03:00
|
|
|
if( pCsr->zStop==0 ) return SQLITE_NOMEM;
|
2019-01-29 19:54:31 +03:00
|
|
|
pCsr->nStop = (int)strlen(pCsr->zStop);
|
2011-02-02 20:30:43 +03:00
|
|
|
}
|
2013-10-15 23:06:17 +04:00
|
|
|
|
|
|
|
if( iLangid>=0 ){
|
|
|
|
iLangVal = sqlite3_value_int(apVal[iLangid]);
|
|
|
|
|
|
|
|
/* If the user specified a negative value for the languageid, use zero
|
|
|
|
** instead. This works, as the "languageid=?" constraint will also
|
|
|
|
** be tested by the VDBE layer. The test will always be false (since
|
|
|
|
** this module will not return a row with a negative languageid), and
|
|
|
|
** so the overall query will return zero rows. */
|
|
|
|
if( iLangVal<0 ) iLangVal = 0;
|
|
|
|
}
|
|
|
|
pCsr->iLangid = iLangVal;
|
2011-02-02 07:26:46 +03:00
|
|
|
|
2013-10-15 23:06:17 +04:00
|
|
|
rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
|
2011-02-02 20:30:43 +03:00
|
|
|
pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
|
|
|
|
);
|
2011-02-02 07:26:46 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** xEof - Return true if the cursor is at EOF, or false otherwise.
|
|
|
|
*/
|
|
|
|
static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
|
|
|
|
Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
|
|
|
|
return pCsr->isEof;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** xColumn - Return a column value.
|
|
|
|
*/
|
|
|
|
static int fts3auxColumnMethod(
|
|
|
|
sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
|
2013-10-15 23:06:17 +04:00
|
|
|
sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
|
2011-02-02 07:26:46 +03:00
|
|
|
int iCol /* Index of column to read value from */
|
|
|
|
){
|
|
|
|
Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
|
|
|
|
|
|
|
|
assert( p->isEof==0 );
|
2013-10-15 23:06:17 +04:00
|
|
|
switch( iCol ){
|
|
|
|
case 0: /* term */
|
|
|
|
sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* col */
|
|
|
|
if( p->iCol ){
|
|
|
|
sqlite3_result_int(pCtx, p->iCol-1);
|
|
|
|
}else{
|
|
|
|
sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* documents */
|
|
|
|
sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* occurrences */
|
|
|
|
sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* languageid */
|
|
|
|
assert( iCol==4 );
|
|
|
|
sqlite3_result_int(pCtx, p->iLangid);
|
|
|
|
break;
|
2011-02-02 07:26:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** xRowid - Return the current rowid for the cursor.
|
|
|
|
*/
|
|
|
|
static int fts3auxRowidMethod(
|
|
|
|
sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
|
|
|
|
sqlite_int64 *pRowid /* OUT: Rowid value */
|
|
|
|
){
|
|
|
|
Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
|
|
|
|
*pRowid = pCsr->iRowid;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Register the fts3aux module with database connection db. Return SQLITE_OK
|
|
|
|
** if successful or an error code if sqlite3_create_module() fails.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts3InitAux(sqlite3 *db){
|
|
|
|
static const sqlite3_module fts3aux_module = {
|
|
|
|
0, /* iVersion */
|
|
|
|
fts3auxConnectMethod, /* xCreate */
|
|
|
|
fts3auxConnectMethod, /* xConnect */
|
|
|
|
fts3auxBestIndexMethod, /* xBestIndex */
|
|
|
|
fts3auxDisconnectMethod, /* xDisconnect */
|
|
|
|
fts3auxDisconnectMethod, /* xDestroy */
|
|
|
|
fts3auxOpenMethod, /* xOpen */
|
|
|
|
fts3auxCloseMethod, /* xClose */
|
|
|
|
fts3auxFilterMethod, /* xFilter */
|
|
|
|
fts3auxNextMethod, /* xNext */
|
|
|
|
fts3auxEofMethod, /* xEof */
|
|
|
|
fts3auxColumnMethod, /* xColumn */
|
|
|
|
fts3auxRowidMethod, /* xRowid */
|
|
|
|
0, /* xUpdate */
|
|
|
|
0, /* xBegin */
|
|
|
|
0, /* xSync */
|
|
|
|
0, /* xCommit */
|
|
|
|
0, /* xRollback */
|
|
|
|
0, /* xFindFunction */
|
2011-05-26 03:18:02 +04:00
|
|
|
0, /* xRename */
|
|
|
|
0, /* xSavepoint */
|
|
|
|
0, /* xRelease */
|
2018-11-06 02:01:45 +03:00
|
|
|
0, /* xRollbackTo */
|
2023-10-06 15:51:05 +03:00
|
|
|
0, /* xShadowName */
|
|
|
|
0 /* xIntegrity */
|
2011-02-02 07:26:46 +03:00
|
|
|
};
|
|
|
|
int rc; /* Return code */
|
|
|
|
|
|
|
|
rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
|