2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** 2014 Jun 09
|
|
|
|
**
|
|
|
|
** 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 is an SQLite module implementing full-text search.
|
|
|
|
*/
|
|
|
|
|
2015-03-04 11:29:24 +03:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
#include "fts5Int.h"
|
|
|
|
|
2015-07-15 22:46:02 +03:00
|
|
|
#define FTS5_DEFAULT_PAGE_SIZE 4050
|
2014-11-28 23:01:13 +03:00
|
|
|
#define FTS5_DEFAULT_AUTOMERGE 4
|
2016-03-09 23:54:14 +03:00
|
|
|
#define FTS5_DEFAULT_USERMERGE 4
|
2015-01-23 09:50:33 +03:00
|
|
|
#define FTS5_DEFAULT_CRISISMERGE 16
|
2015-11-05 21:09:16 +03:00
|
|
|
#define FTS5_DEFAULT_HASHSIZE (1024*1024)
|
2014-11-28 23:01:13 +03:00
|
|
|
|
|
|
|
/* Maximum allowed page size */
|
2019-10-09 18:26:45 +03:00
|
|
|
#define FTS5_MAX_PAGE_SIZE (64*1024)
|
2014-11-27 23:03:45 +03:00
|
|
|
|
2014-12-29 14:24:46 +03:00
|
|
|
static int fts5_iswhitespace(char x){
|
|
|
|
return (x==' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5_isopenquote(char x){
|
|
|
|
return (x=='"' || x=='\'' || x=='[' || x=='`');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Argument pIn points to a character that is part of a nul-terminated
|
|
|
|
** string. Return a pointer to the first character following *pIn in
|
|
|
|
** the string that is not a white-space character.
|
|
|
|
*/
|
|
|
|
static const char *fts5ConfigSkipWhitespace(const char *pIn){
|
|
|
|
const char *p = pIn;
|
|
|
|
if( p ){
|
|
|
|
while( fts5_iswhitespace(*p) ){ p++; }
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Argument pIn points to a character that is part of a nul-terminated
|
|
|
|
** string. Return a pointer to the first character following *pIn in
|
|
|
|
** the string that is not a "bareword" character.
|
|
|
|
*/
|
|
|
|
static const char *fts5ConfigSkipBareword(const char *pIn){
|
|
|
|
const char *p = pIn;
|
2015-05-18 20:50:17 +03:00
|
|
|
while ( sqlite3Fts5IsBareword(*p) ) p++;
|
2014-12-29 14:24:46 +03:00
|
|
|
if( p==pIn ) p = 0;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5_isdigit(char a){
|
|
|
|
return (a>='0' && a<='9');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char *fts5ConfigSkipLiteral(const char *pIn){
|
|
|
|
const char *p = pIn;
|
2015-05-18 20:50:17 +03:00
|
|
|
switch( *p ){
|
|
|
|
case 'n': case 'N':
|
|
|
|
if( sqlite3_strnicmp("null", p, 4)==0 ){
|
|
|
|
p = &p[4];
|
|
|
|
}else{
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x': case 'X':
|
|
|
|
p++;
|
|
|
|
if( *p=='\'' ){
|
2014-12-29 14:24:46 +03:00
|
|
|
p++;
|
2015-05-18 20:50:17 +03:00
|
|
|
while( (*p>='a' && *p<='f')
|
|
|
|
|| (*p>='A' && *p<='F')
|
|
|
|
|| (*p>='0' && *p<='9')
|
|
|
|
){
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if( *p=='\'' && 0==((p-pIn)%2) ){
|
2014-12-29 14:24:46 +03:00
|
|
|
p++;
|
|
|
|
}else{
|
|
|
|
p = 0;
|
|
|
|
}
|
2015-05-18 20:50:17 +03:00
|
|
|
}else{
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
break;
|
2014-12-29 14:24:46 +03:00
|
|
|
|
2015-05-18 20:50:17 +03:00
|
|
|
case '\'':
|
|
|
|
p++;
|
|
|
|
while( p ){
|
|
|
|
if( *p=='\'' ){
|
2014-12-29 14:24:46 +03:00
|
|
|
p++;
|
2015-05-18 20:50:17 +03:00
|
|
|
if( *p!='\'' ) break;
|
2014-12-29 14:24:46 +03:00
|
|
|
}
|
2015-05-18 20:50:17 +03:00
|
|
|
p++;
|
|
|
|
if( *p==0 ) p = 0;
|
|
|
|
}
|
|
|
|
break;
|
2014-12-29 14:24:46 +03:00
|
|
|
|
2015-05-18 20:50:17 +03:00
|
|
|
default:
|
|
|
|
/* maybe a number */
|
|
|
|
if( *p=='+' || *p=='-' ) p++;
|
|
|
|
while( fts5_isdigit(*p) ) p++;
|
2014-12-29 14:24:46 +03:00
|
|
|
|
2015-05-18 20:50:17 +03:00
|
|
|
/* At this point, if the literal was an integer, the parse is
|
|
|
|
** finished. Or, if it is a floating point value, it may continue
|
|
|
|
** with either a decimal point or an 'E' character. */
|
|
|
|
if( *p=='.' && fts5_isdigit(p[1]) ){
|
|
|
|
p += 2;
|
|
|
|
while( fts5_isdigit(*p) ) p++;
|
|
|
|
}
|
|
|
|
if( p==pIn ) p = 0;
|
2014-12-29 14:24:46 +03:00
|
|
|
|
2015-05-18 20:50:17 +03:00
|
|
|
break;
|
2014-12-29 14:24:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-04-24 22:41:43 +03:00
|
|
|
/*
|
|
|
|
** The first character of the string pointed to by argument z is guaranteed
|
|
|
|
** to be an open-quote character (see function fts5_isopenquote()).
|
|
|
|
**
|
|
|
|
** This function searches for the corresponding close-quote character within
|
|
|
|
** the string and, if found, dequotes the string in place and adds a new
|
|
|
|
** nul-terminator byte.
|
|
|
|
**
|
|
|
|
** If the close-quote is found, the value returned is the byte offset of
|
|
|
|
** the character immediately following it. Or, if the close-quote is not
|
|
|
|
** found, -1 is returned. If -1 is returned, the buffer is left in an
|
|
|
|
** undefined state.
|
|
|
|
*/
|
2014-12-29 14:24:46 +03:00
|
|
|
static int fts5Dequote(char *z){
|
|
|
|
char q;
|
|
|
|
int iIn = 1;
|
|
|
|
int iOut = 0;
|
|
|
|
q = z[0];
|
|
|
|
|
2015-04-24 22:41:43 +03:00
|
|
|
/* Set stack variable q to the close-quote character */
|
2014-12-29 14:24:46 +03:00
|
|
|
assert( q=='[' || q=='\'' || q=='"' || q=='`' );
|
|
|
|
if( q=='[' ) q = ']';
|
|
|
|
|
2019-12-10 06:40:11 +03:00
|
|
|
while( z[iIn] ){
|
2014-12-29 14:24:46 +03:00
|
|
|
if( z[iIn]==q ){
|
|
|
|
if( z[iIn+1]!=q ){
|
2015-04-24 22:41:43 +03:00
|
|
|
/* Character iIn was the close quote. */
|
2015-05-18 20:50:17 +03:00
|
|
|
iIn++;
|
|
|
|
break;
|
2015-04-24 22:41:43 +03:00
|
|
|
}else{
|
|
|
|
/* Character iIn and iIn+1 form an escaped quote character. Skip
|
|
|
|
** the input cursor past both and copy a single quote character
|
|
|
|
** to the output buffer. */
|
|
|
|
iIn += 2;
|
|
|
|
z[iOut++] = q;
|
2014-12-29 14:24:46 +03:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
z[iOut++] = z[iIn++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 22:41:43 +03:00
|
|
|
z[iOut] = '\0';
|
2015-05-18 20:50:17 +03:00
|
|
|
return iIn;
|
2014-12-29 14:24:46 +03:00
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** Convert an SQL-style quoted string into a normal string by removing
|
|
|
|
** the quote characters. The conversion is done in-place. If the
|
|
|
|
** input does not begin with a quote character, then this routine
|
|
|
|
** is a no-op.
|
|
|
|
**
|
|
|
|
** Examples:
|
|
|
|
**
|
|
|
|
** "abc" becomes abc
|
|
|
|
** 'xyz' becomes xyz
|
|
|
|
** [pqr] becomes pqr
|
|
|
|
** `mno` becomes mno
|
|
|
|
*/
|
|
|
|
void sqlite3Fts5Dequote(char *z){
|
|
|
|
char quote; /* Quote character (if any ) */
|
|
|
|
|
2014-12-29 14:24:46 +03:00
|
|
|
assert( 0==fts5_iswhitespace(z[0]) );
|
2014-06-23 15:33:22 +04:00
|
|
|
quote = z[0];
|
|
|
|
if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
|
2014-12-29 14:24:46 +03:00
|
|
|
fts5Dequote(z);
|
|
|
|
}
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2015-12-28 22:55:00 +03:00
|
|
|
|
|
|
|
struct Fts5Enum {
|
|
|
|
const char *zName;
|
|
|
|
int eVal;
|
|
|
|
};
|
|
|
|
typedef struct Fts5Enum Fts5Enum;
|
|
|
|
|
|
|
|
static int fts5ConfigSetEnum(
|
|
|
|
const Fts5Enum *aEnum,
|
|
|
|
const char *zEnum,
|
|
|
|
int *peVal
|
|
|
|
){
|
2016-02-11 18:37:18 +03:00
|
|
|
int nEnum = (int)strlen(zEnum);
|
2015-12-28 22:55:00 +03:00
|
|
|
int i;
|
|
|
|
int iVal = -1;
|
|
|
|
|
|
|
|
for(i=0; aEnum[i].zName; i++){
|
|
|
|
if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
|
|
|
|
if( iVal>=0 ) return SQLITE_ERROR;
|
|
|
|
iVal = aEnum[i].eVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*peVal = iVal;
|
|
|
|
return iVal<0 ? SQLITE_ERROR : SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
2015-06-09 23:58:39 +03:00
|
|
|
** Parse a "special" CREATE VIRTUAL TABLE directive and update
|
2014-06-23 15:33:22 +04:00
|
|
|
** configuration object pConfig as appropriate.
|
|
|
|
**
|
|
|
|
** If successful, object pConfig is updated and SQLITE_OK returned. If
|
|
|
|
** an error occurs, an SQLite error code is returned and an error message
|
|
|
|
** may be left in *pzErr. It is the responsibility of the caller to
|
|
|
|
** eventually free any such error message using sqlite3_free().
|
|
|
|
*/
|
|
|
|
static int fts5ConfigParseSpecial(
|
2014-12-29 14:24:46 +03:00
|
|
|
Fts5Global *pGlobal,
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5Config *pConfig, /* Configuration object to update */
|
2014-12-29 14:24:46 +03:00
|
|
|
const char *zCmd, /* Special command to parse */
|
|
|
|
const char *zArg, /* Argument to parse */
|
2014-06-23 15:33:22 +04:00
|
|
|
char **pzErr /* OUT: Error message */
|
|
|
|
){
|
2015-06-09 23:58:39 +03:00
|
|
|
int rc = SQLITE_OK;
|
2015-10-14 23:34:57 +03:00
|
|
|
int nCmd = (int)strlen(zCmd);
|
2014-12-29 14:24:46 +03:00
|
|
|
if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
|
2014-12-18 21:25:48 +03:00
|
|
|
const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
|
2014-12-29 14:24:46 +03:00
|
|
|
const char *p;
|
2015-11-25 14:56:24 +03:00
|
|
|
int bFirst = 1;
|
|
|
|
if( pConfig->aPrefix==0 ){
|
2014-12-18 21:25:48 +03:00
|
|
|
pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
|
2015-11-25 14:56:24 +03:00
|
|
|
if( rc ) return rc;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-11-25 14:56:24 +03:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
p = zArg;
|
2015-11-25 14:56:24 +03:00
|
|
|
while( 1 ){
|
2014-06-23 15:33:22 +04:00
|
|
|
int nPre = 0;
|
2015-11-25 14:56:24 +03:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
while( p[0]==' ' ) p++;
|
2015-11-25 14:56:24 +03:00
|
|
|
if( bFirst==0 && p[0]==',' ){
|
2014-06-23 15:33:22 +04:00
|
|
|
p++;
|
2015-11-25 14:56:24 +03:00
|
|
|
while( p[0]==' ' ) p++;
|
|
|
|
}else if( p[0]=='\0' ){
|
|
|
|
break;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-11-25 14:56:24 +03:00
|
|
|
if( p[0]<'0' || p[0]>'9' ){
|
2014-06-23 15:33:22 +04:00
|
|
|
*pzErr = sqlite3_mprintf("malformed prefix=... directive");
|
2014-12-18 21:25:48 +03:00
|
|
|
rc = SQLITE_ERROR;
|
2015-11-25 14:56:24 +03:00
|
|
|
break;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-11-25 14:56:24 +03:00
|
|
|
|
|
|
|
if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
|
|
|
|
*pzErr = sqlite3_mprintf(
|
|
|
|
"too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
|
|
|
|
);
|
2014-12-18 21:25:48 +03:00
|
|
|
rc = SQLITE_ERROR;
|
2015-11-25 14:56:24 +03:00
|
|
|
break;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-11-25 14:56:24 +03:00
|
|
|
|
|
|
|
while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
|
|
|
|
nPre = nPre*10 + (p[0] - '0');
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2016-01-16 21:58:51 +03:00
|
|
|
if( nPre<=0 || nPre>=1000 ){
|
2015-11-25 14:56:24 +03:00
|
|
|
*pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
pConfig->aPrefix[pConfig->nPrefix] = nPre;
|
|
|
|
pConfig->nPrefix++;
|
2015-11-25 14:56:24 +03:00
|
|
|
bFirst = 0;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-11-25 14:56:24 +03:00
|
|
|
assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES );
|
2014-12-18 21:25:48 +03:00
|
|
|
return rc;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
2014-12-29 14:24:46 +03:00
|
|
|
if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
|
|
|
|
const char *p = (const char*)zArg;
|
2019-01-08 23:02:48 +03:00
|
|
|
sqlite3_int64 nArg = strlen(zArg) + 1;
|
2014-12-29 14:24:46 +03:00
|
|
|
char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg);
|
|
|
|
char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2);
|
|
|
|
char *pSpace = pDel;
|
|
|
|
|
|
|
|
if( azArg && pSpace ){
|
|
|
|
if( pConfig->pTok ){
|
|
|
|
*pzErr = sqlite3_mprintf("multiple tokenize=... directives");
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}else{
|
|
|
|
for(nArg=0; p && *p; nArg++){
|
|
|
|
const char *p2 = fts5ConfigSkipWhitespace(p);
|
2015-05-18 20:50:17 +03:00
|
|
|
if( *p2=='\'' ){
|
2014-12-29 14:24:46 +03:00
|
|
|
p = fts5ConfigSkipLiteral(p2);
|
|
|
|
}else{
|
|
|
|
p = fts5ConfigSkipBareword(p2);
|
|
|
|
}
|
|
|
|
if( p ){
|
|
|
|
memcpy(pSpace, p2, p-p2);
|
|
|
|
azArg[nArg] = pSpace;
|
|
|
|
sqlite3Fts5Dequote(pSpace);
|
|
|
|
pSpace += (p - p2) + 1;
|
|
|
|
p = fts5ConfigSkipWhitespace(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( p==0 ){
|
|
|
|
*pzErr = sqlite3_mprintf("parse error in tokenize directive");
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}else{
|
|
|
|
rc = sqlite3Fts5GetTokenizer(pGlobal,
|
2020-09-30 23:35:37 +03:00
|
|
|
(const char**)azArg, (int)nArg, pConfig,
|
2015-04-29 23:54:08 +03:00
|
|
|
pzErr
|
2014-12-29 14:24:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_free(azArg);
|
|
|
|
sqlite3_free(pDel);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
|
2015-01-05 23:41:39 +03:00
|
|
|
if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
|
2015-01-03 23:44:58 +03:00
|
|
|
*pzErr = sqlite3_mprintf("multiple content=... directives");
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}else{
|
2015-01-05 23:41:39 +03:00
|
|
|
if( zArg[0] ){
|
|
|
|
pConfig->eContent = FTS5_CONTENT_EXTERNAL;
|
2015-06-23 21:47:55 +03:00
|
|
|
pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
|
2015-01-05 23:41:39 +03:00
|
|
|
}else{
|
|
|
|
pConfig->eContent = FTS5_CONTENT_NONE;
|
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
|
|
|
|
if( pConfig->zContentRowid ){
|
|
|
|
*pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}else{
|
2015-05-02 23:35:24 +03:00
|
|
|
pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
|
2015-01-03 23:44:58 +03:00
|
|
|
}
|
|
|
|
return rc;
|
2014-12-03 20:27:35 +03:00
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
|
2015-06-09 23:58:39 +03:00
|
|
|
if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
|
|
|
|
if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
|
|
|
|
*pzErr = sqlite3_mprintf("malformed columnsize=... directive");
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}else{
|
|
|
|
pConfig->bColumnsize = (zArg[0]=='1');
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-12-28 22:55:00 +03:00
|
|
|
if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
|
|
|
|
const Fts5Enum aDetail[] = {
|
|
|
|
{ "none", FTS5_DETAIL_NONE },
|
|
|
|
{ "full", FTS5_DETAIL_FULL },
|
|
|
|
{ "columns", FTS5_DETAIL_COLUMNS },
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
2016-01-14 17:15:54 +03:00
|
|
|
if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
|
2015-12-28 22:55:00 +03:00
|
|
|
*pzErr = sqlite3_mprintf("malformed detail=... directive");
|
2015-12-17 23:36:13 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-01-06 22:08:26 +03:00
|
|
|
*pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
|
2015-01-03 23:44:58 +03:00
|
|
|
return SQLITE_ERROR;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Allocate an instance of the default tokenizer ("simple") at
|
|
|
|
** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
|
|
|
|
** code if an error occurs.
|
|
|
|
*/
|
2014-11-15 23:07:31 +03:00
|
|
|
static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
|
2014-12-29 14:24:46 +03:00
|
|
|
assert( pConfig->pTok==0 && pConfig->pTokApi==0 );
|
2020-09-30 23:35:37 +03:00
|
|
|
return sqlite3Fts5GetTokenizer(pGlobal, 0, 0, pConfig, 0);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
2015-04-24 22:41:43 +03:00
|
|
|
/*
|
|
|
|
** Gobble up the first bareword or quoted word from the input buffer zIn.
|
|
|
|
** Return a pointer to the character immediately following the last in
|
|
|
|
** the gobbled word if successful, or a NULL pointer otherwise (failed
|
|
|
|
** to find close-quote character).
|
|
|
|
**
|
|
|
|
** Before returning, set pzOut to point to a new buffer containing a
|
|
|
|
** nul-terminated, dequoted copy of the gobbled word. If the word was
|
|
|
|
** quoted, *pbQuoted is also set to 1 before returning.
|
|
|
|
**
|
|
|
|
** If *pRc is other than SQLITE_OK when this function is called, it is
|
|
|
|
** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
|
|
|
|
** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
|
|
|
|
** set if a parse error (failed to find close quote) occurs.
|
|
|
|
*/
|
|
|
|
static const char *fts5ConfigGobbleWord(
|
2015-04-24 23:18:21 +03:00
|
|
|
int *pRc, /* IN/OUT: Error code */
|
|
|
|
const char *zIn, /* Buffer to gobble string/bareword from */
|
|
|
|
char **pzOut, /* OUT: malloc'd buffer containing str/bw */
|
|
|
|
int *pbQuoted /* OUT: Set to true if dequoting required */
|
2015-04-24 22:41:43 +03:00
|
|
|
){
|
|
|
|
const char *zRet = 0;
|
2015-05-18 20:50:17 +03:00
|
|
|
|
2019-01-08 23:02:48 +03:00
|
|
|
sqlite3_int64 nIn = strlen(zIn);
|
|
|
|
char *zOut = sqlite3_malloc64(nIn+1);
|
2015-05-18 20:50:17 +03:00
|
|
|
|
|
|
|
assert( *pRc==SQLITE_OK );
|
2015-04-24 22:41:43 +03:00
|
|
|
*pbQuoted = 0;
|
|
|
|
*pzOut = 0;
|
|
|
|
|
2015-05-18 20:50:17 +03:00
|
|
|
if( zOut==0 ){
|
|
|
|
*pRc = SQLITE_NOMEM;
|
|
|
|
}else{
|
2019-03-20 08:45:03 +03:00
|
|
|
memcpy(zOut, zIn, (size_t)(nIn+1));
|
2015-05-18 20:50:17 +03:00
|
|
|
if( fts5_isopenquote(zOut[0]) ){
|
|
|
|
int ii = fts5Dequote(zOut);
|
|
|
|
zRet = &zIn[ii];
|
|
|
|
*pbQuoted = 1;
|
2015-04-24 22:41:43 +03:00
|
|
|
}else{
|
2015-05-18 20:50:17 +03:00
|
|
|
zRet = fts5ConfigSkipBareword(zIn);
|
2016-03-12 19:32:16 +03:00
|
|
|
if( zRet ){
|
|
|
|
zOut[zRet-zIn] = '\0';
|
|
|
|
}
|
2015-04-24 22:41:43 +03:00
|
|
|
}
|
2015-05-18 20:50:17 +03:00
|
|
|
}
|
2015-04-24 22:41:43 +03:00
|
|
|
|
2015-05-18 20:50:17 +03:00
|
|
|
if( zRet==0 ){
|
|
|
|
sqlite3_free(zOut);
|
|
|
|
}else{
|
|
|
|
*pzOut = zOut;
|
2015-04-24 22:41:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return zRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5ConfigParseColumn(
|
|
|
|
Fts5Config *p,
|
|
|
|
char *zCol,
|
|
|
|
char *zArg,
|
|
|
|
char **pzErr
|
|
|
|
){
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME)
|
|
|
|
|| 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME)
|
|
|
|
){
|
|
|
|
*pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}else if( zArg ){
|
|
|
|
if( 0==sqlite3_stricmp(zArg, "unindexed") ){
|
|
|
|
p->abUnindexed[p->nCol] = 1;
|
|
|
|
}else{
|
|
|
|
*pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->azCol[p->nCol++] = zCol;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-04-27 19:21:49 +03:00
|
|
|
/*
|
|
|
|
** Populate the Fts5Config.zContentExprlist string.
|
|
|
|
*/
|
|
|
|
static int fts5ConfigMakeExprlist(Fts5Config *p){
|
|
|
|
int i;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
Fts5Buffer buf = {0, 0, 0};
|
|
|
|
|
|
|
|
sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
|
|
|
|
if( p->eContent!=FTS5_CONTENT_NONE ){
|
|
|
|
for(i=0; i<p->nCol; i++){
|
|
|
|
if( p->eContent==FTS5_CONTENT_EXTERNAL ){
|
|
|
|
sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
|
|
|
|
}else{
|
|
|
|
sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( p->zContentExprlist==0 );
|
|
|
|
p->zContentExprlist = (char*)buf.p;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** Arguments nArg/azArg contain the string arguments passed to the xCreate
|
|
|
|
** or xConnect method of the virtual table. This function attempts to
|
|
|
|
** allocate an instance of Fts5Config containing the results of parsing
|
|
|
|
** those arguments.
|
|
|
|
**
|
|
|
|
** If successful, SQLITE_OK is returned and *ppOut is set to point to the
|
|
|
|
** new Fts5Config object. If an error occurs, an SQLite error code is
|
|
|
|
** returned, *ppOut is set to NULL and an error message may be left in
|
|
|
|
** *pzErr. It is the responsibility of the caller to eventually free any
|
|
|
|
** such error message using sqlite3_free().
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5ConfigParse(
|
2014-11-15 23:07:31 +03:00
|
|
|
Fts5Global *pGlobal,
|
2014-06-23 15:33:22 +04:00
|
|
|
sqlite3 *db,
|
|
|
|
int nArg, /* Number of arguments */
|
|
|
|
const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
|
|
|
|
Fts5Config **ppOut, /* OUT: Results of parse */
|
|
|
|
char **pzErr /* OUT: Error message */
|
|
|
|
){
|
|
|
|
int rc = SQLITE_OK; /* Return code */
|
|
|
|
Fts5Config *pRet; /* New object to return */
|
2014-12-29 14:24:46 +03:00
|
|
|
int i;
|
2019-01-08 23:02:48 +03:00
|
|
|
sqlite3_int64 nByte;
|
2014-06-23 15:33:22 +04:00
|
|
|
|
|
|
|
*ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
|
|
|
|
if( pRet==0 ) return SQLITE_NOMEM;
|
|
|
|
memset(pRet, 0, sizeof(Fts5Config));
|
|
|
|
pRet->db = db;
|
2014-11-28 23:01:13 +03:00
|
|
|
pRet->iCookie = -1;
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2015-04-24 22:41:43 +03:00
|
|
|
nByte = nArg * (sizeof(char*) + sizeof(u8));
|
|
|
|
pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
|
2021-04-12 21:32:33 +03:00
|
|
|
pRet->abUnindexed = pRet->azCol ? (u8*)&pRet->azCol[nArg] : 0;
|
2015-05-02 23:35:24 +03:00
|
|
|
pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
|
|
|
|
pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
|
2015-06-09 23:58:39 +03:00
|
|
|
pRet->bColumnsize = 1;
|
2015-12-28 22:55:00 +03:00
|
|
|
pRet->eDetail = FTS5_DETAIL_FULL;
|
2015-09-08 22:55:26 +03:00
|
|
|
#ifdef SQLITE_DEBUG
|
|
|
|
pRet->bPrefixIndex = 1;
|
|
|
|
#endif
|
2014-12-29 14:24:46 +03:00
|
|
|
if( rc==SQLITE_OK && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){
|
|
|
|
*pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2014-12-29 14:24:46 +03:00
|
|
|
for(i=3; rc==SQLITE_OK && i<nArg; i++){
|
2015-04-24 22:41:43 +03:00
|
|
|
const char *zOrig = azArg[i];
|
|
|
|
const char *z;
|
|
|
|
char *zOne = 0;
|
|
|
|
char *zTwo = 0;
|
|
|
|
int bOption = 0;
|
|
|
|
int bMustBeCol = 0;
|
|
|
|
|
|
|
|
z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
|
|
|
|
z = fts5ConfigSkipWhitespace(z);
|
|
|
|
if( z && *z=='=' ){
|
|
|
|
bOption = 1;
|
2021-10-05 20:41:12 +03:00
|
|
|
assert( zOne!=0 );
|
2015-04-24 22:41:43 +03:00
|
|
|
z++;
|
|
|
|
if( bMustBeCol ) z = 0;
|
|
|
|
}
|
|
|
|
z = fts5ConfigSkipWhitespace(z);
|
|
|
|
if( z && z[0] ){
|
|
|
|
int bDummy;
|
|
|
|
z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
|
|
|
|
if( z && z[0] ) z = 0;
|
|
|
|
}
|
2014-12-29 14:24:46 +03:00
|
|
|
|
2015-04-24 22:41:43 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
if( z==0 ){
|
|
|
|
*pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
|
2014-12-29 14:24:46 +03:00
|
|
|
rc = SQLITE_ERROR;
|
2015-04-24 22:41:43 +03:00
|
|
|
}else{
|
|
|
|
if( bOption ){
|
2021-10-05 20:41:12 +03:00
|
|
|
rc = fts5ConfigParseSpecial(pGlobal, pRet,
|
|
|
|
ALWAYS(zOne)?zOne:"",
|
|
|
|
zTwo?zTwo:"",
|
|
|
|
pzErr
|
|
|
|
);
|
2014-12-29 14:24:46 +03:00
|
|
|
}else{
|
2015-04-24 22:41:43 +03:00
|
|
|
rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr);
|
|
|
|
zOne = 0;
|
2014-12-29 14:24:46 +03:00
|
|
|
}
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-04-24 22:41:43 +03:00
|
|
|
|
|
|
|
sqlite3_free(zOne);
|
|
|
|
sqlite3_free(zTwo);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
2014-12-29 14:24:46 +03:00
|
|
|
/* If a tokenizer= option was successfully parsed, the tokenizer has
|
|
|
|
** already been allocated. Otherwise, allocate an instance of the default
|
2015-04-24 22:41:43 +03:00
|
|
|
** tokenizer (unicode61) now. */
|
2014-11-15 23:07:31 +03:00
|
|
|
if( rc==SQLITE_OK && pRet->pTok==0 ){
|
|
|
|
rc = fts5ConfigDefaultTokenizer(pGlobal, pRet);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
/* If no zContent option was specified, fill in the default values. */
|
2015-06-09 23:58:39 +03:00
|
|
|
if( rc==SQLITE_OK && pRet->zContent==0 ){
|
|
|
|
const char *zTail = 0;
|
|
|
|
assert( pRet->eContent==FTS5_CONTENT_NORMAL
|
|
|
|
|| pRet->eContent==FTS5_CONTENT_NONE
|
|
|
|
);
|
|
|
|
if( pRet->eContent==FTS5_CONTENT_NORMAL ){
|
|
|
|
zTail = "content";
|
|
|
|
}else if( pRet->bColumnsize ){
|
|
|
|
zTail = "docsize";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( zTail ){
|
|
|
|
pRet->zContent = sqlite3Fts5Mprintf(
|
|
|
|
&rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
|
|
|
|
);
|
2015-01-03 23:44:58 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 23:58:39 +03:00
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
if( rc==SQLITE_OK && pRet->zContentRowid==0 ){
|
2015-05-02 23:35:24 +03:00
|
|
|
pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
|
2015-01-03 23:44:58 +03:00
|
|
|
}
|
|
|
|
|
2015-04-27 19:21:49 +03:00
|
|
|
/* Formulate the zContentExprlist text */
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5ConfigMakeExprlist(pRet);
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqlite3Fts5ConfigFree(pRet);
|
|
|
|
*ppOut = 0;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Free the configuration object passed as the only argument.
|
|
|
|
*/
|
|
|
|
void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
|
|
|
|
if( pConfig ){
|
|
|
|
int i;
|
2015-05-18 20:50:17 +03:00
|
|
|
if( pConfig->pTok ){
|
2014-11-15 23:07:31 +03:00
|
|
|
pConfig->pTokApi->xDelete(pConfig->pTok);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
sqlite3_free(pConfig->zDb);
|
|
|
|
sqlite3_free(pConfig->zName);
|
|
|
|
for(i=0; i<pConfig->nCol; i++){
|
|
|
|
sqlite3_free(pConfig->azCol[i]);
|
|
|
|
}
|
|
|
|
sqlite3_free(pConfig->azCol);
|
|
|
|
sqlite3_free(pConfig->aPrefix);
|
2014-12-01 23:05:00 +03:00
|
|
|
sqlite3_free(pConfig->zRank);
|
|
|
|
sqlite3_free(pConfig->zRankArgs);
|
2015-01-03 23:44:58 +03:00
|
|
|
sqlite3_free(pConfig->zContent);
|
|
|
|
sqlite3_free(pConfig->zContentRowid);
|
2015-04-27 19:21:49 +03:00
|
|
|
sqlite3_free(pConfig->zContentExprlist);
|
2014-06-23 15:33:22 +04:00
|
|
|
sqlite3_free(pConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Call sqlite3_declare_vtab() based on the contents of the configuration
|
|
|
|
** object passed as the only argument. Return SQLITE_OK if successful, or
|
|
|
|
** an SQLite error code if an error occurs.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
|
|
|
|
int i;
|
2015-06-23 21:47:55 +03:00
|
|
|
int rc = SQLITE_OK;
|
2014-06-23 15:33:22 +04:00
|
|
|
char *zSql;
|
|
|
|
|
2015-06-23 21:47:55 +03:00
|
|
|
zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
|
2014-06-23 15:33:22 +04:00
|
|
|
for(i=0; zSql && i<pConfig->nCol; i++){
|
2015-06-23 21:47:55 +03:00
|
|
|
const char *zSep = (i==0?"":", ");
|
|
|
|
zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-06-23 21:47:55 +03:00
|
|
|
zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
|
|
|
|
zSql, pConfig->zName, FTS5_RANK_NAME
|
|
|
|
);
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2015-06-23 21:47:55 +03:00
|
|
|
assert( zSql || rc==SQLITE_NOMEM );
|
2014-06-23 15:33:22 +04:00
|
|
|
if( zSql ){
|
|
|
|
rc = sqlite3_declare_vtab(pConfig->db, zSql);
|
|
|
|
sqlite3_free(zSql);
|
|
|
|
}
|
2019-09-12 22:38:40 +03:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Tokenize the text passed via the second and third arguments.
|
|
|
|
**
|
|
|
|
** The callback is invoked once for each token in the input text. The
|
|
|
|
** arguments passed to it are, in order:
|
|
|
|
**
|
|
|
|
** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
|
|
|
|
** const char *pToken // Pointer to buffer containing token
|
|
|
|
** int nToken // Size of token in bytes
|
|
|
|
** int iStart // Byte offset of start of token within input text
|
|
|
|
** int iEnd // Byte offset of end of token within input text
|
|
|
|
** int iPos // Position of token in input (first token is 0)
|
|
|
|
**
|
|
|
|
** If the callback returns a non-zero value the tokenization is abandoned
|
|
|
|
** and no further callbacks are issued.
|
|
|
|
**
|
|
|
|
** This function returns SQLITE_OK if successful or an SQLite error code
|
|
|
|
** if an error occurs. If the tokenization was abandoned early because
|
|
|
|
** the callback returned SQLITE_DONE, this is not an error and this function
|
|
|
|
** still returns SQLITE_OK. Or, if the tokenization was abandoned early
|
|
|
|
** because the callback returned another non-zero value, it is assumed
|
|
|
|
** to be an SQLite error code and returned to the caller.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5Tokenize(
|
|
|
|
Fts5Config *pConfig, /* FTS5 Configuration object */
|
2015-08-28 22:56:47 +03:00
|
|
|
int flags, /* FTS5_TOKENIZE_* flags */
|
2014-06-23 15:33:22 +04:00
|
|
|
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-06-23 15:33:22 +04:00
|
|
|
){
|
2015-06-26 23:08:25 +03:00
|
|
|
if( pText==0 ) return SQLITE_OK;
|
2015-08-28 22:56:47 +03:00
|
|
|
return pConfig->pTokApi->xTokenize(
|
|
|
|
pConfig->pTok, pCtx, flags, pText, nText, xToken
|
|
|
|
);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
2014-12-01 23:05:00 +03:00
|
|
|
/*
|
|
|
|
** Argument pIn points to the first character in what is expected to be
|
|
|
|
** a comma-separated list of SQL literals followed by a ')' character.
|
|
|
|
** If it actually is this, return a pointer to the ')'. Otherwise, return
|
|
|
|
** NULL to indicate a parse error.
|
|
|
|
*/
|
|
|
|
static const char *fts5ConfigSkipArgs(const char *pIn){
|
|
|
|
const char *p = pIn;
|
|
|
|
|
|
|
|
while( 1 ){
|
|
|
|
p = fts5ConfigSkipWhitespace(p);
|
|
|
|
p = fts5ConfigSkipLiteral(p);
|
|
|
|
p = fts5ConfigSkipWhitespace(p);
|
|
|
|
if( p==0 || *p==')' ) break;
|
|
|
|
if( *p!=',' ){
|
|
|
|
p = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Parameter zIn contains a rank() function specification. The format of
|
|
|
|
** this is:
|
|
|
|
**
|
|
|
|
** + Bareword (function name)
|
|
|
|
** + Open parenthesis - "("
|
|
|
|
** + Zero or more SQL literals in a comma separated list
|
|
|
|
** + Close parenthesis - ")"
|
|
|
|
*/
|
2015-01-02 17:55:22 +03:00
|
|
|
int sqlite3Fts5ConfigParseRank(
|
2014-12-01 23:05:00 +03:00
|
|
|
const char *zIn, /* Input string */
|
|
|
|
char **pzRank, /* OUT: Rank function name */
|
|
|
|
char **pzRankArgs /* OUT: Rank function arguments */
|
|
|
|
){
|
|
|
|
const char *p = zIn;
|
|
|
|
const char *pRank;
|
|
|
|
char *zRank = 0;
|
|
|
|
char *zRankArgs = 0;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
|
|
|
*pzRank = 0;
|
|
|
|
*pzRankArgs = 0;
|
|
|
|
|
2015-11-06 15:50:57 +03:00
|
|
|
if( p==0 ){
|
2014-12-01 23:05:00 +03:00
|
|
|
rc = SQLITE_ERROR;
|
2015-11-06 15:50:57 +03:00
|
|
|
}else{
|
2014-12-02 23:18:11 +03:00
|
|
|
p = fts5ConfigSkipWhitespace(p);
|
2015-11-06 15:50:57 +03:00
|
|
|
pRank = p;
|
|
|
|
p = fts5ConfigSkipBareword(p);
|
|
|
|
|
|
|
|
if( p ){
|
|
|
|
zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
|
|
|
|
if( zRank ) memcpy(zRank, pRank, p-pRank);
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
p = fts5ConfigSkipWhitespace(p);
|
|
|
|
if( *p!='(' ) rc = SQLITE_ERROR;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
const char *pArgs;
|
|
|
|
p = fts5ConfigSkipWhitespace(p);
|
|
|
|
pArgs = p;
|
|
|
|
if( *p!=')' ){
|
|
|
|
p = fts5ConfigSkipArgs(p);
|
|
|
|
if( p==0 ){
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}else{
|
|
|
|
zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
|
|
|
|
if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
|
|
|
|
}
|
2014-12-29 14:24:46 +03:00
|
|
|
}
|
2014-12-01 23:05:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqlite3_free(zRank);
|
|
|
|
assert( zRankArgs==0 );
|
|
|
|
}else{
|
|
|
|
*pzRank = zRank;
|
|
|
|
*pzRankArgs = zRankArgs;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-11-27 23:03:45 +03:00
|
|
|
int sqlite3Fts5ConfigSetValue(
|
|
|
|
Fts5Config *pConfig,
|
|
|
|
const char *zKey,
|
|
|
|
sqlite3_value *pVal,
|
|
|
|
int *pbBadkey
|
|
|
|
){
|
|
|
|
int rc = SQLITE_OK;
|
2014-11-28 23:01:13 +03:00
|
|
|
|
2015-05-18 20:50:17 +03:00
|
|
|
if( 0==sqlite3_stricmp(zKey, "pgsz") ){
|
2014-11-28 23:01:13 +03:00
|
|
|
int pgsz = 0;
|
2014-11-27 23:03:45 +03:00
|
|
|
if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
|
2014-11-28 23:01:13 +03:00
|
|
|
pgsz = sqlite3_value_int(pVal);
|
|
|
|
}
|
2019-10-07 23:36:18 +03:00
|
|
|
if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE ){
|
2015-05-18 20:50:17 +03:00
|
|
|
*pbBadkey = 1;
|
2014-11-28 23:01:13 +03:00
|
|
|
}else{
|
|
|
|
pConfig->pgsz = pgsz;
|
2014-11-27 23:03:45 +03:00
|
|
|
}
|
|
|
|
}
|
2014-11-28 23:01:13 +03:00
|
|
|
|
2015-11-05 21:09:16 +03:00
|
|
|
else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
|
|
|
|
int nHashSize = -1;
|
|
|
|
if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
|
|
|
|
nHashSize = sqlite3_value_int(pVal);
|
|
|
|
}
|
|
|
|
if( nHashSize<=0 ){
|
|
|
|
*pbBadkey = 1;
|
|
|
|
}else{
|
|
|
|
pConfig->nHashSize = nHashSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 23:03:45 +03:00
|
|
|
else if( 0==sqlite3_stricmp(zKey, "automerge") ){
|
2014-11-28 23:01:13 +03:00
|
|
|
int nAutomerge = -1;
|
|
|
|
if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
|
|
|
|
nAutomerge = sqlite3_value_int(pVal);
|
|
|
|
}
|
|
|
|
if( nAutomerge<0 || nAutomerge>64 ){
|
2015-05-18 20:50:17 +03:00
|
|
|
*pbBadkey = 1;
|
2014-11-28 23:01:13 +03:00
|
|
|
}else{
|
|
|
|
if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
|
|
|
|
pConfig->nAutomerge = nAutomerge;
|
|
|
|
}
|
2014-11-27 23:03:45 +03:00
|
|
|
}
|
2014-11-28 23:01:13 +03:00
|
|
|
|
2016-03-09 23:54:14 +03:00
|
|
|
else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
|
|
|
|
int nUsermerge = -1;
|
|
|
|
if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
|
|
|
|
nUsermerge = sqlite3_value_int(pVal);
|
|
|
|
}
|
|
|
|
if( nUsermerge<2 || nUsermerge>16 ){
|
|
|
|
*pbBadkey = 1;
|
|
|
|
}else{
|
|
|
|
pConfig->nUsermerge = nUsermerge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 09:50:33 +03:00
|
|
|
else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
|
|
|
|
int nCrisisMerge = -1;
|
|
|
|
if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
|
|
|
|
nCrisisMerge = sqlite3_value_int(pVal);
|
|
|
|
}
|
|
|
|
if( nCrisisMerge<0 ){
|
2015-05-18 20:50:17 +03:00
|
|
|
*pbBadkey = 1;
|
2015-01-23 09:50:33 +03:00
|
|
|
}else{
|
|
|
|
if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
|
2019-10-09 21:36:32 +03:00
|
|
|
if( nCrisisMerge>=FTS5_MAX_SEGMENT ) nCrisisMerge = FTS5_MAX_SEGMENT-1;
|
2015-01-23 09:50:33 +03:00
|
|
|
pConfig->nCrisisMerge = nCrisisMerge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 23:03:45 +03:00
|
|
|
else if( 0==sqlite3_stricmp(zKey, "rank") ){
|
2014-12-01 23:05:00 +03:00
|
|
|
const char *zIn = (const char*)sqlite3_value_text(pVal);
|
|
|
|
char *zRank;
|
|
|
|
char *zRankArgs;
|
2015-01-02 17:55:22 +03:00
|
|
|
rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
|
2014-12-01 23:05:00 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_free(pConfig->zRank);
|
|
|
|
sqlite3_free(pConfig->zRankArgs);
|
|
|
|
pConfig->zRank = zRank;
|
|
|
|
pConfig->zRankArgs = zRankArgs;
|
|
|
|
}else if( rc==SQLITE_ERROR ){
|
|
|
|
rc = SQLITE_OK;
|
2015-05-18 20:50:17 +03:00
|
|
|
*pbBadkey = 1;
|
2014-12-01 23:05:00 +03:00
|
|
|
}
|
2014-11-27 23:03:45 +03:00
|
|
|
}else{
|
2015-05-18 20:50:17 +03:00
|
|
|
*pbBadkey = 1;
|
2014-11-27 23:03:45 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Load the contents of the %_config table into memory.
|
|
|
|
*/
|
2014-11-28 23:01:13 +03:00
|
|
|
int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
|
2014-11-27 23:03:45 +03:00
|
|
|
const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
|
|
|
|
char *zSql;
|
|
|
|
sqlite3_stmt *p = 0;
|
2015-06-23 21:47:55 +03:00
|
|
|
int rc = SQLITE_OK;
|
2015-05-07 22:29:46 +03:00
|
|
|
int iVersion = 0;
|
2014-11-27 23:03:45 +03:00
|
|
|
|
|
|
|
/* Set default values */
|
|
|
|
pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
|
2014-11-28 23:01:13 +03:00
|
|
|
pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
|
2016-03-09 23:54:14 +03:00
|
|
|
pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
|
2015-01-23 09:50:33 +03:00
|
|
|
pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
|
2015-11-05 21:09:16 +03:00
|
|
|
pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
|
2014-11-27 23:03:45 +03:00
|
|
|
|
2015-06-23 21:47:55 +03:00
|
|
|
zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
|
|
|
|
if( zSql ){
|
2014-11-27 23:03:45 +03:00
|
|
|
rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
|
|
|
|
sqlite3_free(zSql);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( rc==SQLITE_OK || p==0 );
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
while( SQLITE_ROW==sqlite3_step(p) ){
|
|
|
|
const char *zK = (const char*)sqlite3_column_text(p, 0);
|
|
|
|
sqlite3_value *pVal = sqlite3_column_value(p, 1);
|
2015-05-07 22:29:46 +03:00
|
|
|
if( 0==sqlite3_stricmp(zK, "version") ){
|
|
|
|
iVersion = sqlite3_value_int(pVal);
|
|
|
|
}else{
|
2015-05-18 20:50:17 +03:00
|
|
|
int bDummy = 0;
|
|
|
|
sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
|
2015-05-07 22:29:46 +03:00
|
|
|
}
|
2014-11-27 23:03:45 +03:00
|
|
|
}
|
2015-05-18 20:50:17 +03:00
|
|
|
rc = sqlite3_finalize(p);
|
2015-05-07 22:29:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK && iVersion!=FTS5_CURRENT_VERSION ){
|
2015-05-08 12:21:05 +03:00
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
if( pConfig->pzErrmsg ){
|
|
|
|
assert( 0==*pConfig->pzErrmsg );
|
|
|
|
*pConfig->pzErrmsg = sqlite3_mprintf(
|
|
|
|
"invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
|
|
|
|
iVersion, FTS5_CURRENT_VERSION
|
|
|
|
);
|
|
|
|
}
|
2014-11-27 23:03:45 +03:00
|
|
|
}
|
|
|
|
|
2014-11-28 23:01:13 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
pConfig->iCookie = iCookie;
|
|
|
|
}
|
2014-11-27 23:03:45 +03:00
|
|
|
return rc;
|
|
|
|
}
|