diff --git a/GRDB/FTS/FTS3Tokenizer.swift b/GRDB/FTS/FTS3Tokenizer.swift index 3865409db..3f90c7e43 100644 --- a/GRDB/FTS/FTS3Tokenizer.swift +++ b/GRDB/FTS/FTS3Tokenizer.swift @@ -1,39 +1,68 @@ -/// TODO +/// An FTS3 tokenizer, suitable for FTS3 and FTS4 table definitions: +/// +/// db.create(virtualTable: "books", using: FTS4()) { t in +/// t.tokenizer = FTS3Tokenizer.simple +/// } +/// +/// See https://www.sqlite.org/fts3.html#tokenizer public struct FTS3Tokenizer { let name: String let options: [String] - /// Creates an FTS3 tokenizer + /// Creates an FTS3 tokenizer. + /// + /// Unless you use a custom tokenizer, you don't need this constructor: + /// + /// Use FTS3Tokenizer.simple, FTS3Tokenizer.porter, or + /// FTS3Tokenizer.unicode61() instead. public init(_ name: String, options: [String] = []) { self.name = name self.options = options } - /// The "simple" tokenizer + /// The "simple" tokenizer. + /// + /// db.create(virtualTable: "books", using: FTS4()) { t in + /// t.tokenizer = .simple + /// } /// /// See https://www.sqlite.org/fts3.html#tokenizer public static let simple = FTS3Tokenizer("simple") - /// The "porter" tokenizer + /// The "porter" tokenizer. + /// + /// db.create(virtualTable: "books", using: FTS4()) { t in + /// t.tokenizer = .porter + /// } /// /// See https://www.sqlite.org/fts3.html#tokenizer public static let porter = FTS3Tokenizer("porter") - /// An "unicode61" tokenizer + /// The "unicode61" tokenizer. /// - /// FTS3Tokenizer.unicode61() + /// db.create(virtualTable: "books", using: FTS4()) { t in + /// t.tokenizer = .unicode61() + /// } + /// + /// - parameters: + /// - removeDiacritics: If true (the default), then SQLite will strip + /// diacritics from latin characters. + /// - separators: Unless empty (the default), SQLite will consider these + /// characters as token separators. + /// - tokenCharacters: Unless empty (the default), SQLite will consider + /// these characters as token characters. /// /// See https://www.sqlite.org/fts3.html#tokenizer - public static func unicode61(removeDiacritics: Bool = true, separators: Set? = nil, tokenCharacters: Set? = nil) -> FTS3Tokenizer { + public static func unicode61(removeDiacritics: Bool = true, separators: Set = [], tokenCharacters: Set = []) -> FTS3Tokenizer { var options: [String] = [] if !removeDiacritics { options.append("remove_diacritics=0") } - if let separators = separators { + if !separators.isEmpty { // TODO: test "=" and "\"", "(" and ")" as separators, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:) options.append("separators=" + separators.sorted().map { String($0) }.joined(separator: "")) } - if let tokenCharacters = tokenCharacters { + if !tokenCharacters.isEmpty { // TODO: test "=" and "\"", "(" and ")" as tokenCharacters, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:) options.append("tokenchars=" + tokenCharacters.sorted().map { String($0) }.joined(separator: "")) } diff --git a/GRDB/FTS/FTS5Tokenizer.swift b/GRDB/FTS/FTS5Tokenizer.swift index d8dc746e0..b2972ddb1 100644 --- a/GRDB/FTS/FTS5Tokenizer.swift +++ b/GRDB/FTS/FTS5Tokenizer.swift @@ -1,4 +1,10 @@ #if SQLITE_ENABLE_FTS5 + /// An FTS5 tokenizer, suitable for FTS5 table definitions: + /// + /// db.create(virtualTable: "books", using: FTS5()) { t in + /// t.tokenizer = FTS5Tokenizer.unicode61() + /// } + /// /// See https://www.sqlite.org/fts5.html#tokenizers public struct FTS5Tokenizer { let components: [String] @@ -8,24 +14,45 @@ self.components = components } - /// Creates an FTS5 tokenizer + /// Creates an FTS5 tokenizer. + /// + /// Unless you use a custom tokenizer, you don't need this constructor: + /// + /// Use FTS5Tokenizer.ascii(), FTS5Tokenizer.porter(), or + /// FTS5Tokenizer.unicode61() instead. public init(_ name: String, options: [String] = []) { self.init(components: [name] + options) } /// The "ascii" tokenizer /// + /// db.create(virtualTable: "books", using: FTS5()) { t in + /// t.tokenizer = .ascii() + /// } + /// + /// - parameters: + /// - separators: Unless empty (the default), SQLite will consider + /// these characters as token separators. + /// /// See https://www.sqlite.org/fts5.html#ascii_tokenizer - public static func ascii(separators: Set? = nil) -> FTS5Tokenizer { - if let separators = separators { - return FTS5Tokenizer("ascii", options: ["separators", separators.map { String($0) }.joined(separator: "").sqlExpression.sql]) - } else { + public static func ascii(separators: Set = []) -> FTS5Tokenizer { + if separators.isEmpty { return FTS5Tokenizer("ascii") + } else { + return FTS5Tokenizer("ascii", options: ["separators", separators.map { String($0) }.joined(separator: "").sqlExpression.sql]) } } /// The "porter" tokenizer /// + /// db.create(virtualTable: "books", using: FTS5()) { t in + /// t.tokenizer = .porter() + /// } + /// + /// - parameters: + /// - base: An eventual wrapping tokenizer which replaces the + // default unicode61() base tokenizer. + /// /// See https://www.sqlite.org/fts5.html#porter_tokenizer public static func porter(wrapping base: FTS5Tokenizer? = nil) -> FTS5Tokenizer { if let base = base { @@ -37,19 +64,29 @@ /// An "unicode61" tokenizer /// - /// FTS5Tokenizer.unicode61() + /// db.create(virtualTable: "books", using: FTS5()) { t in + /// t.tokenizer = .unicode61() + /// } + /// + /// - parameters: + /// - removeDiacritics: If true (the default), then SQLite will + /// strip diacritics from latin characters. + /// - separators: Unless empty (the default), SQLite will consider + /// these characters as token separators. + /// - tokenCharacters: Unless empty (the default), SQLite will + /// consider these characters as token characters. /// /// See https://www.sqlite.org/fts5.html#unicode61_tokenizer - public static func unicode61(removeDiacritics: Bool = true, separators: Set? = nil, tokenCharacters: Set? = nil) -> FTS5Tokenizer { + public static func unicode61(removeDiacritics: Bool = true, separators: Set = [], tokenCharacters: Set = []) -> FTS5Tokenizer { var options: [String] = [] if !removeDiacritics { options.append(contentsOf: ["remove_diacritics", "0"]) } - if let separators = separators, !separators.isEmpty { + if !separators.isEmpty { // TODO: test "=" and "\"", "(" and ")" as separators, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:) options.append(contentsOf: ["separators", separators.sorted().map { String($0) }.joined(separator: "").sqlExpression.sql]) } - if let tokenCharacters = tokenCharacters, !tokenCharacters.isEmpty { + if !tokenCharacters.isEmpty { // TODO: test "=" and "\"", "(" and ")" as tokenCharacters, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:) options.append(contentsOf: ["tokenchars", tokenCharacters.sorted().map { String($0) }.joined(separator: "").sqlExpression.sql]) }