More progress on relationships.
This commit is contained in:
parent
4223808c74
commit
7edc76cd6e
@ -14,6 +14,8 @@ public protocol DatabaseTable {
|
|||||||
var queue: RSDatabaseQueue {get}
|
var queue: RSDatabaseQueue {get}
|
||||||
|
|
||||||
init(name: String, queue: RSDatabaseQueue)
|
init(name: String, queue: RSDatabaseQueue)
|
||||||
|
|
||||||
|
func fetchObjectsWithIDs<T>(_ databaseIDs: Set<String>, _ database: FMDatabase) -> [T]
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension DatabaseTable {
|
public extension DatabaseTable {
|
||||||
|
@ -15,7 +15,7 @@ import Foundation
|
|||||||
// foreignIDsWithNoRelationship: caches the foreignIDs where it’s known that there’s no relationship.
|
// foreignIDsWithNoRelationship: caches the foreignIDs where it’s known that there’s no relationship.
|
||||||
// lookupsByForeignID: caches the LookupValues for a foreignID.
|
// lookupsByForeignID: caches the LookupValues for a foreignID.
|
||||||
|
|
||||||
typealias LookupTableDictionary = [String: Set<LookupValue>] // key is foreignID
|
public typealias LookupTableDictionary = [String: Set<LookupValue>] // key is foreignID
|
||||||
|
|
||||||
public final class LookupTable {
|
public final class LookupTable {
|
||||||
|
|
||||||
@ -64,20 +64,30 @@ public final class LookupTable {
|
|||||||
return lookupTableDictionary(with: lookupValues)
|
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) {
|
public func attachRelationships<T,U>(to objects: [T], idKeyPath: KeyPath<T,String>, relatedIDKeyPath: KeyPath<U,String>, relationshipKeyPath: KeyPath<T,[U]>, table: DatabaseTable, lookupTableDictionary: LookupTableDictionary, database: FMDatabase) {
|
||||||
|
|
||||||
let primaryIDs = primaryIDsInLookupTableDictionary(lookupTableDictionary)
|
let primaryIDs = primaryIDsInLookupTableDictionary(lookupTableDictionary)
|
||||||
let relatedObjects = table.fetchObjectsWithIDs(primaryIDs, database)
|
if (primaryIDs.isEmpty) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let relatedObjects: [U] = table.fetchObjectsWithIDs(primaryIDs, database)
|
||||||
|
if relatedObjects.isEmpty {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let relatedObjectsDictionary = createRelatedObjectsDictionary(relatedObjects, idKeyPath: relatedIDKeyPath)
|
||||||
|
|
||||||
for object in objects {
|
for object in objects {
|
||||||
let identifier = object[keyPath: idKeyPath]
|
let identifier = object[keyPath: idKeyPath]
|
||||||
if let lookupValues = lookupTableDictionary[identifier] {
|
if let lookupValues = lookupTableDictionary[identifier], !lookupValues.isEmpty {
|
||||||
let relatedObjects = lookupValues.flatMap{ (lookupValue) -> U? in
|
let primaryIDs = lookupValues.primaryIDs()
|
||||||
<#code#>
|
let oneObjectRelatedObjects = primaryIDs.flatMap{ (primaryID) -> U? in
|
||||||
|
return relatedObjectsDictionary[primaryID]
|
||||||
}
|
}
|
||||||
|
object[keyPath: relationshipKeyPath] = oneObjectRelatedObjects
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func primaryIDsInLookupTableDictionary(_ lookupTableDictionary: LookupTableDictionary) -> Set<String> {
|
func primaryIDsInLookupTableDictionary(_ lookupTableDictionary: LookupTableDictionary) -> Set<String> {
|
||||||
@ -109,6 +119,18 @@ public final class LookupTable {
|
|||||||
|
|
||||||
private extension LookupTable {
|
private extension LookupTable {
|
||||||
|
|
||||||
|
func createRelatedObjectsDictionary<T>(_ relatedObjects: [T], idKeyPath: KeyPath<T,String>) -> [String: T] {
|
||||||
|
|
||||||
|
var d = [String: T]()
|
||||||
|
|
||||||
|
for object in relatedObjects {
|
||||||
|
let identifier = object[keyPath: idKeyPath]
|
||||||
|
d[identifier] = object
|
||||||
|
}
|
||||||
|
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
func addToLookupTableDictionary(_ lookupValues: Set<LookupValue>, _ table: inout LookupTableDictionary) {
|
func addToLookupTableDictionary(_ lookupValues: Set<LookupValue>, _ table: inout LookupTableDictionary) {
|
||||||
|
|
||||||
for lookupValue in lookupValues {
|
for lookupValue in lookupValues {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user