Hide _SQLAssociation

This commit is contained in:
Gwendal Roué committed 2020-07-05 16:21:28 +02:00
1 parent ed7faa3714
commit 68785d1c2d
13 files changed
+56 -56

No files matched your search

+1 -1
View File
@@ -86,7 +86,7 @@ public final class DatabasePool: DatabaseWriter {
var readerCount = 0
readerPool = Pool(maximumCount: configuration.maximumReaderCount, makeElement: { [unowned self] in
readerCount += 1 // protected by pool (TODO: documented this protection behavior)
readerCount += 1 // protected by pool (TODO: document this protection behavior)
let reader = try SerializedDatabase(
path: path,
configuration: readerConfiguration,
@@ -4,8 +4,8 @@ import Foundation
///
/// :nodoc:
public protocol _Association {
init(sqlAssociation: SQLAssociation)
var _sqlAssociation: SQLAssociation { get }
init(sqlAssociation: _SQLAssociation)
var _sqlAssociation: _SQLAssociation { get }
}
/// The base protocol for all associations that define a connection between two
@@ -46,27 +46,27 @@ public protocol Association: _Association, DerivableRequest {
extension Association {
/// :nodoc:
public func _including(all association: SQLAssociation) -> Self {
public func _including(all association: _SQLAssociation) -> Self {
mapDestinationRelation { $0._including(all: association) }
}
/// :nodoc:
public func _including(optional association: SQLAssociation) -> Self {
public func _including(optional association: _SQLAssociation) -> Self {
mapDestinationRelation { $0._including(optional: association) }
}
/// :nodoc:
public func _including(required association: SQLAssociation) -> Self {
public func _including(required association: _SQLAssociation) -> Self {
mapDestinationRelation { $0._including(required: association) }
}
/// :nodoc:
public func _joining(optional association: SQLAssociation) -> Self {
public func _joining(optional association: _SQLAssociation) -> Self {
mapDestinationRelation { $0._joining(optional: association) }
}
/// :nodoc:
public func _joining(required association: SQLAssociation) -> Self {
public func _joining(required association: _SQLAssociation) -> Self {
mapDestinationRelation { $0._joining(required: association) }
}
}
@@ -66,10 +66,10 @@ public struct BelongsToAssociation<Origin: TableRecord, Destination: TableRecord
public typealias RowDecoder = Destination
/// :nodoc:
public var _sqlAssociation: SQLAssociation
public var _sqlAssociation: _SQLAssociation
/// :nodoc:
public init(sqlAssociation: SQLAssociation) {
public init(sqlAssociation: _SQLAssociation) {
self._sqlAssociation = sqlAssociation
}
@@ -93,7 +93,7 @@ public struct BelongsToAssociation<Origin: TableRecord, Destination: TableRecord
associationKey = .inflected(Destination.databaseTableName)
}
_sqlAssociation = SQLAssociation(
_sqlAssociation = _SQLAssociation(
key: associationKey,
condition: condition,
relation: Destination.relationForAll,
@@ -66,10 +66,10 @@ public struct HasManyAssociation<Origin: TableRecord, Destination: TableRecord>:
public typealias RowDecoder = Destination
/// :nodoc:
public var _sqlAssociation: SQLAssociation
public var _sqlAssociation: _SQLAssociation
/// :nodoc:
public init(sqlAssociation: SQLAssociation) {
public init(sqlAssociation: _SQLAssociation) {
self._sqlAssociation = sqlAssociation
}
@@ -93,7 +93,7 @@ public struct HasManyAssociation<Origin: TableRecord, Destination: TableRecord>:
associationKey = .inflected(Destination.databaseTableName)
}
_sqlAssociation = SQLAssociation(
_sqlAssociation = _SQLAssociation(
key: associationKey,
condition: condition,
relation: Destination.relationForAll,
@@ -49,10 +49,10 @@ public struct HasManyThroughAssociation<Origin: TableRecord, Destination: TableR
public typealias RowDecoder = Destination
/// :nodoc:
public var _sqlAssociation: SQLAssociation
public var _sqlAssociation: _SQLAssociation
/// :nodoc:
public init(sqlAssociation: SQLAssociation) {
public init(sqlAssociation: _SQLAssociation) {
self._sqlAssociation = sqlAssociation
}
}
@@ -68,10 +68,10 @@ public struct HasOneAssociation<Origin: TableRecord, Destination: TableRecord>:
public typealias RowDecoder = Destination
/// :nodoc:
public var _sqlAssociation: SQLAssociation
public var _sqlAssociation: _SQLAssociation
/// :nodoc:
public init(sqlAssociation: SQLAssociation) {
public init(sqlAssociation: _SQLAssociation) {
self._sqlAssociation = sqlAssociation
}
@@ -95,7 +95,7 @@ public struct HasOneAssociation<Origin: TableRecord, Destination: TableRecord>:
associationKey = .inflected(Destination.databaseTableName)
}
_sqlAssociation = SQLAssociation(
_sqlAssociation = _SQLAssociation(
key: associationKey,
condition: condition,
relation: Destination.relationForAll,
@@ -29,10 +29,10 @@ public struct HasOneThroughAssociation<Origin: TableRecord, Destination: TableRe
public typealias RowDecoder = Destination
/// :nodoc:
public var _sqlAssociation: SQLAssociation
public var _sqlAssociation: _SQLAssociation
/// :nodoc:
public init(sqlAssociation: SQLAssociation) {
public init(sqlAssociation: _SQLAssociation) {
self._sqlAssociation = sqlAssociation
}
}
@@ -264,27 +264,27 @@ extension QueryInterfaceRequest: AggregatingRequest {
extension QueryInterfaceRequest: _JoinableRequest {
/// :nodoc:
public func _including(all association: SQLAssociation) -> QueryInterfaceRequest {
public func _including(all association: _SQLAssociation) -> QueryInterfaceRequest {
map(\.query) { $0._including(all: association) }
}
/// :nodoc:
public func _including(optional association: SQLAssociation) -> QueryInterfaceRequest {
public func _including(optional association: _SQLAssociation) -> QueryInterfaceRequest {
map(\.query) { $0._including(optional: association) }
}
/// :nodoc:
public func _including(required association: SQLAssociation) -> QueryInterfaceRequest {
public func _including(required association: _SQLAssociation) -> QueryInterfaceRequest {
map(\.query) { $0._including(required: association) }
}
/// :nodoc:
public func _joining(optional association: SQLAssociation) -> QueryInterfaceRequest {
public func _joining(optional association: _SQLAssociation) -> QueryInterfaceRequest {
map(\.query) { $0._joining(optional: association) }
}
/// :nodoc:
public func _joining(required association: SQLAssociation) -> QueryInterfaceRequest {
public func _joining(required association: _SQLAssociation) -> QueryInterfaceRequest {
map(\.query) { $0._joining(required: association) }
}
}
@@ -467,7 +467,7 @@ extension QueryInterfaceRequest where RowDecoder: MutablePersistableRecord {
// MARK: - Eager loading of hasMany associations
/// Append rows from prefetched associations into the argument rows.
private func prefetch(_ db: Database, associations: [SQLAssociation], in rows: [Row]) throws {
private func prefetch(_ db: Database, associations: [_SQLAssociation], in rows: [Row]) throws {
guard let firstRow = rows.first else {
// No rows -> no prefetch
return
@@ -536,7 +536,7 @@ private func prefetch(_ db: Database, associations: [SQLAssociation], in rows: [
}
// Returns the region of prefetched associations
func prefetchedRegion(_ db: Database, associations: [SQLAssociation]) throws -> DatabaseRegion {
func prefetchedRegion(_ db: Database, associations: [_SQLAssociation]) throws -> DatabaseRegion {
try associations.reduce(into: DatabaseRegion()) { (region, association) in
// CAUTION: Keep this code in sync with prefetch(_:associations:in:)
let prefetchedRegion: DatabaseRegion
@@ -498,27 +498,27 @@ extension OrderedRequest {
/// :nodoc:
public protocol _JoinableRequest {
/// Creates a request that prefetches an association.
func _including(all association: SQLAssociation) -> Self
func _including(all association: _SQLAssociation) -> Self
/// Creates a request that includes an association. The columns of the
/// associated record are selected. The returned request does not
/// require that the associated database table contains a matching row.
func _including(optional association: SQLAssociation) -> Self
func _including(optional association: _SQLAssociation) -> Self
/// Creates a request that includes an association. The columns of the
/// associated record are selected. The returned request requires
/// that the associated database table contains a matching row.
func _including(required association: SQLAssociation) -> Self
func _including(required association: _SQLAssociation) -> Self
/// Creates a request that joins an association. The columns of the
/// associated record are not selected. The returned request does not
/// require that the associated database table contains a matching row.
func _joining(optional association: SQLAssociation) -> Self
func _joining(optional association: _SQLAssociation) -> Self
/// Creates a request that joins an association. The columns of the
/// associated record are not selected. The returned request requires
/// that the associated database table contains a matching row.
func _joining(required association: SQLAssociation) -> Self
func _joining(required association: _SQLAssociation) -> Self
}
/// The protocol for all requests that can be associated.
+6 -6
View File
@@ -1,4 +1,4 @@
// MARK: - SQLAssociation
// MARK: - _SQLAssociation
/// An SQL association is a non-empty chain of steps which starts at the
/// "pivot" and ends on the "destination":
@@ -51,7 +51,7 @@
/// Origin.including(required: association)
///
/// :nodoc:
public /* TODO: internal */ struct SQLAssociation {
public struct _SQLAssociation {
// All steps, from pivot to destination. Never empty.
private(set) var steps: [SQLAssociationStep]
var keyPath: [String] { steps.map(\.keyName) }
@@ -91,8 +91,8 @@ public /* TODO: internal */ struct SQLAssociation {
}
/// Returns a new association
func through(_ other: SQLAssociation) -> Self {
SQLAssociation(steps: other.steps + steps)
func through(_ other: _SQLAssociation) -> Self {
_SQLAssociation(steps: other.steps + steps)
}
/// Returns the destination of the association, reversing the association
@@ -140,12 +140,12 @@ public /* TODO: internal */ struct SQLAssociation {
cardinality: .toOne)
})
.reversed()
let reversedAssociation = SQLAssociation(steps: Array(reversedSteps))
let reversedAssociation = _SQLAssociation(steps: Array(reversedSteps))
return destination.relation.appendingChild(for: reversedAssociation, kind: .oneRequired)
}
}
extension SQLAssociation: Refinable { }
extension _SQLAssociation: Refinable { }
struct SQLAssociationStep: Refinable {
var key: SQLAssociationKey
+5 -5
View File
@@ -73,23 +73,23 @@ extension SQLQuery: AggregatingRequest {
}
extension SQLQuery: _JoinableRequest {
func _including(all association: SQLAssociation) -> Self {
func _including(all association: _SQLAssociation) -> Self {
map(\.relation) { $0._including(all: association) }
}
func _including(optional association: SQLAssociation) -> Self {
func _including(optional association: _SQLAssociation) -> Self {
map(\.relation) { $0._including(optional: association) }
}
func _including(required association: SQLAssociation) -> Self {
func _including(required association: _SQLAssociation) -> Self {
map(\.relation) { $0._including(required: association) }
}
func _joining(optional association: SQLAssociation) -> Self {
func _joining(optional association: _SQLAssociation) -> Self {
map(\.relation) { $0._joining(optional: association) }
}
func _joining(required association: SQLAssociation) -> Self {
func _joining(required association: _SQLAssociation) -> Self {
map(\.relation) { $0._joining(required: association) }
}
}
+11 -11
View File
@@ -138,7 +138,7 @@ struct SQLRelation {
}
}
fileprivate func makeAssociationForKey(_ key: String) -> SQLAssociation {
fileprivate func makeAssociationForKey(_ key: String) -> _SQLAssociation {
let key = SQLAssociationKey.fixed(key)
let cardinality: SQLAssociationCardinality
@@ -149,7 +149,7 @@ struct SQLRelation {
cardinality = .toMany
}
return SQLAssociation(
return _SQLAssociation(
key: key,
condition: condition,
relation: relation,
@@ -166,8 +166,8 @@ struct SQLRelation {
var ordering: SQLRelation.Ordering = SQLRelation.Ordering()
var children: OrderedDictionary<String, Child> = [:]
var prefetchedAssociations: [SQLAssociation] {
children.flatMap { key, child -> [SQLAssociation] in
var prefetchedAssociations: [_SQLAssociation] {
children.flatMap { key, child -> [_SQLAssociation] in
switch child.kind {
case .allPrefetched:
return [child.makeAssociationForKey(key)]
@@ -273,7 +273,7 @@ extension SQLRelation {
/// HasMany in the above examples, but also for indirect associations such
/// as HasManyThrough, which have any number of pivot relations between the
/// origin and the destination.
func appendingChild(for association: SQLAssociation, kind: SQLRelation.Child.Kind) -> Self {
func appendingChild(for association: _SQLAssociation, kind: SQLRelation.Child.Kind) -> Self {
// Preserve association cardinality in intermediate steps of
// including(all:), and force desired cardinality otherwize
let isSingular = (kind == .allNotPrefetched)
@@ -315,7 +315,7 @@ extension SQLRelation {
//
// Let's recurse toward a direct join, by making a new association which
// ends on the last pivot, to which we join our destination:
var reducedAssociation = SQLAssociation(steps: Array(initialSteps))
var reducedAssociation = _SQLAssociation(steps: Array(initialSteps))
reducedAssociation.destination.relation = reducedAssociation.destination.relation
.select([]) // Intermediate steps are not prefetched
@@ -366,23 +366,23 @@ extension SQLRelation {
}
extension SQLRelation: _JoinableRequest {
func _including(all association: SQLAssociation) -> Self {
func _including(all association: _SQLAssociation) -> Self {
appendingChild(for: association, kind: .allPrefetched)
}
func _including(optional association: SQLAssociation) -> Self {
func _including(optional association: _SQLAssociation) -> Self {
appendingChild(for: association, kind: .oneOptional)
}
func _including(required association: SQLAssociation) -> Self {
func _including(required association: _SQLAssociation) -> Self {
appendingChild(for: association, kind: .oneRequired)
}
func _joining(optional association: SQLAssociation) -> Self {
func _joining(optional association: _SQLAssociation) -> Self {
appendingChild(for: association.map(\.destination.relation, { $0.select([]) }), kind: .oneOptional)
}
func _joining(required association: SQLAssociation) -> Self {
func _joining(required association: _SQLAssociation) -> Self {
appendingChild(for: association.map(\.destination.relation, { $0.select([]) }), kind: .oneRequired)
}
}
@@ -7,7 +7,7 @@ struct SQLQueryGenerator: Refinable {
private let limit: SQLLimit?
private let singleResult: Bool
// For database region
private let prefetchedAssociations: [SQLAssociation]
private let prefetchedAssociations: [_SQLAssociation]
/// Creates an SQL query generator.
///