sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / fts5 / fts5_config.c
| 1 | /* |
| 2 | ** 2014 Jun 09 |
| 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 is an SQLite module implementing full-text search. |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | #include "fts5Int.h" |
| 18 | |
| 19 | #define FTS5_DEFAULT_PAGE_SIZE 4050 |
| 20 | #define FTS5_DEFAULT_AUTOMERGE 4 |
| 21 | #define FTS5_DEFAULT_USERMERGE 4 |
| 22 | #define FTS5_DEFAULT_CRISISMERGE 16 |
| 23 | #define FTS5_DEFAULT_HASHSIZE (1024*1024) |
| 24 | |
| 25 | #define FTS5_DEFAULT_DELETE_AUTOMERGE 10 /* default 10% */ |
| 26 | |
| 27 | /* Maximum allowed page size */ |
| 28 | #define FTS5_MAX_PAGE_SIZE (64*1024) |
| 29 | |
| 30 | static int fts5_iswhitespace(char x){ |
| 31 | return (x==' '); |
| 32 | } |
| 33 | |
| 34 | static int fts5_isopenquote(char x){ |
| 35 | return (x=='"' || x=='\'' || x=='[' || x=='`'); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | ** Argument pIn points to a character that is part of a nul-terminated |
| 40 | ** string. Return a pointer to the first character following *pIn in |
| 41 | ** the string that is not a white-space character. |
| 42 | */ |
| 43 | static const char *fts5ConfigSkipWhitespace(const char *pIn){ |
| 44 | const char *p = pIn; |
| 45 | if( p ){ |
| 46 | while( fts5_iswhitespace(*p) ){ p++; } |
| 47 | } |
| 48 | return p; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | ** Argument pIn points to a character that is part of a nul-terminated |
| 53 | ** string. Return a pointer to the first character following *pIn in |
| 54 | ** the string that is not a "bareword" character. |
| 55 | */ |
| 56 | static const char *fts5ConfigSkipBareword(const char *pIn){ |
| 57 | const char *p = pIn; |
| 58 | while ( sqlite3Fts5IsBareword(*p) ) p++; |
| 59 | if( p==pIn ) p = 0; |
| 60 | return p; |
| 61 | } |
| 62 | |
| 63 | static int fts5_isdigit(char a){ |
| 64 | return (a>='0' && a<='9'); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | static const char *fts5ConfigSkipLiteral(const char *pIn){ |
| 70 | const char *p = pIn; |
| 71 | switch( *p ){ |
| 72 | case 'n': case 'N': |
| 73 | if( sqlite3_strnicmp("null", p, 4)==0 ){ |
| 74 | p = &p[4]; |
| 75 | }else{ |
| 76 | p = 0; |
| 77 | } |
| 78 | break; |
| 79 | |
| 80 | case 'x': case 'X': |
| 81 | p++; |
| 82 | if( *p=='\'' ){ |
| 83 | p++; |
| 84 | while( (*p>='a' && *p<='f') |
| 85 | || (*p>='A' && *p<='F') |
| 86 | || (*p>='0' && *p<='9') |
| 87 | ){ |
| 88 | p++; |
| 89 | } |
| 90 | if( *p=='\'' && 0==((p-pIn)%2) ){ |
| 91 | p++; |
| 92 | }else{ |
| 93 | p = 0; |
| 94 | } |
| 95 | }else{ |
| 96 | p = 0; |
| 97 | } |
| 98 | break; |
| 99 | |
| 100 | case '\'': |
| 101 | p++; |
| 102 | while( p ){ |
| 103 | if( *p=='\'' ){ |
| 104 | p++; |
| 105 | if( *p!='\'' ) break; |
| 106 | }else if( *p==0 ){ |
| 107 | p = 0; |
| 108 | break; |
| 109 | } |
| 110 | p++; |
| 111 | if( *p==0 ) p = 0; |
| 112 | } |
| 113 | break; |
| 114 | |
| 115 | default: |
| 116 | /* maybe a number */ |
| 117 | if( *p=='+' || *p=='-' ) p++; |
| 118 | while( fts5_isdigit(*p) ) p++; |
| 119 | |
| 120 | /* At this point, if the literal was an integer, the parse is |
| 121 | ** finished. Or, if it is a floating point value, it may continue |
| 122 | ** with either a decimal point or an 'E' character. */ |
| 123 | if( *p=='.' && fts5_isdigit(p[1]) ){ |
| 124 | p += 2; |
| 125 | while( fts5_isdigit(*p) ) p++; |
| 126 | } |
| 127 | if( p==pIn ) p = 0; |
| 128 | |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | return p; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | ** The first character of the string pointed to by argument z is guaranteed |
| 137 | ** to be an open-quote character (see function fts5_isopenquote()). |
| 138 | ** |
| 139 | ** This function searches for the corresponding close-quote character within |
| 140 | ** the string and, if found, dequotes the string in place and adds a new |
| 141 | ** nul-terminator byte. |
| 142 | ** |
| 143 | ** If the close-quote is found, the value returned is the byte offset of |
| 144 | ** the character immediately following it. Or, if the close-quote is not |
| 145 | ** found, -1 is returned. If -1 is returned, the buffer is left in an |
| 146 | ** undefined state. |
| 147 | */ |
| 148 | static int fts5Dequote(char *z){ |
| 149 | char q; |
| 150 | int iIn = 1; |
| 151 | int iOut = 0; |
| 152 | q = z[0]; |
| 153 | |
| 154 | /* Set stack variable q to the close-quote character */ |
| 155 | assert( q=='[' || q=='\'' || q=='"' || q=='`' ); |
| 156 | if( q=='[' ) q = ']'; |
| 157 | |
| 158 | while( z[iIn] ){ |
| 159 | if( z[iIn]==q ){ |
| 160 | if( z[iIn+1]!=q ){ |
| 161 | /* Character iIn was the close quote. */ |
| 162 | iIn++; |
| 163 | break; |
| 164 | }else{ |
| 165 | /* Character iIn and iIn+1 form an escaped quote character. Skip |
| 166 | ** the input cursor past both and copy a single quote character |
| 167 | ** to the output buffer. */ |
| 168 | iIn += 2; |
| 169 | z[iOut++] = q; |
| 170 | } |
| 171 | }else{ |
| 172 | z[iOut++] = z[iIn++]; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | z[iOut] = '\0'; |
| 177 | return iIn; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | ** Convert an SQL-style quoted string into a normal string by removing |
| 182 | ** the quote characters. The conversion is done in-place. If the |
| 183 | ** input does not begin with a quote character, then this routine |
| 184 | ** is a no-op. |
| 185 | ** |
| 186 | ** Examples: |
| 187 | ** |
| 188 | ** "abc" becomes abc |
| 189 | ** 'xyz' becomes xyz |
| 190 | ** [pqr] becomes pqr |
| 191 | ** `mno` becomes mno |
| 192 | */ |
| 193 | void sqlite3Fts5Dequote(char *z){ |
| 194 | char quote; /* Quote character (if any ) */ |
| 195 | |
| 196 | assert( 0==fts5_iswhitespace(z[0]) ); |
| 197 | quote = z[0]; |
| 198 | if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){ |
| 199 | fts5Dequote(z); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | struct Fts5Enum { |
| 205 | const char *zName; |
| 206 | int eVal; |
| 207 | }; |
| 208 | typedef struct Fts5Enum Fts5Enum; |
| 209 | |
| 210 | static int fts5ConfigSetEnum( |
| 211 | const Fts5Enum *aEnum, |
| 212 | const char *zEnum, |
| 213 | int *peVal |
| 214 | ){ |
| 215 | int nEnum = (int)strlen(zEnum); |
| 216 | int i; |
| 217 | int iVal = -1; |
| 218 | |
| 219 | for(i=0; aEnum[i].zName; i++){ |
| 220 | if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){ |
| 221 | if( iVal>=0 ) return SQLITE_ERROR; |
| 222 | iVal = aEnum[i].eVal; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | *peVal = iVal; |
| 227 | return iVal<0 ? SQLITE_ERROR : SQLITE_OK; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | ** Parse a "special" CREATE VIRTUAL TABLE directive and update |
| 232 | ** configuration object pConfig as appropriate. |
| 233 | ** |
| 234 | ** If successful, object pConfig is updated and SQLITE_OK returned. If |
| 235 | ** an error occurs, an SQLite error code is returned and an error message |
| 236 | ** may be left in *pzErr. It is the responsibility of the caller to |
| 237 | ** eventually free any such error message using sqlite3_free(). |
| 238 | */ |
| 239 | static int fts5ConfigParseSpecial( |
| 240 | Fts5Config *pConfig, /* Configuration object to update */ |
| 241 | const char *zCmd, /* Special command to parse */ |
| 242 | const char *zArg, /* Argument to parse */ |
| 243 | char **pzErr /* OUT: Error message */ |
| 244 | ){ |
| 245 | int rc = SQLITE_OK; |
| 246 | int nCmd = (int)strlen(zCmd); |
| 247 | |
| 248 | if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){ |
| 249 | const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES; |
| 250 | const char *p; |
| 251 | int bFirst = 1; |
| 252 | if( pConfig->aPrefix==0 ){ |
| 253 | pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte); |
| 254 | if( rc ) return rc; |
| 255 | } |
| 256 | |
| 257 | p = zArg; |
| 258 | while( 1 ){ |
| 259 | int nPre = 0; |
| 260 | |
| 261 | while( p[0]==' ' ) p++; |
| 262 | if( bFirst==0 && p[0]==',' ){ |
| 263 | p++; |
| 264 | while( p[0]==' ' ) p++; |
| 265 | }else if( p[0]=='\0' ){ |
| 266 | break; |
| 267 | } |
| 268 | if( p[0]<'0' || p[0]>'9' ){ |
| 269 | *pzErr = sqlite3_mprintf("malformed prefix=... directive"); |
| 270 | rc = SQLITE_ERROR; |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){ |
| 275 | *pzErr = sqlite3_mprintf( |
| 276 | "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES |
| 277 | ); |
| 278 | rc = SQLITE_ERROR; |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | while( p[0]>='0' && p[0]<='9' && nPre<1000 ){ |
| 283 | nPre = nPre*10 + (p[0] - '0'); |
| 284 | p++; |
| 285 | } |
| 286 | |
| 287 | if( nPre<=0 || nPre>=1000 ){ |
| 288 | *pzErr = sqlite3_mprintf("prefix length out of range (max 999)"); |
| 289 | rc = SQLITE_ERROR; |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | pConfig->aPrefix[pConfig->nPrefix] = nPre; |
| 294 | pConfig->nPrefix++; |
| 295 | bFirst = 0; |
| 296 | } |
| 297 | assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES ); |
| 298 | return rc; |
| 299 | } |
| 300 | |
| 301 | if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){ |
| 302 | const char *p = (const char*)zArg; |
| 303 | sqlite3_int64 nArg = strlen(zArg) + 1; |
| 304 | char **azArg = sqlite3Fts5MallocZero(&rc, (sizeof(char*) + 2) * nArg); |
| 305 | |
| 306 | if( azArg ){ |
| 307 | char *pSpace = (char*)&azArg[nArg]; |
| 308 | if( pConfig->t.azArg ){ |
| 309 | *pzErr = sqlite3_mprintf("multiple tokenize=... directives"); |
| 310 | rc = SQLITE_ERROR; |
| 311 | }else{ |
| 312 | for(nArg=0; p && *p; nArg++){ |
| 313 | const char *p2 = fts5ConfigSkipWhitespace(p); |
| 314 | if( *p2=='\'' ){ |
| 315 | p = fts5ConfigSkipLiteral(p2); |
| 316 | }else{ |
| 317 | p = fts5ConfigSkipBareword(p2); |
| 318 | } |
| 319 | if( p ){ |
| 320 | memcpy(pSpace, p2, p-p2); |
| 321 | azArg[nArg] = pSpace; |
| 322 | sqlite3Fts5Dequote(pSpace); |
| 323 | pSpace += (p - p2) + 1; |
| 324 | p = fts5ConfigSkipWhitespace(p); |
| 325 | } |
| 326 | } |
| 327 | if( p==0 ){ |
| 328 | *pzErr = sqlite3_mprintf("parse error in tokenize directive"); |
| 329 | rc = SQLITE_ERROR; |
| 330 | }else{ |
| 331 | pConfig->t.azArg = (const char**)azArg; |
| 332 | pConfig->t.nArg = nArg; |
| 333 | azArg = 0; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | sqlite3_free(azArg); |
| 338 | |
| 339 | return rc; |
| 340 | } |
| 341 | |
| 342 | if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){ |
| 343 | if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){ |
| 344 | *pzErr = sqlite3_mprintf("multiple content=... directives"); |
| 345 | rc = SQLITE_ERROR; |
| 346 | }else{ |
| 347 | if( zArg[0] ){ |
| 348 | pConfig->eContent = FTS5_CONTENT_EXTERNAL; |
| 349 | pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg); |
| 350 | }else{ |
| 351 | pConfig->eContent = FTS5_CONTENT_NONE; |
| 352 | } |
| 353 | } |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | if( sqlite3_strnicmp("contentless_delete", zCmd, nCmd)==0 ){ |
| 358 | if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ |
| 359 | *pzErr = sqlite3_mprintf("malformed contentless_delete=... directive"); |
| 360 | rc = SQLITE_ERROR; |
| 361 | }else{ |
| 362 | pConfig->bContentlessDelete = (zArg[0]=='1'); |
| 363 | } |
| 364 | return rc; |
| 365 | } |
| 366 | |
| 367 | if( sqlite3_strnicmp("contentless_unindexed", zCmd, nCmd)==0 ){ |
| 368 | if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ |
| 369 | *pzErr = sqlite3_mprintf("malformed contentless_delete=... directive"); |
| 370 | rc = SQLITE_ERROR; |
| 371 | }else{ |
| 372 | pConfig->bContentlessUnindexed = (zArg[0]=='1'); |
| 373 | } |
| 374 | return rc; |
| 375 | } |
| 376 | |
| 377 | if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){ |
| 378 | if( pConfig->zContentRowid ){ |
| 379 | *pzErr = sqlite3_mprintf("multiple content_rowid=... directives"); |
| 380 | rc = SQLITE_ERROR; |
| 381 | }else{ |
| 382 | pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1); |
| 383 | } |
| 384 | return rc; |
| 385 | } |
| 386 | |
| 387 | if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){ |
| 388 | if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ |
| 389 | *pzErr = sqlite3_mprintf("malformed columnsize=... directive"); |
| 390 | rc = SQLITE_ERROR; |
| 391 | }else{ |
| 392 | pConfig->bColumnsize = (zArg[0]=='1'); |
| 393 | } |
| 394 | return rc; |
| 395 | } |
| 396 | |
| 397 | if( sqlite3_strnicmp("locale", zCmd, nCmd)==0 ){ |
| 398 | if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ |
| 399 | *pzErr = sqlite3_mprintf("malformed locale=... directive"); |
| 400 | rc = SQLITE_ERROR; |
| 401 | }else{ |
| 402 | pConfig->bLocale = (zArg[0]=='1'); |
| 403 | } |
| 404 | return rc; |
| 405 | } |
| 406 | |
| 407 | if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){ |
| 408 | const Fts5Enum aDetail[] = { |
| 409 | { "none", FTS5_DETAIL_NONE }, |
| 410 | { "full", FTS5_DETAIL_FULL }, |
| 411 | { "columns", FTS5_DETAIL_COLUMNS }, |
| 412 | { 0, 0 } |
| 413 | }; |
| 414 | |
| 415 | if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){ |
| 416 | *pzErr = sqlite3_mprintf("malformed detail=... directive"); |
| 417 | } |
| 418 | return rc; |
| 419 | } |
| 420 | |
| 421 | if( sqlite3_strnicmp("tokendata", zCmd, nCmd)==0 ){ |
| 422 | if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ |
| 423 | *pzErr = sqlite3_mprintf("malformed tokendata=... directive"); |
| 424 | rc = SQLITE_ERROR; |
| 425 | }else{ |
| 426 | pConfig->bTokendata = (zArg[0]=='1'); |
| 427 | } |
| 428 | return rc; |
| 429 | } |
| 430 | |
| 431 | *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd); |
| 432 | return SQLITE_ERROR; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | ** Gobble up the first bareword or quoted word from the input buffer zIn. |
| 437 | ** Return a pointer to the character immediately following the last in |
| 438 | ** the gobbled word if successful, or a NULL pointer otherwise (failed |
| 439 | ** to find close-quote character). |
| 440 | ** |
| 441 | ** Before returning, set pzOut to point to a new buffer containing a |
| 442 | ** nul-terminated, dequoted copy of the gobbled word. If the word was |
| 443 | ** quoted, *pbQuoted is also set to 1 before returning. |
| 444 | ** |
| 445 | ** If *pRc is other than SQLITE_OK when this function is called, it is |
| 446 | ** a no-op (NULL is returned). Otherwise, if an OOM occurs within this |
| 447 | ** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not* |
| 448 | ** set if a parse error (failed to find close quote) occurs. |
| 449 | */ |
| 450 | static const char *fts5ConfigGobbleWord( |
| 451 | int *pRc, /* IN/OUT: Error code */ |
| 452 | const char *zIn, /* Buffer to gobble string/bareword from */ |
| 453 | char **pzOut, /* OUT: malloc'd buffer containing str/bw */ |
| 454 | int *pbQuoted /* OUT: Set to true if dequoting required */ |
| 455 | ){ |
| 456 | const char *zRet = 0; |
| 457 | |
| 458 | sqlite3_int64 nIn = strlen(zIn); |
| 459 | char *zOut = sqlite3_malloc64(nIn+1); |
| 460 | |
| 461 | assert( *pRc==SQLITE_OK ); |
| 462 | *pbQuoted = 0; |
| 463 | *pzOut = 0; |
| 464 | |
| 465 | if( zOut==0 ){ |
| 466 | *pRc = SQLITE_NOMEM; |
| 467 | }else{ |
| 468 | memcpy(zOut, zIn, (size_t)(nIn+1)); |
| 469 | if( fts5_isopenquote(zOut[0]) ){ |
| 470 | int ii = fts5Dequote(zOut); |
| 471 | zRet = &zIn[ii]; |
| 472 | *pbQuoted = 1; |
| 473 | }else{ |
| 474 | zRet = fts5ConfigSkipBareword(zIn); |
| 475 | if( zRet ){ |
| 476 | zOut[zRet-zIn] = '\0'; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if( zRet==0 ){ |
| 482 | sqlite3_free(zOut); |
| 483 | }else{ |
| 484 | *pzOut = zOut; |
| 485 | } |
| 486 | |
| 487 | return zRet; |
| 488 | } |
| 489 | |
| 490 | static int fts5ConfigParseColumn( |
| 491 | Fts5Config *p, |
| 492 | char *zCol, |
| 493 | char *zArg, |
| 494 | char **pzErr, |
| 495 | int *pbUnindexed |
| 496 | ){ |
| 497 | int rc = SQLITE_OK; |
| 498 | if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME) |
| 499 | || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME) |
| 500 | ){ |
| 501 | *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol); |
| 502 | rc = SQLITE_ERROR; |
| 503 | }else if( zArg ){ |
| 504 | if( 0==sqlite3_stricmp(zArg, "unindexed") ){ |
| 505 | p->abUnindexed[p->nCol] = 1; |
| 506 | *pbUnindexed = 1; |
| 507 | }else{ |
| 508 | *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg); |
| 509 | rc = SQLITE_ERROR; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | p->azCol[p->nCol++] = zCol; |
| 514 | return rc; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | ** Populate the Fts5Config.zContentExprlist string. |
| 519 | */ |
| 520 | static int fts5ConfigMakeExprlist(Fts5Config *p){ |
| 521 | int i; |
| 522 | int rc = SQLITE_OK; |
| 523 | Fts5Buffer buf = {0, 0, 0}; |
| 524 | |
| 525 | sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid); |
| 526 | if( p->eContent!=FTS5_CONTENT_NONE ){ |
| 527 | assert( p->eContent==FTS5_CONTENT_EXTERNAL |
| 528 | || p->eContent==FTS5_CONTENT_NORMAL |
| 529 | || p->eContent==FTS5_CONTENT_UNINDEXED |
| 530 | ); |
| 531 | for(i=0; i<p->nCol; i++){ |
| 532 | if( p->eContent==FTS5_CONTENT_EXTERNAL ){ |
| 533 | sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]); |
| 534 | }else if( p->eContent==FTS5_CONTENT_NORMAL || p->abUnindexed[i] ){ |
| 535 | sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i); |
| 536 | }else{ |
| 537 | sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", NULL"); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | if( p->eContent==FTS5_CONTENT_NORMAL && p->bLocale ){ |
| 542 | for(i=0; i<p->nCol; i++){ |
| 543 | if( p->abUnindexed[i]==0 ){ |
| 544 | sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.l%d", i); |
| 545 | }else{ |
| 546 | sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", NULL"); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | assert( p->zContentExprlist==0 ); |
| 552 | p->zContentExprlist = (char*)buf.p; |
| 553 | return rc; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | ** Arguments nArg/azArg contain the string arguments passed to the xCreate |
| 558 | ** or xConnect method of the virtual table. This function attempts to |
| 559 | ** allocate an instance of Fts5Config containing the results of parsing |
| 560 | ** those arguments. |
| 561 | ** |
| 562 | ** If successful, SQLITE_OK is returned and *ppOut is set to point to the |
| 563 | ** new Fts5Config object. If an error occurs, an SQLite error code is |
| 564 | ** returned, *ppOut is set to NULL and an error message may be left in |
| 565 | ** *pzErr. It is the responsibility of the caller to eventually free any |
| 566 | ** such error message using sqlite3_free(). |
| 567 | */ |
| 568 | int sqlite3Fts5ConfigParse( |
| 569 | Fts5Global *pGlobal, |
| 570 | sqlite3 *db, |
| 571 | int nArg, /* Number of arguments */ |
| 572 | const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */ |
| 573 | Fts5Config **ppOut, /* OUT: Results of parse */ |
| 574 | char **pzErr /* OUT: Error message */ |
| 575 | ){ |
| 576 | int rc = SQLITE_OK; /* Return code */ |
| 577 | Fts5Config *pRet; /* New object to return */ |
| 578 | int i; |
| 579 | sqlite3_int64 nByte; |
| 580 | int bUnindexed = 0; /* True if there are one or more UNINDEXED */ |
| 581 | |
| 582 | *ppOut = pRet = (Fts5Config*)sqlite3_malloc64(sizeof(Fts5Config)); |
| 583 | if( pRet==0 ) return SQLITE_NOMEM; |
| 584 | memset(pRet, 0, sizeof(Fts5Config)); |
| 585 | pRet->pGlobal = pGlobal; |
| 586 | pRet->db = db; |
| 587 | pRet->iCookie = -1; |
| 588 | |
| 589 | nByte = nArg * (sizeof(char*) + sizeof(u8)); |
| 590 | pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte); |
| 591 | pRet->abUnindexed = pRet->azCol ? (u8*)&pRet->azCol[nArg] : 0; |
| 592 | pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1); |
| 593 | pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1); |
| 594 | pRet->bColumnsize = 1; |
| 595 | pRet->eDetail = FTS5_DETAIL_FULL; |
| 596 | #ifdef SQLITE_DEBUG |
| 597 | pRet->bPrefixIndex = 1; |
| 598 | #endif |
| 599 | if( rc==SQLITE_OK && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){ |
| 600 | *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName); |
| 601 | rc = SQLITE_ERROR; |
| 602 | } |
| 603 | |
| 604 | assert( (pRet->abUnindexed && pRet->azCol) || rc!=SQLITE_OK ); |
| 605 | for(i=3; rc==SQLITE_OK && i<nArg; i++){ |
| 606 | const char *zOrig = azArg[i]; |
| 607 | const char *z; |
| 608 | char *zOne = 0; |
| 609 | char *zTwo = 0; |
| 610 | int bOption = 0; |
| 611 | int bMustBeCol = 0; |
| 612 | |
| 613 | z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol); |
| 614 | z = fts5ConfigSkipWhitespace(z); |
| 615 | if( z && *z=='=' ){ |
| 616 | bOption = 1; |
| 617 | assert( zOne!=0 ); |
| 618 | z++; |
| 619 | if( bMustBeCol ) z = 0; |
| 620 | } |
| 621 | z = fts5ConfigSkipWhitespace(z); |
| 622 | if( z && z[0] ){ |
| 623 | int bDummy; |
| 624 | z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy); |
| 625 | if( z && z[0] ) z = 0; |
| 626 | } |
| 627 | |
| 628 | if( rc==SQLITE_OK ){ |
| 629 | if( z==0 ){ |
| 630 | *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig); |
| 631 | rc = SQLITE_ERROR; |
| 632 | }else{ |
| 633 | if( bOption ){ |
| 634 | rc = fts5ConfigParseSpecial(pRet, |
| 635 | ALWAYS(zOne)?zOne:"", |
| 636 | zTwo?zTwo:"", |
| 637 | pzErr |
| 638 | ); |
| 639 | }else{ |
| 640 | rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr, &bUnindexed); |
| 641 | zOne = 0; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | sqlite3_free(zOne); |
| 647 | sqlite3_free(zTwo); |
| 648 | } |
| 649 | |
| 650 | /* We only allow contentless_delete=1 if the table is indeed contentless. */ |
| 651 | if( rc==SQLITE_OK |
| 652 | && pRet->bContentlessDelete |
| 653 | && pRet->eContent!=FTS5_CONTENT_NONE |
| 654 | ){ |
| 655 | *pzErr = sqlite3_mprintf( |
| 656 | "contentless_delete=1 requires a contentless table" |
| 657 | ); |
| 658 | rc = SQLITE_ERROR; |
| 659 | } |
| 660 | |
| 661 | /* We only allow contentless_delete=1 if columnsize=0 is not present. |
| 662 | ** |
| 663 | ** This restriction may be removed at some point. |
| 664 | */ |
| 665 | if( rc==SQLITE_OK && pRet->bContentlessDelete && pRet->bColumnsize==0 ){ |
| 666 | *pzErr = sqlite3_mprintf( |
| 667 | "contentless_delete=1 is incompatible with columnsize=0" |
| 668 | ); |
| 669 | rc = SQLITE_ERROR; |
| 670 | } |
| 671 | |
| 672 | /* We only allow contentless_unindexed=1 if the table is actually a |
| 673 | ** contentless one. |
| 674 | */ |
| 675 | if( rc==SQLITE_OK |
| 676 | && pRet->bContentlessUnindexed |
| 677 | && pRet->eContent!=FTS5_CONTENT_NONE |
| 678 | ){ |
| 679 | *pzErr = sqlite3_mprintf( |
| 680 | "contentless_unindexed=1 requires a contentless table" |
| 681 | ); |
| 682 | rc = SQLITE_ERROR; |
| 683 | } |
| 684 | |
| 685 | /* If no zContent option was specified, fill in the default values. */ |
| 686 | if( rc==SQLITE_OK && pRet->zContent==0 ){ |
| 687 | const char *zTail = 0; |
| 688 | assert( pRet->eContent==FTS5_CONTENT_NORMAL |
| 689 | || pRet->eContent==FTS5_CONTENT_NONE |
| 690 | ); |
| 691 | if( pRet->eContent==FTS5_CONTENT_NORMAL ){ |
| 692 | zTail = "content"; |
| 693 | }else if( bUnindexed && pRet->bContentlessUnindexed ){ |
| 694 | pRet->eContent = FTS5_CONTENT_UNINDEXED; |
| 695 | zTail = "content"; |
| 696 | }else if( pRet->bColumnsize ){ |
| 697 | zTail = "docsize"; |
| 698 | } |
| 699 | |
| 700 | if( zTail ){ |
| 701 | pRet->zContent = sqlite3Fts5Mprintf( |
| 702 | &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail |
| 703 | ); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if( rc==SQLITE_OK && pRet->zContentRowid==0 ){ |
| 708 | pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1); |
| 709 | } |
| 710 | |
| 711 | /* Formulate the zContentExprlist text */ |
| 712 | if( rc==SQLITE_OK ){ |
| 713 | rc = fts5ConfigMakeExprlist(pRet); |
| 714 | } |
| 715 | |
| 716 | if( rc!=SQLITE_OK ){ |
| 717 | sqlite3Fts5ConfigFree(pRet); |
| 718 | *ppOut = 0; |
| 719 | } |
| 720 | return rc; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | ** Free the configuration object passed as the only argument. |
| 725 | */ |
| 726 | void sqlite3Fts5ConfigFree(Fts5Config *pConfig){ |
| 727 | if( pConfig ){ |
| 728 | int i; |
| 729 | if( pConfig->t.pTok ){ |
| 730 | if( pConfig->t.pApi1 ){ |
| 731 | pConfig->t.pApi1->xDelete(pConfig->t.pTok); |
| 732 | }else{ |
| 733 | pConfig->t.pApi2->xDelete(pConfig->t.pTok); |
| 734 | } |
| 735 | } |
| 736 | sqlite3_free((char*)pConfig->t.azArg); |
| 737 | sqlite3_free(pConfig->zDb); |
| 738 | sqlite3_free(pConfig->zName); |
| 739 | for(i=0; i<pConfig->nCol; i++){ |
| 740 | sqlite3_free(pConfig->azCol[i]); |
| 741 | } |
| 742 | sqlite3_free(pConfig->azCol); |
| 743 | sqlite3_free(pConfig->aPrefix); |
| 744 | sqlite3_free(pConfig->zRank); |
| 745 | sqlite3_free(pConfig->zRankArgs); |
| 746 | sqlite3_free(pConfig->zContent); |
| 747 | sqlite3_free(pConfig->zContentRowid); |
| 748 | sqlite3_free(pConfig->zContentExprlist); |
| 749 | sqlite3_free(pConfig); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | ** Call sqlite3_declare_vtab() based on the contents of the configuration |
| 755 | ** object passed as the only argument. Return SQLITE_OK if successful, or |
| 756 | ** an SQLite error code if an error occurs. |
| 757 | */ |
| 758 | int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){ |
| 759 | int i; |
| 760 | int rc = SQLITE_OK; |
| 761 | char *zSql; |
| 762 | |
| 763 | zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x("); |
| 764 | for(i=0; zSql && i<pConfig->nCol; i++){ |
| 765 | const char *zSep = (i==0?"":", "); |
| 766 | zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]); |
| 767 | } |
| 768 | zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q BLOB HIDDEN, %s HIDDEN)", |
| 769 | zSql, pConfig->zName, FTS5_RANK_NAME |
| 770 | ); |
| 771 | |
| 772 | assert( zSql || rc==SQLITE_NOMEM ); |
| 773 | if( zSql ){ |
| 774 | rc = sqlite3_declare_vtab(pConfig->db, zSql); |
| 775 | sqlite3_free(zSql); |
| 776 | } |
| 777 | |
| 778 | return rc; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | ** Tokenize the text passed via the second and third arguments. |
| 783 | ** |
| 784 | ** The callback is invoked once for each token in the input text. The |
| 785 | ** arguments passed to it are, in order: |
| 786 | ** |
| 787 | ** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize() |
| 788 | ** const char *pToken // Pointer to buffer containing token |
| 789 | ** int nToken // Size of token in bytes |
| 790 | ** int iStart // Byte offset of start of token within input text |
| 791 | ** int iEnd // Byte offset of end of token within input text |
| 792 | ** int iPos // Position of token in input (first token is 0) |
| 793 | ** |
| 794 | ** If the callback returns a non-zero value the tokenization is abandoned |
| 795 | ** and no further callbacks are issued. |
| 796 | ** |
| 797 | ** This function returns SQLITE_OK if successful or an SQLite error code |
| 798 | ** if an error occurs. If the tokenization was abandoned early because |
| 799 | ** the callback returned SQLITE_DONE, this is not an error and this function |
| 800 | ** still returns SQLITE_OK. Or, if the tokenization was abandoned early |
| 801 | ** because the callback returned another non-zero value, it is assumed |
| 802 | ** to be an SQLite error code and returned to the caller. |
| 803 | */ |
| 804 | int sqlite3Fts5Tokenize( |
| 805 | Fts5Config *pConfig, /* FTS5 Configuration object */ |
| 806 | int flags, /* FTS5_TOKENIZE_* flags */ |
| 807 | const char *pText, int nText, /* Text to tokenize */ |
| 808 | void *pCtx, /* Context passed to xToken() */ |
| 809 | int (*xToken)(void*, int, const char*, int, int, int) /* Callback */ |
| 810 | ){ |
| 811 | int rc = SQLITE_OK; |
| 812 | if( pText ){ |
| 813 | if( pConfig->t.pTok==0 ){ |
| 814 | rc = sqlite3Fts5LoadTokenizer(pConfig); |
| 815 | } |
| 816 | if( rc==SQLITE_OK ){ |
| 817 | if( pConfig->t.pApi1 ){ |
| 818 | rc = pConfig->t.pApi1->xTokenize( |
| 819 | pConfig->t.pTok, pCtx, flags, pText, nText, xToken |
| 820 | ); |
| 821 | }else{ |
| 822 | rc = pConfig->t.pApi2->xTokenize(pConfig->t.pTok, pCtx, flags, |
| 823 | pText, nText, pConfig->t.pLocale, pConfig->t.nLocale, xToken |
| 824 | ); |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | return rc; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | ** Argument pIn points to the first character in what is expected to be |
| 833 | ** a comma-separated list of SQL literals followed by a ')' character. |
| 834 | ** If it actually is this, return a pointer to the ')'. Otherwise, return |
| 835 | ** NULL to indicate a parse error. |
| 836 | */ |
| 837 | static const char *fts5ConfigSkipArgs(const char *pIn){ |
| 838 | const char *p = pIn; |
| 839 | |
| 840 | while( 1 ){ |
| 841 | p = fts5ConfigSkipWhitespace(p); |
| 842 | p = fts5ConfigSkipLiteral(p); |
| 843 | p = fts5ConfigSkipWhitespace(p); |
| 844 | if( p==0 || *p==')' ) break; |
| 845 | if( *p!=',' ){ |
| 846 | p = 0; |
| 847 | break; |
| 848 | } |
| 849 | p++; |
| 850 | } |
| 851 | |
| 852 | return p; |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | ** Parameter zIn contains a rank() function specification. The format of |
| 857 | ** this is: |
| 858 | ** |
| 859 | ** + Bareword (function name) |
| 860 | ** + Open parenthesis - "(" |
| 861 | ** + Zero or more SQL literals in a comma separated list |
| 862 | ** + Close parenthesis - ")" |
| 863 | */ |
| 864 | int sqlite3Fts5ConfigParseRank( |
| 865 | const char *zIn, /* Input string */ |
| 866 | char **pzRank, /* OUT: Rank function name */ |
| 867 | char **pzRankArgs /* OUT: Rank function arguments */ |
| 868 | ){ |
| 869 | const char *p = zIn; |
| 870 | const char *pRank; |
| 871 | char *zRank = 0; |
| 872 | char *zRankArgs = 0; |
| 873 | int rc = SQLITE_OK; |
| 874 | |
| 875 | *pzRank = 0; |
| 876 | *pzRankArgs = 0; |
| 877 | |
| 878 | if( p==0 ){ |
| 879 | rc = SQLITE_ERROR; |
| 880 | }else{ |
| 881 | p = fts5ConfigSkipWhitespace(p); |
| 882 | pRank = p; |
| 883 | p = fts5ConfigSkipBareword(p); |
| 884 | |
| 885 | if( p ){ |
| 886 | zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank); |
| 887 | if( zRank ) memcpy(zRank, pRank, p-pRank); |
| 888 | }else{ |
| 889 | rc = SQLITE_ERROR; |
| 890 | } |
| 891 | |
| 892 | if( rc==SQLITE_OK ){ |
| 893 | p = fts5ConfigSkipWhitespace(p); |
| 894 | if( *p!='(' ) rc = SQLITE_ERROR; |
| 895 | p++; |
| 896 | } |
| 897 | if( rc==SQLITE_OK ){ |
| 898 | const char *pArgs; |
| 899 | p = fts5ConfigSkipWhitespace(p); |
| 900 | pArgs = p; |
| 901 | if( *p!=')' ){ |
| 902 | p = fts5ConfigSkipArgs(p); |
| 903 | if( p==0 ){ |
| 904 | rc = SQLITE_ERROR; |
| 905 | }else{ |
| 906 | zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs); |
| 907 | if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs); |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if( rc!=SQLITE_OK ){ |
| 914 | sqlite3_free(zRank); |
| 915 | assert( zRankArgs==0 ); |
| 916 | }else{ |
| 917 | *pzRank = zRank; |
| 918 | *pzRankArgs = zRankArgs; |
| 919 | } |
| 920 | return rc; |
| 921 | } |
| 922 | |
| 923 | int sqlite3Fts5ConfigSetValue( |
| 924 | Fts5Config *pConfig, |
| 925 | const char *zKey, |
| 926 | sqlite3_value *pVal, |
| 927 | int *pbBadkey |
| 928 | ){ |
| 929 | int rc = SQLITE_OK; |
| 930 | |
| 931 | if( 0==sqlite3_stricmp(zKey, "pgsz") ){ |
| 932 | int pgsz = 0; |
| 933 | if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 934 | pgsz = sqlite3_value_int(pVal); |
| 935 | } |
| 936 | if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE ){ |
| 937 | *pbBadkey = 1; |
| 938 | }else{ |
| 939 | pConfig->pgsz = pgsz; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | else if( 0==sqlite3_stricmp(zKey, "hashsize") ){ |
| 944 | int nHashSize = -1; |
| 945 | if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 946 | nHashSize = sqlite3_value_int(pVal); |
| 947 | } |
| 948 | if( nHashSize<=0 ){ |
| 949 | *pbBadkey = 1; |
| 950 | }else{ |
| 951 | pConfig->nHashSize = nHashSize; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | else if( 0==sqlite3_stricmp(zKey, "automerge") ){ |
| 956 | int nAutomerge = -1; |
| 957 | if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 958 | nAutomerge = sqlite3_value_int(pVal); |
| 959 | } |
| 960 | if( nAutomerge<0 || nAutomerge>64 ){ |
| 961 | *pbBadkey = 1; |
| 962 | }else{ |
| 963 | if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE; |
| 964 | pConfig->nAutomerge = nAutomerge; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | else if( 0==sqlite3_stricmp(zKey, "usermerge") ){ |
| 969 | int nUsermerge = -1; |
| 970 | if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 971 | nUsermerge = sqlite3_value_int(pVal); |
| 972 | } |
| 973 | if( nUsermerge<2 || nUsermerge>16 ){ |
| 974 | *pbBadkey = 1; |
| 975 | }else{ |
| 976 | pConfig->nUsermerge = nUsermerge; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){ |
| 981 | int nCrisisMerge = -1; |
| 982 | if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 983 | nCrisisMerge = sqlite3_value_int(pVal); |
| 984 | } |
| 985 | if( nCrisisMerge<0 ){ |
| 986 | *pbBadkey = 1; |
| 987 | }else{ |
| 988 | if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE; |
| 989 | if( nCrisisMerge>=FTS5_MAX_SEGMENT ) nCrisisMerge = FTS5_MAX_SEGMENT-1; |
| 990 | pConfig->nCrisisMerge = nCrisisMerge; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | else if( 0==sqlite3_stricmp(zKey, "deletemerge") ){ |
| 995 | int nVal = -1; |
| 996 | if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 997 | nVal = sqlite3_value_int(pVal); |
| 998 | }else{ |
| 999 | *pbBadkey = 1; |
| 1000 | } |
| 1001 | if( nVal<0 ) nVal = FTS5_DEFAULT_DELETE_AUTOMERGE; |
| 1002 | if( nVal>100 ) nVal = 0; |
| 1003 | pConfig->nDeleteMerge = nVal; |
| 1004 | } |
| 1005 | |
| 1006 | else if( 0==sqlite3_stricmp(zKey, "rank") ){ |
| 1007 | const char *zIn = (const char*)sqlite3_value_text(pVal); |
| 1008 | char *zRank; |
| 1009 | char *zRankArgs; |
| 1010 | rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs); |
| 1011 | if( rc==SQLITE_OK ){ |
| 1012 | sqlite3_free(pConfig->zRank); |
| 1013 | sqlite3_free(pConfig->zRankArgs); |
| 1014 | pConfig->zRank = zRank; |
| 1015 | pConfig->zRankArgs = zRankArgs; |
| 1016 | }else if( rc==SQLITE_ERROR ){ |
| 1017 | rc = SQLITE_OK; |
| 1018 | *pbBadkey = 1; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | else if( 0==sqlite3_stricmp(zKey, "secure-delete") ){ |
| 1023 | int bVal = -1; |
| 1024 | if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 1025 | bVal = sqlite3_value_int(pVal); |
| 1026 | } |
| 1027 | if( bVal<0 ){ |
| 1028 | *pbBadkey = 1; |
| 1029 | }else{ |
| 1030 | pConfig->bSecureDelete = (bVal ? 1 : 0); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | else if( 0==sqlite3_stricmp(zKey, "insttoken") ){ |
| 1035 | int bVal = -1; |
| 1036 | if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 1037 | bVal = sqlite3_value_int(pVal); |
| 1038 | } |
| 1039 | if( bVal<0 ){ |
| 1040 | *pbBadkey = 1; |
| 1041 | }else{ |
| 1042 | pConfig->bPrefixInsttoken = (bVal ? 1 : 0); |
| 1043 | } |
| 1044 | |
| 1045 | }else{ |
| 1046 | *pbBadkey = 1; |
| 1047 | } |
| 1048 | return rc; |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | ** Load the contents of the %_config table into memory. |
| 1053 | */ |
| 1054 | int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){ |
| 1055 | const char *zSelect = "SELECT k, v FROM %Q.'%q_config'"; |
| 1056 | char *zSql; |
| 1057 | sqlite3_stmt *p = 0; |
| 1058 | int rc = SQLITE_OK; |
| 1059 | int iVersion = 0; |
| 1060 | |
| 1061 | /* Set default values */ |
| 1062 | pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE; |
| 1063 | pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE; |
| 1064 | pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE; |
| 1065 | pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE; |
| 1066 | pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE; |
| 1067 | pConfig->nDeleteMerge = FTS5_DEFAULT_DELETE_AUTOMERGE; |
| 1068 | |
| 1069 | zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName); |
| 1070 | if( zSql ){ |
| 1071 | rc = sqlite3_prepare_v3( |
| 1072 | pConfig->db, zSql, -1, SQLITE_PREPARE_FROM_DDL, &p, 0 |
| 1073 | ); |
| 1074 | sqlite3_free(zSql); |
| 1075 | } |
| 1076 | |
| 1077 | assert( rc==SQLITE_OK || p==0 ); |
| 1078 | if( rc==SQLITE_OK ){ |
| 1079 | while( SQLITE_ROW==sqlite3_step(p) ){ |
| 1080 | const char *zK = (const char*)sqlite3_column_text(p, 0); |
| 1081 | sqlite3_value *pVal = sqlite3_column_value(p, 1); |
| 1082 | if( 0==sqlite3_stricmp(zK, "version") ){ |
| 1083 | iVersion = sqlite3_value_int(pVal); |
| 1084 | }else{ |
| 1085 | int bDummy = 0; |
| 1086 | sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy); |
| 1087 | } |
| 1088 | } |
| 1089 | rc = sqlite3_finalize(p); |
| 1090 | } |
| 1091 | |
| 1092 | if( rc==SQLITE_OK |
| 1093 | && iVersion!=FTS5_CURRENT_VERSION |
| 1094 | && iVersion!=FTS5_CURRENT_VERSION_SECUREDELETE |
| 1095 | ){ |
| 1096 | rc = SQLITE_ERROR; |
| 1097 | sqlite3Fts5ConfigErrmsg(pConfig, "invalid fts5 file format " |
| 1098 | "(found %d, expected %d or %d) - run 'rebuild'", |
| 1099 | iVersion, FTS5_CURRENT_VERSION, FTS5_CURRENT_VERSION_SECUREDELETE |
| 1100 | ); |
| 1101 | }else{ |
| 1102 | pConfig->iVersion = iVersion; |
| 1103 | } |
| 1104 | |
| 1105 | if( rc==SQLITE_OK ){ |
| 1106 | pConfig->iCookie = iCookie; |
| 1107 | } |
| 1108 | return rc; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | ** Set (*pConfig->pzErrmsg) to point to an sqlite3_malloc()ed buffer |
| 1113 | ** containing the error message created using printf() style formatting |
| 1114 | ** string zFmt and its trailing arguments. |
| 1115 | */ |
| 1116 | void sqlite3Fts5ConfigErrmsg(Fts5Config *pConfig, const char *zFmt, ...){ |
| 1117 | va_list ap; /* ... printf arguments */ |
| 1118 | char *zMsg = 0; |
| 1119 | |
| 1120 | va_start(ap, zFmt); |
| 1121 | zMsg = sqlite3_vmprintf(zFmt, ap); |
| 1122 | if( pConfig->pzErrmsg ){ |
| 1123 | sqlite3_free(*pConfig->pzErrmsg); |
| 1124 | *pConfig->pzErrmsg = zMsg; |
| 1125 | }else{ |
| 1126 | sqlite3_free(zMsg); |
| 1127 | } |
| 1128 | |
| 1129 | va_end(ap); |
| 1130 | } |