modernc.org/cc@v1.0.1/v2/testdata/_sqlite/ext/fts3/fts3_tokenizer.c (about)

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