mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Bake the single result expectation right into filter(key:) methods
This better hides this flag inside SQLSelectQuery, and avoids explicit support from FetchableRecord and TableRecord
This commit is contained in:
1 parent
46791c8f74
commit
4716f91d8b
5 files changed
+48
-28
No files matched your search
@@ -44,9 +44,9 @@ extension QueryInterfaceRequest : FetchRequest {
|
||||
/// :nodoc:
|
||||
public func prepare(_ db: Database, forSingleResult singleResult: Bool) throws -> (SelectStatement, RowAdapter?) {
|
||||
var query = self.query
|
||||
|
||||
|
||||
// Optimize query by setting a limit of 1 when appropriate
|
||||
if singleResult && !query.expectsSingleRecord {
|
||||
if singleResult && !query.expectsSingleResult {
|
||||
query.limit = SQLLimit(limit: 1, offset: query.limit?.offset)
|
||||
}
|
||||
|
||||
@@ -191,6 +191,17 @@ extension QueryInterfaceRequest : DerivableRequest, AggregatingRequest {
|
||||
return mapQuery { $0.filter(predicate) }
|
||||
}
|
||||
|
||||
/// Creates a request which expects a single result.
|
||||
///
|
||||
/// It is unlikely you need to call this method. Its net effect is that
|
||||
/// QueryInterfaceRequest does not use any `LIMIT 1` sql clause when you
|
||||
/// call a `fetchOne` method.
|
||||
///
|
||||
/// :nodoc:
|
||||
public func expectingSingleResult() -> QueryInterfaceRequest {
|
||||
return mapQuery { $0.expectingSingleResult() }
|
||||
}
|
||||
|
||||
/// Creates a request grouped according to *expressions promise*.
|
||||
public func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> QueryInterfaceRequest {
|
||||
return mapQuery { $0.group(expressions) }
|
||||
@@ -468,7 +479,7 @@ extension TableRecord {
|
||||
/// all requests by the `TableRecord.databaseSelection` property, or
|
||||
/// for individual requests with the `TableRecord.select` method.
|
||||
public static func filter<PrimaryKeyType: DatabaseValueConvertible>(key: PrimaryKeyType?) -> QueryInterfaceRequest<Self> {
|
||||
return all().mapQuery { $0.expectingSingleRecord() }.filter(key: key)
|
||||
return all().filter(key: key)
|
||||
}
|
||||
|
||||
/// Creates a request with the provided primary key *predicate*.
|
||||
@@ -495,7 +506,7 @@ extension TableRecord {
|
||||
/// all requests by the `TableRecord.databaseSelection` property, or
|
||||
/// for individual requests with the `TableRecord.select` method.
|
||||
public static func filter(key: [String: DatabaseValueConvertible?]?) -> QueryInterfaceRequest<Self> {
|
||||
return all().mapQuery { $0.expectingSingleRecord() }.filter(key: key)
|
||||
return all().filter(key: key)
|
||||
}
|
||||
|
||||
/// Creates a request with the provided primary key *predicate*.
|
||||
|
||||
@@ -101,10 +101,23 @@ public protocol FilteredRequest {
|
||||
/// var request = Player.all()
|
||||
/// request = request.filter { db in true }
|
||||
func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> Self
|
||||
|
||||
/// Creates a request which expects a single result.
|
||||
///
|
||||
/// Requests expecting a single result may ignore the second parameter of
|
||||
/// the `FetchRequest.prepare(_:forSingleResult:)` method, in order to
|
||||
/// produce sharply tailored SQL.
|
||||
///
|
||||
/// This method has a default implementation which returns self.
|
||||
func expectingSingleResult() -> Self
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
extension FilteredRequest {
|
||||
public func expectingSingleResult() -> Self {
|
||||
return self
|
||||
}
|
||||
|
||||
/// Creates a request with the provided *predicate* added to the
|
||||
/// eventual set of already applied predicates.
|
||||
///
|
||||
@@ -198,19 +211,21 @@ extension TableRequest where Self: FilteredRequest {
|
||||
|
||||
/// Creates a request with the provided primary key *predicate*.
|
||||
public func filter<Sequence: Swift.Sequence>(keys: Sequence) -> Self where Sequence.Element: DatabaseValueConvertible {
|
||||
var request = self
|
||||
let keys = Array(keys)
|
||||
let makePredicate: (Column) -> SQLExpression
|
||||
switch keys.count {
|
||||
case 0:
|
||||
return none()
|
||||
case 1:
|
||||
request = request.expectingSingleResult()
|
||||
makePredicate = { $0 == keys[0] }
|
||||
default:
|
||||
makePredicate = { keys.contains($0) }
|
||||
}
|
||||
|
||||
let databaseTableName = self.databaseTableName
|
||||
return filter { db in
|
||||
return request.filter { db in
|
||||
let primaryKey = try db.primaryKey(databaseTableName)
|
||||
GRDBPrecondition(
|
||||
primaryKey.columns.count == 1,
|
||||
@@ -235,12 +250,18 @@ extension TableRequest where Self: FilteredRequest {
|
||||
/// When executed, this request raises a fatal error if there is no unique
|
||||
/// index on the key columns.
|
||||
public func filter(keys: [[String: DatabaseValueConvertible?]]) -> Self {
|
||||
guard !keys.isEmpty else {
|
||||
var request = self
|
||||
switch keys.count {
|
||||
case 0:
|
||||
return none()
|
||||
case 1:
|
||||
request = request.expectingSingleResult()
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
let databaseTableName = self.databaseTableName
|
||||
return filter { db in
|
||||
return request.filter { db in
|
||||
try keys
|
||||
.map { key in
|
||||
// Prevent filter(keys: [["foo": 1, "bar": 2]]) where
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
struct SQLSelectQuery {
|
||||
var relation: SQLRelation
|
||||
var isDistinct: Bool
|
||||
var expectsSingleRecord: Bool
|
||||
var expectsSingleResult: Bool
|
||||
var groupPromise: DatabasePromise<[SQLExpression]>?
|
||||
var havingExpression: SQLExpression?
|
||||
var limit: SQLLimit?
|
||||
@@ -12,14 +12,14 @@ struct SQLSelectQuery {
|
||||
init(
|
||||
relation: SQLRelation,
|
||||
isDistinct: Bool = false,
|
||||
expectsSingleRecord: Bool = false,
|
||||
expectsSingleResult: Bool = false,
|
||||
groupPromise: DatabasePromise<[SQLExpression]>? = nil,
|
||||
havingExpression: SQLExpression? = nil,
|
||||
limit: SQLLimit? = nil)
|
||||
{
|
||||
self.relation = relation
|
||||
self.isDistinct = isDistinct
|
||||
self.expectsSingleRecord = expectsSingleRecord
|
||||
self.expectsSingleResult = expectsSingleResult
|
||||
self.groupPromise = groupPromise
|
||||
self.havingExpression = havingExpression
|
||||
self.limit = limit
|
||||
@@ -41,9 +41,9 @@ extension SQLSelectQuery: SelectionRequest, FilteredRequest, OrderedRequest {
|
||||
return query
|
||||
}
|
||||
|
||||
func expectingSingleRecord() -> SQLSelectQuery {
|
||||
func expectingSingleResult() -> SQLSelectQuery {
|
||||
var query = self
|
||||
query.expectsSingleRecord = true
|
||||
query.expectsSingleResult = true
|
||||
return query
|
||||
}
|
||||
|
||||
|
||||
@@ -121,8 +121,7 @@ extension FetchableRecord where Self: TableRecord {
|
||||
// Avoid hitting the database
|
||||
return nil
|
||||
}
|
||||
// Fetch using `forSingleResult: false` to omit unnecessary "LIMIT 1"
|
||||
return try filter(key: key).fetchOne(db, forSingleResult: false)
|
||||
return try filter(key: key).fetchOne(db)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +186,6 @@ extension FetchableRecord where Self: TableRecord {
|
||||
// Avoid hitting the database
|
||||
return nil
|
||||
}
|
||||
// Fetch using `forSingleResult: false` to omit unnecessary "LIMIT 1"
|
||||
return try filter(key: key).fetchOne(db, forSingleResult: false)
|
||||
return try filter(key: key).fetchOne(db)
|
||||
}
|
||||
}
|
||||
@@ -313,12 +313,7 @@ extension FetchableRecord {
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
@inlinable
|
||||
public static func fetchOne<R: FetchRequest>(_ db: Database, _ request: R) throws -> Self? {
|
||||
return try Self.fetchOne(db, request, forSingleResult: true)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func fetchOne<R: FetchRequest>(_ db: Database, _ request: R, forSingleResult singleResult: Bool) throws -> Self? {
|
||||
let (statement, adapter) = try request.prepare(db, forSingleResult: singleResult)
|
||||
let (statement, adapter) = try request.prepare(db, forSingleResult: true)
|
||||
return try fetchOne(statement, adapter: adapter)
|
||||
}
|
||||
}
|
||||
@@ -373,12 +368,7 @@ extension FetchRequest where RowDecoder: FetchableRecord {
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
@inlinable
|
||||
public func fetchOne(_ db: Database) throws -> RowDecoder? {
|
||||
return try fetchOne(db, forSingleResult: true)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func fetchOne(_ db: Database, forSingleResult singleResult: Bool) throws -> RowDecoder? {
|
||||
return try RowDecoder.fetchOne(db, self, forSingleResult: singleResult)
|
||||
return try RowDecoder.fetchOne(db, self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user