diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 91d673ebd..d61e33645 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -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 = ""; }; 560432A2228F1667009D3FE2 /* AssociationPrefetchingObservationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingObservationTests.swift; sourceTree = ""; }; 56057C4E2291B16900A7CB10 /* AssociationHasManyRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyRowScopeTests.swift; sourceTree = ""; }; + 56057C612291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughRowScopeTests.swift; sourceTree = ""; }; 5605F1491C672E4000235C62 /* CGFloat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGFloat.swift; sourceTree = ""; }; 5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateComponents.swift; sourceTree = ""; }; 5605F14F1C672E4000235C62 /* Date.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = ""; }; @@ -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 */, diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 005f11fe1..7d7c098b6 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -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 { 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] } diff --git a/GRDB/QueryInterface/Association/HasManyThroughAssociation.swift b/GRDB/QueryInterface/Association/HasManyThroughAssociation.swift index 83eaba2fe..7015ef0f9 100644 --- a/GRDB/QueryInterface/Association/HasManyThroughAssociation.swift +++ b/GRDB/QueryInterface/Association/HasManyThroughAssociation.swift @@ -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( _ destination: Target.RowDecoder.Type, through pivot: Pivot, - using target: Target) + using target: Target, + key: String? = nil) -> HasManyThroughAssociation where Pivot: Association, Target: Association, Pivot.OriginRowDecoder == Self, Pivot.RowDecoder == Target.OriginRowDecoder { - return HasManyThroughAssociation(sqlAssociation: target.sqlAssociation.through(pivot.sqlAssociation)) + let association = HasManyThroughAssociation( + sqlAssociation: target.sqlAssociation.through(pivot.sqlAssociation)) + + if let key = key { + return association.forKey(key) + } else { + return association + } } } diff --git a/GRDB/QueryInterface/Association/HasOneThroughAssociation.swift b/GRDB/QueryInterface/Association/HasOneThroughAssociation.swift index 3acd155fb..10f1cf680 100644 --- a/GRDB/QueryInterface/Association/HasOneThroughAssociation.swift +++ b/GRDB/QueryInterface/Association/HasOneThroughAssociation.swift @@ -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( _ destination: Target.RowDecoder.Type, through pivot: Pivot, - using target: Target) + using target: Target, + key: String? = nil) -> HasOneThroughAssociation where Pivot: AssociationToOne, Target: AssociationToOne, Pivot.OriginRowDecoder == Self, Pivot.RowDecoder == Target.OriginRowDecoder { - return HasOneThroughAssociation(sqlAssociation: target.sqlAssociation.through(pivot.sqlAssociation)) + let association = HasOneThroughAssociation( + sqlAssociation: target.sqlAssociation.through(pivot.sqlAssociation)) + + if let key = key { + return association.forKey(key) + } else { + return association + } } } diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index 39d1dc5bf..dc0cd25e4 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -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 = ""; }; 560432A5228F167A009D3FE2 /* AssociationPrefetchingObservationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingObservationTests.swift; sourceTree = ""; }; 56057C572291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyRowScopeTests.swift; sourceTree = ""; }; + 56057C642291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughRowScopeTests.swift; sourceTree = ""; }; 5605F1491C672E4000235C62 /* CGFloat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGFloat.swift; sourceTree = ""; }; 5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateComponents.swift; sourceTree = ""; }; 5605F14F1C672E4000235C62 /* Date.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = ""; }; @@ -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 */, diff --git a/Tests/CocoaPods/SQLCipher3/GRDBTests.xcodeproj/project.pbxproj b/Tests/CocoaPods/SQLCipher3/GRDBTests.xcodeproj/project.pbxproj index 56dc3d095..06ca367a6 100644 --- a/Tests/CocoaPods/SQLCipher3/GRDBTests.xcodeproj/project.pbxproj +++ b/Tests/CocoaPods/SQLCipher3/GRDBTests.xcodeproj/project.pbxproj @@ -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 = ""; }; 560432A8228F1752009D3FE2 /* AssociationPrefetchingObservationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingObservationTests.swift; sourceTree = ""; }; 56057C5E2291B8E400A7CB10 /* AssociationHasManyRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyRowScopeTests.swift; sourceTree = ""; }; + 56057C672291D0AB00A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughRowScopeTests.swift; sourceTree = ""; }; 560714EF227DD1810091BB10 /* AssociationPrefetchingSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingSQLTests.swift; sourceTree = ""; }; 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 = ""; }; @@ -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 */, diff --git a/Tests/CocoaPods/SQLCipher4/GRDBTests.xcodeproj/project.pbxproj b/Tests/CocoaPods/SQLCipher4/GRDBTests.xcodeproj/project.pbxproj index 01dae6bd2..6afde2b0a 100644 --- a/Tests/CocoaPods/SQLCipher4/GRDBTests.xcodeproj/project.pbxproj +++ b/Tests/CocoaPods/SQLCipher4/GRDBTests.xcodeproj/project.pbxproj @@ -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 = ""; }; 560432AB228F1761009D3FE2 /* AssociationPrefetchingObservationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingObservationTests.swift; sourceTree = ""; }; 56057C5B2291B8D600A7CB10 /* AssociationHasManyRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyRowScopeTests.swift; sourceTree = ""; }; + 56057C6A2291D0C000A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughRowScopeTests.swift; sourceTree = ""; }; 560714F2227DD1AD0091BB10 /* AssociationPrefetchingSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingSQLTests.swift; sourceTree = ""; }; 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 = ""; }; @@ -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 */, diff --git a/Tests/GRDBTests/AssociationHasManyRowScopeTests.swift b/Tests/GRDBTests/AssociationHasManyRowScopeTests.swift index c2f262687..c7194f37f 100644 --- a/Tests/GRDBTests/AssociationHasManyRowScopeTests.swift +++ b/Tests/GRDBTests/AssociationHasManyRowScopeTests.swift @@ -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]) + } } } } diff --git a/Tests/GRDBTests/AssociationHasManyThroughRowScopeTests.swift b/Tests/GRDBTests/AssociationHasManyThroughRowScopeTests.swift new file mode 100644 index 000000000..44f42f802 --- /dev/null +++ b/Tests/GRDBTests/AssociationHasManyThroughRowScopeTests.swift @@ -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]) + } + } + } +}