|
| 1 | +// |
| 2 | +// FMDatabase+FTS3.h |
| 3 | +// fmdb |
| 4 | +// |
| 5 | +// Created by Andrew on 3/27/14. |
| 6 | +// Copyright (c) 2014 Andrew Goodale. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "FMDatabase.h" |
| 10 | + |
| 11 | +/** |
| 12 | + Names of commands that can be issued against an FTS table. |
| 13 | + */ |
| 14 | +extern NSString *const kFTSCommandOptimize; // "optimize" |
| 15 | +extern NSString *const kFTSCommandRebuild; // "rebuild" |
| 16 | +extern NSString *const kFTSCommandIntegrityCheck; // "integrity-check" |
| 17 | +extern NSString *const kFTSCommandMerge; // "merge=%u,%u" |
| 18 | +extern NSString *const kFTSCommandAutoMerge; // "automerge=%u" |
| 19 | + |
| 20 | +@protocol FMTokenizerDelegate; |
| 21 | + |
| 22 | +/** |
| 23 | + This category provides methods to access the FTS3 extensions in SQLite. |
| 24 | + */ |
| 25 | +@interface FMDatabase (FTS3) |
| 26 | + |
| 27 | +/** |
| 28 | + Register a delegate implementation in the global table. This should be used when using a single tokenizer. |
| 29 | + */ |
| 30 | ++ (void)registerTokenizer:(id<FMTokenizerDelegate>)tokenizer; |
| 31 | + |
| 32 | +/** |
| 33 | + Register a delegate implementation in the global table. The key should be used |
| 34 | + as a parameter when creating the table. |
| 35 | + */ |
| 36 | ++ (void)registerTokenizer:(id<FMTokenizerDelegate>)tokenizer withKey:(NSString *)key; |
| 37 | + |
| 38 | +/** |
| 39 | + Calls the `fts3_tokenizer()` function on this database, installing tokenizer module with the 'fmdb' name. |
| 40 | + */ |
| 41 | +- (BOOL)installTokenizerModule; |
| 42 | + |
| 43 | +/** |
| 44 | + Calls the `fts3_tokenizer()` function on this database, installing the tokenizer module with specified name. |
| 45 | + */ |
| 46 | +- (BOOL)installTokenizerModuleWithName:(NSString *)name; |
| 47 | + |
| 48 | +/** |
| 49 | + Runs a "special command" for FTS3/FTS4 tables. |
| 50 | + */ |
| 51 | +- (BOOL)issueCommand:(NSString *)command forTable:(NSString *)tableName; |
| 52 | + |
| 53 | +@end |
| 54 | + |
| 55 | +#pragma mark |
| 56 | + |
| 57 | +/* Extend this structure with your own custom cursor data */ |
| 58 | +typedef struct FMTokenizerCursor |
| 59 | +{ |
| 60 | + void *tokenizer; /* Internal SQLite reference */ |
| 61 | + CFStringRef inputString; /* The input text being tokenized */ |
| 62 | + CFRange currentRange; /* The current offset within `inputString` */ |
| 63 | + CFStringRef tokenString; /* The contents of the current token */ |
| 64 | + CFTypeRef userObject; /* Additional state for the cursor */ |
| 65 | + int tokenIndex; /* Index of next token to be returned */ |
| 66 | + UInt8 outputBuf[128]; /* Result for SQLite */ |
| 67 | +} FMTokenizerCursor; |
| 68 | + |
| 69 | +@protocol FMTokenizerDelegate |
| 70 | + |
| 71 | +- (void)openTokenizerCursor:(FMTokenizerCursor *)cursor; |
| 72 | + |
| 73 | +- (BOOL)nextTokenForCursor:(FMTokenizerCursor *)cursor; |
| 74 | + |
| 75 | +- (void)closeTokenizerCursor:(FMTokenizerCursor *)cursor; |
| 76 | + |
| 77 | +@end |
| 78 | + |
| 79 | +#pragma mark |
| 80 | + |
| 81 | +/** |
| 82 | + The container of offset information. |
| 83 | + */ |
| 84 | +@interface FMTextOffsets : NSObject |
| 85 | + |
| 86 | +- (instancetype)initWithDBOffsets:(const char *)offsets; |
| 87 | + |
| 88 | +/** |
| 89 | + Enumerate each set of offsets in the result. The column number can be turned into a column name |
| 90 | + using `[FMResultSet columnNameForIndex:]`. The `matchRange` is in UTF-8 byte positions, so it must be |
| 91 | + modified to use with `NSString` data. |
| 92 | + */ |
| 93 | +- (void)enumerateWithBlock:(void (^)(NSInteger columnNumber, NSInteger termNumber, NSRange matchRange))block; |
| 94 | + |
| 95 | +@end |
| 96 | + |
| 97 | +/** |
| 98 | + A category that adds support for the encoded data returned by FTS3 functions. |
| 99 | + */ |
| 100 | +@interface FMResultSet (FTS3) |
| 101 | + |
| 102 | +/** |
| 103 | + Returns a structure containing values from the `offsets()` function. Make sure the column index corresponds |
| 104 | + to the column index in the SQL query. |
| 105 | + |
| 106 | + @param columnIdx Zero-based index for column. |
| 107 | + |
| 108 | + @return `FMTextOffsets` structure. |
| 109 | + */ |
| 110 | +- (FMTextOffsets *)offsetsForColumnIndex:(int)columnIdx; |
| 111 | + |
| 112 | +@end |
0 commit comments