mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Fix HasManyThrough row scopes
This commit is contained in:
1 parent
95c7c10f35
commit
5b15a88974
9 files changed
+366
-38
No files matched your search
@@ -14,6 +14,8 @@
|
||||
560432A4228F1668009D3FE2 /* AssociationPrefetchingObservationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560432A2228F1667009D3FE2 /* AssociationPrefetchingObservationTests.swift */; };
|
||||
56057C552291B16A00A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C4E2291B16900A7CB10 /* AssociationHasManyRowScopeTests.swift */; };
|
||||
56057C562291B16A00A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C4E2291B16900A7CB10 /* AssociationHasManyRowScopeTests.swift */; };
|
||||
56057C622291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C612291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */; };
|
||||
56057C632291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C612291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */; };
|
||||
5605F1591C672E4000235C62 /* CGFloat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1491C672E4000235C62 /* CGFloat.swift */; };
|
||||
5605F15A1C672E4000235C62 /* CGFloat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1491C672E4000235C62 /* CGFloat.swift */; };
|
||||
5605F15D1C672E4000235C62 /* DatabaseDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */; };
|
||||
@@ -944,6 +946,7 @@
|
||||
56043299228F00C2009D3FE2 /* OrderedDictionaryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrderedDictionaryTests.swift; sourceTree = "<group>"; };
|
||||
560432A2228F1667009D3FE2 /* AssociationPrefetchingObservationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingObservationTests.swift; sourceTree = "<group>"; };
|
||||
56057C4E2291B16900A7CB10 /* AssociationHasManyRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
56057C612291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
5605F1491C672E4000235C62 /* CGFloat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGFloat.swift; sourceTree = "<group>"; };
|
||||
5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateComponents.swift; sourceTree = "<group>"; };
|
||||
5605F14F1C672E4000235C62 /* Date.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = "<group>"; };
|
||||
@@ -1658,6 +1661,7 @@
|
||||
5653EACF20944B4E00F46237 /* AssociationChainSQLTests.swift */,
|
||||
56057C4E2291B16900A7CB10 /* AssociationHasManyRowScopeTests.swift */,
|
||||
56959632222D056D002CB7C9 /* AssociationHasManySQLTests.swift */,
|
||||
56057C612291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */,
|
||||
56959615222C456C002CB7C9 /* AssociationHasManyThroughSQLTests.swift */,
|
||||
5653EACA20944B4D00F46237 /* AssociationHasOneSQLDerivationTests.swift */,
|
||||
5653EACC20944B4D00F46237 /* AssociationHasOneSQLTests.swift */,
|
||||
@@ -2897,6 +2901,7 @@
|
||||
569BBA3722905FFA00478429 /* InflectionsTests.swift in Sources */,
|
||||
5672DE6A1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */,
|
||||
5698AC4D1DA2D48A0056AF8C /* FTS3RecordTests.swift in Sources */,
|
||||
56057C632291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
56B15D0B1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift in Sources */,
|
||||
56A238681B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */,
|
||||
5615B261222AE1B400061C1C /* AssociationHasOneThroughSQLDerivationTests.swift in Sources */,
|
||||
@@ -3094,6 +3099,7 @@
|
||||
569BBA3622905FFA00478429 /* InflectionsTests.swift in Sources */,
|
||||
56D496931D81316E008276D7 /* FetchableRecordTests.swift in Sources */,
|
||||
56D496881D81316E008276D7 /* DatabaseValueConversionTests.swift in Sources */,
|
||||
56057C622291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
56D496BA1D813482008276D7 /* DatabaseQueueSchemaCacheTests.swift in Sources */,
|
||||
56D496781D81309E008276D7 /* RecordSubClassTests.swift in Sources */,
|
||||
5615B262222AE1B400061C1C /* AssociationHasOneThroughSQLDerivationTests.swift in Sources */,
|
||||
|
||||
@@ -33,6 +33,22 @@ public protocol Association: DerivableRequest {
|
||||
/// :nodoc:
|
||||
var sqlAssociation: SQLAssociation { get }
|
||||
|
||||
/// Creates an association with the given key.
|
||||
///
|
||||
/// This new key impacts how rows fetched from the resulting association
|
||||
/// should be consumed:
|
||||
///
|
||||
/// struct Player: TableRecord {
|
||||
/// static let team = belongsTo(Team.self)
|
||||
/// }
|
||||
///
|
||||
/// // Consume rows:
|
||||
/// let request = Player.including(required: Player.team.forKey("custom"))
|
||||
/// for row in Row.fetchAll(db, request) {
|
||||
/// let team: Team = row["custom"]
|
||||
/// }
|
||||
func forKey(_ key: String) -> Self
|
||||
|
||||
/// :nodoc:
|
||||
init(sqlAssociation: SQLAssociation)
|
||||
}
|
||||
@@ -181,30 +197,6 @@ extension Association {
|
||||
return mapDestinationRelation { $0.unordered() }
|
||||
}
|
||||
|
||||
/// Creates an association with the given key.
|
||||
///
|
||||
/// This new key impacts how rows fetched from the resulting association
|
||||
/// should be consumed:
|
||||
///
|
||||
/// struct Player: TableRecord {
|
||||
/// static let team = belongsTo(Team.self)
|
||||
/// }
|
||||
///
|
||||
/// // Consume rows:
|
||||
/// let request = Player.including(required: Player.team.forKey("custom"))
|
||||
/// for row in Row.fetchAll(db, request) {
|
||||
/// let team: Team = row["custom"]
|
||||
/// }
|
||||
public func forKey(_ key: String) -> Self {
|
||||
let associationKey: SQLAssociationKey
|
||||
if sqlAssociation.destinationIsSingular {
|
||||
associationKey = .fixedSingular(key)
|
||||
} else {
|
||||
associationKey = .fixedPlural(key)
|
||||
}
|
||||
return Self.init(sqlAssociation: sqlAssociation.forDestinationKey(associationKey))
|
||||
}
|
||||
|
||||
/// Creates an association with the given key.
|
||||
///
|
||||
/// This new key helps Decodable records decode rows fetched from the
|
||||
@@ -361,6 +353,13 @@ extension Association where Self: TableRequest, RowDecoder: TableRecord {
|
||||
/// The base protocol for all associations that define a one-to-many connection.
|
||||
public protocol AssociationToMany: Association { }
|
||||
|
||||
extension AssociationToMany {
|
||||
public func forKey(_ key: String) -> Self {
|
||||
let associationKey = SQLAssociationKey.fixedPlural(key)
|
||||
return Self.init(sqlAssociation: sqlAssociation.forDestinationKey(associationKey))
|
||||
}
|
||||
}
|
||||
|
||||
extension AssociationToMany where OriginRowDecoder: TableRecord {
|
||||
private func makeAggregate(_ expression: SQLExpression) -> AssociationAggregate<OriginRowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
@@ -461,6 +460,13 @@ extension AssociationToMany where OriginRowDecoder: TableRecord {
|
||||
/// The base protocol for all associations that define a one-to-one connection.
|
||||
public protocol AssociationToOne: Association { }
|
||||
|
||||
extension AssociationToOne {
|
||||
public func forKey(_ key: String) -> Self {
|
||||
let associationKey = SQLAssociationKey.fixedSingular(key)
|
||||
return Self.init(sqlAssociation: sqlAssociation.forDestinationKey(associationKey))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLAssociationKey
|
||||
|
||||
/// Associations are meant to be consumed, most often into Decodable records.
|
||||
@@ -637,7 +643,6 @@ public /* TODO: internal */ struct SQLAssociation {
|
||||
set { steps[steps.count - 1] = newValue }
|
||||
}
|
||||
var destinationKey: SQLAssociationKey { return destination.key }
|
||||
var destinationIsSingular: Bool { return destination.isSingular }
|
||||
|
||||
private var pivot: AssociationStep {
|
||||
get { return steps[0] }
|
||||
|
||||
@@ -119,19 +119,29 @@ extension TableRecord {
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The record type at the other side of the association.
|
||||
/// - through: An association from Self to the intermediate type.
|
||||
/// - using: An association from the intermediate type to the
|
||||
/// - pivot: An association from Self to the intermediate type.
|
||||
/// - target: A target association from the intermediate type to the
|
||||
/// destination type.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is the same key as the target.
|
||||
public static func hasMany<Pivot, Target>(
|
||||
_ destination: Target.RowDecoder.Type,
|
||||
through pivot: Pivot,
|
||||
using target: Target)
|
||||
using target: Target,
|
||||
key: String? = nil)
|
||||
-> HasManyThroughAssociation<Self, Target.RowDecoder>
|
||||
where Pivot: Association,
|
||||
Target: Association,
|
||||
Pivot.OriginRowDecoder == Self,
|
||||
Pivot.RowDecoder == Target.OriginRowDecoder
|
||||
{
|
||||
return HasManyThroughAssociation(sqlAssociation: target.sqlAssociation.through(pivot.sqlAssociation))
|
||||
let association = HasManyThroughAssociation<Self, Target.RowDecoder>(
|
||||
sqlAssociation: target.sqlAssociation.through(pivot.sqlAssociation))
|
||||
|
||||
if let key = key {
|
||||
return association.forKey(key)
|
||||
} else {
|
||||
return association
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,19 +97,29 @@ extension TableRecord {
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The record type at the other side of the association.
|
||||
/// - through: An association from Self to the intermediate type.
|
||||
/// - using: An association from the intermediate type to the
|
||||
/// - pivot: An association from Self to the intermediate type.
|
||||
/// - target: A target association from the intermediate type to the
|
||||
/// destination type.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is the same key as the target.
|
||||
public static func hasOne<Pivot, Target>(
|
||||
_ destination: Target.RowDecoder.Type,
|
||||
through pivot: Pivot,
|
||||
using target: Target)
|
||||
using target: Target,
|
||||
key: String? = nil)
|
||||
-> HasOneThroughAssociation<Self, Target.RowDecoder>
|
||||
where Pivot: AssociationToOne,
|
||||
Target: AssociationToOne,
|
||||
Pivot.OriginRowDecoder == Self,
|
||||
Pivot.RowDecoder == Target.OriginRowDecoder
|
||||
{
|
||||
return HasOneThroughAssociation(sqlAssociation: target.sqlAssociation.through(pivot.sqlAssociation))
|
||||
let association = HasOneThroughAssociation<Self, Target.RowDecoder>(
|
||||
sqlAssociation: target.sqlAssociation.through(pivot.sqlAssociation))
|
||||
|
||||
if let key = key {
|
||||
return association.forKey(key)
|
||||
} else {
|
||||
return association
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@
|
||||
560432A7228F167A009D3FE2 /* AssociationPrefetchingObservationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560432A5228F167A009D3FE2 /* AssociationPrefetchingObservationTests.swift */; };
|
||||
56057C592291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C572291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift */; };
|
||||
56057C5A2291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C572291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift */; };
|
||||
56057C652291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C642291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */; };
|
||||
56057C662291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C642291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */; };
|
||||
5607149B227618660091BB10 /* Row+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56071499227618660091BB10 /* Row+QueryInterface.swift */; };
|
||||
5607149C227618660091BB10 /* Row+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56071499227618660091BB10 /* Row+QueryInterface.swift */; };
|
||||
5607149E227618800091BB10 /* FetchableRecord+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5607149D227618800091BB10 /* FetchableRecord+QueryInterface.swift */; };
|
||||
@@ -698,6 +700,7 @@
|
||||
56043295228F00A9009D3FE2 /* OrderedDictionaryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderedDictionaryTests.swift; sourceTree = "<group>"; };
|
||||
560432A5228F167A009D3FE2 /* AssociationPrefetchingObservationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingObservationTests.swift; sourceTree = "<group>"; };
|
||||
56057C572291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
56057C642291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
5605F1491C672E4000235C62 /* CGFloat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGFloat.swift; sourceTree = "<group>"; };
|
||||
5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateComponents.swift; sourceTree = "<group>"; };
|
||||
5605F14F1C672E4000235C62 /* Date.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = "<group>"; };
|
||||
@@ -1375,6 +1378,7 @@
|
||||
5653EB6320961FB200F46237 /* AssociationChainSQLTests.swift */,
|
||||
56057C572291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift */,
|
||||
56959635222D058B002CB7C9 /* AssociationHasManySQLTests.swift */,
|
||||
56057C642291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */,
|
||||
5695961E222C4589002CB7C9 /* AssociationHasManyThroughSQLTests.swift */,
|
||||
5653EB5F20961FB100F46237 /* AssociationHasOneSQLDerivationTests.swift */,
|
||||
5653EB5D20961FB100F46237 /* AssociationHasOneSQLTests.swift */,
|
||||
@@ -2225,6 +2229,7 @@
|
||||
569BBA40229065CF00478429 /* InflectionsTests.swift in Sources */,
|
||||
5653EB7F20961FB200F46237 /* AssociationChainSQLTests.swift in Sources */,
|
||||
6340BF871E5E3F7900832805 /* RecordPersistenceConflictPolicy.swift in Sources */,
|
||||
56057C662291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
56AF74721D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */,
|
||||
F3BA80D21CFB2FF3003DC1BA /* PersistableRecordTests.swift in Sources */,
|
||||
5615B259222AE19100061C1C /* AssociationHasOneThroughSQLDerivationTests.swift in Sources */,
|
||||
@@ -2553,6 +2558,7 @@
|
||||
569BBA41229065CF00478429 /* InflectionsTests.swift in Sources */,
|
||||
5653EB7E20961FB200F46237 /* AssociationChainSQLTests.swift in Sources */,
|
||||
F3BA80EC1CFB3017003DC1BA /* RowCopiedFromStatementTests.swift in Sources */,
|
||||
56057C652291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
F3BA80D41CFB2FF4003DC1BA /* PersistableRecordTests.swift in Sources */,
|
||||
6340BF831E5E3F7900832805 /* RecordPersistenceConflictPolicy.swift in Sources */,
|
||||
5615B258222AE19100061C1C /* AssociationHasOneThroughSQLDerivationTests.swift in Sources */,
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
560432AA228F1752009D3FE2 /* AssociationPrefetchingObservationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560432A8228F1752009D3FE2 /* AssociationPrefetchingObservationTests.swift */; };
|
||||
56057C5F2291B8E400A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C5E2291B8E400A7CB10 /* AssociationHasManyRowScopeTests.swift */; };
|
||||
56057C602291B8E400A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C5E2291B8E400A7CB10 /* AssociationHasManyRowScopeTests.swift */; };
|
||||
56057C682291D0AC00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C672291D0AB00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */; };
|
||||
56057C692291D0AC00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C672291D0AB00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */; };
|
||||
560714F0227DD1810091BB10 /* AssociationPrefetchingSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560714EF227DD1810091BB10 /* AssociationPrefetchingSQLTests.swift */; };
|
||||
560714F1227DD1810091BB10 /* AssociationPrefetchingSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560714EF227DD1810091BB10 /* AssociationPrefetchingSQLTests.swift */; };
|
||||
564A1FDE226B89E1001F64F1 /* MutablePersistableRecordDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A1F27226B89CE001F64F1 /* MutablePersistableRecordDeleteTests.swift */; };
|
||||
@@ -395,6 +397,7 @@
|
||||
47C5D1B9AFFE795AA1D6EA5D /* Pods-GRDBTestsEncrypted.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GRDBTestsEncrypted.release.xcconfig"; path = "Target Support Files/Pods-GRDBTestsEncrypted/Pods-GRDBTestsEncrypted.release.xcconfig"; sourceTree = "<group>"; };
|
||||
560432A8228F1752009D3FE2 /* AssociationPrefetchingObservationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingObservationTests.swift; sourceTree = "<group>"; };
|
||||
56057C5E2291B8E400A7CB10 /* AssociationHasManyRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
56057C672291D0AB00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
560714EF227DD1810091BB10 /* AssociationPrefetchingSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingSQLTests.swift; sourceTree = "<group>"; };
|
||||
564A1F1D226B876D001F64F1 /* GRDBTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
564A1F22226B876D001F64F1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
@@ -647,6 +650,7 @@
|
||||
564A1F4D226B89D2001F64F1 /* AssociationChainSQLTests.swift */,
|
||||
56057C5E2291B8E400A7CB10 /* AssociationHasManyRowScopeTests.swift */,
|
||||
564A1F7C226B89D7001F64F1 /* AssociationHasManySQLTests.swift */,
|
||||
56057C672291D0AB00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */,
|
||||
564A1FB4226B89DD001F64F1 /* AssociationHasManyThroughSQLTests.swift */,
|
||||
564A1FAE226B89DC001F64F1 /* AssociationHasOneSQLDerivationTests.swift */,
|
||||
564A1F9D226B89DA001F64F1 /* AssociationHasOneSQLTests.swift */,
|
||||
@@ -1188,6 +1192,7 @@
|
||||
564A205F226B89E1001F64F1 /* SQLLiteralTests.swift in Sources */,
|
||||
564A206C226B89E1001F64F1 /* FTS5CustomTokenizerTests.swift in Sources */,
|
||||
564A2002226B89E1001F64F1 /* DerivableRequestTests.swift in Sources */,
|
||||
56057C682291D0AC00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
564A1FEB226B89E1001F64F1 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
564A1FFE226B89E1001F64F1 /* FilterCursorTests.swift in Sources */,
|
||||
564A208A226B89E1001F64F1 /* MutablePersistableRecordChangesTests.swift in Sources */,
|
||||
@@ -1383,6 +1388,7 @@
|
||||
564A2122226B8E18001F64F1 /* SQLLiteralTests.swift in Sources */,
|
||||
564A2123226B8E18001F64F1 /* FTS5CustomTokenizerTests.swift in Sources */,
|
||||
564A2124226B8E18001F64F1 /* DerivableRequestTests.swift in Sources */,
|
||||
56057C692291D0AC00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
564A2125226B8E18001F64F1 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
564A2126226B8E18001F64F1 /* FilterCursorTests.swift in Sources */,
|
||||
564A2127226B8E18001F64F1 /* MutablePersistableRecordChangesTests.swift in Sources */,
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
560432AD228F1761009D3FE2 /* AssociationPrefetchingObservationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560432AB228F1761009D3FE2 /* AssociationPrefetchingObservationTests.swift */; };
|
||||
56057C5C2291B8D600A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C5B2291B8D600A7CB10 /* AssociationHasManyRowScopeTests.swift */; };
|
||||
56057C5D2291B8D600A7CB10 /* AssociationHasManyRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C5B2291B8D600A7CB10 /* AssociationHasManyRowScopeTests.swift */; };
|
||||
56057C6B2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C6A2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */; };
|
||||
56057C6C2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56057C6A2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */; };
|
||||
560714F3227DD1AE0091BB10 /* AssociationPrefetchingSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560714F2227DD1AD0091BB10 /* AssociationPrefetchingSQLTests.swift */; };
|
||||
560714F4227DD1AE0091BB10 /* AssociationPrefetchingSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560714F2227DD1AD0091BB10 /* AssociationPrefetchingSQLTests.swift */; };
|
||||
564A1FDE226B89E1001F64F1 /* MutablePersistableRecordDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A1F27226B89CE001F64F1 /* MutablePersistableRecordDeleteTests.swift */; };
|
||||
@@ -397,6 +399,7 @@
|
||||
47C5D1B9AFFE795AA1D6EA5D /* Pods-GRDBTestsEncrypted.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GRDBTestsEncrypted.release.xcconfig"; path = "Target Support Files/Pods-GRDBTestsEncrypted/Pods-GRDBTestsEncrypted.release.xcconfig"; sourceTree = "<group>"; };
|
||||
560432AB228F1761009D3FE2 /* AssociationPrefetchingObservationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingObservationTests.swift; sourceTree = "<group>"; };
|
||||
56057C5B2291B8D600A7CB10 /* AssociationHasManyRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
56057C6A2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
560714F2227DD1AD0091BB10 /* AssociationPrefetchingSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingSQLTests.swift; sourceTree = "<group>"; };
|
||||
564A1F1D226B876D001F64F1 /* GRDBTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
564A1F22226B876D001F64F1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
@@ -650,6 +653,7 @@
|
||||
564A1F4D226B89D2001F64F1 /* AssociationChainSQLTests.swift */,
|
||||
56057C5B2291B8D600A7CB10 /* AssociationHasManyRowScopeTests.swift */,
|
||||
564A1F7C226B89D7001F64F1 /* AssociationHasManySQLTests.swift */,
|
||||
56057C6A2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */,
|
||||
564A1FB4226B89DD001F64F1 /* AssociationHasManyThroughSQLTests.swift */,
|
||||
564A1FAE226B89DC001F64F1 /* AssociationHasOneSQLDerivationTests.swift */,
|
||||
564A1F9D226B89DA001F64F1 /* AssociationHasOneSQLTests.swift */,
|
||||
@@ -1194,6 +1198,7 @@
|
||||
564A205F226B89E1001F64F1 /* SQLLiteralTests.swift in Sources */,
|
||||
564A206C226B89E1001F64F1 /* FTS5CustomTokenizerTests.swift in Sources */,
|
||||
564A2002226B89E1001F64F1 /* DerivableRequestTests.swift in Sources */,
|
||||
56057C6B2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
564A1FEB226B89E1001F64F1 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
564A1FFE226B89E1001F64F1 /* FilterCursorTests.swift in Sources */,
|
||||
564A208A226B89E1001F64F1 /* MutablePersistableRecordChangesTests.swift in Sources */,
|
||||
@@ -1389,6 +1394,7 @@
|
||||
564A2122226B8E18001F64F1 /* SQLLiteralTests.swift in Sources */,
|
||||
564A2123226B8E18001F64F1 /* FTS5CustomTokenizerTests.swift in Sources */,
|
||||
564A2124226B8E18001F64F1 /* DerivableRequestTests.swift in Sources */,
|
||||
56057C6C2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
564A2125226B8E18001F64F1 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
564A2126226B8E18001F64F1 /* FilterCursorTests.swift in Sources */,
|
||||
564A2127226B8E18001F64F1 /* MutablePersistableRecordChangesTests.swift in Sources */,
|
||||
|
||||
@@ -72,7 +72,8 @@ class AssociationHasManyRowScopeTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
struct Parent : TableRecord {
|
||||
static let children = hasMany(Child.self, key: "littlePuppies")
|
||||
static let littlePuppies = hasMany(Child.self, key: "littlePuppies")
|
||||
static let kittens = hasMany(Child.self).forKey("kittens")
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
@@ -89,10 +90,18 @@ class AssociationHasManyRowScopeTests: GRDBTestCase {
|
||||
INSERT INTO child (id, parentId) VALUES (2, 1);
|
||||
""")
|
||||
|
||||
let request = Parent.including(required: Parent.children)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 1])
|
||||
XCTAssertEqual(row.scopes["littlePuppy"], ["id": 2, "parentId": 1])
|
||||
do {
|
||||
let request = Parent.including(required: Parent.littlePuppies)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 1])
|
||||
XCTAssertEqual(row.scopes["littlePuppy"], ["id": 2, "parentId": 1])
|
||||
}
|
||||
do {
|
||||
let request = Parent.including(required: Parent.kittens)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 1])
|
||||
XCTAssertEqual(row.scopes["kitten"], ["id": 2, "parentId": 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
import XCTest
|
||||
#if GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
/// Test SQL generation
|
||||
|
||||
class AssociationHasManyThroughRowScopeTests: GRDBTestCase {
|
||||
|
||||
func testBelongsToHasManySingularTable() throws {
|
||||
struct Parent: TableRecord {
|
||||
static let child = belongsTo(Child.self)
|
||||
static let grandChildren = hasMany(GrandChild.self, through: child, using: Child.grandChildren)
|
||||
}
|
||||
struct Child: TableRecord {
|
||||
static let grandChildren = hasMany(GrandChild.self)
|
||||
}
|
||||
struct GrandChild: TableRecord {
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "child") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "parent") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("childId").references("child")
|
||||
}
|
||||
try db.create(table: "grandChild") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("childId").references("child")
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO child (id) VALUES (1);
|
||||
INSERT INTO parent (id, childId) VALUES (2, 1);
|
||||
INSERT INTO grandChild (id, childId) VALUES (3, 1);
|
||||
""")
|
||||
|
||||
let request = Parent.including(required: Parent.grandChildren)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 2, "childId": 1])
|
||||
XCTAssertEqual(Set(row.scopes.names), ["child"])
|
||||
XCTAssertEqual(row.scopesTree["grandChild"], ["id": 3, "childId": 1])
|
||||
}
|
||||
}
|
||||
|
||||
func testBelongsToHasManyPluralTable() throws {
|
||||
struct Parent: TableRecord {
|
||||
static let databaseTableName = "parents"
|
||||
static let child = belongsTo(Child.self)
|
||||
static let grandChildren = hasMany(GrandChild.self, through: child, using: Child.grandChildren)
|
||||
}
|
||||
struct Child: TableRecord {
|
||||
static let databaseTableName = "children"
|
||||
static let grandChildren = hasMany(GrandChild.self)
|
||||
}
|
||||
struct GrandChild: TableRecord {
|
||||
static let databaseTableName = "grandChildren"
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "children") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "parents") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("childId").references("children")
|
||||
}
|
||||
try db.create(table: "grandChildren") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("childId").references("children")
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO children (id) VALUES (1);
|
||||
INSERT INTO parents (id, childId) VALUES (2, 1);
|
||||
INSERT INTO grandChildren (id, childId) VALUES (3, 1);
|
||||
""")
|
||||
|
||||
let request = Parent.including(required: Parent.grandChildren)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 2, "childId": 1])
|
||||
XCTAssertEqual(Set(row.scopes.names), ["child"])
|
||||
XCTAssertEqual(row.scopesTree["grandChild"], ["id": 3, "childId": 1])
|
||||
}
|
||||
}
|
||||
|
||||
func testBelongsToHasManyCustomKey() throws {
|
||||
struct Parent: TableRecord {
|
||||
static let databaseTableName = "parents"
|
||||
static let child = belongsTo(Child.self)
|
||||
static let littlePuppies = hasMany(GrandChild.self, through: child, using: Child.grandChildren, key: "littlePuppies")
|
||||
static let kittens = hasMany(GrandChild.self, through: child, using: Child.grandChildren).forKey("kittens")
|
||||
}
|
||||
struct Child: TableRecord {
|
||||
static let databaseTableName = "children"
|
||||
static let grandChildren = hasMany(GrandChild.self)
|
||||
}
|
||||
struct GrandChild: TableRecord {
|
||||
static let databaseTableName = "grandChildren"
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "children") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "parents") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("childId").references("children")
|
||||
}
|
||||
try db.create(table: "grandChildren") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("childId").references("children")
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO children (id) VALUES (1);
|
||||
INSERT INTO parents (id, childId) VALUES (2, 1);
|
||||
INSERT INTO grandChildren (id, childId) VALUES (3, 1);
|
||||
""")
|
||||
|
||||
do {
|
||||
let request = Parent.including(required: Parent.littlePuppies)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 2, "childId": 1])
|
||||
XCTAssertEqual(Set(row.scopes.names), ["child"])
|
||||
XCTAssertEqual(row.scopesTree["littlePuppy"], ["id": 3, "childId": 1])
|
||||
}
|
||||
|
||||
do {
|
||||
let request = Parent.including(required: Parent.kittens)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 2, "childId": 1])
|
||||
XCTAssertEqual(Set(row.scopes.names), ["child"])
|
||||
XCTAssertEqual(row.scopesTree["kitten"], ["id": 3, "childId": 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testHasManyBelongsToSingularTable() throws {
|
||||
struct Parent: TableRecord {
|
||||
static let children = hasMany(Child.self)
|
||||
static let grandChildren = hasMany(GrandChild.self, through: children, using: Child.grandChild)
|
||||
}
|
||||
struct Child: TableRecord {
|
||||
static let grandChild = belongsTo(GrandChild.self)
|
||||
}
|
||||
struct GrandChild: TableRecord {
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "parent") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "grandChild") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "child") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("parentId").references("parent")
|
||||
t.column("grandChildId").references("grandChild")
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO parent (id) VALUES (1);
|
||||
INSERT INTO grandChild (id) VALUES (2);
|
||||
INSERT INTO child (id, parentId, grandChildId) VALUES (3, 1, 2);
|
||||
""")
|
||||
|
||||
let request = Parent.including(required: Parent.grandChildren)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 1])
|
||||
XCTAssertEqual(Set(row.scopes.names), ["child"])
|
||||
XCTAssertEqual(row.scopesTree["grandChild"], ["id": 2])
|
||||
}
|
||||
}
|
||||
|
||||
func testHasManyBelongsToPluralTable() throws {
|
||||
struct Parent: TableRecord {
|
||||
static let databaseTableName = "parents"
|
||||
static let children = hasMany(Child.self)
|
||||
static let grandChildren = hasMany(GrandChild.self, through: children, using: Child.grandChild)
|
||||
}
|
||||
struct Child: TableRecord {
|
||||
static let databaseTableName = "children"
|
||||
static let grandChild = belongsTo(GrandChild.self)
|
||||
}
|
||||
struct GrandChild: TableRecord {
|
||||
static let databaseTableName = "grandChildren"
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "parents") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "grandChildren") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "children") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("parentId").references("parents")
|
||||
t.column("grandChildId").references("grandChildren")
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO parents (id) VALUES (1);
|
||||
INSERT INTO grandChildren (id) VALUES (2);
|
||||
INSERT INTO children (id, parentId, grandChildId) VALUES (3, 1, 2);
|
||||
""")
|
||||
|
||||
let request = Parent.including(required: Parent.grandChildren)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 1])
|
||||
XCTAssertEqual(Set(row.scopes.names), ["child"])
|
||||
XCTAssertEqual(row.scopesTree["grandChild"], ["id": 2])
|
||||
}
|
||||
}
|
||||
|
||||
func testHasManyBelongsToCustomKey() throws {
|
||||
struct Parent: TableRecord {
|
||||
static let children = hasMany(Child.self)
|
||||
static let littlePuppies = hasMany(GrandChild.self, through: children, using: Child.grandChild, key: "littlePuppies")
|
||||
static let kittens = hasMany(GrandChild.self, through: children, using: Child.grandChild).forKey("kittens")
|
||||
}
|
||||
struct Child: TableRecord {
|
||||
static let grandChild = belongsTo(GrandChild.self)
|
||||
}
|
||||
struct GrandChild: TableRecord {
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "parent") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "grandChild") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "child") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("parentId").references("parent")
|
||||
t.column("grandChildId").references("grandChild")
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO parent (id) VALUES (1);
|
||||
INSERT INTO grandChild (id) VALUES (2);
|
||||
INSERT INTO child (id, parentId, grandChildId) VALUES (3, 1, 2);
|
||||
""")
|
||||
|
||||
do {
|
||||
let request = Parent.including(required: Parent.littlePuppies)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 1])
|
||||
XCTAssertEqual(Set(row.scopes.names), ["child"])
|
||||
XCTAssertEqual(row.scopesTree["littlePuppy"], ["id": 2])
|
||||
}
|
||||
|
||||
do {
|
||||
let request = Parent.including(required: Parent.kittens)
|
||||
let row = try Row.fetchOne(db, request)!
|
||||
XCTAssertEqual(row.unscoped, ["id": 1])
|
||||
XCTAssertEqual(Set(row.scopes.names), ["child"])
|
||||
XCTAssertEqual(row.scopesTree["kitten"], ["id": 2])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user