Make progress on relationships.

This commit is contained in:
Brent Simmons 2017-08-06 21:46:47 -07:00
parent 570d70d8fe
commit 4223808c74
2 changed files with 36 additions and 2 deletions

View File

@ -78,8 +78,8 @@ private extension AuthorsTable {
func fetchAuthorsForArticleIDs(_ articleIDs: Set<String>, _ database: FMDatabase) -> [String: Set<Author>]? {
let lookupValueDictionary = authorsLookupTable.fetchLookupTableDictionary(articleIDs, database)
let authorIDs = Set(lookupValues.map { $0.primaryID })
let lookupTableDictionary = authorsLookupTable.fetchLookupTableDictionary(articleIDs, database)
let authorIDs = authorsLookupTable.primaryIDsInLookupTableDictionary(lookupTableDictionary)
if authorIDs.isEmpty {
return nil
}

View File

@ -64,6 +64,33 @@ public final class LookupTable {
return lookupTableDictionary(with: lookupValues)
}
public func attachRelationships<T,U>(_ objects: [T], idKeyPath: KeyPath<T,String>, relationshipKeyPath: KeyPath<T,[U]>, table: DatabaseTable, lookupTableDictionary: LookupTableDictionary, database: FMDatabase) {
let primaryIDs = primaryIDsInLookupTableDictionary(lookupTableDictionary)
let relatedObjects = table.fetchObjectsWithIDs(primaryIDs, database)
for object in objects {
let identifier = object[keyPath: idKeyPath]
if let lookupValues = lookupTableDictionary[identifier] {
let relatedObjects = lookupValues.flatMap{ (lookupValue) -> U? in
<#code#>
}
}
}
}
func primaryIDsInLookupTableDictionary(_ lookupTableDictionary: LookupTableDictionary) -> Set<String> {
var primaryIDs = Set<String>()
for (_, lookupValues) in lookupTableDictionary {
primaryIDs.formUnion(lookupValues.primaryIDs())
}
return primaryIDs
}
public func removeLookupsForForeignIDs(_ foreignIDs: Set<String>, _ database: FMDatabase) {
let foreignIDsToRemove = foreignIDs.subtracting(foreignIDsWithNoRelationship)
@ -156,3 +183,10 @@ public struct LookupValue: Hashable {
}
}
private extension Set where Element == LookupValue {
func primaryIDs() -> Set<String> {
return Set(map { $0.primaryID })
}
}