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 ** |
(...skipping 12 matching lines...) Expand all Loading... |
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 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) | 26 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
27 | 27 |
28 #include "sqlite3ext.h" | 28 #include "sqlite3ext.h" |
29 #ifndef SQLITE_CORE | 29 #ifndef SQLITE_CORE |
30 SQLITE_EXTENSION_INIT1 | 30 SQLITE_EXTENSION_INIT1 |
31 #endif | 31 #endif |
32 | 32 |
33 #include "fts3_hash.h" | 33 #include "fts3Int.h" |
34 #include "fts3_tokenizer.h" | |
35 #include <assert.h> | 34 #include <assert.h> |
36 #include <stddef.h> | 35 #include <stddef.h> |
| 36 #include <string.h> |
37 | 37 |
38 /* | 38 /* |
39 ** Implementation of the SQL scalar function for accessing the underlying | 39 ** Implementation of the SQL scalar function for accessing the underlying |
40 ** hash table. This function may be called as follows: | 40 ** hash table. This function may be called as follows: |
41 ** | 41 ** |
42 ** SELECT <function-name>(<key-name>); | 42 ** SELECT <function-name>(<key-name>); |
43 ** SELECT <function-name>(<key-name>, <pointer>); | 43 ** SELECT <function-name>(<key-name>, <pointer>); |
44 ** | 44 ** |
45 ** where <function-name> is the name passed as the second argument | 45 ** where <function-name> is the name passed as the second argument |
46 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer'). | 46 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer'). |
47 ** | 47 ** |
48 ** If the <pointer> argument is specified, it must be a blob value | 48 ** If the <pointer> argument is specified, it must be a blob value |
49 ** containing a pointer to be stored as the hash data corresponding | 49 ** containing a pointer to be stored as the hash data corresponding |
50 ** to the string <key-name>. If <pointer> is not specified, then | 50 ** to the string <key-name>. If <pointer> is not specified, then |
51 ** the string <key-name> must already exist in the has table. Otherwise, | 51 ** the string <key-name> must already exist in the has table. Otherwise, |
52 ** an error is returned. | 52 ** an error is returned. |
53 ** | 53 ** |
54 ** Whether or not the <pointer> argument is specified, the value returned | 54 ** Whether or not the <pointer> argument is specified, the value returned |
55 ** is a blob containing the pointer stored as the hash data corresponding | 55 ** is a blob containing the pointer stored as the hash data corresponding |
56 ** to string <key-name> (after the hash-table is updated, if applicable). | 56 ** to string <key-name> (after the hash-table is updated, if applicable). |
57 */ | 57 */ |
58 static void scalarFunc( | 58 static void scalarFunc( |
59 sqlite3_context *context, | 59 sqlite3_context *context, |
60 int argc, | 60 int argc, |
61 sqlite3_value **argv | 61 sqlite3_value **argv |
62 ){ | 62 ){ |
63 fts3Hash *pHash; | 63 Fts3Hash *pHash; |
64 void *pPtr = 0; | 64 void *pPtr = 0; |
65 const unsigned char *zName; | 65 const unsigned char *zName; |
66 int nName; | 66 int nName; |
67 | 67 |
68 assert( argc==1 || argc==2 ); | 68 assert( argc==1 || argc==2 ); |
69 | 69 |
70 pHash = (fts3Hash *)sqlite3_user_data(context); | 70 pHash = (Fts3Hash *)sqlite3_user_data(context); |
71 | 71 |
72 zName = sqlite3_value_text(argv[0]); | 72 zName = sqlite3_value_text(argv[0]); |
73 nName = sqlite3_value_bytes(argv[0])+1; | 73 nName = sqlite3_value_bytes(argv[0])+1; |
74 | 74 |
75 if( argc==2 ){ | 75 if( argc==2 ){ |
76 void *pOld; | 76 void *pOld; |
77 int n = sqlite3_value_bytes(argv[1]); | 77 int n = sqlite3_value_bytes(argv[1]); |
78 if( n!=sizeof(pPtr) ){ | 78 if( n!=sizeof(pPtr) ){ |
79 sqlite3_result_error(context, "argument type mismatch", -1); | 79 sqlite3_result_error(context, "argument type mismatch", -1); |
80 return; | 80 return; |
(...skipping 10 matching lines...) Expand all Loading... |
91 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); | 91 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); |
92 sqlite3_result_error(context, zErr, -1); | 92 sqlite3_result_error(context, zErr, -1); |
93 sqlite3_free(zErr); | 93 sqlite3_free(zErr); |
94 return; | 94 return; |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); | 98 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); |
99 } | 99 } |
100 | 100 |
| 101 static int fts3IsIdChar(char c){ |
| 102 static const char isFtsIdChar[] = { |
| 103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ |
| 104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ |
| 105 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ |
| 106 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ |
| 107 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ |
| 108 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ |
| 109 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ |
| 110 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ |
| 111 }; |
| 112 return (c&0x80 || isFtsIdChar[(int)(c)]); |
| 113 } |
| 114 |
| 115 const char *sqlite3Fts3NextToken(const char *zStr, int *pn){ |
| 116 const char *z1; |
| 117 const char *z2 = 0; |
| 118 |
| 119 /* Find the start of the next token. */ |
| 120 z1 = zStr; |
| 121 while( z2==0 ){ |
| 122 char c = *z1; |
| 123 switch( c ){ |
| 124 case '\0': return 0; /* No more tokens here */ |
| 125 case '\'': |
| 126 case '"': |
| 127 case '`': { |
| 128 z2 = z1; |
| 129 while( *++z2 && (*z2!=c || *++z2==c) ); |
| 130 break; |
| 131 } |
| 132 case '[': |
| 133 z2 = &z1[1]; |
| 134 while( *z2 && z2[0]!=']' ) z2++; |
| 135 if( *z2 ) z2++; |
| 136 break; |
| 137 |
| 138 default: |
| 139 if( fts3IsIdChar(*z1) ){ |
| 140 z2 = &z1[1]; |
| 141 while( fts3IsIdChar(*z2) ) z2++; |
| 142 }else{ |
| 143 z1++; |
| 144 } |
| 145 } |
| 146 } |
| 147 |
| 148 *pn = (int)(z2-z1); |
| 149 return z1; |
| 150 } |
| 151 |
| 152 int sqlite3Fts3InitTokenizer( |
| 153 Fts3Hash *pHash, /* Tokenizer hash table */ |
| 154 const char *zArg, /* Possible tokenizer specification */ |
| 155 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */ |
| 156 const char **pzTokenizer, /* OUT: Set to zArg if is tokenizer */ |
| 157 char **pzErr /* OUT: Set to malloced error message */ |
| 158 ){ |
| 159 int rc; |
| 160 char *z = (char *)zArg; |
| 161 int n; |
| 162 char *zCopy; |
| 163 char *zEnd; /* Pointer to nul-term of zCopy */ |
| 164 sqlite3_tokenizer_module *m; |
| 165 |
| 166 if( !z ){ |
| 167 zCopy = sqlite3_mprintf("simple"); |
| 168 }else{ |
| 169 if( sqlite3_strnicmp(z, "tokenize", 8) || fts3IsIdChar(z[8])){ |
| 170 return SQLITE_OK; |
| 171 } |
| 172 zCopy = sqlite3_mprintf("%s", &z[8]); |
| 173 *pzTokenizer = zArg; |
| 174 } |
| 175 if( !zCopy ){ |
| 176 return SQLITE_NOMEM; |
| 177 } |
| 178 |
| 179 zEnd = &zCopy[strlen(zCopy)]; |
| 180 |
| 181 z = (char *)sqlite3Fts3NextToken(zCopy, &n); |
| 182 z[n] = '\0'; |
| 183 sqlite3Fts3Dequote(z); |
| 184 |
| 185 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, z, (int)strlen(z)+1
); |
| 186 if( !m ){ |
| 187 *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z); |
| 188 rc = SQLITE_ERROR; |
| 189 }else{ |
| 190 char const **aArg = 0; |
| 191 int iArg = 0; |
| 192 z = &z[n+1]; |
| 193 while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){ |
| 194 int nNew = sizeof(char *)*(iArg+1); |
| 195 char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew); |
| 196 if( !aNew ){ |
| 197 sqlite3_free(zCopy); |
| 198 sqlite3_free((void *)aArg); |
| 199 return SQLITE_NOMEM; |
| 200 } |
| 201 aArg = aNew; |
| 202 aArg[iArg++] = z; |
| 203 z[n] = '\0'; |
| 204 sqlite3Fts3Dequote(z); |
| 205 z = &z[n+1]; |
| 206 } |
| 207 rc = m->xCreate(iArg, aArg, ppTok); |
| 208 assert( rc!=SQLITE_OK || *ppTok ); |
| 209 if( rc!=SQLITE_OK ){ |
| 210 *pzErr = sqlite3_mprintf("unknown tokenizer"); |
| 211 }else{ |
| 212 (*ppTok)->pModule = m; |
| 213 } |
| 214 sqlite3_free((void *)aArg); |
| 215 } |
| 216 |
| 217 sqlite3_free(zCopy); |
| 218 return rc; |
| 219 } |
| 220 |
| 221 |
101 #ifdef SQLITE_TEST | 222 #ifdef SQLITE_TEST |
102 | 223 |
103 #include <tcl.h> | 224 #include <tcl.h> |
104 #include <string.h> | 225 #include <string.h> |
105 | 226 |
106 /* | 227 /* |
107 ** Implementation of a special SQL scalar function for testing tokenizers | 228 ** Implementation of a special SQL scalar function for testing tokenizers |
108 ** designed to be used in concert with the Tcl testing framework. This | 229 ** designed to be used in concert with the Tcl testing framework. This |
109 ** function must be called with two arguments: | 230 ** function must be called with two arguments: |
110 ** | 231 ** |
(...skipping 16 matching lines...) Expand all Loading... |
127 ** will return the string: | 248 ** will return the string: |
128 ** | 249 ** |
129 ** "{0 i I 1 dont don't 2 see see 3 how how}" | 250 ** "{0 i I 1 dont don't 2 see see 3 how how}" |
130 ** | 251 ** |
131 */ | 252 */ |
132 static void testFunc( | 253 static void testFunc( |
133 sqlite3_context *context, | 254 sqlite3_context *context, |
134 int argc, | 255 int argc, |
135 sqlite3_value **argv | 256 sqlite3_value **argv |
136 ){ | 257 ){ |
137 fts3Hash *pHash; | 258 Fts3Hash *pHash; |
138 sqlite3_tokenizer_module *p; | 259 sqlite3_tokenizer_module *p; |
139 sqlite3_tokenizer *pTokenizer = 0; | 260 sqlite3_tokenizer *pTokenizer = 0; |
140 sqlite3_tokenizer_cursor *pCsr = 0; | 261 sqlite3_tokenizer_cursor *pCsr = 0; |
141 | 262 |
142 const char *zErr = 0; | 263 const char *zErr = 0; |
143 | 264 |
144 const char *zName; | 265 const char *zName; |
145 int nName; | 266 int nName; |
146 const char *zInput; | 267 const char *zInput; |
147 int nInput; | 268 int nInput; |
(...skipping 12 matching lines...) Expand all Loading... |
160 | 281 |
161 nName = sqlite3_value_bytes(argv[0]); | 282 nName = sqlite3_value_bytes(argv[0]); |
162 zName = (const char *)sqlite3_value_text(argv[0]); | 283 zName = (const char *)sqlite3_value_text(argv[0]); |
163 nInput = sqlite3_value_bytes(argv[argc-1]); | 284 nInput = sqlite3_value_bytes(argv[argc-1]); |
164 zInput = (const char *)sqlite3_value_text(argv[argc-1]); | 285 zInput = (const char *)sqlite3_value_text(argv[argc-1]); |
165 | 286 |
166 if( argc==3 ){ | 287 if( argc==3 ){ |
167 zArg = (const char *)sqlite3_value_text(argv[1]); | 288 zArg = (const char *)sqlite3_value_text(argv[1]); |
168 } | 289 } |
169 | 290 |
170 pHash = (fts3Hash *)sqlite3_user_data(context); | 291 pHash = (Fts3Hash *)sqlite3_user_data(context); |
171 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); | 292 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); |
172 | 293 |
173 if( !p ){ | 294 if( !p ){ |
174 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); | 295 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); |
175 sqlite3_result_error(context, zErr, -1); | 296 sqlite3_result_error(context, zErr, -1); |
176 sqlite3_free(zErr); | 297 sqlite3_free(zErr); |
177 return; | 298 return; |
178 } | 299 } |
179 | 300 |
180 pRet = Tcl_NewObj(); | 301 pRet = Tcl_NewObj(); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 372 |
252 *pp = 0; | 373 *pp = 0; |
253 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); | 374 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
254 if( rc!=SQLITE_OK ){ | 375 if( rc!=SQLITE_OK ){ |
255 return rc; | 376 return rc; |
256 } | 377 } |
257 | 378 |
258 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); | 379 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); |
259 if( SQLITE_ROW==sqlite3_step(pStmt) ){ | 380 if( SQLITE_ROW==sqlite3_step(pStmt) ){ |
260 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ | 381 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ |
261 memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); | 382 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); |
262 } | 383 } |
263 } | 384 } |
264 | 385 |
265 return sqlite3_finalize(pStmt); | 386 return sqlite3_finalize(pStmt); |
266 } | 387 } |
267 | 388 |
268 void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); | 389 void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); |
269 | 390 |
270 /* | 391 /* |
271 ** Implementation of the scalar function fts3_tokenizer_internal_test(). | 392 ** Implementation of the scalar function fts3_tokenizer_internal_test(). |
(...skipping 16 matching lines...) Expand all Loading... |
288 static void intTestFunc( | 409 static void intTestFunc( |
289 sqlite3_context *context, | 410 sqlite3_context *context, |
290 int argc, | 411 int argc, |
291 sqlite3_value **argv | 412 sqlite3_value **argv |
292 ){ | 413 ){ |
293 int rc; | 414 int rc; |
294 const sqlite3_tokenizer_module *p1; | 415 const sqlite3_tokenizer_module *p1; |
295 const sqlite3_tokenizer_module *p2; | 416 const sqlite3_tokenizer_module *p2; |
296 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context); | 417 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context); |
297 | 418 |
| 419 UNUSED_PARAMETER(argc); |
| 420 UNUSED_PARAMETER(argv); |
| 421 |
298 /* Test the query function */ | 422 /* Test the query function */ |
299 sqlite3Fts3SimpleTokenizerModule(&p1); | 423 sqlite3Fts3SimpleTokenizerModule(&p1); |
300 rc = queryTokenizer(db, "simple", &p2); | 424 rc = queryTokenizer(db, "simple", &p2); |
301 assert( rc==SQLITE_OK ); | 425 assert( rc==SQLITE_OK ); |
302 assert( p1==p2 ); | 426 assert( p1==p2 ); |
303 rc = queryTokenizer(db, "nosuchtokenizer", &p2); | 427 rc = queryTokenizer(db, "nosuchtokenizer", &p2); |
304 assert( rc==SQLITE_ERROR ); | 428 assert( rc==SQLITE_ERROR ); |
305 assert( p2==0 ); | 429 assert( p2==0 ); |
306 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); | 430 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); |
307 | 431 |
(...skipping 21 matching lines...) Expand all Loading... |
329 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is | 453 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is |
330 ** defined at compilation time, a temporary virtual table (see header | 454 ** defined at compilation time, a temporary virtual table (see header |
331 ** comment above struct HashTableVtab) to the database schema. Both | 455 ** comment above struct HashTableVtab) to the database schema. Both |
332 ** provide read/write access to the contents of *pHash. | 456 ** provide read/write access to the contents of *pHash. |
333 ** | 457 ** |
334 ** The third argument to this function, zName, is used as the name | 458 ** The third argument to this function, zName, is used as the name |
335 ** of both the scalar and, if created, the virtual table. | 459 ** of both the scalar and, if created, the virtual table. |
336 */ | 460 */ |
337 int sqlite3Fts3InitHashTable( | 461 int sqlite3Fts3InitHashTable( |
338 sqlite3 *db, | 462 sqlite3 *db, |
339 fts3Hash *pHash, | 463 Fts3Hash *pHash, |
340 const char *zName | 464 const char *zName |
341 ){ | 465 ){ |
342 int rc = SQLITE_OK; | 466 int rc = SQLITE_OK; |
343 void *p = (void *)pHash; | 467 void *p = (void *)pHash; |
344 const int any = SQLITE_ANY; | 468 const int any = SQLITE_ANY; |
| 469 |
| 470 #ifdef SQLITE_TEST |
345 char *zTest = 0; | 471 char *zTest = 0; |
346 char *zTest2 = 0; | 472 char *zTest2 = 0; |
347 | |
348 #ifdef SQLITE_TEST | |
349 void *pdb = (void *)db; | 473 void *pdb = (void *)db; |
350 zTest = sqlite3_mprintf("%s_test", zName); | 474 zTest = sqlite3_mprintf("%s_test", zName); |
351 zTest2 = sqlite3_mprintf("%s_internal_test", zName); | 475 zTest2 = sqlite3_mprintf("%s_internal_test", zName); |
352 if( !zTest || !zTest2 ){ | 476 if( !zTest || !zTest2 ){ |
353 rc = SQLITE_NOMEM; | 477 rc = SQLITE_NOMEM; |
354 } | 478 } |
355 #endif | 479 #endif |
356 | 480 |
357 if( rc!=SQLITE_OK | 481 if( SQLITE_OK!=rc |
358 || (rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0)) | 482 || SQLITE_OK!=(rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc,
0, 0)) |
359 || (rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0)) | 483 || SQLITE_OK!=(rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc,
0, 0)) |
360 #ifdef SQLITE_TEST | 484 #ifdef SQLITE_TEST |
361 || (rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0)) | 485 || SQLITE_OK!=(rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0
, 0)) |
362 || (rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0)) | 486 || SQLITE_OK!=(rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0
, 0)) |
363 || (rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0)) | 487 || SQLITE_OK!=(rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestF
unc, 0, 0)) |
364 #endif | 488 #endif |
365 ); | 489 ); |
366 | 490 |
| 491 #ifdef SQLITE_TEST |
367 sqlite3_free(zTest); | 492 sqlite3_free(zTest); |
368 sqlite3_free(zTest2); | 493 sqlite3_free(zTest2); |
| 494 #endif |
| 495 |
369 return rc; | 496 return rc; |
370 } | 497 } |
371 | 498 |
372 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ | 499 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |
OLD | NEW |