diff --git a/GRDB/FTS/FTS3Pattern.swift b/GRDB/FTS/FTS3Pattern.swift index 29d3731ca..ea0678530 100644 --- a/GRDB/FTS/FTS3Pattern.swift +++ b/GRDB/FTS/FTS3Pattern.swift @@ -88,6 +88,31 @@ extension FTS3Pattern : DatabaseValueConvertible { } } +extension QueryInterfaceRequest { + + // MARK: Full Text Search + + /// TODO + public func matching(_ pattern: FTS3Pattern) -> QueryInterfaceRequest { + switch query.source { + case .table(let name, let alias)?: + return filter(SQLExpressionBinary(.match, Column(alias ?? name), pattern)) + default: + fatalError("fts3 match requires a table") + } + } +} + +extension TableMapping { + + // MARK: Full Text Search + + /// TODO + public static func matching(_ pattern: FTS3Pattern) -> QueryInterfaceRequest { + return all().matching(pattern) + } +} + extension Column { /// TODO public func match(_ pattern: FTS3Pattern) -> SQLExpression { diff --git a/GRDB/FTS/FTS5Pattern.swift b/GRDB/FTS/FTS5Pattern.swift index d2cd7d571..c31e06158 100644 --- a/GRDB/FTS/FTS5Pattern.swift +++ b/GRDB/FTS/FTS5Pattern.swift @@ -101,9 +101,27 @@ extension FTS5Pattern : DatabaseValueConvertible { } } -extension Column { +extension QueryInterfaceRequest { + + // MARK: Full Text Search + /// TODO - public func match(_ pattern: FTS5Pattern) -> SQLExpression { - return SQLExpressionBinary(.match, self, pattern) + public func matching(_ pattern: FTS5Pattern) -> QueryInterfaceRequest { + switch query.source { + case .table(let name, let alias)?: + return filter(SQLExpressionBinary(.match, Column(alias ?? name), pattern)) + default: + fatalError("fts5 match requires a table") + } + } +} + +extension TableMapping { + + // MARK: Full Text Search + + /// TODO + public static func matching(_ pattern: FTS5Pattern) -> QueryInterfaceRequest { + return all().matching(pattern) } } diff --git a/README.md b/README.md index f13281c4c..f5b1ed1d1 100644 --- a/README.md +++ b/README.md @@ -2474,7 +2474,7 @@ let idColumn = Column("id") let nameColumn = Column("name") ``` -You can now build requests with the following methods: `all`, `select`, `distinct`, `filter`, `group`, `having`, `order`, `reversed`, `limit`. All those methods return another request, which you can further refine by applying another method: `Person.select(...).filter(...).order(...)`. +You can now build requests with the following methods: `all`, `select`, `distinct`, `filter`, `matching`, `group`, `having`, `order`, `reversed`, `limit`. All those methods return another request, which you can further refine by applying another method: `Person.select(...).filter(...).order(...)`. - `all()`: the request for all rows. @@ -2510,6 +2510,14 @@ You can now build requests with the following methods: `all`, `select`, `distinc Person.filter(nameColumn != nil && heightColumn > 1.75) ``` +- `matching(pattern)` performs [full-text Search](#full-text-search). + + ```swift + // SELECT * FROM documents WHERE documents MATCH 'sqlite AND database' + let pattern = FTS3Pattern(matchingAllTokensIn: "SQLite database") + Document.matching(pattern) + ``` + - `group(expression, ...)` groups rows. ```swift @@ -2577,7 +2585,7 @@ You can refine requests by chaining those methods: Person.order(nameColumn).filter(emailColumn != nil) ``` -The `select`, `order`, `group`, and `limit` methods ignore and replace previously applied selection, orderings, grouping, and limits. On the opposite, `filter`, and `having` methods extend the query: +The `select`, `order`, `group`, and `limit` methods ignore and replace previously applied selection, orderings, grouping, and limits. On the opposite, `filter`, `matching`, and `having` methods extend the query: ```swift Person // SELECT * FROM persons @@ -2707,15 +2715,29 @@ Feed [requests](#requests) with SQL expressions built from your Swift code: - `MATCH` - The full-text MATCH operator is available with the `match` Swift method: + The full-text MATCH operator is available through [FTS3Pattern](#fts3pattern) (for FTS3 and FTS4 tables) and [FTS5Pattern](#fts5pattern) (for FTS5): + + FTS3 and FTS4: ```swift + let pattern = FTS3Pattern(matchingAllTokensIn: "SQLite database") + + // SELECT * FROM documents WHERE documents MATCH 'sqlite AND database' + Document.matching(pattern) + // // SELECT * FROM documents WHERE content MATCH 'sqlite AND database' let pattern = FTS3Pattern(matchingAllTokensIn: "SQLite database") Document.filter(contentColumn.match(pattern)) ``` - [FTS3Pattern](#fts3pattern) targets FTS3 and FTS4 tables, while [FTS5Pattern](#fts5pattern) targets FTS5. + FTS5: + + ```swift + let pattern = FTS5Pattern(matchingAllTokensIn: "SQLite database") + + // SELECT * FROM documents WHERE documents MATCH 'sqlite AND database' + Document.matching(pattern) + ``` ### SQL Functions @@ -3256,9 +3278,13 @@ let documents = Document.fetchAll(db, arguments: [pattern]) ``` -Use them in the [query interface](#the-query-interface) with the `match` method: +Use them in the [query interface](#the-query-interface): ```swift +// Search in all columns +let documents = Document.matching(pattern).fetchAll(db) + +// Search in a specific column: let documents = Document.filter(Column("content").match(pattern)).fetchAll(db) ``` @@ -3448,10 +3474,10 @@ let documents = Document.fetchAll(db, arguments: [pattern]) ``` -Use them in the [query interface](#the-query-interface) with the `match` method: +Use them in the [query interface](#the-query-interface): ```swift -let documents = Document.filter(Column("documents").match(pattern)).fetchAll(db) +let documents = Document.matching(pattern).fetchAll(db) ``` diff --git a/Tests/Public/FTS/FTS3RecordTests.swift b/Tests/Public/FTS/FTS3RecordTests.swift index c6b43c32d..7d3eb3284 100644 --- a/Tests/Public/FTS/FTS3RecordTests.swift +++ b/Tests/Public/FTS/FTS3RecordTests.swift @@ -92,8 +92,10 @@ class FTS3RecordTests: GRDBTestCase { } let pattern = FTS3Pattern(matchingAllTokensIn: "Herman Melville")! - let request = Book.filter(Column("books").match(pattern)) - XCTAssertEqual(request.fetchCount(db), 1) + XCTAssertEqual(Book.matching(pattern).fetchCount(db), 1) + XCTAssertEqual(Book.filter(Column("books").match(pattern)).fetchCount(db), 1) + XCTAssertEqual(Book.filter(Column("author").match(pattern)).fetchCount(db), 1) + XCTAssertEqual(Book.filter(Column("title").match(pattern)).fetchCount(db), 0) } } } diff --git a/Tests/Public/FTS/FTS5RecordTests.swift b/Tests/Public/FTS/FTS5RecordTests.swift index 6f962d68d..fef0e1c36 100644 --- a/Tests/Public/FTS/FTS5RecordTests.swift +++ b/Tests/Public/FTS/FTS5RecordTests.swift @@ -92,8 +92,7 @@ class FTS5RecordTests: GRDBTestCase { } let pattern = FTS5Pattern(matchingAllTokensIn: "Herman Melville")! - let request = Book.filter(Column("books").match(pattern)) - XCTAssertEqual(request.fetchCount(db), 1) + XCTAssertEqual(Book.matching(pattern).fetchCount(db), 1) } } }