This commit is contained in:
Gwendal Roué committed 2020-07-11 10:57:24 +02:00
1 parent 813950f832
commit ae41859bbb
5 files changed
+63 -62

No files matched your search

+5 -4
View File
@@ -877,10 +877,11 @@ extension DatabasePool: DatabaseReader {
// We can avoid this second fetch when SQLite is compiled with the
// SQLITE_ENABLE_SNAPSHOT option:
//
// 1. Perform the initial fetch in a DatabaseSnapshot. The snapshot
// acquires a lock that will prevent checkpointing until we get a writer
// access, so that we can reliably compare database versions with
// `sqlite3_snapshot`: https://www.sqlite.org/c3ref/snapshot.html.
// 1. Perform the initial fetch in a DatabaseSnapshot. Its long running
// transaction acquires a lock that will prevent checkpointing until we
// get a writer access, so that we can reliably compare database
// versions with `sqlite3_snapshot`:
// https://www.sqlite.org/c3ref/snapshot.html.
//
// 2. Get a writer access, and compare the versions of the initial
// snapshot, and the current state of the database: if versions are
@@ -475,9 +475,6 @@ private func prefetch(_ db: Database, associations: [_SQLAssociation], in rows:
// CAUTION: Keep this code in sync with prefetchedRegion(_:_:)
for association in associations {
let prefetchedGroups: [[DatabaseValue] : [Row]]
let groupingIndexes: [Int]
switch association.pivot.condition {
case let .foreignKey(request: foreignKeyRequest, originIsLeft: originIsLeft):
// Annotate prefetched rows with pivot columns, so that we can
@@ -516,21 +513,21 @@ private func prefetch(_ db: Database, associations: [_SQLAssociation], in rows:
})
.destinationRelation()
// Annotate with the pivot columns that allow grouping
.annotated(with: pivotColumns.map { pivotAlias[Column($0)].forKey("grdb_\($0)") })
.annotated(with: pivotColumns.map { pivotAlias[$0].forKey("grdb_\($0)") })
prefetchedGroups = try QueryInterfaceRequest<Row>(relation: prefetchedRelation)
let prefetchedGroups = try QueryInterfaceRequest<Row>(relation: prefetchedRelation)
.fetchAll(db)
.grouped(byDatabaseValuesOnColumns: pivotColumns.map { "grdb_\($0)" })
// TODO: can we remove those grdb_ columns from user's sight,
// now that grouping has been done?
groupingIndexes = firstRow.indexes(forColumns: pivotMapping.map(\.left))
}
for row in rows {
let groupingKey = groupingIndexes.map { row.impl.databaseValue(atUncheckedIndex: $0) }
let prefetchedRows = prefetchedGroups[groupingKey, default: []]
row.prefetchedRows.setRows(prefetchedRows, forKeyPath: association.keyPath)
let groupingIndexes = firstRow.indexes(forColumns: pivotMapping.map(\.left))
for row in rows {
let groupingKey = groupingIndexes.map { row.impl.databaseValue(atUncheckedIndex: $0) }
let prefetchedRows = prefetchedGroups[groupingKey, default: []]
row.prefetchedRows.setRows(prefetchedRows, forKeyPath: association.keyPath)
}
}
}
}
@@ -539,8 +536,6 @@ private func prefetch(_ db: Database, associations: [_SQLAssociation], in rows:
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
switch association.pivot.condition {
case let .foreignKey(request: foreignKeyRequest, originIsLeft: originIsLeft):
// Filter the pivot on a `NullRow` in order to make sure all join
@@ -561,12 +556,12 @@ func prefetchedRegion(_ db: Database, associations: [_SQLAssociation]) throws ->
let prefetchedQuery = SQLQuery(relation: prefetchedRelation)
// Union prefetched region
prefetchedRegion = try SQLQueryGenerator(query: prefetchedQuery)
let prefetchedRegion = try SQLQueryGenerator(query: prefetchedQuery)
.makeSelectStatement(db)
.databaseRegion // contains region of nested associations
region.formUnion(prefetchedRegion)
}
region.formUnion(prefetchedRegion)
}
}
@@ -376,44 +376,23 @@ public struct _SQLExpressionContains: SQLExpression {
let collection: SQLCollection
let isNegated: Bool
/// - precondition: !collection.expressions.isEmpty
init(_ value: SQLExpressible, _ collection: _SQLExpressionsArray, negated: Bool = false) {
// Make it easier for _SQLExpressionVisitor
assert(!collection.expressions.isEmpty)
self.init(
expression: value.sqlExpression,
collection: collection,
isNegated: negated)
}
init(_ value: SQLExpressible, _ collection: SQLRequestProtocol, negated: Bool = false) {
self.init(
expression: value.sqlExpression,
collection: collection,
isNegated: negated)
}
private init(expression: SQLExpression, collection: SQLCollection, isNegated: Bool) {
self.expression = expression
init(_ value: SQLExpressible, _ collection: SQLCollection, negated: Bool = false) {
self.expression = value.sqlExpression
self.collection = collection
self.isNegated = isNegated
self.isNegated = negated
}
/// :nodoc:
public var _negated: SQLExpression {
_SQLExpressionContains(
expression: expression,
collection: collection,
isNegated: !isNegated)
_SQLExpressionContains(expression, collection, negated: !isNegated)
}
/// :nodoc:
public func _qualifiedExpression(with alias: TableAlias) -> SQLExpression {
_SQLExpressionContains(
expression: expression._qualifiedExpression(with: alias),
collection: collection._qualifiedCollection(with: alias),
isNegated: isNegated)
expression._qualifiedExpression(with: alias),
collection._qualifiedCollection(with: alias),
negated: isNegated)
}
/// :nodoc:
+33 -13
View File
@@ -22,7 +22,7 @@ public protocol SQLOrderingTerm: _SQLOrderingTerm { }
// MARK: - _SQLOrdering
/// :nodoc:
public enum _SQLOrdering: SQLOrderingTerm {
public enum _SQLOrdering: SQLOrderingTerm, Refinable {
case asc(SQLExpression)
case desc(SQLExpression)
#if GRDBCUSTOMSQLITE
@@ -30,6 +30,37 @@ public enum _SQLOrdering: SQLOrderingTerm {
case descNullsFirst(SQLExpression)
#endif
var expression: SQLExpression {
get {
switch self {
case .asc(let expression):
return expression
case .desc(let expression):
return expression
#if GRDBCUSTOMSQLITE
case .ascNullsLast(let expression):
return expression
case .descNullsFirst(let expression):
return expression
#endif
}
}
set {
switch self {
case .asc:
self = .asc(newValue)
case .desc:
self = .desc(newValue)
#if GRDBCUSTOMSQLITE
case .ascNullsLast:
self = .ascNullsLast(newValue)
case .descNullsFirst:
self = .descNullsFirst(newValue)
#endif
}
}
}
/// :nodoc:
public var _reversed: SQLOrderingTerm {
switch self {
@@ -48,18 +79,7 @@ public enum _SQLOrdering: SQLOrderingTerm {
/// :nodoc:
public func _qualifiedOrdering(with alias: TableAlias) -> SQLOrderingTerm {
switch self {
case .asc(let expression):
return _SQLOrdering.asc(expression._qualifiedExpression(with: alias))
case .desc(let expression):
return _SQLOrdering.desc(expression._qualifiedExpression(with: alias))
#if GRDBCUSTOMSQLITE
case .ascNullsLast(let expression):
return _SQLOrdering.ascNullsLast(expression._qualifiedExpression(with: alias))
case .descNullsFirst(let expression):
return _SQLOrdering.descNullsFirst(expression._qualifiedExpression(with: alias))
#endif
}
map(\.expression) { $0._qualifiedExpression(with: alias) }
}
/// :nodoc:
@@ -357,6 +357,12 @@ public class TableAlias: Hashable {
ordering._qualifiedOrdering(with: self)
}
/// Returns a qualified columnn that is able to resolve ambiguities in
/// joined queries.
public subscript(_ column: String) -> SQLExpression {
Column(column)._qualifiedExpression(with: self)
}
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// An expression that evaluates to true if the record refered by this