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
83104769ef
commit
e05a64f1cb
5 files changed
+39
-42
No files matched your search
@@ -201,32 +201,28 @@ extension Association {
|
||||
/// Creates an association that includes another one. The columns of the
|
||||
/// associated record are selected. The returned association does not
|
||||
/// require that the associated database table contains a matching row.
|
||||
public func including<A: Association>(optional association: A) -> Self where A.LeftAssociated == RightAssociated
|
||||
{
|
||||
public func including<A: Association>(optional association: A) -> Self where A.LeftAssociated == RightAssociated {
|
||||
return mapRequest { $0.chain(.optional, association) }
|
||||
}
|
||||
|
||||
/// Creates an association that includes another one. The columns of the
|
||||
/// associated record are selected. The returned association requires
|
||||
/// that the associated database table contains a matching row.
|
||||
public func including<A: Association>(required association: A) -> Self where A.LeftAssociated == RightAssociated
|
||||
{
|
||||
public func including<A: Association>(required association: A) -> Self where A.LeftAssociated == RightAssociated {
|
||||
return mapRequest { $0.chain(.required, association) }
|
||||
}
|
||||
|
||||
/// Creates an association that joins another one. The columns of the
|
||||
/// associated record are not selected. The returned association does not
|
||||
/// require that the associated database table contains a matching row.
|
||||
public func joining<A: Association>(optional association: A) -> Self where A.LeftAssociated == RightAssociated
|
||||
{
|
||||
public func joining<A: Association>(optional association: A) -> Self where A.LeftAssociated == RightAssociated {
|
||||
return mapRequest { $0.chain(.optional, association.select([])) }
|
||||
}
|
||||
|
||||
/// Creates an association that joins another one. The columns of the
|
||||
/// associated record are not selected. The returned association requires
|
||||
/// that the associated database table contains a matching row.
|
||||
public func joining<A: Association>(required association: A) -> Self where A.LeftAssociated == RightAssociated
|
||||
{
|
||||
public func joining<A: Association>(required association: A) -> Self where A.LeftAssociated == RightAssociated {
|
||||
return mapRequest { $0.chain(.required, association.select([])) }
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,12 @@ struct AssociationQuery {
|
||||
if let qualifier = qualifier {
|
||||
qualifiers.append(qualifier)
|
||||
}
|
||||
qualifiers.append(contentsOf: joins.flatMap { $0.rightQuery.allQualifiers })
|
||||
return qualifiers
|
||||
return joins.reduce(into: qualifiers) {
|
||||
$0.append(contentsOf: $1.query.allQualifiers)
|
||||
}
|
||||
}
|
||||
|
||||
var preparedQuery: AssociationQuery {
|
||||
var qualifiedQuery: AssociationQuery {
|
||||
var qualifier = SQLTableQualifier(tableName: source.tableName!)
|
||||
|
||||
var query = self
|
||||
@@ -28,7 +29,7 @@ struct AssociationQuery {
|
||||
query.ordering = ordering.qualified(with: qualifier)
|
||||
query.joins = joins.map {
|
||||
var join = $0
|
||||
join.rightQuery = join.rightQuery.preparedQuery
|
||||
join.query = join.query.qualifiedQuery
|
||||
return join
|
||||
}
|
||||
return query
|
||||
@@ -36,13 +37,13 @@ struct AssociationQuery {
|
||||
|
||||
var completeSelection: [SQLSelectable] {
|
||||
return joins.reduce(into: selection) {
|
||||
$0.append(contentsOf: $1.rightQuery.completeSelection)
|
||||
$0.append(contentsOf: $1.query.completeSelection)
|
||||
}
|
||||
}
|
||||
|
||||
var completeOrdering: QueryOrdering {
|
||||
return joins.reduce(ordering) {
|
||||
$0.appending($1.rightQuery.completeOrdering)
|
||||
$0.appending($1.query.completeOrdering)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +55,7 @@ struct AssociationQuery {
|
||||
var endIndex = startIndex + selectionWidth
|
||||
var scopes: [String: RowAdapter] = [:]
|
||||
for join in joins {
|
||||
if let (joinAdapter, joinEndIndex) = try join.rightQuery.rowAdapter(db, fromIndex: endIndex, forKeyPath: keyPath + [join.key]) {
|
||||
if let (joinAdapter, joinEndIndex) = try join.query.rowAdapter(db, fromIndex: endIndex, forKeyPath: keyPath + [join.key]) {
|
||||
GRDBPrecondition(scopes[join.key] == nil, "The association key \"\((keyPath + [join.key]).joined(separator: "."))\" is ambiguous. Use the Association.forKey(_:) method is order to disambiguate.")
|
||||
scopes[join.key] = joinAdapter
|
||||
endIndex = joinEndIndex
|
||||
|
||||
@@ -48,7 +48,7 @@ extension AssociationRequest {
|
||||
{
|
||||
let join = AssociationJoin(
|
||||
op: chainOp,
|
||||
rightQuery: association.request.query,
|
||||
query: association.request.query,
|
||||
key: association.key,
|
||||
associationMapping: association.associationMapping)
|
||||
return AssociationRequest(query: query.joining(join))
|
||||
|
||||
@@ -149,13 +149,13 @@ struct QueryInterfaceQuery {
|
||||
|
||||
var completeSelection: [SQLSelectable] {
|
||||
return joins.reduce(into: selection) {
|
||||
$0.append(contentsOf: $1.rightQuery.completeSelection)
|
||||
$0.append(contentsOf: $1.query.completeSelection)
|
||||
}
|
||||
}
|
||||
|
||||
var completeOrdering: QueryOrdering {
|
||||
return joins.reduce(ordering) {
|
||||
$0.appending($1.rightQuery.completeOrdering)
|
||||
$0.appending($1.query.completeOrdering)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,16 +165,17 @@ struct QueryInterfaceQuery {
|
||||
|
||||
var allQualifiers: [SQLTableQualifier] {
|
||||
var qualifiers: [SQLTableQualifier] = []
|
||||
if let qualifier = source.qualifier {
|
||||
if let qualifier = qualifier {
|
||||
qualifiers.append(qualifier)
|
||||
}
|
||||
qualifiers.append(contentsOf: joins.flatMap { $0.rightQuery.allQualifiers })
|
||||
return qualifiers
|
||||
return joins.reduce(into: qualifiers) {
|
||||
$0.append(contentsOf: $1.query.allQualifiers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceQuery {
|
||||
private var preparedQuery: QueryInterfaceQuery {
|
||||
private var qualifiedQuery: QueryInterfaceQuery {
|
||||
var query = self
|
||||
|
||||
if !joins.isEmpty {
|
||||
@@ -192,7 +193,7 @@ extension QueryInterfaceQuery {
|
||||
|
||||
query.joins = query.joins.map {
|
||||
var join = $0
|
||||
join.rightQuery = join.rightQuery.preparedQuery
|
||||
join.query = join.query.qualifiedQuery
|
||||
return join
|
||||
}
|
||||
|
||||
@@ -200,16 +201,16 @@ extension QueryInterfaceQuery {
|
||||
return query
|
||||
}
|
||||
|
||||
/// precondition: self is the result of preparedQuery
|
||||
/// precondition: self is the result of qualifiedQuery
|
||||
private func makeSelectStatement(_ db: Database) throws -> SelectStatement {
|
||||
var arguments: StatementArguments? = StatementArguments()
|
||||
let sql = try preparedQuery.sql(db, &arguments)
|
||||
let sql = try self.sql(db, &arguments)
|
||||
let statement = try db.makeSelectStatement(sql)
|
||||
try statement.setArgumentsWithValidation(arguments!)
|
||||
return statement
|
||||
}
|
||||
|
||||
/// precondition: self is the result of preparedQuery
|
||||
/// precondition: self is the result of qualifiedQuery
|
||||
private func rowAdapter(_ db: Database) throws -> RowAdapter? {
|
||||
if joins.isEmpty {
|
||||
return nil
|
||||
@@ -222,7 +223,7 @@ extension QueryInterfaceQuery {
|
||||
var endIndex = selectionWidth
|
||||
var scopes: [String: RowAdapter] = [:]
|
||||
for join in joins {
|
||||
if let (joinAdapter, joinEndIndex) = try join.rightQuery.rowAdapter(db, fromIndex: endIndex, forKeyPath: [join.key]) {
|
||||
if let (joinAdapter, joinEndIndex) = try join.query.rowAdapter(db, fromIndex: endIndex, forKeyPath: [join.key]) {
|
||||
GRDBPrecondition(scopes[join.key] == nil, "The association key \"\(join.key)\" is ambiguous. Use the Association.forKey(_:) method is order to disambiguate.")
|
||||
scopes[join.key] = joinAdapter
|
||||
endIndex = joinEndIndex
|
||||
@@ -238,7 +239,7 @@ extension QueryInterfaceQuery {
|
||||
}
|
||||
|
||||
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
||||
let query = preparedQuery
|
||||
let query = qualifiedQuery
|
||||
return try (query.makeSelectStatement(db), query.rowAdapter(db))
|
||||
}
|
||||
|
||||
@@ -249,7 +250,7 @@ extension QueryInterfaceQuery {
|
||||
|
||||
/// The database region that the request looks into.
|
||||
func fetchedRegion(_ db: Database) throws -> DatabaseRegion {
|
||||
let statement = try preparedQuery.makeSelectStatement(db)
|
||||
let statement = try qualifiedQuery.makeSelectStatement(db)
|
||||
let region = statement.fetchedRegion
|
||||
|
||||
// Can we intersect the region with rowIds?
|
||||
@@ -421,7 +422,7 @@ extension QueryInterfaceQuery {
|
||||
|
||||
struct AssociationJoin {
|
||||
var op: AssociationChainOperator
|
||||
var rightQuery: AssociationQuery
|
||||
var query: AssociationQuery
|
||||
var key: String
|
||||
var associationMapping: (Database) throws -> AssociationMapping
|
||||
|
||||
@@ -440,20 +441,19 @@ struct AssociationJoin {
|
||||
sql += "JOIN"
|
||||
}
|
||||
|
||||
let rightQualifier = rightQuery.qualifier!
|
||||
|
||||
sql += try " " + rightQuery.source.sourceSQL(db, &arguments)
|
||||
sql += try " " + query.source.sourceSQL(db, &arguments)
|
||||
|
||||
let qualifier = query.qualifier!
|
||||
let filters = try [
|
||||
associationMapping(db)(leftQualifier, rightQualifier),
|
||||
rightQuery.filterPromise.resolve(db)
|
||||
associationMapping(db)(leftQualifier, qualifier),
|
||||
query.filterPromise.resolve(db)
|
||||
].compactMap { $0 }
|
||||
if !filters.isEmpty {
|
||||
sql += " ON " + filters.joined(operator: .and).expressionSQL(&arguments)
|
||||
}
|
||||
|
||||
for join in rightQuery.joins {
|
||||
sql += try " " + join.joinSQL(db, &arguments, leftQualifier: rightQualifier, isRequiredAllowed: isRequiredAllowed)
|
||||
for join in query.joins {
|
||||
sql += try " " + join.joinSQL(db, &arguments, leftQualifier: qualifier, isRequiredAllowed: isRequiredAllowed)
|
||||
}
|
||||
|
||||
return sql
|
||||
|
||||
@@ -5,7 +5,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
|
||||
{
|
||||
let join = AssociationJoin(
|
||||
op: chainOp,
|
||||
rightQuery: association.request.query,
|
||||
query: association.request.query,
|
||||
key: association.key,
|
||||
associationMapping: association.associationMapping)
|
||||
return QueryInterfaceRequest(query: query.joining(join))
|
||||
@@ -56,16 +56,16 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
|
||||
|
||||
extension Association where LeftAssociated: MutablePersistableRecord {
|
||||
func request(from record: LeftAssociated) -> QueryInterfaceRequest<RightAssociated> {
|
||||
var query = request.query.preparedQuery // make sure query has a qualifier
|
||||
let qualifier = query.source.qualifier!
|
||||
var query = request.query.qualifiedQuery // make sure query has a qualifier
|
||||
let qualifier = query.qualifier!
|
||||
let recordQualifier = SQLTableQualifier.init(tableName: LeftAssociated.databaseTableName)
|
||||
|
||||
|
||||
query = query.filter { db in
|
||||
let associationMapping = try self.associationMapping(db)
|
||||
guard let filter = associationMapping(recordQualifier, qualifier) else {
|
||||
fatalError("Can't request from record without association mapping")
|
||||
}
|
||||
let container = PersistenceContainer(record)
|
||||
let container = PersistenceContainer(record) // support for record classes: late construction of container
|
||||
return filter.resolvedExpression(inContext: [recordQualifier: container])
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user