FMDatabase+FTS3.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "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 delgate implementation in the global table. The name should be used
  24. as a parameter when creating the table.
  25. */
  26. + (void)registerTokenizer:(id<FMTokenizerDelegate>)tokenizer withName:(NSString *)name;
  27. /**
  28. Calls the `fts3_tokenizer()` function on this database, installing the "fmdb" tokenizer module.
  29. */
  30. - (BOOL)installTokenizerModule;
  31. /**
  32. Runs a "special command" for FTS3/FTS4 tables.
  33. */
  34. - (BOOL)issueCommand:(NSString *)command forTable:(NSString *)tableName;
  35. @end
  36. #pragma mark
  37. /* Extend this structure with your own custom cursor data */
  38. typedef struct FMTokenizerCursor
  39. {
  40. void *tokenizer; /* Internal SQLite reference */
  41. CFStringRef inputString; /* The input text being tokenized */
  42. CFRange currentRange; /* The current offset within `inputString` */
  43. CFStringRef tokenString; /* The contents of the current token */
  44. CFTypeRef userObject; /* Additional state for the cursor */
  45. int tokenIndex; /* Index of next token to be returned */
  46. UInt8 outputBuf[128]; /* Result for SQLite */
  47. } FMTokenizerCursor;
  48. @protocol FMTokenizerDelegate
  49. - (void)openTokenizerCursor:(FMTokenizerCursor *)cursor;
  50. - (BOOL)nextTokenForCursor:(FMTokenizerCursor *)cursor;
  51. - (void)closeTokenizerCursor:(FMTokenizerCursor *)cursor;
  52. @end
  53. #pragma mark
  54. /**
  55. The container of offset information.
  56. */
  57. @interface FMTextOffsets : NSObject
  58. - (instancetype)initWithDBOffsets:(const char *)offsets;
  59. /**
  60. Enumerate each set of offsets in the result. The column number can be turned into a column name
  61. using `[FMResultSet columnNameForIndex:]`. The `matchRange` is in UTF-8 byte positions, so it must be
  62. modified to use with `NSString` data.
  63. */
  64. - (void)enumerateWithBlock:(void (^)(NSInteger columnNumber, NSInteger termNumber, NSRange matchRange))block;
  65. @end
  66. /**
  67. A category that adds support for the encoded data returned by FTS3 functions.
  68. */
  69. @interface FMResultSet (FTS3)
  70. /**
  71. Returns a structure containing values from the `offsets()` function. Make sure the column index corresponds
  72. to the column index in the SQL query.
  73. @param columnIdx Zero-based index for column.
  74. @return `FMTextOffsets` structure.
  75. */
  76. - (FMTextOffsets *)offsetsForColumnIndex:(int)columnIdx;
  77. @end