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