FMDatabase+FTS3.h 3.3 KB

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