1 /* 2 ** 2008 Nov 28 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ****************************************************************************** 12 ** 13 ** This module contains code that implements a parser for fts3 query strings 14 ** (the right-hand argument to the MATCH operator). Because the supported 15 ** syntax is relatively simple, the whole tokenizer/parser system is 16 ** hand-coded. 17 */ 18 #include "fts3Int.h" 19 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) 20 21 /* 22 ** By default, this module parses the legacy syntax that has been 23 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS 24 ** is defined, then it uses the new syntax. The differences between 25 ** the new and the old syntaxes are: 26 ** 27 ** a) The new syntax supports parenthesis. The old does not. 28 ** 29 ** b) The new syntax supports the AND and NOT operators. The old does not. 30 ** 31 ** c) The old syntax supports the "-" token qualifier. This is not 32 ** supported by the new syntax (it is replaced by the NOT operator). 33 ** 34 ** d) When using the old syntax, the OR operator has a greater precedence 35 ** than an implicit AND. When using the new, both implicity and explicit 36 ** AND operators have a higher precedence than OR. 37 ** 38 ** If compiled with SQLITE_TEST defined, then this module exports the 39 ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable 40 ** to zero causes the module to use the old syntax. If it is set to 41 ** non-zero the new syntax is activated. This is so both syntaxes can 42 ** be tested using a single build of testfixture. 43 ** 44 ** The following describes the syntax supported by the fts3 MATCH 45 ** operator in a similar format to that used by the lemon parser 46 ** generator. This module does not use actually lemon, it uses a 47 ** custom parser. 48 ** 49 ** query ::= andexpr (OR andexpr)*. 50 ** 51 ** andexpr ::= notexpr (AND? notexpr)*. 52 ** 53 ** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*. 54 ** notexpr ::= LP query RP. 55 ** 56 ** nearexpr ::= phrase (NEAR distance_opt nearexpr)*. 57 ** 58 ** distance_opt ::= . 59 ** distance_opt ::= / INTEGER. 60 ** 61 ** phrase ::= TOKEN. 62 ** phrase ::= COLUMN:TOKEN. 63 ** phrase ::= "TOKEN TOKEN TOKEN...". 64 */ 65 66 #ifdef SQLITE_TEST 67 int sqlite3_fts3_enable_parentheses = 0; 68 #else 69 # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS 70 # define sqlite3_fts3_enable_parentheses 1 71 # else 72 # define sqlite3_fts3_enable_parentheses 0 73 # endif 74 #endif 75 76 /* 77 ** Default span for NEAR operators. 78 */ 79 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10 80 81 #include <string.h> 82 #include <assert.h> 83 84 /* 85 ** isNot: 86 ** This variable is used by function getNextNode(). When getNextNode() is 87 ** called, it sets ParseContext.isNot to true if the 'next node' is a 88 ** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the 89 ** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to 90 ** zero. 91 */ 92 typedef struct ParseContext ParseContext; 93 struct ParseContext { 94 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */ 95 int iLangid; /* Language id used with tokenizer */ 96 const char **azCol; /* Array of column names for fts3 table */ 97 int bFts4; /* True to allow FTS4-only syntax */ 98 int nCol; /* Number of entries in azCol[] */ 99 int iDefaultCol; /* Default column to query */ 100 int isNot; /* True if getNextNode() sees a unary - */ 101 sqlite3_context *pCtx; /* Write error message here */ 102 int nNest; /* Number of nested brackets */ 103 }; 104 105 /* 106 ** This function is equivalent to the standard isspace() function. 107 ** 108 ** The standard isspace() can be awkward to use safely, because although it 109 ** is defined to accept an argument of type int, its behavior when passed 110 ** an integer that falls outside of the range of the unsigned char type 111 ** is undefined (and sometimes, "undefined" means segfault). This wrapper 112 ** is defined to accept an argument of type char, and always returns 0 for 113 ** any values that fall outside of the range of the unsigned char type (i.e. 114 ** negative values). 115 */ 116 static int fts3isspace(char c){ 117 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f'; 118 } 119 120 /* 121 ** Allocate nByte bytes of memory using sqlite3_malloc(). If successful, 122 ** zero the memory before returning a pointer to it. If unsuccessful, 123 ** return NULL. 124 */ 125 static void *fts3MallocZero(int nByte){ 126 void *pRet = sqlite3_malloc(nByte); 127 if( pRet ) memset(pRet, 0, nByte); 128 return pRet; 129 } 130 131 int sqlite3Fts3OpenTokenizer( 132 sqlite3_tokenizer *pTokenizer, 133 int iLangid, 134 const char *z, 135 int n, 136 sqlite3_tokenizer_cursor **ppCsr 137 ){ 138 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; 139 sqlite3_tokenizer_cursor *pCsr = 0; 140 int rc; 141 142 rc = pModule->xOpen(pTokenizer, z, n, &pCsr); 143 assert( rc==SQLITE_OK || pCsr==0 ); 144 if( rc==SQLITE_OK ){ 145 pCsr->pTokenizer = pTokenizer; 146 if( pModule->iVersion>=1 ){ 147 rc = pModule->xLanguageid(pCsr, iLangid); 148 if( rc!=SQLITE_OK ){ 149 pModule->xClose(pCsr); 150 pCsr = 0; 151 } 152 } 153 } 154 *ppCsr = pCsr; 155 return rc; 156 } 157 158 /* 159 ** Function getNextNode(), which is called by fts3ExprParse(), may itself 160 ** call fts3ExprParse(). So this forward declaration is required. 161 */ 162 static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *); 163 164 /* 165 ** Extract the next token from buffer z (length n) using the tokenizer 166 ** and other information (column names etc.) in pParse. Create an Fts3Expr 167 ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this 168 ** single token and set *ppExpr to point to it. If the end of the buffer is 169 ** reached before a token is found, set *ppExpr to zero. It is the 170 ** responsibility of the caller to eventually deallocate the allocated 171 ** Fts3Expr structure (if any) by passing it to sqlite3_free(). 172 ** 173 ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation 174 ** fails. 175 */ 176 static int getNextToken( 177 ParseContext *pParse, /* fts3 query parse context */ 178 int iCol, /* Value for Fts3Phrase.iColumn */ 179 const char *z, int n, /* Input string */ 180 Fts3Expr **ppExpr, /* OUT: expression */ 181 int *pnConsumed /* OUT: Number of bytes consumed */ 182 ){ 183 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer; 184 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; 185 int rc; 186 sqlite3_tokenizer_cursor *pCursor; 187 Fts3Expr *pRet = 0; 188 int nConsumed = 0; 189 190 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor); 191 if( rc==SQLITE_OK ){ 192 const char *zToken; 193 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0; 194 int nByte; /* total space to allocate */ 195 196 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition); 197 198 if( (rc==SQLITE_OK || rc==SQLITE_DONE) && sqlite3_fts3_enable_parentheses ){ 199 int i; 200 if( rc==SQLITE_DONE ) iStart = n; 201 for(i=0; i<iStart; i++){ 202 if( z[i]=='(' ){ 203 pParse->nNest++; 204 rc = fts3ExprParse(pParse, &z[i+1], n-i-1, &pRet, &nConsumed); 205 if( rc==SQLITE_OK && !pRet ){ 206 rc = SQLITE_DONE; 207 } 208 nConsumed = (int)(i + 1 + nConsumed); 209 break; 210 } 211 212 if( z[i]==')' ){ 213 rc = SQLITE_DONE; 214 pParse->nNest--; 215 nConsumed = i+1; 216 break; 217 } 218 } 219 } 220 221 if( nConsumed==0 && rc==SQLITE_OK ){ 222 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken; 223 pRet = (Fts3Expr *)fts3MallocZero(nByte); 224 if( !pRet ){ 225 rc = SQLITE_NOMEM; 226 }else{ 227 pRet->eType = FTSQUERY_PHRASE; 228 pRet->pPhrase = (Fts3Phrase *)&pRet[1]; 229 pRet->pPhrase->nToken = 1; 230 pRet->pPhrase->iColumn = iCol; 231 pRet->pPhrase->aToken[0].n = nToken; 232 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1]; 233 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken); 234 235 if( iEnd<n && z[iEnd]=='*' ){ 236 pRet->pPhrase->aToken[0].isPrefix = 1; 237 iEnd++; 238 } 239 240 while( 1 ){ 241 if( !sqlite3_fts3_enable_parentheses 242 && iStart>0 && z[iStart-1]=='-' 243 ){ 244 pParse->isNot = 1; 245 iStart--; 246 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){ 247 pRet->pPhrase->aToken[0].bFirst = 1; 248 iStart--; 249 }else{ 250 break; 251 } 252 } 253 254 } 255 nConsumed = iEnd; 256 } 257 258 pModule->xClose(pCursor); 259 } 260 261 *pnConsumed = nConsumed; 262 *ppExpr = pRet; 263 return rc; 264 } 265 266 267 /* 268 ** Enlarge a memory allocation. If an out-of-memory allocation occurs, 269 ** then free the old allocation. 270 */ 271 static void *fts3ReallocOrFree(void *pOrig, int nNew){ 272 void *pRet = sqlite3_realloc(pOrig, nNew); 273 if( !pRet ){ 274 sqlite3_free(pOrig); 275 } 276 return pRet; 277 } 278 279 /* 280 ** Buffer zInput, length nInput, contains the contents of a quoted string 281 ** that appeared as part of an fts3 query expression. Neither quote character 282 ** is included in the buffer. This function attempts to tokenize the entire 283 ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE 284 ** containing the results. 285 ** 286 ** If successful, SQLITE_OK is returned and *ppExpr set to point at the 287 ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory 288 ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set 289 ** to 0. 290 */ 291 static int getNextString( 292 ParseContext *pParse, /* fts3 query parse context */ 293 const char *zInput, int nInput, /* Input string */ 294 Fts3Expr **ppExpr /* OUT: expression */ 295 ){ 296 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer; 297 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; 298 int rc; 299 Fts3Expr *p = 0; 300 sqlite3_tokenizer_cursor *pCursor = 0; 301 char *zTemp = 0; 302 int nTemp = 0; 303 304 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase); 305 int nToken = 0; 306 307 /* The final Fts3Expr data structure, including the Fts3Phrase, 308 ** Fts3PhraseToken structures token buffers are all stored as a single 309 ** allocation so that the expression can be freed with a single call to 310 ** sqlite3_free(). Setting this up requires a two pass approach. 311 ** 312 ** The first pass, in the block below, uses a tokenizer cursor to iterate 313 ** through the tokens in the expression. This pass uses fts3ReallocOrFree() 314 ** to assemble data in two dynamic buffers: 315 ** 316 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase 317 ** structure, followed by the array of Fts3PhraseToken 318 ** structures. This pass only populates the Fts3PhraseToken array. 319 ** 320 ** Buffer zTemp: Contains copies of all tokens. 321 ** 322 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below, 323 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase 324 ** structures. 325 */ 326 rc = sqlite3Fts3OpenTokenizer( 327 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor); 328 if( rc==SQLITE_OK ){ 329 int ii; 330 for(ii=0; rc==SQLITE_OK; ii++){ 331 const char *zByte; 332 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0; 333 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos); 334 if( rc==SQLITE_OK ){ 335 Fts3PhraseToken *pToken; 336 337 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken)); 338 if( !p ) goto no_mem; 339 340 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte); 341 if( !zTemp ) goto no_mem; 342 343 assert( nToken==ii ); 344 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii]; 345 memset(pToken, 0, sizeof(Fts3PhraseToken)); 346 347 memcpy(&zTemp[nTemp], zByte, nByte); 348 nTemp += nByte; 349 350 pToken->n = nByte; 351 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*'); 352 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^'); 353 nToken = ii+1; 354 } 355 } 356 357 pModule->xClose(pCursor); 358 pCursor = 0; 359 } 360 361 if( rc==SQLITE_DONE ){ 362 int jj; 363 char *zBuf = 0; 364 365 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp); 366 if( !p ) goto no_mem; 367 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p); 368 p->eType = FTSQUERY_PHRASE; 369 p->pPhrase = (Fts3Phrase *)&p[1]; 370 p->pPhrase->iColumn = pParse->iDefaultCol; 371 p->pPhrase->nToken = nToken; 372 373 zBuf = (char *)&p->pPhrase->aToken[nToken]; 374 if( zTemp ){ 375 memcpy(zBuf, zTemp, nTemp); 376 sqlite3_free(zTemp); 377 }else{ 378 assert( nTemp==0 ); 379 } 380 381 for(jj=0; jj<p->pPhrase->nToken; jj++){ 382 p->pPhrase->aToken[jj].z = zBuf; 383 zBuf += p->pPhrase->aToken[jj].n; 384 } 385 rc = SQLITE_OK; 386 } 387 388 *ppExpr = p; 389 return rc; 390 no_mem: 391 392 if( pCursor ){ 393 pModule->xClose(pCursor); 394 } 395 sqlite3_free(zTemp); 396 sqlite3_free(p); 397 *ppExpr = 0; 398 return SQLITE_NOMEM; 399 } 400 401 /* 402 ** The output variable *ppExpr is populated with an allocated Fts3Expr 403 ** structure, or set to 0 if the end of the input buffer is reached. 404 ** 405 ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM 406 ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered. 407 ** If SQLITE_ERROR is returned, pContext is populated with an error message. 408 */ 409 static int getNextNode( 410 ParseContext *pParse, /* fts3 query parse context */ 411 const char *z, int n, /* Input string */ 412 Fts3Expr **ppExpr, /* OUT: expression */ 413 int *pnConsumed /* OUT: Number of bytes consumed */ 414 ){ 415 static const struct Fts3Keyword { 416 char *z; /* Keyword text */ 417 unsigned char n; /* Length of the keyword */ 418 unsigned char parenOnly; /* Only valid in paren mode */ 419 unsigned char eType; /* Keyword code */ 420 } aKeyword[] = { 421 { "OR" , 2, 0, FTSQUERY_OR }, 422 { "AND", 3, 1, FTSQUERY_AND }, 423 { "NOT", 3, 1, FTSQUERY_NOT }, 424 { "NEAR", 4, 0, FTSQUERY_NEAR } 425 }; 426 int ii; 427 int iCol; 428 int iColLen; 429 int rc; 430 Fts3Expr *pRet = 0; 431 432 const char *zInput = z; 433 int nInput = n; 434 435 pParse->isNot = 0; 436 437 /* Skip over any whitespace before checking for a keyword, an open or 438 ** close bracket, or a quoted string. 439 */ 440 while( nInput>0 && fts3isspace(*zInput) ){ 441 nInput--; 442 zInput++; 443 } 444 if( nInput==0 ){ 445 return SQLITE_DONE; 446 } 447 448 /* See if we are dealing with a keyword. */ 449 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){ 450 const struct Fts3Keyword *pKey = &aKeyword[ii]; 451 452 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){ 453 continue; 454 } 455 456 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){ 457 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM; 458 int nKey = pKey->n; 459 char cNext; 460 461 /* If this is a "NEAR" keyword, check for an explicit nearness. */ 462 if( pKey->eType==FTSQUERY_NEAR ){ 463 assert( nKey==4 ); 464 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){ 465 nNear = 0; 466 for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){ 467 nNear = nNear * 10 + (zInput[nKey] - '0'); 468 } 469 } 470 } 471 472 /* At this point this is probably a keyword. But for that to be true, 473 ** the next byte must contain either whitespace, an open or close 474 ** parenthesis, a quote character, or EOF. 475 */ 476 cNext = zInput[nKey]; 477 if( fts3isspace(cNext) 478 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0 479 ){ 480 pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr)); 481 if( !pRet ){ 482 return SQLITE_NOMEM; 483 } 484 pRet->eType = pKey->eType; 485 pRet->nNear = nNear; 486 *ppExpr = pRet; 487 *pnConsumed = (int)((zInput - z) + nKey); 488 return SQLITE_OK; 489 } 490 491 /* Turns out that wasn't a keyword after all. This happens if the 492 ** user has supplied a token such as "ORacle". Continue. 493 */ 494 } 495 } 496 497 /* See if we are dealing with a quoted phrase. If this is the case, then 498 ** search for the closing quote and pass the whole string to getNextString() 499 ** for processing. This is easy to do, as fts3 has no syntax for escaping 500 ** a quote character embedded in a string. 501 */ 502 if( *zInput=='"' ){ 503 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++); 504 *pnConsumed = (int)((zInput - z) + ii + 1); 505 if( ii==nInput ){ 506 return SQLITE_ERROR; 507 } 508 return getNextString(pParse, &zInput[1], ii-1, ppExpr); 509 } 510 511 512 /* If control flows to this point, this must be a regular token, or 513 ** the end of the input. Read a regular token using the sqlite3_tokenizer 514 ** interface. Before doing so, figure out if there is an explicit 515 ** column specifier for the token. 516 ** 517 ** TODO: Strangely, it is not possible to associate a column specifier 518 ** with a quoted phrase, only with a single token. Not sure if this was 519 ** an implementation artifact or an intentional decision when fts3 was 520 ** first implemented. Whichever it was, this module duplicates the 521 ** limitation. 522 */ 523 iCol = pParse->iDefaultCol; 524 iColLen = 0; 525 for(ii=0; ii<pParse->nCol; ii++){ 526 const char *zStr = pParse->azCol[ii]; 527 int nStr = (int)strlen(zStr); 528 if( nInput>nStr && zInput[nStr]==':' 529 && sqlite3_strnicmp(zStr, zInput, nStr)==0 530 ){ 531 iCol = ii; 532 iColLen = (int)((zInput - z) + nStr + 1); 533 break; 534 } 535 } 536 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed); 537 *pnConsumed += iColLen; 538 return rc; 539 } 540 541 /* 542 ** The argument is an Fts3Expr structure for a binary operator (any type 543 ** except an FTSQUERY_PHRASE). Return an integer value representing the 544 ** precedence of the operator. Lower values have a higher precedence (i.e. 545 ** group more tightly). For example, in the C language, the == operator 546 ** groups more tightly than ||, and would therefore have a higher precedence. 547 ** 548 ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS 549 ** is defined), the order of the operators in precedence from highest to 550 ** lowest is: 551 ** 552 ** NEAR 553 ** NOT 554 ** AND (including implicit ANDs) 555 ** OR 556 ** 557 ** Note that when using the old query syntax, the OR operator has a higher 558 ** precedence than the AND operator. 559 */ 560 static int opPrecedence(Fts3Expr *p){ 561 assert( p->eType!=FTSQUERY_PHRASE ); 562 if( sqlite3_fts3_enable_parentheses ){ 563 return p->eType; 564 }else if( p->eType==FTSQUERY_NEAR ){ 565 return 1; 566 }else if( p->eType==FTSQUERY_OR ){ 567 return 2; 568 } 569 assert( p->eType==FTSQUERY_AND ); 570 return 3; 571 } 572 573 /* 574 ** Argument ppHead contains a pointer to the current head of a query 575 ** expression tree being parsed. pPrev is the expression node most recently 576 ** inserted into the tree. This function adds pNew, which is always a binary 577 ** operator node, into the expression tree based on the relative precedence 578 ** of pNew and the existing nodes of the tree. This may result in the head 579 ** of the tree changing, in which case *ppHead is set to the new root node. 580 */ 581 static void insertBinaryOperator( 582 Fts3Expr **ppHead, /* Pointer to the root node of a tree */ 583 Fts3Expr *pPrev, /* Node most recently inserted into the tree */ 584 Fts3Expr *pNew /* New binary node to insert into expression tree */ 585 ){ 586 Fts3Expr *pSplit = pPrev; 587 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){ 588 pSplit = pSplit->pParent; 589 } 590 591 if( pSplit->pParent ){ 592 assert( pSplit->pParent->pRight==pSplit ); 593 pSplit->pParent->pRight = pNew; 594 pNew->pParent = pSplit->pParent; 595 }else{ 596 *ppHead = pNew; 597 } 598 pNew->pLeft = pSplit; 599 pSplit->pParent = pNew; 600 } 601 602 /* 603 ** Parse the fts3 query expression found in buffer z, length n. This function 604 ** returns either when the end of the buffer is reached or an unmatched 605 ** closing bracket - ')' - is encountered. 606 ** 607 ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the 608 ** parsed form of the expression and *pnConsumed is set to the number of 609 ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM 610 ** (out of memory error) or SQLITE_ERROR (parse error) is returned. 611 */ 612 static int fts3ExprParse( 613 ParseContext *pParse, /* fts3 query parse context */ 614 const char *z, int n, /* Text of MATCH query */ 615 Fts3Expr **ppExpr, /* OUT: Parsed query structure */ 616 int *pnConsumed /* OUT: Number of bytes consumed */ 617 ){ 618 Fts3Expr *pRet = 0; 619 Fts3Expr *pPrev = 0; 620 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */ 621 int nIn = n; 622 const char *zIn = z; 623 int rc = SQLITE_OK; 624 int isRequirePhrase = 1; 625 626 while( rc==SQLITE_OK ){ 627 Fts3Expr *p = 0; 628 int nByte = 0; 629 rc = getNextNode(pParse, zIn, nIn, &p, &nByte); 630 if( rc==SQLITE_OK ){ 631 int isPhrase; 632 633 if( !sqlite3_fts3_enable_parentheses 634 && p->eType==FTSQUERY_PHRASE && pParse->isNot 635 ){ 636 /* Create an implicit NOT operator. */ 637 Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr)); 638 if( !pNot ){ 639 sqlite3Fts3ExprFree(p); 640 rc = SQLITE_NOMEM; 641 goto exprparse_out; 642 } 643 pNot->eType = FTSQUERY_NOT; 644 pNot->pRight = p; 645 p->pParent = pNot; 646 if( pNotBranch ){ 647 pNot->pLeft = pNotBranch; 648 pNotBranch->pParent = pNot; 649 } 650 pNotBranch = pNot; 651 p = pPrev; 652 }else{ 653 int eType = p->eType; 654 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft); 655 656 /* The isRequirePhrase variable is set to true if a phrase or 657 ** an expression contained in parenthesis is required. If a 658 ** binary operator (AND, OR, NOT or NEAR) is encounted when 659 ** isRequirePhrase is set, this is a syntax error. 660 */ 661 if( !isPhrase && isRequirePhrase ){ 662 sqlite3Fts3ExprFree(p); 663 rc = SQLITE_ERROR; 664 goto exprparse_out; 665 } 666 667 if( isPhrase && !isRequirePhrase ){ 668 /* Insert an implicit AND operator. */ 669 Fts3Expr *pAnd; 670 assert( pRet && pPrev ); 671 pAnd = fts3MallocZero(sizeof(Fts3Expr)); 672 if( !pAnd ){ 673 sqlite3Fts3ExprFree(p); 674 rc = SQLITE_NOMEM; 675 goto exprparse_out; 676 } 677 pAnd->eType = FTSQUERY_AND; 678 insertBinaryOperator(&pRet, pPrev, pAnd); 679 pPrev = pAnd; 680 } 681 682 /* This test catches attempts to make either operand of a NEAR 683 ** operator something other than a phrase. For example, either of 684 ** the following: 685 ** 686 ** (bracketed expression) NEAR phrase 687 ** phrase NEAR (bracketed expression) 688 ** 689 ** Return an error in either case. 690 */ 691 if( pPrev && ( 692 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE) 693 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR) 694 )){ 695 sqlite3Fts3ExprFree(p); 696 rc = SQLITE_ERROR; 697 goto exprparse_out; 698 } 699 700 if( isPhrase ){ 701 if( pRet ){ 702 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 ); 703 pPrev->pRight = p; 704 p->pParent = pPrev; 705 }else{ 706 pRet = p; 707 } 708 }else{ 709 insertBinaryOperator(&pRet, pPrev, p); 710 } 711 isRequirePhrase = !isPhrase; 712 } 713 assert( nByte>0 ); 714 } 715 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) ); 716 nIn -= nByte; 717 zIn += nByte; 718 pPrev = p; 719 } 720 721 if( rc==SQLITE_DONE && pRet && isRequirePhrase ){ 722 rc = SQLITE_ERROR; 723 } 724 725 if( rc==SQLITE_DONE ){ 726 rc = SQLITE_OK; 727 if( !sqlite3_fts3_enable_parentheses && pNotBranch ){ 728 if( !pRet ){ 729 rc = SQLITE_ERROR; 730 }else{ 731 Fts3Expr *pIter = pNotBranch; 732 while( pIter->pLeft ){ 733 pIter = pIter->pLeft; 734 } 735 pIter->pLeft = pRet; 736 pRet->pParent = pIter; 737 pRet = pNotBranch; 738 } 739 } 740 } 741 *pnConsumed = n - nIn; 742 743 exprparse_out: 744 if( rc!=SQLITE_OK ){ 745 sqlite3Fts3ExprFree(pRet); 746 sqlite3Fts3ExprFree(pNotBranch); 747 pRet = 0; 748 } 749 *ppExpr = pRet; 750 return rc; 751 } 752 753 /* 754 ** Return SQLITE_ERROR if the maximum depth of the expression tree passed 755 ** as the only argument is more than nMaxDepth. 756 */ 757 static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){ 758 int rc = SQLITE_OK; 759 if( p ){ 760 if( nMaxDepth<0 ){ 761 rc = SQLITE_TOOBIG; 762 }else{ 763 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1); 764 if( rc==SQLITE_OK ){ 765 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1); 766 } 767 } 768 } 769 return rc; 770 } 771 772 /* 773 ** This function attempts to transform the expression tree at (*pp) to 774 ** an equivalent but more balanced form. The tree is modified in place. 775 ** If successful, SQLITE_OK is returned and (*pp) set to point to the 776 ** new root expression node. 777 ** 778 ** nMaxDepth is the maximum allowable depth of the balanced sub-tree. 779 ** 780 ** Otherwise, if an error occurs, an SQLite error code is returned and 781 ** expression (*pp) freed. 782 */ 783 static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){ 784 int rc = SQLITE_OK; /* Return code */ 785 Fts3Expr *pRoot = *pp; /* Initial root node */ 786 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */ 787 int eType = pRoot->eType; /* Type of node in this tree */ 788 789 if( nMaxDepth==0 ){ 790 rc = SQLITE_ERROR; 791 } 792 793 if( rc==SQLITE_OK && (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){ 794 Fts3Expr **apLeaf; 795 apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth); 796 if( 0==apLeaf ){ 797 rc = SQLITE_NOMEM; 798 }else{ 799 memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth); 800 } 801 802 if( rc==SQLITE_OK ){ 803 int i; 804 Fts3Expr *p; 805 806 /* Set $p to point to the left-most leaf in the tree of eType nodes. */ 807 for(p=pRoot; p->eType==eType; p=p->pLeft){ 808 assert( p->pParent==0 || p->pParent->pLeft==p ); 809 assert( p->pLeft && p->pRight ); 810 } 811 812 /* This loop runs once for each leaf in the tree of eType nodes. */ 813 while( 1 ){ 814 int iLvl; 815 Fts3Expr *pParent = p->pParent; /* Current parent of p */ 816 817 assert( pParent==0 || pParent->pLeft==p ); 818 p->pParent = 0; 819 if( pParent ){ 820 pParent->pLeft = 0; 821 }else{ 822 pRoot = 0; 823 } 824 rc = fts3ExprBalance(&p, nMaxDepth-1); 825 if( rc!=SQLITE_OK ) break; 826 827 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){ 828 if( apLeaf[iLvl]==0 ){ 829 apLeaf[iLvl] = p; 830 p = 0; 831 }else{ 832 assert( pFree ); 833 pFree->pLeft = apLeaf[iLvl]; 834 pFree->pRight = p; 835 pFree->pLeft->pParent = pFree; 836 pFree->pRight->pParent = pFree; 837 838 p = pFree; 839 pFree = pFree->pParent; 840 p->pParent = 0; 841 apLeaf[iLvl] = 0; 842 } 843 } 844 if( p ){ 845 sqlite3Fts3ExprFree(p); 846 rc = SQLITE_TOOBIG; 847 break; 848 } 849 850 /* If that was the last leaf node, break out of the loop */ 851 if( pParent==0 ) break; 852 853 /* Set $p to point to the next leaf in the tree of eType nodes */ 854 for(p=pParent->pRight; p->eType==eType; p=p->pLeft); 855 856 /* Remove pParent from the original tree. */ 857 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent ); 858 pParent->pRight->pParent = pParent->pParent; 859 if( pParent->pParent ){ 860 pParent->pParent->pLeft = pParent->pRight; 861 }else{ 862 assert( pParent==pRoot ); 863 pRoot = pParent->pRight; 864 } 865 866 /* Link pParent into the free node list. It will be used as an 867 ** internal node of the new tree. */ 868 pParent->pParent = pFree; 869 pFree = pParent; 870 } 871 872 if( rc==SQLITE_OK ){ 873 p = 0; 874 for(i=0; i<nMaxDepth; i++){ 875 if( apLeaf[i] ){ 876 if( p==0 ){ 877 p = apLeaf[i]; 878 p->pParent = 0; 879 }else{ 880 assert( pFree!=0 ); 881 pFree->pRight = p; 882 pFree->pLeft = apLeaf[i]; 883 pFree->pLeft->pParent = pFree; 884 pFree->pRight->pParent = pFree; 885 886 p = pFree; 887 pFree = pFree->pParent; 888 p->pParent = 0; 889 } 890 } 891 } 892 pRoot = p; 893 }else{ 894 /* An error occurred. Delete the contents of the apLeaf[] array 895 ** and pFree list. Everything else is cleaned up by the call to 896 ** sqlite3Fts3ExprFree(pRoot) below. */ 897 Fts3Expr *pDel; 898 for(i=0; i<nMaxDepth; i++){ 899 sqlite3Fts3ExprFree(apLeaf[i]); 900 } 901 while( (pDel=pFree)!=0 ){ 902 pFree = pDel->pParent; 903 sqlite3_free(pDel); 904 } 905 } 906 907 assert( pFree==0 ); 908 sqlite3_free( apLeaf ); 909 } 910 } 911 912 if( rc!=SQLITE_OK ){ 913 sqlite3Fts3ExprFree(pRoot); 914 pRoot = 0; 915 } 916 *pp = pRoot; 917 return rc; 918 } 919 920 /* 921 ** This function is similar to sqlite3Fts3ExprParse(), with the following 922 ** differences: 923 ** 924 ** 1. It does not do expression rebalancing. 925 ** 2. It does not check that the expression does not exceed the 926 ** maximum allowable depth. 927 ** 3. Even if it fails, *ppExpr may still be set to point to an 928 ** expression tree. It should be deleted using sqlite3Fts3ExprFree() 929 ** in this case. 930 */ 931 static int fts3ExprParseUnbalanced( 932 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */ 933 int iLangid, /* Language id for tokenizer */ 934 char **azCol, /* Array of column names for fts3 table */ 935 int bFts4, /* True to allow FTS4-only syntax */ 936 int nCol, /* Number of entries in azCol[] */ 937 int iDefaultCol, /* Default column to query */ 938 const char *z, int n, /* Text of MATCH query */ 939 Fts3Expr **ppExpr /* OUT: Parsed query structure */ 940 ){ 941 int nParsed; 942 int rc; 943 ParseContext sParse; 944 945 memset(&sParse, 0, sizeof(ParseContext)); 946 sParse.pTokenizer = pTokenizer; 947 sParse.iLangid = iLangid; 948 sParse.azCol = (const char **)azCol; 949 sParse.nCol = nCol; 950 sParse.iDefaultCol = iDefaultCol; 951 sParse.bFts4 = bFts4; 952 if( z==0 ){ 953 *ppExpr = 0; 954 return SQLITE_OK; 955 } 956 if( n<0 ){ 957 n = (int)strlen(z); 958 } 959 rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed); 960 assert( rc==SQLITE_OK || *ppExpr==0 ); 961 962 /* Check for mismatched parenthesis */ 963 if( rc==SQLITE_OK && sParse.nNest ){ 964 rc = SQLITE_ERROR; 965 } 966 967 return rc; 968 } 969 970 /* 971 ** Parameters z and n contain a pointer to and length of a buffer containing 972 ** an fts3 query expression, respectively. This function attempts to parse the 973 ** query expression and create a tree of Fts3Expr structures representing the 974 ** parsed expression. If successful, *ppExpr is set to point to the head 975 ** of the parsed expression tree and SQLITE_OK is returned. If an error 976 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse 977 ** error) is returned and *ppExpr is set to 0. 978 ** 979 ** If parameter n is a negative number, then z is assumed to point to a 980 ** nul-terminated string and the length is determined using strlen(). 981 ** 982 ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to 983 ** use to normalize query tokens while parsing the expression. The azCol[] 984 ** array, which is assumed to contain nCol entries, should contain the names 985 ** of each column in the target fts3 table, in order from left to right. 986 ** Column names must be nul-terminated strings. 987 ** 988 ** The iDefaultCol parameter should be passed the index of the table column 989 ** that appears on the left-hand-side of the MATCH operator (the default 990 ** column to match against for tokens for which a column name is not explicitly 991 ** specified as part of the query string), or -1 if tokens may by default 992 ** match any table column. 993 */ 994 int sqlite3Fts3ExprParse( 995 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */ 996 int iLangid, /* Language id for tokenizer */ 997 char **azCol, /* Array of column names for fts3 table */ 998 int bFts4, /* True to allow FTS4-only syntax */ 999 int nCol, /* Number of entries in azCol[] */ 1000 int iDefaultCol, /* Default column to query */ 1001 const char *z, int n, /* Text of MATCH query */ 1002 Fts3Expr **ppExpr, /* OUT: Parsed query structure */ 1003 char **pzErr /* OUT: Error message (sqlite3_malloc) */ 1004 ){ 1005 int rc = fts3ExprParseUnbalanced( 1006 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr 1007 ); 1008 1009 /* Rebalance the expression. And check that its depth does not exceed 1010 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */ 1011 if( rc==SQLITE_OK && *ppExpr ){ 1012 rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); 1013 if( rc==SQLITE_OK ){ 1014 rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); 1015 } 1016 } 1017 1018 if( rc!=SQLITE_OK ){ 1019 sqlite3Fts3ExprFree(*ppExpr); 1020 *ppExpr = 0; 1021 if( rc==SQLITE_TOOBIG ){ 1022 *pzErr = sqlite3_mprintf( 1023 "FTS expression tree is too large (maximum depth %d)", 1024 SQLITE_FTS3_MAX_EXPR_DEPTH 1025 ); 1026 rc = SQLITE_ERROR; 1027 }else if( rc==SQLITE_ERROR ){ 1028 *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z); 1029 } 1030 } 1031 1032 return rc; 1033 } 1034 1035 /* 1036 ** Free a single node of an expression tree. 1037 */ 1038 static void fts3FreeExprNode(Fts3Expr *p){ 1039 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 ); 1040 sqlite3Fts3EvalPhraseCleanup(p->pPhrase); 1041 sqlite3_free(p->aMI); 1042 sqlite3_free(p); 1043 } 1044 1045 /* 1046 ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse(). 1047 ** 1048 ** This function would be simpler if it recursively called itself. But 1049 ** that would mean passing a sufficiently large expression to ExprParse() 1050 ** could cause a stack overflow. 1051 */ 1052 void sqlite3Fts3ExprFree(Fts3Expr *pDel){ 1053 Fts3Expr *p; 1054 assert( pDel==0 || pDel->pParent==0 ); 1055 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){ 1056 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft ); 1057 } 1058 while( p ){ 1059 Fts3Expr *pParent = p->pParent; 1060 fts3FreeExprNode(p); 1061 if( pParent && p==pParent->pLeft && pParent->pRight ){ 1062 p = pParent->pRight; 1063 while( p && (p->pLeft || p->pRight) ){ 1064 assert( p==p->pParent->pRight || p==p->pParent->pLeft ); 1065 p = (p->pLeft ? p->pLeft : p->pRight); 1066 } 1067 }else{ 1068 p = pParent; 1069 } 1070 } 1071 } 1072 1073 /**************************************************************************** 1074 ***************************************************************************** 1075 ** Everything after this point is just test code. 1076 */ 1077 1078 #ifdef SQLITE_TEST 1079 1080 #include <stdio.h> 1081 1082 /* 1083 ** Function to query the hash-table of tokenizers (see README.tokenizers). 1084 */ 1085 static int queryTestTokenizer( 1086 sqlite3 *db, 1087 const char *zName, 1088 const sqlite3_tokenizer_module **pp 1089 ){ 1090 int rc; 1091 sqlite3_stmt *pStmt; 1092 const char zSql[] = "SELECT fts3_tokenizer(?)"; 1093 1094 *pp = 0; 1095 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 1096 if( rc!=SQLITE_OK ){ 1097 return rc; 1098 } 1099 1100 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); 1101 if( SQLITE_ROW==sqlite3_step(pStmt) ){ 1102 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ 1103 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); 1104 } 1105 } 1106 1107 return sqlite3_finalize(pStmt); 1108 } 1109 1110 /* 1111 ** Return a pointer to a buffer containing a text representation of the 1112 ** expression passed as the first argument. The buffer is obtained from 1113 ** sqlite3_malloc(). It is the responsibility of the caller to use 1114 ** sqlite3_free() to release the memory. If an OOM condition is encountered, 1115 ** NULL is returned. 1116 ** 1117 ** If the second argument is not NULL, then its contents are prepended to 1118 ** the returned expression text and then freed using sqlite3_free(). 1119 */ 1120 static char *exprToString(Fts3Expr *pExpr, char *zBuf){ 1121 if( pExpr==0 ){ 1122 return sqlite3_mprintf(""); 1123 } 1124 switch( pExpr->eType ){ 1125 case FTSQUERY_PHRASE: { 1126 Fts3Phrase *pPhrase = pExpr->pPhrase; 1127 int i; 1128 zBuf = sqlite3_mprintf( 1129 "%zPHRASE %d 0", zBuf, pPhrase->iColumn); 1130 for(i=0; zBuf && i<pPhrase->nToken; i++){ 1131 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, 1132 pPhrase->aToken[i].n, pPhrase->aToken[i].z, 1133 (pPhrase->aToken[i].isPrefix?"+":"") 1134 ); 1135 } 1136 return zBuf; 1137 } 1138 1139 case FTSQUERY_NEAR: 1140 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear); 1141 break; 1142 case FTSQUERY_NOT: 1143 zBuf = sqlite3_mprintf("%zNOT ", zBuf); 1144 break; 1145 case FTSQUERY_AND: 1146 zBuf = sqlite3_mprintf("%zAND ", zBuf); 1147 break; 1148 case FTSQUERY_OR: 1149 zBuf = sqlite3_mprintf("%zOR ", zBuf); 1150 break; 1151 } 1152 1153 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf); 1154 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf); 1155 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf); 1156 1157 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf); 1158 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf); 1159 1160 return zBuf; 1161 } 1162 1163 /* 1164 ** This is the implementation of a scalar SQL function used to test the 1165 ** expression parser. It should be called as follows: 1166 ** 1167 ** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...); 1168 ** 1169 ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used 1170 ** to parse the query expression (see README.tokenizers). The second argument 1171 ** is the query expression to parse. Each subsequent argument is the name 1172 ** of a column of the fts3 table that the query expression may refer to. 1173 ** For example: 1174 ** 1175 ** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2'); 1176 */ 1177 static void fts3ExprTest( 1178 sqlite3_context *context, 1179 int argc, 1180 sqlite3_value **argv 1181 ){ 1182 sqlite3_tokenizer_module const *pModule = 0; 1183 sqlite3_tokenizer *pTokenizer = 0; 1184 int rc; 1185 char **azCol = 0; 1186 const char *zExpr; 1187 int nExpr; 1188 int nCol; 1189 int ii; 1190 Fts3Expr *pExpr; 1191 char *zBuf = 0; 1192 sqlite3 *db = sqlite3_context_db_handle(context); 1193 1194 if( argc<3 ){ 1195 sqlite3_result_error(context, 1196 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1 1197 ); 1198 return; 1199 } 1200 1201 rc = queryTestTokenizer(db, 1202 (const char *)sqlite3_value_text(argv[0]), &pModule); 1203 if( rc==SQLITE_NOMEM ){ 1204 sqlite3_result_error_nomem(context); 1205 goto exprtest_out; 1206 }else if( !pModule ){ 1207 sqlite3_result_error(context, "No such tokenizer module", -1); 1208 goto exprtest_out; 1209 } 1210 1211 rc = pModule->xCreate(0, 0, &pTokenizer); 1212 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); 1213 if( rc==SQLITE_NOMEM ){ 1214 sqlite3_result_error_nomem(context); 1215 goto exprtest_out; 1216 } 1217 pTokenizer->pModule = pModule; 1218 1219 zExpr = (const char *)sqlite3_value_text(argv[1]); 1220 nExpr = sqlite3_value_bytes(argv[1]); 1221 nCol = argc-2; 1222 azCol = (char **)sqlite3_malloc(nCol*sizeof(char *)); 1223 if( !azCol ){ 1224 sqlite3_result_error_nomem(context); 1225 goto exprtest_out; 1226 } 1227 for(ii=0; ii<nCol; ii++){ 1228 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]); 1229 } 1230 1231 if( sqlite3_user_data(context) ){ 1232 char *zDummy = 0; 1233 rc = sqlite3Fts3ExprParse( 1234 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy 1235 ); 1236 assert( rc==SQLITE_OK || pExpr==0 ); 1237 sqlite3_free(zDummy); 1238 }else{ 1239 rc = fts3ExprParseUnbalanced( 1240 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr 1241 ); 1242 } 1243 1244 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){ 1245 sqlite3Fts3ExprFree(pExpr); 1246 sqlite3_result_error(context, "Error parsing expression", -1); 1247 }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){ 1248 sqlite3_result_error_nomem(context); 1249 }else{ 1250 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 1251 sqlite3_free(zBuf); 1252 } 1253 1254 sqlite3Fts3ExprFree(pExpr); 1255 1256 exprtest_out: 1257 if( pModule && pTokenizer ){ 1258 rc = pModule->xDestroy(pTokenizer); 1259 } 1260 sqlite3_free(azCol); 1261 } 1262 1263 /* 1264 ** Register the query expression parser test function fts3_exprtest() 1265 ** with database connection db. 1266 */ 1267 int sqlite3Fts3ExprInitTestInterface(sqlite3* db){ 1268 int rc = sqlite3_create_function( 1269 db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0 1270 ); 1271 if( rc==SQLITE_OK ){ 1272 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance", 1273 -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0 1274 ); 1275 } 1276 return rc; 1277 } 1278 1279 #endif 1280 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ 1281