OLD | NEW |
1 /* | 1 /* |
2 ** 2007 June 22 | 2 ** 2007 June 22 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ****************************************************************************** | 11 ****************************************************************************** |
12 ** | 12 ** |
13 ** This is part of an SQLite module implementing full-text search. | 13 ** This is part of an SQLite module implementing full-text search. |
14 ** This particular file implements the generic tokenizer interface. | 14 ** This particular file implements the generic tokenizer interface. |
15 */ | 15 */ |
16 | 16 |
17 /* | 17 /* |
18 ** The code in this file is only compiled if: | 18 ** The code in this file is only compiled if: |
19 ** | 19 ** |
20 ** * The FTS3 module is being built as an extension | 20 ** * The FTS3 module is being built as an extension |
21 ** (in which case SQLITE_CORE is not defined), or | 21 ** (in which case SQLITE_CORE is not defined), or |
22 ** | 22 ** |
23 ** * The FTS3 module is being built into the core of | 23 ** * The FTS3 module is being built into the core of |
24 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). | 24 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
25 */ | 25 */ |
| 26 #include "fts3Int.h" |
26 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) | 27 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
27 | 28 |
28 #include "sqlite3ext.h" | |
29 #ifndef SQLITE_CORE | |
30 SQLITE_EXTENSION_INIT1 | |
31 #endif | |
32 | |
33 #include "fts3Int.h" | |
34 #include <assert.h> | 29 #include <assert.h> |
35 #include <string.h> | 30 #include <string.h> |
36 | 31 |
37 /* | 32 /* |
38 ** Implementation of the SQL scalar function for accessing the underlying | 33 ** Implementation of the SQL scalar function for accessing the underlying |
39 ** hash table. This function may be called as follows: | 34 ** hash table. This function may be called as follows: |
40 ** | 35 ** |
41 ** SELECT <function-name>(<key-name>); | 36 ** SELECT <function-name>(<key-name>); |
42 ** SELECT <function-name>(<key-name>, <pointer>); | 37 ** SELECT <function-name>(<key-name>, <pointer>); |
43 ** | 38 ** |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 144 } |
150 | 145 |
151 int sqlite3Fts3InitTokenizer( | 146 int sqlite3Fts3InitTokenizer( |
152 Fts3Hash *pHash, /* Tokenizer hash table */ | 147 Fts3Hash *pHash, /* Tokenizer hash table */ |
153 const char *zArg, /* Tokenizer name */ | 148 const char *zArg, /* Tokenizer name */ |
154 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */ | 149 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */ |
155 char **pzErr /* OUT: Set to malloced error message */ | 150 char **pzErr /* OUT: Set to malloced error message */ |
156 ){ | 151 ){ |
157 int rc; | 152 int rc; |
158 char *z = (char *)zArg; | 153 char *z = (char *)zArg; |
159 int n; | 154 int n = 0; |
160 char *zCopy; | 155 char *zCopy; |
161 char *zEnd; /* Pointer to nul-term of zCopy */ | 156 char *zEnd; /* Pointer to nul-term of zCopy */ |
162 sqlite3_tokenizer_module *m; | 157 sqlite3_tokenizer_module *m; |
163 | 158 |
164 zCopy = sqlite3_mprintf("%s", zArg); | 159 zCopy = sqlite3_mprintf("%s", zArg); |
165 if( !zCopy ) return SQLITE_NOMEM; | 160 if( !zCopy ) return SQLITE_NOMEM; |
166 zEnd = &zCopy[strlen(zCopy)]; | 161 zEnd = &zCopy[strlen(zCopy)]; |
167 | 162 |
168 z = (char *)sqlite3Fts3NextToken(zCopy, &n); | 163 z = (char *)sqlite3Fts3NextToken(zCopy, &n); |
169 z[n] = '\0'; | 164 z[n] = '\0'; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 | 202 |
208 | 203 |
209 #ifdef SQLITE_TEST | 204 #ifdef SQLITE_TEST |
210 | 205 |
211 #include <tcl.h> | 206 #include <tcl.h> |
212 #include <string.h> | 207 #include <string.h> |
213 | 208 |
214 /* | 209 /* |
215 ** Implementation of a special SQL scalar function for testing tokenizers | 210 ** Implementation of a special SQL scalar function for testing tokenizers |
216 ** designed to be used in concert with the Tcl testing framework. This | 211 ** designed to be used in concert with the Tcl testing framework. This |
217 ** function must be called with two arguments: | 212 ** function must be called with two or more arguments: |
218 ** | 213 ** |
219 ** SELECT <function-name>(<key-name>, <input-string>); | 214 ** SELECT <function-name>(<key-name>, ..., <input-string>); |
220 ** SELECT <function-name>(<key-name>, <pointer>); | |
221 ** | 215 ** |
222 ** where <function-name> is the name passed as the second argument | 216 ** where <function-name> is the name passed as the second argument |
223 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer') | 217 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer') |
224 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test'). | 218 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test'). |
225 ** | 219 ** |
226 ** The return value is a string that may be interpreted as a Tcl | 220 ** The return value is a string that may be interpreted as a Tcl |
227 ** list. For each token in the <input-string>, three elements are | 221 ** list. For each token in the <input-string>, three elements are |
228 ** added to the returned list. The first is the token position, the | 222 ** added to the returned list. The first is the token position, the |
229 ** second is the token text (folded, stemmed, etc.) and the third is the | 223 ** second is the token text (folded, stemmed, etc.) and the third is the |
230 ** substring of <input-string> associated with the token. For example, | 224 ** substring of <input-string> associated with the token. For example, |
(...skipping 16 matching lines...) Expand all Loading... |
247 sqlite3_tokenizer *pTokenizer = 0; | 241 sqlite3_tokenizer *pTokenizer = 0; |
248 sqlite3_tokenizer_cursor *pCsr = 0; | 242 sqlite3_tokenizer_cursor *pCsr = 0; |
249 | 243 |
250 const char *zErr = 0; | 244 const char *zErr = 0; |
251 | 245 |
252 const char *zName; | 246 const char *zName; |
253 int nName; | 247 int nName; |
254 const char *zInput; | 248 const char *zInput; |
255 int nInput; | 249 int nInput; |
256 | 250 |
257 const char *zArg = 0; | 251 const char *azArg[64]; |
258 | 252 |
259 const char *zToken; | 253 const char *zToken; |
260 int nToken; | 254 int nToken = 0; |
261 int iStart; | 255 int iStart = 0; |
262 int iEnd; | 256 int iEnd = 0; |
263 int iPos; | 257 int iPos = 0; |
| 258 int i; |
264 | 259 |
265 Tcl_Obj *pRet; | 260 Tcl_Obj *pRet; |
266 | 261 |
267 assert( argc==2 || argc==3 ); | 262 if( argc<2 ){ |
| 263 sqlite3_result_error(context, "insufficient arguments", -1); |
| 264 return; |
| 265 } |
268 | 266 |
269 nName = sqlite3_value_bytes(argv[0]); | 267 nName = sqlite3_value_bytes(argv[0]); |
270 zName = (const char *)sqlite3_value_text(argv[0]); | 268 zName = (const char *)sqlite3_value_text(argv[0]); |
271 nInput = sqlite3_value_bytes(argv[argc-1]); | 269 nInput = sqlite3_value_bytes(argv[argc-1]); |
272 zInput = (const char *)sqlite3_value_text(argv[argc-1]); | 270 zInput = (const char *)sqlite3_value_text(argv[argc-1]); |
273 | 271 |
274 if( argc==3 ){ | |
275 zArg = (const char *)sqlite3_value_text(argv[1]); | |
276 } | |
277 | |
278 pHash = (Fts3Hash *)sqlite3_user_data(context); | 272 pHash = (Fts3Hash *)sqlite3_user_data(context); |
279 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); | 273 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); |
280 | 274 |
281 if( !p ){ | 275 if( !p ){ |
282 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); | 276 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); |
283 sqlite3_result_error(context, zErr, -1); | 277 sqlite3_result_error(context, zErr, -1); |
284 sqlite3_free(zErr); | 278 sqlite3_free(zErr); |
285 return; | 279 return; |
286 } | 280 } |
287 | 281 |
288 pRet = Tcl_NewObj(); | 282 pRet = Tcl_NewObj(); |
289 Tcl_IncrRefCount(pRet); | 283 Tcl_IncrRefCount(pRet); |
290 | 284 |
291 if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){ | 285 for(i=1; i<argc-1; i++){ |
| 286 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]); |
| 287 } |
| 288 |
| 289 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){ |
292 zErr = "error in xCreate()"; | 290 zErr = "error in xCreate()"; |
293 goto finish; | 291 goto finish; |
294 } | 292 } |
295 pTokenizer->pModule = p; | 293 pTokenizer->pModule = p; |
296 if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){ | 294 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){ |
297 zErr = "error in xOpen()"; | 295 zErr = "error in xOpen()"; |
298 goto finish; | 296 goto finish; |
299 } | 297 } |
300 pCsr->pTokenizer = pTokenizer; | |
301 | 298 |
302 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){ | 299 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){ |
303 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos)); | 300 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos)); |
304 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); | 301 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); |
305 zToken = &zInput[iStart]; | 302 zToken = &zInput[iStart]; |
306 nToken = iEnd-iStart; | 303 nToken = iEnd-iStart; |
307 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); | 304 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); |
308 } | 305 } |
309 | 306 |
310 if( SQLITE_OK!=p->xClose(pCsr) ){ | 307 if( SQLITE_OK!=p->xClose(pCsr) ){ |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 assert( p2==p1 ); | 421 assert( p2==p1 ); |
425 | 422 |
426 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); | 423 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); |
427 } | 424 } |
428 | 425 |
429 #endif | 426 #endif |
430 | 427 |
431 /* | 428 /* |
432 ** Set up SQL objects in database db used to access the contents of | 429 ** Set up SQL objects in database db used to access the contents of |
433 ** the hash table pointed to by argument pHash. The hash table must | 430 ** the hash table pointed to by argument pHash. The hash table must |
434 ** been initialised to use string keys, and to take a private copy | 431 ** been initialized to use string keys, and to take a private copy |
435 ** of the key when a value is inserted. i.e. by a call similar to: | 432 ** of the key when a value is inserted. i.e. by a call similar to: |
436 ** | 433 ** |
437 ** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); | 434 ** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); |
438 ** | 435 ** |
439 ** This function adds a scalar function (see header comment above | 436 ** This function adds a scalar function (see header comment above |
440 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is | 437 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is |
441 ** defined at compilation time, a temporary virtual table (see header | 438 ** defined at compilation time, a temporary virtual table (see header |
442 ** comment above struct HashTableVtab) to the database schema. Both | 439 ** comment above struct HashTableVtab) to the database schema. Both |
443 ** provide read/write access to the contents of *pHash. | 440 ** provide read/write access to the contents of *pHash. |
444 ** | 441 ** |
(...skipping 21 matching lines...) Expand all Loading... |
466 #endif | 463 #endif |
467 | 464 |
468 if( SQLITE_OK==rc ){ | 465 if( SQLITE_OK==rc ){ |
469 rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0); | 466 rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0); |
470 } | 467 } |
471 if( SQLITE_OK==rc ){ | 468 if( SQLITE_OK==rc ){ |
472 rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0); | 469 rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0); |
473 } | 470 } |
474 #ifdef SQLITE_TEST | 471 #ifdef SQLITE_TEST |
475 if( SQLITE_OK==rc ){ | 472 if( SQLITE_OK==rc ){ |
476 rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0); | 473 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0); |
477 } | |
478 if( SQLITE_OK==rc ){ | |
479 rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0); | |
480 } | 474 } |
481 if( SQLITE_OK==rc ){ | 475 if( SQLITE_OK==rc ){ |
482 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0); | 476 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0); |
483 } | 477 } |
484 #endif | 478 #endif |
485 | 479 |
486 #ifdef SQLITE_TEST | 480 #ifdef SQLITE_TEST |
487 sqlite3_free(zTest); | 481 sqlite3_free(zTest); |
488 sqlite3_free(zTest2); | 482 sqlite3_free(zTest2); |
489 #endif | 483 #endif |
490 | 484 |
491 return rc; | 485 return rc; |
492 } | 486 } |
493 | 487 |
494 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ | 488 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |
OLD | NEW |