mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Cleanup
This commit is contained in:
1 parent
a3189f79b6
commit
8e2cdeebc3
3 files changed
+23
-22
No files matched your search
@@ -280,19 +280,18 @@ extension Association where LeftAssociated: MutablePersistableRecord {
|
||||
/// let team: Team = ...
|
||||
/// let players = try team.players.fetchAll(db) // [Player]
|
||||
func request(from record: LeftAssociated) -> QueryInterfaceRequest<RightAssociated> {
|
||||
// Turn `JOIN associated ON associated.recordId = record.id`
|
||||
// into `FROM associated WHERE associated.recordId = 1`
|
||||
let associationAlias = TableAlias()
|
||||
let query = request.query.qualified(with: associationAlias)
|
||||
let recordAlias = TableAlias(tableName: LeftAssociated.databaseTableName)
|
||||
|
||||
// Turn `associated.recordId = record.id` into `associated.recordId = 1`
|
||||
let resolvedQuery = query.filter { db in
|
||||
guard let joinCondition = try self.joinCondition(db)(recordAlias, associationAlias) else {
|
||||
fatalError("Can't request from record without join condition")
|
||||
}
|
||||
let container = PersistenceContainer(record) // support for record classes: late construction of container
|
||||
return joinCondition.resolvedExpression(inContext: [recordAlias: container])
|
||||
return QueryInterfaceRequest(request)
|
||||
.aliased(associationAlias)
|
||||
.filter { db in
|
||||
guard let joinCondition = try self.joinCondition(db)(recordAlias, associationAlias) else {
|
||||
fatalError("Can't request from record without join condition")
|
||||
}
|
||||
let container = PersistenceContainer(record) // support for record classes: late construction of container
|
||||
return joinCondition.resolvedExpression(inContext: [recordAlias: container])
|
||||
}
|
||||
|
||||
return QueryInterfaceRequest(query: QueryInterfaceQuery(resolvedQuery))
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,15 @@ struct QueryInterfaceQuery {
|
||||
self.joins = joins
|
||||
}
|
||||
|
||||
init(_ query: AssociationQuery) {
|
||||
self.init(
|
||||
source: query.source,
|
||||
selection: query.selection,
|
||||
filterPromise: query.filterPromise,
|
||||
ordering: query.ordering,
|
||||
joins: query.joins)
|
||||
}
|
||||
|
||||
func sql(_ db: Database, _ context: inout SQLGenerationContext) throws -> String {
|
||||
var sql = "SELECT"
|
||||
|
||||
@@ -393,17 +402,6 @@ extension QueryInterfaceQuery {
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceQuery {
|
||||
init(_ query: AssociationQuery) {
|
||||
self.init(
|
||||
source: query.source,
|
||||
selection: query.selection,
|
||||
filterPromise: query.filterPromise,
|
||||
ordering: query.ordering,
|
||||
joins: query.joins)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AssociationJoin
|
||||
|
||||
/// Not to be mismatched with SQL join operators (inner join, left join).
|
||||
|
||||
@@ -7,6 +7,10 @@ public struct QueryInterfaceRequest<T> {
|
||||
init(query: QueryInterfaceQuery) {
|
||||
self.query = query
|
||||
}
|
||||
|
||||
init(_ request: AssociationRequest<T>) {
|
||||
self.query = QueryInterfaceQuery(request.query)
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest : FetchRequest {
|
||||
|
||||
Reference in new issue
Block a user