2017-09-02 19:11:19 +02:00
|
|
|
|
//
|
|
|
|
|
// DatabaseRelatedObjectsTable.swift
|
|
|
|
|
// RSDatabase
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 9/2/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2017-09-11 22:11:29 +02:00
|
|
|
|
// Protocol for a database table for related objects — authors, tags, and attachments in Evergreen, for instance.
|
|
|
|
|
|
2017-09-02 19:11:19 +02:00
|
|
|
|
public protocol DatabaseRelatedObjectsTable: DatabaseTable {
|
|
|
|
|
|
|
|
|
|
var databaseIDKey: String { get}
|
|
|
|
|
|
2017-09-13 06:19:45 +02:00
|
|
|
|
func fetchObjectsWithIDs(_ databaseIDs: Set<String>, in database: FMDatabase) -> [DatabaseObject]?
|
2017-09-02 19:11:19 +02:00
|
|
|
|
func objectsWithResultSet(_ resultSet: FMResultSet) -> [DatabaseObject]
|
|
|
|
|
func objectWithRow(_ row: FMResultSet) -> DatabaseObject?
|
|
|
|
|
|
|
|
|
|
func save(_ objects: [DatabaseObject], in database: FMDatabase)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public extension DatabaseRelatedObjectsTable {
|
|
|
|
|
|
|
|
|
|
// MARK: Default implementations
|
|
|
|
|
|
2017-09-13 06:19:45 +02:00
|
|
|
|
func fetchObjectsWithIDs(_ databaseIDs: Set<String>, in database: FMDatabase) -> [DatabaseObject]? {
|
2017-09-02 19:11:19 +02:00
|
|
|
|
|
|
|
|
|
guard let resultSet = selectRowsWhere(key: databaseIDKey, inValues: Array(databaseIDs), in: database) else {
|
2017-09-13 06:19:45 +02:00
|
|
|
|
return nil
|
2017-09-02 19:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
return objectsWithResultSet(resultSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func objectsWithResultSet(_ resultSet: FMResultSet) -> [DatabaseObject] {
|
|
|
|
|
|
|
|
|
|
return resultSet.flatMap(objectWithRow)
|
|
|
|
|
}
|
|
|
|
|
}
|