Extract ForeignKey

This commit is contained in:
Gwendal Roué committed 2021-02-09 18:14:35 +01:00
1 parent 7914805524
commit ae6f5dd0f5
4 files changed
+125 -108

No files matched your search

+10
View File
@@ -128,6 +128,10 @@
5623936D1DEE0CD200A6B01F /* FlattenCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393681DEE0CD200A6B01F /* FlattenCursorTests.swift */; };
562393721DEE104400A6B01F /* MapCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393711DEE104400A6B01F /* MapCursorTests.swift */; };
562393761DEE104400A6B01F /* MapCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393711DEE104400A6B01F /* MapCursorTests.swift */; };
56256ED925D1B316008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ED825D1B316008C2BDD /* ForeignKey.swift */; };
56256EDA25D1B316008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ED825D1B316008C2BDD /* ForeignKey.swift */; };
56256EDB25D1B316008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ED825D1B316008C2BDD /* ForeignKey.swift */; };
56256EDC25D1B316008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ED825D1B316008C2BDD /* ForeignKey.swift */; };
562756431E963AAC0035B653 /* DatabaseWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562756421E963AAC0035B653 /* DatabaseWriterTests.swift */; };
562756471E963AAC0035B653 /* DatabaseWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562756421E963AAC0035B653 /* DatabaseWriterTests.swift */; };
562EA8261F17B2AC00FA528C /* CompilationProtocolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562EA8251F17B2AC00FA528C /* CompilationProtocolTests.swift */; };
@@ -1290,6 +1294,7 @@
562393681DEE0CD200A6B01F /* FlattenCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlattenCursorTests.swift; sourceTree = "<group>"; };
562393711DEE104400A6B01F /* MapCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapCursorTests.swift; sourceTree = "<group>"; };
5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTestCase.swift; sourceTree = "<group>"; };
56256ED825D1B316008C2BDD /* ForeignKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForeignKey.swift; sourceTree = "<group>"; };
562756421E963AAC0035B653 /* DatabaseWriterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseWriterTests.swift; sourceTree = "<group>"; };
562EA8251F17B2AC00FA528C /* CompilationProtocolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompilationProtocolTests.swift; sourceTree = "<group>"; };
562EA82E1F17B9EB00FA528C /* CompilationSubClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompilationSubClassTests.swift; sourceTree = "<group>"; };
@@ -1877,6 +1882,7 @@
56300B6D1C53F592005A543B /* QueryInterface */ = {
isa = PBXGroup;
children = (
56256ED825D1B316008C2BDD /* ForeignKey.swift */,
56CEB5101EAA324B00BFAF62 /* FTS3+QueryInterface.swift */,
56CEB5181EAA328900BFAF62 /* FTS5+QueryInterface.swift */,
5656A8182295B044001FF3FF /* Request */,
@@ -2990,6 +2996,7 @@
563EF42F2161180D007DAACD /* AssociationAggregate.swift in Sources */,
565490BA1D5AE236005622CB /* DatabaseQueue.swift in Sources */,
56781B0D243F86E600650A83 /* Refinable.swift in Sources */,
56256EDB25D1B316008C2BDD /* ForeignKey.swift in Sources */,
565490CD1D5AE252005622CB /* Date.swift in Sources */,
5653EB0820944C7C00F46237 /* SQLForeignKeyRequest.swift in Sources */,
565490BE1D5AE236005622CB /* DatabaseValue.swift in Sources */,
@@ -3081,6 +3088,7 @@
56A2387C1B9C75030082EB20 /* Configuration.swift in Sources */,
56DAA2DE1DE9C827006E10C8 /* Cursor.swift in Sources */,
560A37A51C8F625000949E71 /* DatabasePool.swift in Sources */,
56256EDA25D1B316008C2BDD /* ForeignKey.swift in Sources */,
5674A7081F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift in Sources */,
56AE64132229A53700AD1B0B /* HasOneThroughAssociation.swift in Sources */,
56A2388C1B9C75030082EB20 /* Statement.swift in Sources */,
@@ -3667,6 +3675,7 @@
AAA4DCB0230F1E0600C74B15 /* Configuration.swift in Sources */,
AAA4DCB1230F1E0600C74B15 /* Cursor.swift in Sources */,
AAA4DCB2230F1E0600C74B15 /* DatabasePool.swift in Sources */,
56256EDC25D1B316008C2BDD /* ForeignKey.swift in Sources */,
AAA4DCB3230F1E0600C74B15 /* DatabaseValueConvertible+ReferenceConvertible.swift in Sources */,
AAA4DCB4230F1E0600C74B15 /* HasOneThroughAssociation.swift in Sources */,
AAA4DCB5230F1E0600C74B15 /* Statement.swift in Sources */,
@@ -4027,6 +4036,7 @@
56AE64122229A53700AD1B0B /* HasOneThroughAssociation.swift in Sources */,
56CEB55A1EAA359A00BFAF62 /* SQLOrdering.swift in Sources */,
56A238811B9C75030082EB20 /* DatabaseError.swift in Sources */,
56256ED925D1B316008C2BDD /* ForeignKey.swift in Sources */,
56D51D001EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */,
566475BA1D981AD200FF74B8 /* SQLSpecificExpressible+QueryInterface.swift in Sources */,
5653EB0920944C7C00F46237 /* Association.swift in Sources */,
+109
View File
@@ -0,0 +1,109 @@
/// A ForeignKey helps building associations when GRDB can't infer a foreign
/// key from the database schema.
///
/// Sometimes the database schema does not define any foreign key between two
/// tables. And sometimes, there are several foreign keys from a table
/// to another:
///
/// | Table book | | Table person |
/// | ------------ | | ------------ |
/// | id | +-->• id |
/// | authorId •---+ | name |
/// | translatorId •---+
/// | title |
///
/// When this happens, associations can't be automatically inferred from the
/// database schema. GRDB will complain with a fatal error such as "Ambiguous
/// foreign key from book to person", or "Could not infer foreign key from book
/// to person".
///
/// Your help is needed. You have to instruct GRDB which foreign key to use:
///
/// struct Book: TableRecord {
/// // Define foreign keys
/// static let authorForeignKey = ForeignKey(["authorId"]))
/// static let translatorForeignKey = ForeignKey(["translatorId"]))
///
/// // Use foreign keys to define associations:
/// static let author = belongsTo(Person.self, using: authorForeignKey)
/// static let translator = belongsTo(Person.self, using: translatorForeignKey)
/// }
///
/// Foreign keys are always defined from the table that contains the columns at
/// the origin of the foreign key. Person's symmetric HasMany associations reuse
/// Book's foreign keys:
///
/// struct Person: TableRecord {
/// static let writtenBooks = hasMany(Book.self, using: Book.authorForeignKey)
/// static let translatedBooks = hasMany(Book.self, using: Book.translatorForeignKey)
/// }
///
/// Foreign keys can also be defined from query interface columns:
///
/// struct Book: TableRecord {
/// enum Columns: String, ColumnExpression {
/// case id, title, authorId, translatorId
/// }
///
/// static let authorForeignKey = ForeignKey([Columns.authorId]))
/// static let translatorForeignKey = ForeignKey([Columns.translatorId]))
/// }
///
/// When the destination table of a foreign key does not define any primary key,
/// you need to provide the full definition of a foreign key:
///
/// struct Book: TableRecord {
/// static let authorForeignKey = ForeignKey(["authorId"], to: ["id"]))
/// static let author = belongsTo(Person.self, using: authorForeignKey)
/// }
public struct ForeignKey: Equatable {
var originColumns: [String]
var destinationColumns: [String]?
/// Creates a ForeignKey intended to define a record association.
///
/// struct Book: TableRecord {
/// // Define foreign keys
/// static let authorForeignKey = ForeignKey(["authorId"]))
/// static let translatorForeignKey = ForeignKey(["translatorId"]))
///
/// // Use foreign keys to define associations:
/// static let author = belongsTo(Person.self, using: authorForeignKey)
/// static let translator = belongsTo(Person.self, using: translatorForeignKey)
/// }
///
/// - parameter originColumns: The columns at the origin of the foreign key.
/// - parameter destinationColumns: The columns at the destination of the
/// foreign key. When nil (the default), GRDB automatically uses the
/// primary key.
public init(_ originColumns: [String], to destinationColumns: [String]? = nil) {
self.originColumns = originColumns
self.destinationColumns = destinationColumns
}
/// Creates a ForeignKey intended to define a record association.
///
/// struct Book: TableRecord {
/// // Define columns
/// enum Columns: String, ColumnExpression {
/// case id, title, authorId, translatorId
/// }
///
/// // Define foreign keys
/// static let authorForeignKey = ForeignKey([Columns.authorId]))
/// static let translatorForeignKey = ForeignKey([Columns.translatorId]))
///
/// // Use foreign keys to define associations:
/// static let author = belongsTo(Person.self, using: authorForeignKey)
/// static let translator = belongsTo(Person.self, using: translatorForeignKey)
/// }
///
/// - parameter originColumns: The columns at the origin of the foreign key.
/// - parameter destinationColumns: The columns at the destination of the
/// foreign key. When nil (the default), GRDB automatically uses the
/// primary key.
public init(_ originColumns: [ColumnExpression], to destinationColumns: [ColumnExpression]? = nil) {
self.init(originColumns.map(\.name), to: destinationColumns?.map(\.name))
}
}
@@ -371,114 +371,6 @@ extension TableRecord {
}
}
/// A ForeignKey helps building associations when GRDB can't infer a foreign
/// key from the database schema.
///
/// Sometimes the database schema does not define any foreign key between two
/// tables. And sometimes, there are several foreign keys from a table
/// to another:
///
/// | Table book | | Table person |
/// | ------------ | | ------------ |
/// | id | +-->• id |
/// | authorId •---+ | name |
/// | translatorId •---+
/// | title |
///
/// When this happens, associations can't be automatically inferred from the
/// database schema. GRDB will complain with a fatal error such as "Ambiguous
/// foreign key from book to person", or "Could not infer foreign key from book
/// to person".
///
/// Your help is needed. You have to instruct GRDB which foreign key to use:
///
/// struct Book: TableRecord {
/// // Define foreign keys
/// static let authorForeignKey = ForeignKey(["authorId"]))
/// static let translatorForeignKey = ForeignKey(["translatorId"]))
///
/// // Use foreign keys to define associations:
/// static let author = belongsTo(Person.self, using: authorForeignKey)
/// static let translator = belongsTo(Person.self, using: translatorForeignKey)
/// }
///
/// Foreign keys are always defined from the table that contains the columns at
/// the origin of the foreign key. Person's symmetric HasMany associations reuse
/// Book's foreign keys:
///
/// struct Person: TableRecord {
/// static let writtenBooks = hasMany(Book.self, using: Book.authorForeignKey)
/// static let translatedBooks = hasMany(Book.self, using: Book.translatorForeignKey)
/// }
///
/// Foreign keys can also be defined from query interface columns:
///
/// struct Book: TableRecord {
/// enum Columns: String, ColumnExpression {
/// case id, title, authorId, translatorId
/// }
///
/// static let authorForeignKey = ForeignKey([Columns.authorId]))
/// static let translatorForeignKey = ForeignKey([Columns.translatorId]))
/// }
///
/// When the destination table of a foreign key does not define any primary key,
/// you need to provide the full definition of a foreign key:
///
/// struct Book: TableRecord {
/// static let authorForeignKey = ForeignKey(["authorId"], to: ["id"]))
/// static let author = belongsTo(Person.self, using: authorForeignKey)
/// }
public struct ForeignKey: Equatable {
var originColumns: [String]
var destinationColumns: [String]?
/// Creates a ForeignKey intended to define a record association.
///
/// struct Book: TableRecord {
/// // Define foreign keys
/// static let authorForeignKey = ForeignKey(["authorId"]))
/// static let translatorForeignKey = ForeignKey(["translatorId"]))
///
/// // Use foreign keys to define associations:
/// static let author = belongsTo(Person.self, using: authorForeignKey)
/// static let translator = belongsTo(Person.self, using: translatorForeignKey)
/// }
///
/// - parameter originColumns: The columns at the origin of the foreign key.
/// - parameter destinationColumns: The columns at the destination of the
/// foreign key. When nil (the default), GRDB automatically uses the
/// primary key.
public init(_ originColumns: [String], to destinationColumns: [String]? = nil) {
self.originColumns = originColumns
self.destinationColumns = destinationColumns
}
/// Creates a ForeignKey intended to define a record association.
///
/// struct Book: TableRecord {
/// // Define columns
/// enum Columns: String, ColumnExpression {
/// case id, title, authorId, translatorId
/// }
///
/// // Define foreign keys
/// static let authorForeignKey = ForeignKey([Columns.authorId]))
/// static let translatorForeignKey = ForeignKey([Columns.translatorId]))
///
/// // Use foreign keys to define associations:
/// static let author = belongsTo(Person.self, using: authorForeignKey)
/// static let translator = belongsTo(Person.self, using: translatorForeignKey)
/// }
///
/// - parameter originColumns: The columns at the origin of the foreign key.
/// - parameter destinationColumns: The columns at the destination of the
/// foreign key. When nil (the default), GRDB automatically uses the
/// primary key.
public init(_ originColumns: [ColumnExpression], to destinationColumns: [ColumnExpression]? = nil) {
self.init(originColumns.map(\.name), to: destinationColumns?.map(\.name))
}
}
extension TableRecord where Self: EncodableRecord {
/// Creates a request that fetches the associated record(s).
+6
View File
@@ -68,6 +68,8 @@
562393701DEE0CD200A6B01F /* FlattenCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393681DEE0CD200A6B01F /* FlattenCursorTests.swift */; };
562393751DEE104400A6B01F /* MapCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393711DEE104400A6B01F /* MapCursorTests.swift */; };
562393791DEE104400A6B01F /* MapCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393711DEE104400A6B01F /* MapCursorTests.swift */; };
56256EDE25D1BC07008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256EDD25D1BC07008C2BDD /* ForeignKey.swift */; };
56256EDF25D1BC07008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256EDD25D1BC07008C2BDD /* ForeignKey.swift */; };
562756461E963AAC0035B653 /* DatabaseWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562756421E963AAC0035B653 /* DatabaseWriterTests.swift */; };
5627564A1E963AAC0035B653 /* DatabaseWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562756421E963AAC0035B653 /* DatabaseWriterTests.swift */; };
562EA8291F17B2AC00FA528C /* CompilationProtocolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562EA8251F17B2AC00FA528C /* CompilationProtocolTests.swift */; };
@@ -803,6 +805,7 @@
562393681DEE0CD200A6B01F /* FlattenCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlattenCursorTests.swift; sourceTree = "<group>"; };
562393711DEE104400A6B01F /* MapCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapCursorTests.swift; sourceTree = "<group>"; };
5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTestCase.swift; sourceTree = "<group>"; };
56256EDD25D1BC07008C2BDD /* ForeignKey.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForeignKey.swift; sourceTree = "<group>"; };
562756421E963AAC0035B653 /* DatabaseWriterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseWriterTests.swift; sourceTree = "<group>"; };
562EA8251F17B2AC00FA528C /* CompilationProtocolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompilationProtocolTests.swift; sourceTree = "<group>"; };
562EA82E1F17B9EB00FA528C /* CompilationSubClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompilationSubClassTests.swift; sourceTree = "<group>"; };
@@ -1483,6 +1486,7 @@
5656A8252295BD56001FF3FF /* QueryInterface */ = {
isa = PBXGroup;
children = (
56256EDD25D1BC07008C2BDD /* ForeignKey.swift */,
5656A82F2295BD56001FF3FF /* FTS3+QueryInterface.swift */,
5656A8282295BD56001FF3FF /* FTS5+QueryInterface.swift */,
5656A8302295BD56001FF3FF /* Request */,
@@ -2322,6 +2326,7 @@
56D51D051EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */,
56DF37A823D77AA0009AAA05 /* Refinable.swift in Sources */,
F3BA80151CFB2876003DC1BA /* DatabaseWriter.swift in Sources */,
56256EDF25D1BC07008C2BDD /* ForeignKey.swift in Sources */,
F3BA800A1CFB286A003DC1BA /* Configuration.swift in Sources */,
5656A86C2295BD56001FF3FF /* HasManyAssociation.swift in Sources */,
5656A88E2295BD56001FF3FF /* SQLSelectable.swift in Sources */,
@@ -2682,6 +2687,7 @@
56D51D021EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */,
56DF37A723D77AA0009AAA05 /* Refinable.swift in Sources */,
F3BA80711CFB2E55003DC1BA /* DatabaseWriter.swift in Sources */,
56256EDE25D1BC07008C2BDD /* ForeignKey.swift in Sources */,
F3BA80661CFB2E55003DC1BA /* Configuration.swift in Sources */,
5656A86B2295BD56001FF3FF /* HasManyAssociation.swift in Sources */,
5656A88D2295BD56001FF3FF /* SQLSelectable.swift in Sources */,