fts3_tokenizer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. ** 2007 June 22
  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 part of an SQLite module implementing full-text search.
  14. ** This particular file implements the generic tokenizer interface.
  15. */
  16. /*
  17. ** The code in this file is only compiled if:
  18. **
  19. ** * The FTS3 module is being built as an extension
  20. ** (in which case SQLITE_CORE is not defined), or
  21. **
  22. ** * The FTS3 module is being built into the core of
  23. ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
  24. */
  25. #include "fts3Int.h"
  26. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  27. #include <assert.h>
  28. #include <string.h>
  29. /*
  30. ** Return true if the two-argument version of fts3_tokenizer()
  31. ** has been activated via a prior call to sqlite3_db_config(db,
  32. ** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
  33. */
  34. static int fts3TokenizerEnabled(sqlite3_context *context){
  35. sqlite3 *db = sqlite3_context_db_handle(context);
  36. int isEnabled = 0;
  37. sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
  38. return isEnabled;
  39. }
  40. /*
  41. ** Implementation of the SQL scalar function for accessing the underlying
  42. ** hash table. This function may be called as follows:
  43. **
  44. ** SELECT <function-name>(<key-name>);
  45. ** SELECT <function-name>(<key-name>, <pointer>);
  46. **
  47. ** where <function-name> is the name passed as the second argument
  48. ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
  49. **
  50. ** If the <pointer> argument is specified, it must be a blob value
  51. ** containing a pointer to be stored as the hash data corresponding
  52. ** to the string <key-name>. If <pointer> is not specified, then
  53. ** the string <key-name> must already exist in the has table. Otherwise,
  54. ** an error is returned.
  55. **
  56. ** Whether or not the <pointer> argument is specified, the value returned
  57. ** is a blob containing the pointer stored as the hash data corresponding
  58. ** to string <key-name> (after the hash-table is updated, if applicable).
  59. */
  60. static void fts3TokenizerFunc(
  61. sqlite3_context *context,
  62. int argc,
  63. sqlite3_value **argv
  64. ){
  65. Fts3Hash *pHash;
  66. void *pPtr = 0;
  67. const unsigned char *zName;
  68. int nName;
  69. assert( argc==1 || argc==2 );
  70. pHash = (Fts3Hash *)sqlite3_user_data(context);
  71. zName = sqlite3_value_text(argv[0]);
  72. nName = sqlite3_value_bytes(argv[0])+1;
  73. if( argc==2 ){
  74. if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){
  75. void *pOld;
  76. int n = sqlite3_value_bytes(argv[1]);
  77. if( zName==0 || n!=sizeof(pPtr) ){
  78. sqlite3_result_error(context, "argument type mismatch", -1);
  79. return;
  80. }
  81. pPtr = *(void **)sqlite3_value_blob(argv[1]);
  82. pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
  83. if( pOld==pPtr ){
  84. sqlite3_result_error(context, "out of memory", -1);
  85. }
  86. }else{
  87. sqlite3_result_error(context, "fts3tokenize disabled", -1);
  88. return;
  89. }
  90. }else{
  91. if( zName ){
  92. pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
  93. }
  94. if( !pPtr ){
  95. char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
  96. sqlite3_result_error(context, zErr, -1);
  97. sqlite3_free(zErr);
  98. return;
  99. }
  100. }
  101. if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){
  102. sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
  103. }
  104. }
  105. int sqlite3Fts3IsIdChar(char c){
  106. static const char isFtsIdChar[] = {
  107. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
  108. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
  109. 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
  110. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
  111. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
  112. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
  113. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
  114. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
  115. };
  116. return (c&0x80 || isFtsIdChar[(int)(c)]);
  117. }
  118. const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
  119. const char *z1;
  120. const char *z2 = 0;
  121. /* Find the start of the next token. */
  122. z1 = zStr;
  123. while( z2==0 ){
  124. char c = *z1;
  125. switch( c ){
  126. case '\0': return 0; /* No more tokens here */
  127. case '\'':
  128. case '"':
  129. case '`': {
  130. z2 = z1;
  131. while( *++z2 && (*z2!=c || *++z2==c) );
  132. break;
  133. }
  134. case '[':
  135. z2 = &z1[1];
  136. while( *z2 && z2[0]!=']' ) z2++;
  137. if( *z2 ) z2++;
  138. break;
  139. default:
  140. if( sqlite3Fts3IsIdChar(*z1) ){
  141. z2 = &z1[1];
  142. while( sqlite3Fts3IsIdChar(*z2) ) z2++;
  143. }else{
  144. z1++;
  145. }
  146. }
  147. }
  148. *pn = (int)(z2-z1);
  149. return z1;
  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 = 0;
  160. char *zCopy;
  161. char *zEnd; /* Pointer to nul-term of zCopy */
  162. sqlite3_tokenizer_module *m;
  163. zCopy = sqlite3_mprintf("%s", zArg);
  164. if( !zCopy ) return SQLITE_NOMEM;
  165. zEnd = &zCopy[strlen(zCopy)];
  166. z = (char *)sqlite3Fts3NextToken(zCopy, &n);
  167. if( z==0 ){
  168. assert( n==0 );
  169. z = zCopy;
  170. }
  171. z[n] = '\0';
  172. sqlite3Fts3Dequote(z);
  173. m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
  174. if( !m ){
  175. sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
  176. rc = SQLITE_ERROR;
  177. }else{
  178. char const **aArg = 0;
  179. int iArg = 0;
  180. z = &z[n+1];
  181. while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
  182. sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
  183. char const **aNew = (const char **)sqlite3_realloc64((void *)aArg, nNew);
  184. if( !aNew ){
  185. sqlite3_free(zCopy);
  186. sqlite3_free((void *)aArg);
  187. return SQLITE_NOMEM;
  188. }
  189. aArg = aNew;
  190. aArg[iArg++] = z;
  191. z[n] = '\0';
  192. sqlite3Fts3Dequote(z);
  193. z = &z[n+1];
  194. }
  195. rc = m->xCreate(iArg, aArg, ppTok);
  196. assert( rc!=SQLITE_OK || *ppTok );
  197. if( rc!=SQLITE_OK ){
  198. sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
  199. }else{
  200. (*ppTok)->pModule = m;
  201. }
  202. sqlite3_free((void *)aArg);
  203. }
  204. sqlite3_free(zCopy);
  205. return rc;
  206. }
  207. #ifdef SQLITE_TEST
  208. #include "tclsqlite.h"
  209. #include <string.h>
  210. /*
  211. ** Implementation of a special SQL scalar function for testing tokenizers
  212. ** designed to be used in concert with the Tcl testing framework. This
  213. ** function must be called with two or more arguments:
  214. **
  215. ** SELECT <function-name>(<key-name>, ..., <input-string>);
  216. **
  217. ** where <function-name> is the name passed as the second argument
  218. ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
  219. ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
  220. **
  221. ** The return value is a string that may be interpreted as a Tcl
  222. ** list. For each token in the <input-string>, three elements are
  223. ** added to the returned list. The first is the token position, the
  224. ** second is the token text (folded, stemmed, etc.) and the third is the
  225. ** substring of <input-string> associated with the token. For example,
  226. ** using the built-in "simple" tokenizer:
  227. **
  228. ** SELECT fts_tokenizer_test('simple', 'I don't see how');
  229. **
  230. ** will return the string:
  231. **
  232. ** "{0 i I 1 dont don't 2 see see 3 how how}"
  233. **
  234. */
  235. static void testFunc(
  236. sqlite3_context *context,
  237. int argc,
  238. sqlite3_value **argv
  239. ){
  240. Fts3Hash *pHash;
  241. sqlite3_tokenizer_module *p;
  242. sqlite3_tokenizer *pTokenizer = 0;
  243. sqlite3_tokenizer_cursor *pCsr = 0;
  244. const char *zErr = 0;
  245. const char *zName;
  246. int nName;
  247. const char *zInput;
  248. int nInput;
  249. const char *azArg[64];
  250. const char *zToken;
  251. int nToken = 0;
  252. int iStart = 0;
  253. int iEnd = 0;
  254. int iPos = 0;
  255. int i;
  256. Tcl_Obj *pRet;
  257. if( argc<2 ){
  258. sqlite3_result_error(context, "insufficient arguments", -1);
  259. return;
  260. }
  261. nName = sqlite3_value_bytes(argv[0]);
  262. zName = (const char *)sqlite3_value_text(argv[0]);
  263. nInput = sqlite3_value_bytes(argv[argc-1]);
  264. zInput = (const char *)sqlite3_value_text(argv[argc-1]);
  265. pHash = (Fts3Hash *)sqlite3_user_data(context);
  266. p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
  267. if( !p ){
  268. char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
  269. sqlite3_result_error(context, zErr2, -1);
  270. sqlite3_free(zErr2);
  271. return;
  272. }
  273. pRet = Tcl_NewObj();
  274. Tcl_IncrRefCount(pRet);
  275. for(i=1; i<argc-1; i++){
  276. azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
  277. }
  278. if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
  279. zErr = "error in xCreate()";
  280. goto finish;
  281. }
  282. pTokenizer->pModule = p;
  283. if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
  284. zErr = "error in xOpen()";
  285. goto finish;
  286. }
  287. while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
  288. Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
  289. Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
  290. zToken = &zInput[iStart];
  291. nToken = iEnd-iStart;
  292. Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
  293. }
  294. if( SQLITE_OK!=p->xClose(pCsr) ){
  295. zErr = "error in xClose()";
  296. goto finish;
  297. }
  298. if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
  299. zErr = "error in xDestroy()";
  300. goto finish;
  301. }
  302. finish:
  303. if( zErr ){
  304. sqlite3_result_error(context, zErr, -1);
  305. }else{
  306. sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
  307. }
  308. Tcl_DecrRefCount(pRet);
  309. }
  310. static
  311. int registerTokenizer(
  312. sqlite3 *db,
  313. char *zName,
  314. const sqlite3_tokenizer_module *p
  315. ){
  316. int rc;
  317. sqlite3_stmt *pStmt;
  318. const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
  319. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  320. if( rc!=SQLITE_OK ){
  321. return rc;
  322. }
  323. sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
  324. sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
  325. sqlite3_step(pStmt);
  326. return sqlite3_finalize(pStmt);
  327. }
  328. static
  329. int queryTokenizer(
  330. sqlite3 *db,
  331. char *zName,
  332. const sqlite3_tokenizer_module **pp
  333. ){
  334. int rc;
  335. sqlite3_stmt *pStmt;
  336. const char zSql[] = "SELECT fts3_tokenizer(?)";
  337. *pp = 0;
  338. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  339. if( rc!=SQLITE_OK ){
  340. return rc;
  341. }
  342. sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
  343. if( SQLITE_ROW==sqlite3_step(pStmt) ){
  344. if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB
  345. && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp)
  346. ){
  347. memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
  348. }
  349. }
  350. return sqlite3_finalize(pStmt);
  351. }
  352. void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  353. /*
  354. ** Implementation of the scalar function fts3_tokenizer_internal_test().
  355. ** This function is used for testing only, it is not included in the
  356. ** build unless SQLITE_TEST is defined.
  357. **
  358. ** The purpose of this is to test that the fts3_tokenizer() function
  359. ** can be used as designed by the C-code in the queryTokenizer and
  360. ** registerTokenizer() functions above. These two functions are repeated
  361. ** in the README.tokenizer file as an example, so it is important to
  362. ** test them.
  363. **
  364. ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
  365. ** function with no arguments. An assert() will fail if a problem is
  366. ** detected. i.e.:
  367. **
  368. ** SELECT fts3_tokenizer_internal_test();
  369. **
  370. */
  371. static void intTestFunc(
  372. sqlite3_context *context,
  373. int argc,
  374. sqlite3_value **argv
  375. ){
  376. int rc;
  377. const sqlite3_tokenizer_module *p1;
  378. const sqlite3_tokenizer_module *p2;
  379. sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
  380. UNUSED_PARAMETER(argc);
  381. UNUSED_PARAMETER(argv);
  382. /* Test the query function */
  383. sqlite3Fts3SimpleTokenizerModule(&p1);
  384. rc = queryTokenizer(db, "simple", &p2);
  385. assert( rc==SQLITE_OK );
  386. assert( p1==p2 );
  387. rc = queryTokenizer(db, "nosuchtokenizer", &p2);
  388. assert( rc==SQLITE_ERROR );
  389. assert( p2==0 );
  390. assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
  391. /* Test the storage function */
  392. if( fts3TokenizerEnabled(context) ){
  393. rc = registerTokenizer(db, "nosuchtokenizer", p1);
  394. assert( rc==SQLITE_OK );
  395. rc = queryTokenizer(db, "nosuchtokenizer", &p2);
  396. assert( rc==SQLITE_OK );
  397. assert( p2==p1 );
  398. }
  399. sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
  400. }
  401. #endif
  402. /*
  403. ** Set up SQL objects in database db used to access the contents of
  404. ** the hash table pointed to by argument pHash. The hash table must
  405. ** been initialized to use string keys, and to take a private copy
  406. ** of the key when a value is inserted. i.e. by a call similar to:
  407. **
  408. ** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
  409. **
  410. ** This function adds a scalar function (see header comment above
  411. ** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
  412. ** defined at compilation time, a temporary virtual table (see header
  413. ** comment above struct HashTableVtab) to the database schema. Both
  414. ** provide read/write access to the contents of *pHash.
  415. **
  416. ** The third argument to this function, zName, is used as the name
  417. ** of both the scalar and, if created, the virtual table.
  418. */
  419. int sqlite3Fts3InitHashTable(
  420. sqlite3 *db,
  421. Fts3Hash *pHash,
  422. const char *zName
  423. ){
  424. int rc = SQLITE_OK;
  425. void *p = (void *)pHash;
  426. const int any = SQLITE_UTF8|SQLITE_DIRECTONLY;
  427. #ifdef SQLITE_TEST
  428. char *zTest = 0;
  429. char *zTest2 = 0;
  430. void *pdb = (void *)db;
  431. zTest = sqlite3_mprintf("%s_test", zName);
  432. zTest2 = sqlite3_mprintf("%s_internal_test", zName);
  433. if( !zTest || !zTest2 ){
  434. rc = SQLITE_NOMEM;
  435. }
  436. #endif
  437. if( SQLITE_OK==rc ){
  438. rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
  439. }
  440. if( SQLITE_OK==rc ){
  441. rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
  442. }
  443. #ifdef SQLITE_TEST
  444. if( SQLITE_OK==rc ){
  445. rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
  446. }
  447. if( SQLITE_OK==rc ){
  448. rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
  449. }
  450. #endif
  451. #ifdef SQLITE_TEST
  452. sqlite3_free(zTest);
  453. sqlite3_free(zTest2);
  454. #endif
  455. return rc;
  456. }
  457. #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */