sl@0: /* sl@0: ** 2007 June 22 sl@0: ** sl@0: ** The author disclaims copyright to this source code. In place of sl@0: ** a legal notice, here is a blessing: sl@0: ** sl@0: ** May you do good and not evil. sl@0: ** May you find forgiveness for yourself and forgive others. sl@0: ** May you share freely, never taking more than you give. sl@0: ** sl@0: ****************************************************************************** sl@0: ** sl@0: ** This is part of an SQLite module implementing full-text search. sl@0: ** This particular file implements the generic tokenizer interface. sl@0: */ sl@0: sl@0: /* sl@0: ** The code in this file is only compiled if: sl@0: ** sl@0: ** * The FTS3 module is being built as an extension sl@0: ** (in which case SQLITE_CORE is not defined), or sl@0: ** sl@0: ** * The FTS3 module is being built into the core of sl@0: ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). sl@0: */ sl@0: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) sl@0: sl@0: #include "sqlite3ext.h" sl@0: #ifndef SQLITE_CORE sl@0: SQLITE_EXTENSION_INIT1 sl@0: #endif sl@0: sl@0: #include "fts3_hash.h" sl@0: #include "fts3_tokenizer.h" sl@0: #include sl@0: sl@0: /* sl@0: ** Implementation of the SQL scalar function for accessing the underlying sl@0: ** hash table. This function may be called as follows: sl@0: ** sl@0: ** SELECT (); sl@0: ** SELECT (, ); sl@0: ** sl@0: ** where is the name passed as the second argument sl@0: ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer'). sl@0: ** sl@0: ** If the argument is specified, it must be a blob value sl@0: ** containing a pointer to be stored as the hash data corresponding sl@0: ** to the string . If is not specified, then sl@0: ** the string must already exist in the has table. Otherwise, sl@0: ** an error is returned. sl@0: ** sl@0: ** Whether or not the argument is specified, the value returned sl@0: ** is a blob containing the pointer stored as the hash data corresponding sl@0: ** to string (after the hash-table is updated, if applicable). sl@0: */ sl@0: static void scalarFunc( sl@0: sqlite3_context *context, sl@0: int argc, sl@0: sqlite3_value **argv sl@0: ){ sl@0: fts3Hash *pHash; sl@0: void *pPtr = 0; sl@0: const unsigned char *zName; sl@0: int nName; sl@0: sl@0: assert( argc==1 || argc==2 ); sl@0: sl@0: pHash = (fts3Hash *)sqlite3_user_data(context); sl@0: sl@0: zName = sqlite3_value_text(argv[0]); sl@0: nName = sqlite3_value_bytes(argv[0])+1; sl@0: sl@0: if( argc==2 ){ sl@0: void *pOld; sl@0: int n = sqlite3_value_bytes(argv[1]); sl@0: if( n!=sizeof(pPtr) ){ sl@0: sqlite3_result_error(context, "argument type mismatch", -1); sl@0: return; sl@0: } sl@0: pPtr = *(void **)sqlite3_value_blob(argv[1]); sl@0: pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); sl@0: if( pOld==pPtr ){ sl@0: sqlite3_result_error(context, "out of memory", -1); sl@0: return; sl@0: } sl@0: }else{ sl@0: pPtr = sqlite3Fts3HashFind(pHash, zName, nName); sl@0: if( !pPtr ){ sl@0: char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); sl@0: sqlite3_result_error(context, zErr, -1); sl@0: sqlite3_free(zErr); sl@0: return; sl@0: } sl@0: } sl@0: sl@0: sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); sl@0: } sl@0: sl@0: #ifdef SQLITE_TEST sl@0: sl@0: #include "tcl.h" sl@0: #include sl@0: sl@0: /* sl@0: ** Implementation of a special SQL scalar function for testing tokenizers sl@0: ** designed to be used in concert with the Tcl testing framework. This sl@0: ** function must be called with two arguments: sl@0: ** sl@0: ** SELECT (, ); sl@0: ** SELECT (, ); sl@0: ** sl@0: ** where is the name passed as the second argument sl@0: ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer') sl@0: ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test'). sl@0: ** sl@0: ** The return value is a string that may be interpreted as a Tcl sl@0: ** list. For each token in the , three elements are sl@0: ** added to the returned list. The first is the token position, the sl@0: ** second is the token text (folded, stemmed, etc.) and the third is the sl@0: ** substring of associated with the token. For example, sl@0: ** using the built-in "simple" tokenizer: sl@0: ** sl@0: ** SELECT fts_tokenizer_test('simple', 'I don't see how'); sl@0: ** sl@0: ** will return the string: sl@0: ** sl@0: ** "{0 i I 1 dont don't 2 see see 3 how how}" sl@0: ** sl@0: */ sl@0: static void testFunc( sl@0: sqlite3_context *context, sl@0: int argc, sl@0: sqlite3_value **argv sl@0: ){ sl@0: fts3Hash *pHash; sl@0: sqlite3_tokenizer_module *p; sl@0: sqlite3_tokenizer *pTokenizer = 0; sl@0: sqlite3_tokenizer_cursor *pCsr = 0; sl@0: sl@0: const char *zErr = 0; sl@0: sl@0: const char *zName; sl@0: int nName; sl@0: const char *zInput; sl@0: int nInput; sl@0: sl@0: const char *zArg = 0; sl@0: sl@0: const char *zToken; sl@0: int nToken; sl@0: int iStart; sl@0: int iEnd; sl@0: int iPos; sl@0: sl@0: Tcl_Obj *pRet; sl@0: sl@0: assert( argc==2 || argc==3 ); sl@0: sl@0: nName = sqlite3_value_bytes(argv[0]); sl@0: zName = (const char *)sqlite3_value_text(argv[0]); sl@0: nInput = sqlite3_value_bytes(argv[argc-1]); sl@0: zInput = (const char *)sqlite3_value_text(argv[argc-1]); sl@0: sl@0: if( argc==3 ){ sl@0: zArg = (const char *)sqlite3_value_text(argv[1]); sl@0: } sl@0: sl@0: pHash = (fts3Hash *)sqlite3_user_data(context); sl@0: p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); sl@0: sl@0: if( !p ){ sl@0: char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); sl@0: sqlite3_result_error(context, zErr, -1); sl@0: sqlite3_free(zErr); sl@0: return; sl@0: } sl@0: sl@0: pRet = Tcl_NewObj(); sl@0: Tcl_IncrRefCount(pRet); sl@0: sl@0: if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){ sl@0: zErr = "error in xCreate()"; sl@0: goto finish; sl@0: } sl@0: pTokenizer->pModule = p; sl@0: if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){ sl@0: zErr = "error in xOpen()"; sl@0: goto finish; sl@0: } sl@0: pCsr->pTokenizer = pTokenizer; sl@0: sl@0: while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){ sl@0: Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos)); sl@0: Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); sl@0: zToken = &zInput[iStart]; sl@0: nToken = iEnd-iStart; sl@0: Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); sl@0: } sl@0: sl@0: if( SQLITE_OK!=p->xClose(pCsr) ){ sl@0: zErr = "error in xClose()"; sl@0: goto finish; sl@0: } sl@0: if( SQLITE_OK!=p->xDestroy(pTokenizer) ){ sl@0: zErr = "error in xDestroy()"; sl@0: goto finish; sl@0: } sl@0: sl@0: finish: sl@0: if( zErr ){ sl@0: sqlite3_result_error(context, zErr, -1); sl@0: }else{ sl@0: sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); sl@0: } sl@0: Tcl_DecrRefCount(pRet); sl@0: } sl@0: sl@0: static sl@0: int registerTokenizer( sl@0: sqlite3 *db, sl@0: char *zName, sl@0: const sqlite3_tokenizer_module *p sl@0: ){ sl@0: int rc; sl@0: sqlite3_stmt *pStmt; sl@0: const char zSql[] = "SELECT fts3_tokenizer(?, ?)"; sl@0: sl@0: rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); sl@0: if( rc!=SQLITE_OK ){ sl@0: return rc; sl@0: } sl@0: sl@0: sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); sl@0: sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC); sl@0: sqlite3_step(pStmt); sl@0: sl@0: return sqlite3_finalize(pStmt); sl@0: } sl@0: sl@0: static sl@0: int queryTokenizer( sl@0: sqlite3 *db, sl@0: char *zName, sl@0: const sqlite3_tokenizer_module **pp sl@0: ){ sl@0: int rc; sl@0: sqlite3_stmt *pStmt; sl@0: const char zSql[] = "SELECT fts3_tokenizer(?)"; sl@0: sl@0: *pp = 0; sl@0: rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); sl@0: if( rc!=SQLITE_OK ){ sl@0: return rc; sl@0: } sl@0: sl@0: sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); sl@0: if( SQLITE_ROW==sqlite3_step(pStmt) ){ sl@0: if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ sl@0: memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); sl@0: } sl@0: } sl@0: sl@0: return sqlite3_finalize(pStmt); sl@0: } sl@0: sl@0: void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); sl@0: sl@0: /* sl@0: ** Implementation of the scalar function fts3_tokenizer_internal_test(). sl@0: ** This function is used for testing only, it is not included in the sl@0: ** build unless SQLITE_TEST is defined. sl@0: ** sl@0: ** The purpose of this is to test that the fts3_tokenizer() function sl@0: ** can be used as designed by the C-code in the queryTokenizer and sl@0: ** registerTokenizer() functions above. These two functions are repeated sl@0: ** in the README.tokenizer file as an example, so it is important to sl@0: ** test them. sl@0: ** sl@0: ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar sl@0: ** function with no arguments. An assert() will fail if a problem is sl@0: ** detected. i.e.: sl@0: ** sl@0: ** SELECT fts3_tokenizer_internal_test(); sl@0: ** sl@0: */ sl@0: static void intTestFunc( sl@0: sqlite3_context *context, sl@0: int argc, sl@0: sqlite3_value **argv sl@0: ){ sl@0: int rc; sl@0: const sqlite3_tokenizer_module *p1; sl@0: const sqlite3_tokenizer_module *p2; sl@0: sqlite3 *db = (sqlite3 *)sqlite3_user_data(context); sl@0: sl@0: /* Test the query function */ sl@0: sqlite3Fts3SimpleTokenizerModule(&p1); sl@0: rc = queryTokenizer(db, "simple", &p2); sl@0: assert( rc==SQLITE_OK ); sl@0: assert( p1==p2 ); sl@0: rc = queryTokenizer(db, "nosuchtokenizer", &p2); sl@0: assert( rc==SQLITE_ERROR ); sl@0: assert( p2==0 ); sl@0: assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); sl@0: sl@0: /* Test the storage function */ sl@0: rc = registerTokenizer(db, "nosuchtokenizer", p1); sl@0: assert( rc==SQLITE_OK ); sl@0: rc = queryTokenizer(db, "nosuchtokenizer", &p2); sl@0: assert( rc==SQLITE_OK ); sl@0: assert( p2==p1 ); sl@0: sl@0: sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); sl@0: } sl@0: sl@0: #endif sl@0: sl@0: /* sl@0: ** Set up SQL objects in database db used to access the contents of sl@0: ** the hash table pointed to by argument pHash. The hash table must sl@0: ** been initialised to use string keys, and to take a private copy sl@0: ** of the key when a value is inserted. i.e. by a call similar to: sl@0: ** sl@0: ** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); sl@0: ** sl@0: ** This function adds a scalar function (see header comment above sl@0: ** scalarFunc() in this file for details) and, if ENABLE_TABLE is sl@0: ** defined at compilation time, a temporary virtual table (see header sl@0: ** comment above struct HashTableVtab) to the database schema. Both sl@0: ** provide read/write access to the contents of *pHash. sl@0: ** sl@0: ** The third argument to this function, zName, is used as the name sl@0: ** of both the scalar and, if created, the virtual table. sl@0: */ sl@0: int sqlite3Fts3InitHashTable( sl@0: sqlite3 *db, sl@0: fts3Hash *pHash, sl@0: const char *zName sl@0: ){ sl@0: int rc = SQLITE_OK; sl@0: void *p = (void *)pHash; sl@0: const int any = SQLITE_ANY; sl@0: char *zTest = 0; sl@0: char *zTest2 = 0; sl@0: sl@0: #ifdef SQLITE_TEST sl@0: void *pdb = (void *)db; sl@0: zTest = sqlite3_mprintf("%s_test", zName); sl@0: zTest2 = sqlite3_mprintf("%s_internal_test", zName); sl@0: if( !zTest || !zTest2 ){ sl@0: rc = SQLITE_NOMEM; sl@0: } sl@0: #endif sl@0: sl@0: if( rc!=SQLITE_OK sl@0: || (rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0)) sl@0: || (rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0)) sl@0: #ifdef SQLITE_TEST sl@0: || (rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0)) sl@0: || (rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0)) sl@0: || (rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0)) sl@0: #endif sl@0: ); sl@0: sl@0: sqlite3_free(zTest); sl@0: sqlite3_free(zTest2); sl@0: return rc; sl@0: } sl@0: sl@0: #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */