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 / fts3 / fts3_aux.c
| 1 | /* |
| 2 | ** 2011 Jan 27 |
| 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 | */ |
| 14 | #include "fts3Int.h" |
| 15 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <assert.h> |
| 19 | |
| 20 | typedef struct Fts3auxTable Fts3auxTable; |
| 21 | typedef struct Fts3auxCursor Fts3auxCursor; |
| 22 | |
| 23 | struct Fts3auxTable { |
| 24 | sqlite3_vtab base; /* Base class used by SQLite core */ |
| 25 | Fts3Table *pFts3Tab; |
| 26 | }; |
| 27 | |
| 28 | struct Fts3auxCursor { |
| 29 | sqlite3_vtab_cursor base; /* Base class used by SQLite core */ |
| 30 | Fts3MultiSegReader csr; /* Must be right after "base" */ |
| 31 | Fts3SegFilter filter; |
| 32 | char *zStop; |
| 33 | int nStop; /* Byte-length of string zStop */ |
| 34 | int iLangid; /* Language id to query */ |
| 35 | int isEof; /* True if cursor is at EOF */ |
| 36 | sqlite3_int64 iRowid; /* Current rowid */ |
| 37 | |
| 38 | int iCol; /* Current value of 'col' column */ |
| 39 | int nStat; /* Size of aStat[] array */ |
| 40 | struct Fts3auxColstats { |
| 41 | sqlite3_int64 nDoc; /* 'documents' values for current csr row */ |
| 42 | sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */ |
| 43 | } *aStat; |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | ** Schema of the terms table. |
| 48 | */ |
| 49 | #define FTS3_AUX_SCHEMA \ |
| 50 | "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN, " \ |
| 51 | "PRIMARY KEY(term, col, languageid)) WITHOUT ROWID" |
| 52 | |
| 53 | /* |
| 54 | ** This function does all the work for both the xConnect and xCreate methods. |
| 55 | ** These tables have no persistent representation of their own, so xConnect |
| 56 | ** and xCreate are identical operations. |
| 57 | */ |
| 58 | static int fts3auxConnectMethod( |
| 59 | sqlite3 *db, /* Database connection */ |
| 60 | void *pUnused, /* Unused */ |
| 61 | int argc, /* Number of elements in argv array */ |
| 62 | const char * const *argv, /* xCreate/xConnect argument array */ |
| 63 | sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ |
| 64 | char **pzErr /* OUT: sqlite3_malloc'd error message */ |
| 65 | ){ |
| 66 | char const *zDb; /* Name of database (e.g. "main") */ |
| 67 | char const *zFts3; /* Name of fts3 table */ |
| 68 | int nDb; /* Result of strlen(zDb) */ |
| 69 | int nFts3; /* Result of strlen(zFts3) */ |
| 70 | sqlite3_int64 nByte; /* Bytes of space to allocate here */ |
| 71 | int rc; /* value returned by declare_vtab() */ |
| 72 | Fts3auxTable *p; /* Virtual table object to return */ |
| 73 | |
| 74 | UNUSED_PARAMETER(pUnused); |
| 75 | |
| 76 | /* The user should invoke this in one of two forms: |
| 77 | ** |
| 78 | ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table); |
| 79 | ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table); |
| 80 | */ |
| 81 | if( argc!=4 && argc!=5 ) goto bad_args; |
| 82 | |
| 83 | zDb = argv[1]; |
| 84 | nDb = (int)strlen(zDb); |
| 85 | if( argc==5 ){ |
| 86 | if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){ |
| 87 | zDb = argv[3]; |
| 88 | nDb = (int)strlen(zDb); |
| 89 | zFts3 = argv[4]; |
| 90 | }else{ |
| 91 | goto bad_args; |
| 92 | } |
| 93 | }else{ |
| 94 | zFts3 = argv[3]; |
| 95 | } |
| 96 | nFts3 = (int)strlen(zFts3); |
| 97 | |
| 98 | rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA); |
| 99 | if( rc!=SQLITE_OK ) return rc; |
| 100 | |
| 101 | nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2; |
| 102 | p = (Fts3auxTable *)sqlite3_malloc64(nByte); |
| 103 | if( !p ) return SQLITE_NOMEM; |
| 104 | memset(p, 0, nByte); |
| 105 | |
| 106 | p->pFts3Tab = (Fts3Table *)&p[1]; |
| 107 | p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1]; |
| 108 | p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1]; |
| 109 | p->pFts3Tab->db = db; |
| 110 | p->pFts3Tab->nIndex = 1; |
| 111 | |
| 112 | memcpy((char *)p->pFts3Tab->zDb, zDb, nDb); |
| 113 | memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3); |
| 114 | sqlite3Fts3Dequote((char *)p->pFts3Tab->zName); |
| 115 | |
| 116 | *ppVtab = (sqlite3_vtab *)p; |
| 117 | return SQLITE_OK; |
| 118 | |
| 119 | bad_args: |
| 120 | sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor"); |
| 121 | return SQLITE_ERROR; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | ** This function does the work for both the xDisconnect and xDestroy methods. |
| 126 | ** These tables have no persistent representation of their own, so xDisconnect |
| 127 | ** and xDestroy are identical operations. |
| 128 | */ |
| 129 | static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){ |
| 130 | Fts3auxTable *p = (Fts3auxTable *)pVtab; |
| 131 | Fts3Table *pFts3 = p->pFts3Tab; |
| 132 | int i; |
| 133 | |
| 134 | /* Free any prepared statements held */ |
| 135 | for(i=0; i<SizeofArray(pFts3->aStmt); i++){ |
| 136 | sqlite3_finalize(pFts3->aStmt[i]); |
| 137 | } |
| 138 | sqlite3_free(pFts3->zSegmentsTbl); |
| 139 | sqlite3_free(p); |
| 140 | return SQLITE_OK; |
| 141 | } |
| 142 | |
| 143 | #define FTS4AUX_EQ_CONSTRAINT 1 |
| 144 | #define FTS4AUX_GE_CONSTRAINT 2 |
| 145 | #define FTS4AUX_LE_CONSTRAINT 4 |
| 146 | |
| 147 | /* |
| 148 | ** Return true if constraint iCons of pInfo uses the "binary" collation |
| 149 | ** sequence. Or false it it uses anything else. |
| 150 | */ |
| 151 | static int fts3auxIsBinary(sqlite3_index_info *pInfo, int iCons){ |
| 152 | return 0==sqlite3_stricmp("binary", sqlite3_vtab_collation(pInfo, iCons)); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | ** xBestIndex - Analyze a WHERE and ORDER BY clause. |
| 157 | */ |
| 158 | static int fts3auxBestIndexMethod( |
| 159 | sqlite3_vtab *pVTab, |
| 160 | sqlite3_index_info *pInfo |
| 161 | ){ |
| 162 | int i; |
| 163 | int iEq = -1; |
| 164 | int iGe = -1; |
| 165 | int iLe = -1; |
| 166 | int iLangid = -1; |
| 167 | int iNext = 1; /* Next free argvIndex value */ |
| 168 | |
| 169 | UNUSED_PARAMETER(pVTab); |
| 170 | |
| 171 | /* This vtab delivers always results in "ORDER BY term ASC" order. */ |
| 172 | if( pInfo->nOrderBy==1 |
| 173 | && pInfo->aOrderBy[0].iColumn==0 |
| 174 | && pInfo->aOrderBy[0].desc==0 |
| 175 | ){ |
| 176 | pInfo->orderByConsumed = 1; |
| 177 | } |
| 178 | |
| 179 | /* Search for equality and range constraints on the "term" column. |
| 180 | ** And equality constraints on the hidden "languageid" column. */ |
| 181 | for(i=0; i<pInfo->nConstraint; i++){ |
| 182 | if( pInfo->aConstraint[i].usable && fts3auxIsBinary(pInfo, i) ){ |
| 183 | int op = pInfo->aConstraint[i].op; |
| 184 | int iCol = pInfo->aConstraint[i].iColumn; |
| 185 | |
| 186 | if( iCol==0 ){ |
| 187 | if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i; |
| 188 | if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i; |
| 189 | if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i; |
| 190 | if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i; |
| 191 | if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i; |
| 192 | } |
| 193 | if( iCol==4 ){ |
| 194 | if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if( iEq>=0 ){ |
| 200 | pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT; |
| 201 | pInfo->aConstraintUsage[iEq].argvIndex = iNext++; |
| 202 | pInfo->estimatedCost = 5; |
| 203 | }else{ |
| 204 | pInfo->idxNum = 0; |
| 205 | pInfo->estimatedCost = 20000; |
| 206 | if( iGe>=0 ){ |
| 207 | pInfo->idxNum += FTS4AUX_GE_CONSTRAINT; |
| 208 | pInfo->aConstraintUsage[iGe].argvIndex = iNext++; |
| 209 | pInfo->estimatedCost /= 2; |
| 210 | } |
| 211 | if( iLe>=0 ){ |
| 212 | pInfo->idxNum += FTS4AUX_LE_CONSTRAINT; |
| 213 | pInfo->aConstraintUsage[iLe].argvIndex = iNext++; |
| 214 | pInfo->estimatedCost /= 2; |
| 215 | } |
| 216 | } |
| 217 | if( iLangid>=0 ){ |
| 218 | pInfo->aConstraintUsage[iLangid].argvIndex = iNext++; |
| 219 | pInfo->estimatedCost--; |
| 220 | } |
| 221 | |
| 222 | return SQLITE_OK; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | ** xOpen - Open a cursor. |
| 227 | */ |
| 228 | static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ |
| 229 | Fts3auxCursor *pCsr; /* Pointer to cursor object to return */ |
| 230 | |
| 231 | UNUSED_PARAMETER(pVTab); |
| 232 | |
| 233 | pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor)); |
| 234 | if( !pCsr ) return SQLITE_NOMEM; |
| 235 | memset(pCsr, 0, sizeof(Fts3auxCursor)); |
| 236 | |
| 237 | *ppCsr = (sqlite3_vtab_cursor *)pCsr; |
| 238 | return SQLITE_OK; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | ** xClose - Close a cursor. |
| 243 | */ |
| 244 | static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){ |
| 245 | Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; |
| 246 | Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; |
| 247 | |
| 248 | sqlite3Fts3SegmentsClose(pFts3); |
| 249 | sqlite3Fts3SegReaderFinish(&pCsr->csr); |
| 250 | sqlite3_free((void *)pCsr->filter.zTerm); |
| 251 | sqlite3_free(pCsr->zStop); |
| 252 | sqlite3_free(pCsr->aStat); |
| 253 | sqlite3_free(pCsr); |
| 254 | return SQLITE_OK; |
| 255 | } |
| 256 | |
| 257 | static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){ |
| 258 | if( nSize>pCsr->nStat ){ |
| 259 | struct Fts3auxColstats *aNew; |
| 260 | aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat, |
| 261 | sizeof(struct Fts3auxColstats) * nSize |
| 262 | ); |
| 263 | if( aNew==0 ) return SQLITE_NOMEM; |
| 264 | memset(&aNew[pCsr->nStat], 0, |
| 265 | sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat) |
| 266 | ); |
| 267 | pCsr->aStat = aNew; |
| 268 | pCsr->nStat = nSize; |
| 269 | } |
| 270 | return SQLITE_OK; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | ** xNext - Advance the cursor to the next row, if any. |
| 275 | */ |
| 276 | static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){ |
| 277 | Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; |
| 278 | Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; |
| 279 | int rc; |
| 280 | |
| 281 | /* Increment our pretend rowid value. */ |
| 282 | pCsr->iRowid++; |
| 283 | |
| 284 | for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){ |
| 285 | if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK; |
| 286 | } |
| 287 | |
| 288 | rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr); |
| 289 | if( rc==SQLITE_ROW ){ |
| 290 | int i = 0; |
| 291 | int nDoclist = pCsr->csr.nDoclist; |
| 292 | char *aDoclist = pCsr->csr.aDoclist; |
| 293 | int iCol; |
| 294 | |
| 295 | int eState = 0; |
| 296 | |
| 297 | if( pCsr->zStop ){ |
| 298 | int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm; |
| 299 | int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n); |
| 300 | if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){ |
| 301 | pCsr->isEof = 1; |
| 302 | return SQLITE_OK; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM; |
| 307 | memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat); |
| 308 | iCol = 0; |
| 309 | rc = SQLITE_OK; |
| 310 | |
| 311 | while( i<nDoclist ){ |
| 312 | sqlite3_int64 v = 0; |
| 313 | |
| 314 | i += sqlite3Fts3GetVarint(&aDoclist[i], &v); |
| 315 | switch( eState ){ |
| 316 | /* State 0. In this state the integer just read was a docid. */ |
| 317 | case 0: |
| 318 | pCsr->aStat[0].nDoc++; |
| 319 | eState = 1; |
| 320 | iCol = 0; |
| 321 | break; |
| 322 | |
| 323 | /* State 1. In this state we are expecting either a 1, indicating |
| 324 | ** that the following integer will be a column number, or the |
| 325 | ** start of a position list for column 0. |
| 326 | ** |
| 327 | ** The only difference between state 1 and state 2 is that if the |
| 328 | ** integer encountered in state 1 is not 0 or 1, then we need to |
| 329 | ** increment the column 0 "nDoc" count for this term. |
| 330 | */ |
| 331 | case 1: |
| 332 | assert( iCol==0 ); |
| 333 | if( v>1 ){ |
| 334 | pCsr->aStat[1].nDoc++; |
| 335 | } |
| 336 | eState = 2; |
| 337 | /* no break */ deliberate_fall_through |
| 338 | |
| 339 | case 2: |
| 340 | if( v==0 ){ /* 0x00. Next integer will be a docid. */ |
| 341 | eState = 0; |
| 342 | }else if( v==1 ){ /* 0x01. Next integer will be a column number. */ |
| 343 | eState = 3; |
| 344 | }else{ /* 2 or greater. A position. */ |
| 345 | pCsr->aStat[iCol+1].nOcc++; |
| 346 | pCsr->aStat[0].nOcc++; |
| 347 | } |
| 348 | break; |
| 349 | |
| 350 | /* State 3. The integer just read is a column number. */ |
| 351 | default: assert( eState==3 ); |
| 352 | iCol = (int)v; |
| 353 | if( iCol<1 || iCol>32767 ){ |
| 354 | rc = SQLITE_CORRUPT_VTAB; |
| 355 | break; |
| 356 | } |
| 357 | if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM; |
| 358 | pCsr->aStat[iCol+1].nDoc++; |
| 359 | eState = 2; |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | pCsr->iCol = 0; |
| 365 | }else{ |
| 366 | pCsr->isEof = 1; |
| 367 | } |
| 368 | return rc; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | ** xFilter - Initialize a cursor to point at the start of its data. |
| 373 | */ |
| 374 | static int fts3auxFilterMethod( |
| 375 | sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ |
| 376 | int idxNum, /* Strategy index */ |
| 377 | const char *idxStr, /* Unused */ |
| 378 | int nVal, /* Number of elements in apVal */ |
| 379 | sqlite3_value **apVal /* Arguments for the indexing scheme */ |
| 380 | ){ |
| 381 | Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; |
| 382 | Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; |
| 383 | int rc; |
| 384 | int isScan = 0; |
| 385 | int iLangVal = 0; /* Language id to query */ |
| 386 | |
| 387 | int iEq = -1; /* Index of term=? value in apVal */ |
| 388 | int iGe = -1; /* Index of term>=? value in apVal */ |
| 389 | int iLe = -1; /* Index of term<=? value in apVal */ |
| 390 | int iLangid = -1; /* Index of languageid=? value in apVal */ |
| 391 | int iNext = 0; |
| 392 | |
| 393 | UNUSED_PARAMETER(nVal); |
| 394 | UNUSED_PARAMETER(idxStr); |
| 395 | |
| 396 | assert( idxStr==0 ); |
| 397 | assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0 |
| 398 | || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT |
| 399 | || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) |
| 400 | ); |
| 401 | |
| 402 | if( idxNum==FTS4AUX_EQ_CONSTRAINT ){ |
| 403 | iEq = iNext++; |
| 404 | }else{ |
| 405 | isScan = 1; |
| 406 | if( idxNum & FTS4AUX_GE_CONSTRAINT ){ |
| 407 | iGe = iNext++; |
| 408 | } |
| 409 | if( idxNum & FTS4AUX_LE_CONSTRAINT ){ |
| 410 | iLe = iNext++; |
| 411 | } |
| 412 | } |
| 413 | if( iNext<nVal ){ |
| 414 | iLangid = iNext++; |
| 415 | } |
| 416 | |
| 417 | /* In case this cursor is being reused, close and zero it. */ |
| 418 | testcase(pCsr->filter.zTerm); |
| 419 | sqlite3Fts3SegReaderFinish(&pCsr->csr); |
| 420 | sqlite3_free((void *)pCsr->filter.zTerm); |
| 421 | sqlite3_free(pCsr->aStat); |
| 422 | sqlite3_free(pCsr->zStop); |
| 423 | memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr); |
| 424 | |
| 425 | pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY; |
| 426 | if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN; |
| 427 | |
| 428 | if( (iEq>=0 || iGe>=0) && sqlite3_value_type(apVal[0])==SQLITE_TEXT ){ |
| 429 | const unsigned char *zStr = sqlite3_value_text(apVal[0]); |
| 430 | assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) ); |
| 431 | if( zStr ){ |
| 432 | pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr); |
| 433 | if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM; |
| 434 | pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if( iLe>=0 && sqlite3_value_type(apVal[iLe])==SQLITE_TEXT ){ |
| 439 | pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe])); |
| 440 | if( pCsr->zStop==0 ) return SQLITE_NOMEM; |
| 441 | pCsr->nStop = (int)strlen(pCsr->zStop); |
| 442 | } |
| 443 | |
| 444 | if( iLangid>=0 ){ |
| 445 | iLangVal = sqlite3_value_int(apVal[iLangid]); |
| 446 | |
| 447 | /* If the user specified a negative value for the languageid, use zero |
| 448 | ** instead. This works, as the "languageid=?" constraint will also |
| 449 | ** be tested by the VDBE layer. The test will always be false (since |
| 450 | ** this module will not return a row with a negative languageid), and |
| 451 | ** so the overall query will return zero rows. */ |
| 452 | if( iLangVal<0 ) iLangVal = 0; |
| 453 | } |
| 454 | pCsr->iLangid = iLangVal; |
| 455 | |
| 456 | rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL, |
| 457 | pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr |
| 458 | ); |
| 459 | if( rc==SQLITE_OK ){ |
| 460 | rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter); |
| 461 | } |
| 462 | |
| 463 | if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor); |
| 464 | return rc; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | ** xEof - Return true if the cursor is at EOF, or false otherwise. |
| 469 | */ |
| 470 | static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){ |
| 471 | Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; |
| 472 | return pCsr->isEof; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | ** xColumn - Return a column value. |
| 477 | */ |
| 478 | static int fts3auxColumnMethod( |
| 479 | sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ |
| 480 | sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ |
| 481 | int iCol /* Index of column to read value from */ |
| 482 | ){ |
| 483 | Fts3auxCursor *p = (Fts3auxCursor *)pCursor; |
| 484 | |
| 485 | assert( p->isEof==0 ); |
| 486 | switch( iCol ){ |
| 487 | case 0: /* term */ |
| 488 | sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT); |
| 489 | break; |
| 490 | |
| 491 | case 1: /* col */ |
| 492 | if( p->iCol ){ |
| 493 | sqlite3_result_int(pCtx, p->iCol-1); |
| 494 | }else{ |
| 495 | sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC); |
| 496 | } |
| 497 | break; |
| 498 | |
| 499 | case 2: /* documents */ |
| 500 | sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc); |
| 501 | break; |
| 502 | |
| 503 | case 3: /* occurrences */ |
| 504 | sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc); |
| 505 | break; |
| 506 | |
| 507 | default: /* languageid */ |
| 508 | assert( iCol==4 ); |
| 509 | sqlite3_result_int(pCtx, p->iLangid); |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | return SQLITE_OK; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | ** xRowid - Return the current rowid for the cursor. |
| 518 | */ |
| 519 | static int fts3auxRowidMethod( |
| 520 | sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ |
| 521 | sqlite_int64 *pRowid /* OUT: Rowid value */ |
| 522 | ){ |
| 523 | Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; |
| 524 | *pRowid = pCsr->iRowid; |
| 525 | return SQLITE_OK; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | ** Register the fts3aux module with database connection db. Return SQLITE_OK |
| 530 | ** if successful or an error code if sqlite3_create_module() fails. |
| 531 | */ |
| 532 | int sqlite3Fts3InitAux(sqlite3 *db){ |
| 533 | static const sqlite3_module fts3aux_module = { |
| 534 | 0, /* iVersion */ |
| 535 | fts3auxConnectMethod, /* xCreate */ |
| 536 | fts3auxConnectMethod, /* xConnect */ |
| 537 | fts3auxBestIndexMethod, /* xBestIndex */ |
| 538 | fts3auxDisconnectMethod, /* xDisconnect */ |
| 539 | fts3auxDisconnectMethod, /* xDestroy */ |
| 540 | fts3auxOpenMethod, /* xOpen */ |
| 541 | fts3auxCloseMethod, /* xClose */ |
| 542 | fts3auxFilterMethod, /* xFilter */ |
| 543 | fts3auxNextMethod, /* xNext */ |
| 544 | fts3auxEofMethod, /* xEof */ |
| 545 | fts3auxColumnMethod, /* xColumn */ |
| 546 | fts3auxRowidMethod, /* xRowid */ |
| 547 | 0, /* xUpdate */ |
| 548 | 0, /* xBegin */ |
| 549 | 0, /* xSync */ |
| 550 | 0, /* xCommit */ |
| 551 | 0, /* xRollback */ |
| 552 | 0, /* xFindFunction */ |
| 553 | 0, /* xRename */ |
| 554 | 0, /* xSavepoint */ |
| 555 | 0, /* xRelease */ |
| 556 | 0, /* xRollbackTo */ |
| 557 | 0, /* xShadowName */ |
| 558 | 0 /* xIntegrity */ |
| 559 | }; |
| 560 | int rc; /* Return code */ |
| 561 | |
| 562 | rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0); |
| 563 | return rc; |
| 564 | } |
| 565 | |
| 566 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |