mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
FTS5Pattern(matchingPrefixPhrase:)
This commit is contained in:
1 parent
dd40e3be5b
commit
1da85dff58
2 files changed
+46
-1
No files matched your search
@@ -44,6 +44,19 @@
|
||||
try? self.init(rawPattern: "\"" + tokens.joined(separator: " ") + "\"")
|
||||
}
|
||||
|
||||
/// Creates a pattern that matches a contiguous string prefix; returns
|
||||
/// nil if no pattern could be built.
|
||||
///
|
||||
/// FTS5Pattern(matchingPrefixPhrase: "") // nil
|
||||
/// FTS5Pattern(matchingPrefixPhrase: "foo bar") // ^"foo bar"
|
||||
///
|
||||
/// - parameter string: The string to turn into an FTS5 pattern
|
||||
public init?(matchingPrefixPhrase string: String) {
|
||||
guard let tokens = try? DatabaseQueue().inDatabase({ db in try db.makeTokenizer(.ascii()).nonSynonymTokens(in: string, for: .query) }) else { return nil }
|
||||
guard !tokens.isEmpty else { return nil }
|
||||
try? self.init(rawPattern: "^\"" + tokens.joined(separator: " ") + "\"")
|
||||
}
|
||||
|
||||
init(rawPattern: String, allowedColumns: [String] = []) throws {
|
||||
// Correctness above all: use SQLite to validate the pattern.
|
||||
//
|
||||
|
||||
@@ -17,7 +17,7 @@ class FTS5PatternTests: GRDBTestCase {
|
||||
t.column("author")
|
||||
t.column("body")
|
||||
}
|
||||
try db.execute("INSERT INTO books (title, author, body) VALUES (?, ?, ?)", arguments: ["Moby Dick", "Herman Melville", "Call me Ishmael. Some years ago--never mind how long precisely--having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world."])
|
||||
try db.execute("INSERT INTO books (title, author, body) VALUES (?, ?, ?)", arguments: ["Moby-Dick", "Herman Melville", "Call me Ishmael. Some years ago--never mind how long precisely--having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world."])
|
||||
try db.execute("INSERT INTO books (title, author, body) VALUES (?, ?, ?)", arguments: ["Red Mars", "Kim Stanley Robinson", "History is not evolution! It is a false analogy! Evolution is a matter of environment and chance, acting over millions of years. But history is a matter of environment and choice, acting within lifetimes, and sometimes within years, or months, or days! History is Lamarckian!"])
|
||||
try db.execute("INSERT INTO books (title, author, body) VALUES (?, ?, ?)", arguments: ["Querelle de Brest", "Jean Genet", "L’idée de mer évoque souvent l’idée de mer, de marins. Mer et marins ne se présentent pas alors avec la précision d’une image, le meurtre plutôt fait en nous l’émotion déferler par vagues."])
|
||||
try db.execute("INSERT INTO books (title, author, body) VALUES (?, ?, ?)", arguments: ["Éden, Éden, Éden", "Pierre Guyotat", "/ Les soldats, casqués, jambes ouvertes, foulent, muscles retenus, les nouveau-nés emmaillotés dans les châles écarlates, violets : les bébés roulent hors des bras des femmes accroupies sur les tôles mitraillées des G. M. C. ;"])
|
||||
@@ -161,5 +161,37 @@ class FTS5PatternTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFTS5PatternWithPrefixPhrase() throws {
|
||||
let wrongInputs = ["", "*", "^", " ", "(", "()", "\"", "?!"]
|
||||
for string in wrongInputs {
|
||||
if let pattern = FTS5Pattern(matchingPrefixPhrase: string) {
|
||||
let rawPattern = String.fromDatabaseValue(pattern.databaseValue)!
|
||||
XCTFail("Unexpected raw pattern \(String(reflecting: rawPattern)) from string \(String(reflecting: string))")
|
||||
}
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
// Couples (pattern, expected raw pattern, expected count of matching rows)
|
||||
let cases = [
|
||||
("écarlates", "^\"écarlates\"", 0),
|
||||
("^Moby-dick*", "^\"moby dick\"", 1),
|
||||
("not evolution", "^\"not evolution\"", 0),
|
||||
("HISTORY IS", "^\"history is\"", 1),
|
||||
(" \t\nyears \t\nmonths \t\n", "^\"years months\"", 0),
|
||||
("\"years months days\"", "^\"years months days\"", 0),
|
||||
("FOOÉı👨👨🏿🇫🇷🇨🇮", "^\"fooÉı👨👨🏿🇫🇷🇨🇮\"", 0),
|
||||
]
|
||||
for (string, expectedRawPattern, expectedCount) in cases {
|
||||
if let pattern = FTS5Pattern(matchingPrefixPhrase: string) {
|
||||
let rawPattern = String.fromDatabaseValue(pattern.databaseValue)!
|
||||
XCTAssertEqual(rawPattern, expectedRawPattern)
|
||||
let count = try Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: [pattern])!
|
||||
XCTAssertEqual(count, expectedCount, "Expected pattern \(String(reflecting: rawPattern)) to yield \(expectedCount) results")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user