mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Query interface matching() method
This commit is contained in:
1 parent
f864fba78b
commit
bdc25e4fb5
5 files changed
+84
-14
No files matched your search
@@ -88,6 +88,31 @@ extension FTS3Pattern : DatabaseValueConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest {
|
||||
|
||||
// MARK: Full Text Search
|
||||
|
||||
/// TODO
|
||||
public func matching(_ pattern: FTS3Pattern) -> QueryInterfaceRequest<T> {
|
||||
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<Self> {
|
||||
return all().matching(pattern)
|
||||
}
|
||||
}
|
||||
|
||||
extension Column {
|
||||
/// TODO
|
||||
public func match(_ pattern: FTS3Pattern) -> SQLExpression {
|
||||
|
||||
@@ -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<T> {
|
||||
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<Self> {
|
||||
return all().matching(pattern)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user