mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
matching() and match() methods accept nil FTS patterns
This commit is contained in:
1 parent
ad8fe7923a
commit
7630642c56
7 files changed
+97
-11
No files matched your search
@@ -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
|
||||
|
||||
@@ -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<T> {
|
||||
///
|
||||
/// If the search pattern is nil, the request does not match any
|
||||
/// database row.
|
||||
public func matching(_ pattern: FTS3Pattern?) -> QueryInterfaceRequest<T> {
|
||||
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<Self> {
|
||||
///
|
||||
/// If the search pattern is nil, the request does not match any
|
||||
/// database row.
|
||||
public static func matching(_ pattern: FTS3Pattern?) -> QueryInterfaceRequest<Self> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -112,10 +112,17 @@
|
||||
/// // SELECT * FROM books WHERE books MATCH '...'
|
||||
/// var request = Book.all()
|
||||
/// request = request.matching(pattern)
|
||||
public func matching(_ pattern: FTS5Pattern) -> QueryInterfaceRequest<T> {
|
||||
///
|
||||
/// If the search pattern is nil, the request does not match any
|
||||
/// database row.
|
||||
public func matching(_ pattern: FTS5Pattern?) -> QueryInterfaceRequest<T> {
|
||||
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<Self> {
|
||||
///
|
||||
/// If the search pattern is nil, the request does not match any
|
||||
/// database row.
|
||||
public static func matching(_ pattern: FTS5Pattern?) -> QueryInterfaceRequest<Self> {
|
||||
return all().matching(pattern)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
```
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user