2014-07-16 23:15:57 +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.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
**
|
|
|
|
** Interfaces to extend FTS5. Using the interfaces defined in this file,
|
|
|
|
** FTS5 may be extended with:
|
|
|
|
**
|
|
|
|
** * custom tokenizers, and
|
|
|
|
** * custom auxiliary functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _FTS5_H
|
|
|
|
#define _FTS5_H
|
|
|
|
|
|
|
|
#include "sqlite3.h"
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
** CUSTOM AUXILIARY FUNCTIONS
|
|
|
|
**
|
2014-08-18 23:30:01 +04:00
|
|
|
** Virtual table implementations may overload SQL functions by implementing
|
2014-07-16 23:15:57 +04:00
|
|
|
** the sqlite3_module.xFindFunction() method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct Fts5ExtensionApi Fts5ExtensionApi;
|
|
|
|
typedef struct Fts5Context Fts5Context;
|
2015-08-12 15:11:28 +03:00
|
|
|
typedef struct Fts5PhraseIter Fts5PhraseIter;
|
2014-07-16 23:15:57 +04:00
|
|
|
|
|
|
|
typedef void (*fts5_extension_function)(
|
|
|
|
const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
|
|
|
|
Fts5Context *pFts, /* First arg to pass to pApi functions */
|
|
|
|
sqlite3_context *pCtx, /* Context for returning result/error */
|
|
|
|
int nVal, /* Number of values in apVal[] array */
|
|
|
|
sqlite3_value **apVal /* Array of trailing arguments */
|
|
|
|
);
|
|
|
|
|
2015-08-12 15:11:28 +03:00
|
|
|
struct Fts5PhraseIter {
|
|
|
|
const unsigned char *a;
|
|
|
|
const unsigned char *b;
|
|
|
|
};
|
|
|
|
|
2014-07-16 23:15:57 +04:00
|
|
|
/*
|
2014-08-25 23:58:54 +04:00
|
|
|
** EXTENSION API FUNCTIONS
|
2014-07-26 00:30:47 +04:00
|
|
|
**
|
2014-08-25 23:58:54 +04:00
|
|
|
** xUserData(pFts):
|
2014-07-19 19:35:09 +04:00
|
|
|
** Return a copy of the context pointer the extension function was
|
|
|
|
** registered with.
|
|
|
|
**
|
2014-07-26 00:30:47 +04:00
|
|
|
** xColumnTotalSize(pFts, iCol, pnToken):
|
2014-12-23 22:18:34 +03:00
|
|
|
** If parameter iCol is less than zero, set output variable *pnToken
|
|
|
|
** to the total number of tokens in the FTS5 table. Or, if iCol is
|
|
|
|
** non-negative but less than the number of columns in the table, return
|
|
|
|
** the total number of tokens in column iCol, considering all rows in
|
|
|
|
** the FTS5 table.
|
|
|
|
**
|
|
|
|
** If parameter iCol is greater than or equal to the number of columns
|
|
|
|
** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
|
|
|
|
** an OOM condition or IO error), an appropriate SQLite error code is
|
|
|
|
** returned.
|
2014-07-26 00:30:47 +04:00
|
|
|
**
|
2015-08-04 23:29:00 +03:00
|
|
|
** xColumnCount(pFts):
|
|
|
|
** Return the number of columns in the table.
|
|
|
|
**
|
|
|
|
** xColumnSize(pFts, iCol, pnToken):
|
2015-07-29 17:58:47 +03:00
|
|
|
** If parameter iCol is less than zero, set output variable *pnToken
|
|
|
|
** to the total number of tokens in the current row. Or, if iCol is
|
|
|
|
** non-negative but less than the number of columns in the table, set
|
|
|
|
** *pnToken to the number of tokens in column iCol of the current row.
|
|
|
|
**
|
|
|
|
** If parameter iCol is greater than or equal to the number of columns
|
|
|
|
** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
|
|
|
|
** an OOM condition or IO error), an appropriate SQLite error code is
|
|
|
|
** returned.
|
2014-07-16 23:15:57 +04:00
|
|
|
**
|
2014-07-20 00:27:54 +04:00
|
|
|
** xColumnText:
|
2015-06-06 22:23:32 +03:00
|
|
|
** This function attempts to retrieve the text of column iCol of the
|
|
|
|
** current document. If successful, (*pz) is set to point to a buffer
|
|
|
|
** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
|
|
|
|
** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
|
|
|
|
** if an error occurs, an SQLite error code is returned and the final values
|
|
|
|
** of (*pz) and (*pn) are undefined.
|
2014-07-20 00:27:54 +04:00
|
|
|
**
|
2014-07-16 23:15:57 +04:00
|
|
|
** xPhraseCount:
|
|
|
|
** Returns the number of phrases in the current query expression.
|
|
|
|
**
|
|
|
|
** xPhraseSize:
|
|
|
|
** Returns the number of tokens in phrase iPhrase of the query. Phrases
|
|
|
|
** are numbered starting from zero.
|
|
|
|
**
|
2014-11-24 19:24:33 +03:00
|
|
|
** xInstCount:
|
|
|
|
** Set *pnInst to the total number of occurrences of all phrases within
|
|
|
|
** the query within the current row. Return SQLITE_OK if successful, or
|
|
|
|
** an error code (i.e. SQLITE_NOMEM) if an error occurs.
|
|
|
|
**
|
|
|
|
** xInst:
|
|
|
|
** Query for the details of phrase match iIdx within the current row.
|
|
|
|
** Phrase matches are numbered starting from zero, so the iIdx argument
|
|
|
|
** should be greater than or equal to zero and smaller than the value
|
|
|
|
** output by xInstCount().
|
|
|
|
**
|
|
|
|
** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM)
|
|
|
|
** if an error occurs.
|
|
|
|
**
|
2014-07-16 23:15:57 +04:00
|
|
|
** xRowid:
|
|
|
|
** Returns the rowid of the current row.
|
|
|
|
**
|
2014-07-21 18:22:38 +04:00
|
|
|
** xTokenize:
|
|
|
|
** Tokenize text using the tokenizer belonging to the FTS5 table.
|
2014-07-26 00:30:47 +04:00
|
|
|
**
|
|
|
|
** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
|
|
|
|
** This API function is used to query the FTS table for phrase iPhrase
|
|
|
|
** of the current query. Specifically, a query equivalent to:
|
|
|
|
**
|
2015-03-07 14:50:31 +03:00
|
|
|
** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
|
2014-07-26 00:30:47 +04:00
|
|
|
**
|
|
|
|
** with $p set to a phrase equivalent to the phrase iPhrase of the
|
|
|
|
** current query is executed. For each row visited, the callback function
|
|
|
|
** passed as the fourth argument is invoked. The context and API objects
|
|
|
|
** passed to the callback function may be used to access the properties of
|
|
|
|
** each matched row. Invoking Api.xUserData() returns a copy of the pointer
|
|
|
|
** passed as the third argument to pUserData.
|
|
|
|
**
|
|
|
|
** If the callback function returns any value other than SQLITE_OK, the
|
|
|
|
** query is abandoned and the xQueryPhrase function returns immediately.
|
|
|
|
** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
|
|
|
|
** Otherwise, the error code is propagated upwards.
|
|
|
|
**
|
|
|
|
** If the query runs to completion without incident, SQLITE_OK is returned.
|
|
|
|
** Or, if some error occurs before the query completes or is aborted by
|
|
|
|
** the callback, an SQLite error code is returned.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** xSetAuxdata(pFts5, pAux, xDelete)
|
|
|
|
**
|
|
|
|
** Save the pointer passed as the second argument as the extension functions
|
|
|
|
** "auxiliary data". The pointer may then be retrieved by the current or any
|
|
|
|
** future invocation of the same fts5 extension function made as part of
|
|
|
|
** of the same MATCH query using the xGetAuxdata() API.
|
|
|
|
**
|
2014-12-23 00:01:52 +03:00
|
|
|
** Each extension function is allocated a single auxiliary data slot for
|
|
|
|
** each FTS query (MATCH expression). If the extension function is invoked
|
|
|
|
** more than once for a single FTS query, then all invocations share a
|
|
|
|
** single auxiliary data context.
|
2014-07-26 00:30:47 +04:00
|
|
|
**
|
|
|
|
** If there is already an auxiliary data pointer when this function is
|
|
|
|
** invoked, then it is replaced by the new pointer. If an xDelete callback
|
|
|
|
** was specified along with the original pointer, it is invoked at this
|
|
|
|
** point.
|
|
|
|
**
|
|
|
|
** The xDelete callback, if one is specified, is also invoked on the
|
|
|
|
** auxiliary data pointer after the FTS5 query has finished.
|
|
|
|
**
|
2014-12-23 00:01:52 +03:00
|
|
|
** If an error (e.g. an OOM condition) occurs within this function, an
|
|
|
|
** the auxiliary data is set to NULL and an error code returned. If the
|
|
|
|
** xDelete parameter was not NULL, it is invoked on the auxiliary data
|
|
|
|
** pointer before returning.
|
|
|
|
**
|
2014-07-26 00:30:47 +04:00
|
|
|
**
|
|
|
|
** xGetAuxdata(pFts5, bClear)
|
|
|
|
**
|
|
|
|
** Returns the current auxiliary data pointer for the fts5 extension
|
|
|
|
** function. See the xSetAuxdata() method for details.
|
|
|
|
**
|
|
|
|
** If the bClear argument is non-zero, then the auxiliary data is cleared
|
|
|
|
** (set to NULL) before this function returns. In this case the xDelete,
|
|
|
|
** if any, is not invoked.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** xRowCount(pFts5, pnRow)
|
|
|
|
**
|
|
|
|
** This function is used to retrieve the total number of rows in the table.
|
|
|
|
** In other words, the same value that would be returned by:
|
|
|
|
**
|
|
|
|
** SELECT count(*) FROM ftstable;
|
2015-08-12 15:11:28 +03:00
|
|
|
**
|
|
|
|
** xPhraseFirst()
|
|
|
|
** This function is used, along with type Fts5PhraseIter and the xPhraseNext
|
|
|
|
** method, to iterate through all instances of a single query phrase within
|
|
|
|
** the current row. This is the same information as is accessible via the
|
|
|
|
** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
|
|
|
|
** to use, this API may be faster under some circumstances. To iterate
|
|
|
|
** through instances of phrase iPhrase, use the following code:
|
|
|
|
**
|
|
|
|
** Fts5PhraseIter iter;
|
|
|
|
** int iCol, iOff;
|
|
|
|
** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
|
|
|
|
** iOff>=0;
|
|
|
|
** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
|
|
|
|
** ){
|
|
|
|
** // An instance of phrase iPhrase at offset iOff of column iCol
|
|
|
|
** }
|
|
|
|
**
|
|
|
|
** The Fts5PhraseIter structure is defined above. Applications should not
|
|
|
|
** modify this structure directly - it should only be used as shown above
|
|
|
|
** with the xPhraseFirst() and xPhraseNext() API methods.
|
|
|
|
**
|
|
|
|
** xPhraseNext()
|
|
|
|
** See xPhraseFirst above.
|
2014-07-16 23:15:57 +04:00
|
|
|
*/
|
|
|
|
struct Fts5ExtensionApi {
|
|
|
|
int iVersion; /* Currently always set to 1 */
|
|
|
|
|
|
|
|
void *(*xUserData)(Fts5Context*);
|
|
|
|
|
|
|
|
int (*xColumnCount)(Fts5Context*);
|
2014-07-26 00:30:47 +04:00
|
|
|
int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
|
|
|
|
int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
|
|
|
|
|
2014-07-16 23:15:57 +04:00
|
|
|
int (*xTokenize)(Fts5Context*,
|
|
|
|
const char *pText, int nText, /* Text to tokenize */
|
|
|
|
void *pCtx, /* Context passed to xToken() */
|
2015-08-29 18:44:27 +03:00
|
|
|
int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
|
2014-07-16 23:15:57 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
int (*xPhraseCount)(Fts5Context*);
|
|
|
|
int (*xPhraseSize)(Fts5Context*, int iPhrase);
|
|
|
|
|
2014-11-24 19:24:33 +03:00
|
|
|
int (*xInstCount)(Fts5Context*, int *pnInst);
|
|
|
|
int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
|
|
|
|
|
2014-07-16 23:15:57 +04:00
|
|
|
sqlite3_int64 (*xRowid)(Fts5Context*);
|
|
|
|
int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
|
|
|
|
int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
|
2014-07-26 00:30:47 +04:00
|
|
|
|
|
|
|
int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
|
|
|
|
int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
|
|
|
|
);
|
|
|
|
int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
|
|
|
|
void *(*xGetAuxdata)(Fts5Context*, int bClear);
|
2015-08-12 15:11:28 +03:00
|
|
|
|
|
|
|
void (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
|
|
|
|
void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
|
2014-07-16 23:15:57 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** CUSTOM AUXILIARY FUNCTIONS
|
|
|
|
*************************************************************************/
|
2014-08-25 23:58:54 +04:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
** CUSTOM TOKENIZERS
|
|
|
|
**
|
|
|
|
** Applications may also register custom tokenizer types. A tokenizer
|
|
|
|
** is registered by providing fts5 with a populated instance of the
|
2015-05-18 20:50:17 +03:00
|
|
|
** following structure. All structure methods must be defined, setting
|
|
|
|
** any member of the fts5_tokenizer struct to NULL leads to undefined
|
|
|
|
** behaviour. The structure methods are expected to function as follows:
|
2014-08-25 23:58:54 +04:00
|
|
|
**
|
|
|
|
** xCreate:
|
|
|
|
** This function is used to allocate and inititalize a tokenizer instance.
|
|
|
|
** A tokenizer instance is required to actually tokenize text.
|
|
|
|
**
|
|
|
|
** The first argument passed to this function is a copy of the (void*)
|
|
|
|
** pointer provided by the application when the fts5_tokenizer object
|
2015-01-10 23:34:27 +03:00
|
|
|
** was registered with FTS5 (the third argument to xCreateTokenizer()).
|
|
|
|
** The second and third arguments are an array of nul-terminated strings
|
|
|
|
** containing the tokenizer arguments, if any, specified following the
|
|
|
|
** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
|
|
|
|
** to create the FTS5 table.
|
2014-08-25 23:58:54 +04:00
|
|
|
**
|
|
|
|
** The final argument is an output variable. If successful, (*ppOut)
|
|
|
|
** should be set to point to the new tokenizer handle and SQLITE_OK
|
|
|
|
** returned. If an error occurs, some value other than SQLITE_OK should
|
|
|
|
** be returned. In this case, fts5 assumes that the final value of *ppOut
|
|
|
|
** is undefined.
|
|
|
|
**
|
|
|
|
** xDelete:
|
|
|
|
** This function is invoked to delete a tokenizer handle previously
|
|
|
|
** allocated using xCreate(). Fts5 guarantees that this function will
|
|
|
|
** be invoked exactly once for each successful call to xCreate().
|
|
|
|
**
|
|
|
|
** xTokenize:
|
|
|
|
** This function is expected to tokenize the nText byte string indicated
|
|
|
|
** by argument pText. pText may not be nul-terminated. The first argument
|
|
|
|
** passed to this function is a pointer to an Fts5Tokenizer object returned
|
|
|
|
** by an earlier call to xCreate().
|
|
|
|
**
|
|
|
|
** For each token in the input string, the supplied callback xToken() must
|
|
|
|
** be invoked. The first argument to it should be a copy of the pointer
|
|
|
|
** passed as the second argument to xTokenize(). The next two arguments
|
|
|
|
** are a pointer to a buffer containing the token text, and the size of
|
|
|
|
** the token in bytes. The 4th and 5th arguments are the byte offsets of
|
|
|
|
** the first byte of and first byte immediately following the text from
|
2015-01-10 23:34:27 +03:00
|
|
|
** which the token is derived within the input.
|
2014-08-25 23:58:54 +04:00
|
|
|
**
|
2015-01-10 23:34:27 +03:00
|
|
|
** FTS5 assumes the xToken() callback is invoked for each token in the
|
|
|
|
** order that they occur within the input text.
|
2014-08-25 23:58:54 +04:00
|
|
|
**
|
|
|
|
** If an xToken() callback returns any value other than SQLITE_OK, then
|
|
|
|
** the tokenization should be abandoned and the xTokenize() method should
|
|
|
|
** immediately return a copy of the xToken() return value. Or, if the
|
|
|
|
** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
|
|
|
|
** if an error occurs with the xTokenize() implementation itself, it
|
|
|
|
** may abandon the tokenization and return any error code other than
|
|
|
|
** SQLITE_OK or SQLITE_DONE.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
typedef struct Fts5Tokenizer Fts5Tokenizer;
|
2015-01-10 23:34:27 +03:00
|
|
|
typedef struct fts5_tokenizer fts5_tokenizer;
|
2014-08-25 23:58:54 +04:00
|
|
|
struct fts5_tokenizer {
|
|
|
|
int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
|
|
|
|
void (*xDelete)(Fts5Tokenizer*);
|
|
|
|
int (*xTokenize)(Fts5Tokenizer*,
|
|
|
|
void *pCtx,
|
2015-08-28 22:56:47 +03:00
|
|
|
int flags,
|
2014-08-25 23:58:54 +04:00
|
|
|
const char *pText, int nText,
|
|
|
|
int (*xToken)(
|
|
|
|
void *pCtx, /* Copy of 2nd argument to xTokenize() */
|
2015-08-29 18:44:27 +03:00
|
|
|
int tflags, /* Mask of FTS5_TOKEN_* flags */
|
2014-08-25 23:58:54 +04:00
|
|
|
const char *pToken, /* Pointer to buffer containing token */
|
|
|
|
int nToken, /* Size of token in bytes */
|
|
|
|
int iStart, /* Byte offset of token within input text */
|
2015-08-29 18:44:27 +03:00
|
|
|
int iEnd /* Byte offset of end of token within input text */
|
2014-08-25 23:58:54 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-08-29 18:44:27 +03:00
|
|
|
/* Flags that may be passed as the third argument to xTokenize() */
|
2015-08-28 22:56:47 +03:00
|
|
|
#define FTS5_TOKENIZE_QUERY 0x0001
|
|
|
|
#define FTS5_TOKENIZE_PREFIX 0x0002
|
|
|
|
#define FTS5_TOKENIZE_DOCUMENT 0x0004
|
|
|
|
#define FTS5_TOKENIZE_AUX 0x0008
|
|
|
|
|
2015-08-29 18:44:27 +03:00
|
|
|
/* Flags that may be passed by the tokenizer implementation back to FTS5
|
|
|
|
** as the third argument to the supplied xToken callback. */
|
|
|
|
#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
|
|
|
|
|
2014-08-25 23:58:54 +04:00
|
|
|
/*
|
|
|
|
** END OF CUSTOM TOKENIZERS
|
|
|
|
*************************************************************************/
|
|
|
|
|
2014-11-15 23:07:31 +03:00
|
|
|
/*************************************************************************
|
|
|
|
** FTS5 EXTENSION REGISTRATION API
|
|
|
|
*/
|
|
|
|
typedef struct fts5_api fts5_api;
|
|
|
|
struct fts5_api {
|
2015-08-28 22:56:47 +03:00
|
|
|
int iVersion; /* Currently always set to 2 */
|
2014-11-15 23:07:31 +03:00
|
|
|
|
|
|
|
/* Create a new tokenizer */
|
|
|
|
int (*xCreateTokenizer)(
|
|
|
|
fts5_api *pApi,
|
|
|
|
const char *zName,
|
|
|
|
void *pContext,
|
|
|
|
fts5_tokenizer *pTokenizer,
|
|
|
|
void (*xDestroy)(void*)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Find an existing tokenizer */
|
|
|
|
int (*xFindTokenizer)(
|
|
|
|
fts5_api *pApi,
|
|
|
|
const char *zName,
|
2014-12-29 14:24:46 +03:00
|
|
|
void **ppContext,
|
2014-11-15 23:07:31 +03:00
|
|
|
fts5_tokenizer *pTokenizer
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Create a new auxiliary function */
|
|
|
|
int (*xCreateFunction)(
|
|
|
|
fts5_api *pApi,
|
|
|
|
const char *zName,
|
|
|
|
void *pContext,
|
|
|
|
fts5_extension_function xFunction,
|
|
|
|
void (*xDestroy)(void*)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** END OF REGISTRATION API
|
|
|
|
*************************************************************************/
|
|
|
|
|
2014-07-16 23:15:57 +04:00
|
|
|
#endif /* _FTS5_H */
|
|
|
|
|