|
| 1 | +// |
| 2 | +// SQLite.swift |
| 3 | +// https://github.com/stephencelis/SQLite.swift |
| 4 | +// Copyright © 2014-2015 Stephen Celis. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +// |
| 24 | + |
| 25 | +#import "SQLiteObjc.h" |
| 26 | +#import "fts3_tokenizer.h" |
| 27 | + |
| 28 | +#pragma mark - FTS |
| 29 | + |
| 30 | +typedef struct __SQLiteTokenizer { |
| 31 | + sqlite3_tokenizer base; |
| 32 | + __unsafe_unretained _SQLiteTokenizerNextCallback callback; |
| 33 | +} __SQLiteTokenizer; |
| 34 | + |
| 35 | +typedef struct __SQLiteTokenizerCursor { |
| 36 | + void * base; |
| 37 | + const char * input; |
| 38 | + int inputOffset; |
| 39 | + int inputLength; |
| 40 | + int idx; |
| 41 | +} __SQLiteTokenizerCursor; |
| 42 | + |
| 43 | +static NSMutableDictionary * __SQLiteTokenizerMap; |
| 44 | + |
| 45 | +static int __SQLiteTokenizerCreate(int argc, const char * const * argv, sqlite3_tokenizer ** ppTokenizer) { |
| 46 | + __SQLiteTokenizer * tokenizer = (__SQLiteTokenizer *)sqlite3_malloc(sizeof(__SQLiteTokenizer)); |
| 47 | + if (!tokenizer) { |
| 48 | + return SQLITE_NOMEM; |
| 49 | + } |
| 50 | + memset(tokenizer, 0, sizeof(* tokenizer)); |
| 51 | + |
| 52 | + NSString * key = [NSString stringWithUTF8String:argv[0]]; |
| 53 | + tokenizer->callback = [__SQLiteTokenizerMap objectForKey:key]; |
| 54 | + if (!tokenizer->callback) { |
| 55 | + return SQLITE_ERROR; |
| 56 | + } |
| 57 | + |
| 58 | + *ppTokenizer = &tokenizer->base; |
| 59 | + return SQLITE_OK; |
| 60 | +} |
| 61 | + |
| 62 | +static int __SQLiteTokenizerDestroy(sqlite3_tokenizer * pTokenizer) { |
| 63 | + sqlite3_free(pTokenizer); |
| 64 | + return SQLITE_OK; |
| 65 | +} |
| 66 | + |
| 67 | +static int __SQLiteTokenizerOpen(sqlite3_tokenizer * pTokenizer, const char * pInput, int nBytes, sqlite3_tokenizer_cursor ** ppCursor) { |
| 68 | + __SQLiteTokenizerCursor * cursor = (__SQLiteTokenizerCursor *)sqlite3_malloc(sizeof(__SQLiteTokenizerCursor)); |
| 69 | + if (!cursor) { |
| 70 | + return SQLITE_NOMEM; |
| 71 | + } |
| 72 | + |
| 73 | + cursor->input = pInput; |
| 74 | + cursor->inputOffset = 0; |
| 75 | + cursor->inputLength = 0; |
| 76 | + cursor->idx = 0; |
| 77 | + |
| 78 | + *ppCursor = (sqlite3_tokenizer_cursor *)cursor; |
| 79 | + return SQLITE_OK; |
| 80 | +} |
| 81 | + |
| 82 | +static int __SQLiteTokenizerClose(sqlite3_tokenizer_cursor * pCursor) { |
| 83 | + sqlite3_free(pCursor); |
| 84 | + return SQLITE_OK; |
| 85 | +} |
| 86 | + |
| 87 | +static int __SQLiteTokenizerNext(sqlite3_tokenizer_cursor * pCursor, const char ** ppToken, int * pnBytes, int * piStartOffset, int * piEndOffset, int * piPosition) { |
| 88 | + __SQLiteTokenizerCursor * cursor = (__SQLiteTokenizerCursor *)pCursor; |
| 89 | + __SQLiteTokenizer * tokenizer = (__SQLiteTokenizer *)cursor->base; |
| 90 | + |
| 91 | + cursor->inputOffset += cursor->inputLength; |
| 92 | + const char * input = cursor->input + cursor->inputOffset; |
| 93 | + const char * token = [tokenizer->callback(input, &cursor->inputOffset, &cursor->inputLength) cStringUsingEncoding:NSUTF8StringEncoding]; |
| 94 | + if (!token) { |
| 95 | + return SQLITE_DONE; |
| 96 | + } |
| 97 | + |
| 98 | + *ppToken = token; |
| 99 | + *pnBytes = (int)strlen(token); |
| 100 | + *piStartOffset = cursor->inputOffset; |
| 101 | + *piEndOffset = cursor->inputOffset + cursor->inputLength; |
| 102 | + *piPosition = cursor->idx++; |
| 103 | + return SQLITE_OK; |
| 104 | +} |
| 105 | + |
| 106 | +static const sqlite3_tokenizer_module __SQLiteTokenizerModule = { |
| 107 | + 0, |
| 108 | + __SQLiteTokenizerCreate, |
| 109 | + __SQLiteTokenizerDestroy, |
| 110 | + __SQLiteTokenizerOpen, |
| 111 | + __SQLiteTokenizerClose, |
| 112 | + __SQLiteTokenizerNext |
| 113 | +}; |
| 114 | + |
| 115 | +int _SQLiteRegisterTokenizer(sqlite3 *db, const char * moduleName, const char * submoduleName, _SQLiteTokenizerNextCallback callback) { |
| 116 | + static dispatch_once_t onceToken; |
| 117 | + dispatch_once(&onceToken, ^{ |
| 118 | + __SQLiteTokenizerMap = [NSMutableDictionary new]; |
| 119 | + }); |
| 120 | + |
| 121 | + sqlite3_stmt * stmt; |
| 122 | + int status = sqlite3_prepare_v2(db, "SELECT fts3_tokenizer(?, ?)", -1, &stmt, 0); |
| 123 | + if (status != SQLITE_OK ){ |
| 124 | + return status; |
| 125 | + } |
| 126 | + const sqlite3_tokenizer_module * pModule = &__SQLiteTokenizerModule; |
| 127 | + sqlite3_bind_text(stmt, 1, moduleName, -1, SQLITE_STATIC); |
| 128 | + sqlite3_bind_blob(stmt, 2, &pModule, sizeof(pModule), SQLITE_STATIC); |
| 129 | + sqlite3_step(stmt); |
| 130 | + status = sqlite3_finalize(stmt); |
| 131 | + if (status != SQLITE_OK ){ |
| 132 | + return status; |
| 133 | + } |
| 134 | + |
| 135 | + [__SQLiteTokenizerMap setObject:[callback copy] forKey:[NSString stringWithUTF8String:submoduleName]]; |
| 136 | + |
| 137 | + return SQLITE_OK; |
| 138 | +} |
0 commit comments