diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a2c5c629..5982fe927 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,18 @@ Release Notes ============= +## Next Version + +**New** + +- Full-text matching methods accept nil search patterns: + + ```swift + let pattern = FTS3SearchPattern(matchingAllTokensIn: "") // nil + let documents = Document.matching(pattern).fetchAll(db) // Empty array + ``` + + ## 0.87.0 Released October 12, 2016 diff --git a/GRDB/FTS/FTS3Pattern.swift b/GRDB/FTS/FTS3Pattern.swift index dabdb30ac..74c4a59dd 100644 --- a/GRDB/FTS/FTS3Pattern.swift +++ b/GRDB/FTS/FTS3Pattern.swift @@ -96,10 +96,13 @@ extension QueryInterfaceRequest { /// // SELECT * FROM books WHERE books MATCH '...' /// var request = Book.all() /// request = request.matching(pattern) - public func matching(_ pattern: FTS3Pattern) -> QueryInterfaceRequest { + /// + /// If the search pattern is nil, the request does not match any + /// database row. + public func matching(_ pattern: FTS3Pattern?) -> QueryInterfaceRequest { switch query.source { case .table(let name, let alias)?: - return filter(SQLExpressionBinary(.match, Column(alias ?? name), pattern)) + return filter(SQLExpressionBinary(.match, Column(alias ?? name), pattern ?? DatabaseValue.null)) default: fatalError("fts3 match requires a table") } @@ -120,7 +123,10 @@ extension TableMapping { /// /// // SELECT *, rowid FROM books WHERE books MATCH '...' /// var request = Book.matching(pattern) - public static func matching(_ pattern: FTS3Pattern) -> QueryInterfaceRequest { + /// + /// If the search pattern is nil, the request does not match any + /// database row. + public static func matching(_ pattern: FTS3Pattern?) -> QueryInterfaceRequest { return all().matching(pattern) } } @@ -129,8 +135,11 @@ extension Column { /// A matching SQL expression with the `MATCH` SQL operator. /// /// // content MATCH '...' - /// Column("content") MATCH '...' - public func match(_ pattern: FTS3Pattern) -> SQLExpression { - return SQLExpressionBinary(.match, self, pattern) + /// Column("content").match(pattern) + /// + /// If the search pattern is nil, SQLite will evaluate the expression + /// to false. + public func match(_ pattern: FTS3Pattern?) -> SQLExpression { + return SQLExpressionBinary(.match, self, pattern ?? DatabaseValue.null) } } diff --git a/GRDB/FTS/FTS5Pattern.swift b/GRDB/FTS/FTS5Pattern.swift index 0d2616c9b..d123f638a 100644 --- a/GRDB/FTS/FTS5Pattern.swift +++ b/GRDB/FTS/FTS5Pattern.swift @@ -112,10 +112,17 @@ /// // SELECT * FROM books WHERE books MATCH '...' /// var request = Book.all() /// request = request.matching(pattern) - public func matching(_ pattern: FTS5Pattern) -> QueryInterfaceRequest { + /// + /// If the search pattern is nil, the request does not match any + /// database row. + public func matching(_ pattern: FTS5Pattern?) -> QueryInterfaceRequest { switch query.source { case .table(let name, let alias)?: - return filter(SQLExpressionBinary(.match, Column(alias ?? name), pattern)) + if let pattern = pattern { + return filter(SQLExpressionBinary(.match, Column(alias ?? name), pattern)) + } else { + return filter(false) + } default: fatalError("fts5 match requires a table") } @@ -136,7 +143,10 @@ /// /// // SELECT *, rowid FROM books WHERE books MATCH '...' /// var request = Book.matching(pattern) - public static func matching(_ pattern: FTS5Pattern) -> QueryInterfaceRequest { + /// + /// If the search pattern is nil, the request does not match any + /// database row. + public static func matching(_ pattern: FTS5Pattern?) -> QueryInterfaceRequest { return all().matching(pattern) } } diff --git a/README.md b/README.md index 19febc8f3..1e58f5b8c 100644 --- a/README.md +++ b/README.md @@ -2654,6 +2654,8 @@ You can now build requests with the following methods: `all`, `select`, `distinc let pattern = FTS3Pattern(matchingAllTokensIn: "SQLite database") Document.matching(pattern) ``` + + When the pattern is nil, no row will match. - `group(expression, ...)` groups rows. @@ -2861,9 +2863,8 @@ Feed [requests](#requests) with SQL expressions built from your Swift code: // SELECT * FROM documents WHERE documents MATCH 'sqlite database' Document.matching(pattern) - // + // SELECT * FROM documents WHERE content MATCH 'sqlite database' - let pattern = FTS3Pattern(matchingAllTokensIn: "SQLite database") Document.filter(contentColumn.match(pattern)) ``` diff --git a/Tests/Public/FTS/FTS3RecordTests.swift b/Tests/Public/FTS/FTS3RecordTests.swift index ab8e012d5..c993e0688 100644 --- a/Tests/Public/FTS/FTS3RecordTests.swift +++ b/Tests/Public/FTS/FTS3RecordTests.swift @@ -102,6 +102,25 @@ class FTS3RecordTests: GRDBTestCase { } } + func testMatchNil() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + do { + var book = Book(id: nil, title: "Moby Dick", author: "Herman Melville", body: "Call me Ishmael.") + try book.insert(db) + } + + let pattern = FTS3Pattern(matchingAllTokensIn: "") + XCTAssertTrue(pattern == nil) + XCTAssertEqual(Book.matching(pattern).fetchCount(db), 0) + XCTAssertEqual(Book.filter(Column("books").match(pattern)).fetchCount(db), 0) + XCTAssertEqual(Book.filter(Column("author").match(pattern)).fetchCount(db), 0) + XCTAssertEqual(Book.filter(Column("title").match(pattern)).fetchCount(db), 0) + } + } + } + func testFetchCount() { assertNoError { let dbQueue = try makeDatabaseQueue() diff --git a/Tests/Public/FTS/FTS4RecordTests.swift b/Tests/Public/FTS/FTS4RecordTests.swift index 201d0cb91..859cd6323 100644 --- a/Tests/Public/FTS/FTS4RecordTests.swift +++ b/Tests/Public/FTS/FTS4RecordTests.swift @@ -102,6 +102,25 @@ class FTS4RecordTests: GRDBTestCase { } } + func testMatchNil() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + do { + var book = Book(id: nil, title: "Moby Dick", author: "Herman Melville", body: "Call me Ishmael.") + try book.insert(db) + } + + let pattern = FTS3Pattern(matchingAllTokensIn: "") + XCTAssertTrue(pattern == nil) + XCTAssertEqual(Book.matching(pattern).fetchCount(db), 0) + XCTAssertEqual(Book.filter(Column("books").match(pattern)).fetchCount(db), 0) + XCTAssertEqual(Book.filter(Column("author").match(pattern)).fetchCount(db), 0) + XCTAssertEqual(Book.filter(Column("title").match(pattern)).fetchCount(db), 0) + } + } + } + func testFetchCount() { assertNoError { let dbQueue = try makeDatabaseQueue() diff --git a/Tests/Public/FTS/FTS5RecordTests.swift b/Tests/Public/FTS/FTS5RecordTests.swift index 1ec070573..9abc75777 100644 --- a/Tests/Public/FTS/FTS5RecordTests.swift +++ b/Tests/Public/FTS/FTS5RecordTests.swift @@ -99,6 +99,22 @@ class FTS5RecordTests: GRDBTestCase { } } + func testMatchNil() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + do { + var book = Book(id: nil, title: "Moby Dick", author: "Herman Melville", body: "Call me Ishmael.") + try book.insert(db) + } + + let pattern = FTS5Pattern(matchingAllTokensIn: "") + XCTAssertTrue(pattern == nil) + XCTAssertEqual(Book.matching(pattern).fetchCount(db), 0) + } + } + } + func testFetchCount() { assertNoError { let dbQueue = try makeDatabaseQueue()