2017-07-17 04:36:38 +02:00
|
|
|
//
|
2017-07-17 05:51:08 +02:00
|
|
|
// AttachmentsTable.swift
|
2017-07-17 04:36:38 +02:00
|
|
|
// Database
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/15/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSDatabase
|
|
|
|
import Data
|
|
|
|
|
2017-09-02 19:13:37 +02:00
|
|
|
final class AttachmentsTable: DatabaseRelatedObjectsTable {
|
2017-07-17 04:36:38 +02:00
|
|
|
|
2017-07-29 21:08:10 +02:00
|
|
|
let name: String
|
2017-08-21 02:46:15 +02:00
|
|
|
let databaseIDKey = DatabaseKey.attachmentID
|
2017-09-13 06:19:45 +02:00
|
|
|
var cache = [String: Attachment]()
|
2017-07-29 21:13:38 +02:00
|
|
|
|
2017-08-21 00:56:58 +02:00
|
|
|
init(name: String) {
|
2017-07-29 21:08:10 +02:00
|
|
|
|
|
|
|
self.name = name
|
|
|
|
}
|
2017-07-17 05:51:08 +02:00
|
|
|
|
2017-09-13 06:19:45 +02:00
|
|
|
// MARK: DatabaseRelatedObjectsTable
|
|
|
|
|
|
|
|
func fetchObjectsWithIDs(_ databaseIDs: Set<String>, in database: FMDatabase) -> [DatabaseObject]? {
|
|
|
|
|
|
|
|
if databaseIDs.isEmpty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var cachedAttachments = Set<Attachment>()
|
|
|
|
var databaseIDsToFetch = Set<String>()
|
|
|
|
|
|
|
|
for attachmentID in databaseIDs {
|
|
|
|
if let cachedAttachment = cache[attachmentID] {
|
|
|
|
cachedAttachments.insert(cachedAttachment)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
databaseIDsToFetch.insert(attachmentID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if databaseIDsToFetch.isEmpty {
|
|
|
|
return cachedAttachments.databaseObjects()
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let resultSet = selectRowsWhere(key: databaseIDKey, inValues: Array(databaseIDsToFetch), in: database) else {
|
|
|
|
return cachedAttachments.databaseObjects()
|
|
|
|
}
|
|
|
|
let fetchedDatabaseObjects = objectsWithResultSet(resultSet)
|
|
|
|
let fetchedAttachments = Set(fetchedDatabaseObjects.map { $0 as Attachment })
|
|
|
|
cacheAttachments(fetchedAttachments)
|
|
|
|
|
|
|
|
let allAttachments = cachedAttachments.union(fetchedAttachments)
|
|
|
|
return allAttachments.databaseObjects()
|
|
|
|
}
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
func objectWithRow(_ row: FMResultSet) -> DatabaseObject? {
|
2017-07-29 20:26:19 +02:00
|
|
|
|
2017-08-21 07:43:46 +02:00
|
|
|
if let attachment = attachmentWithRow(row) {
|
|
|
|
return attachment as DatabaseObject
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func save(_ objects: [DatabaseObject], in database: FMDatabase) {
|
2017-09-13 06:19:45 +02:00
|
|
|
|
|
|
|
let attachments = objects.map { $0 as! Attachment }
|
|
|
|
|
|
|
|
// Attachments in cache must already exist in database. Filter them out.
|
|
|
|
let attachmentsToSave = Set(attachments.filter { (attachment) -> Bool in
|
|
|
|
if let _ = cache[attachment.attachmentID] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
cacheAttachments(attachmentsToSave)
|
|
|
|
|
|
|
|
insertRows(attachmentsToSave.databaseDictionaries(), insertType: .orIgnore, in: database)
|
2017-07-17 04:36:38 +02:00
|
|
|
}
|
2017-08-21 02:46:15 +02:00
|
|
|
}
|
2017-07-17 04:36:38 +02:00
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
private extension AttachmentsTable {
|
2017-07-17 04:36:38 +02:00
|
|
|
|
2017-09-13 06:19:45 +02:00
|
|
|
func cacheAttachments(_ attachments: Set<Attachment>) {
|
2017-07-17 04:36:38 +02:00
|
|
|
|
2017-09-13 06:19:45 +02:00
|
|
|
for attachment in attachments {
|
|
|
|
cache[attachment.attachmentID] = attachment
|
2017-08-21 07:43:46 +02:00
|
|
|
}
|
2017-09-13 06:19:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func attachmentWithRow(_ row: FMResultSet) -> Attachment? {
|
|
|
|
|
|
|
|
// attachmentID is non-null in database schema.
|
|
|
|
let attachmentID = row.string(forColumn: DatabaseKey.attachmentID)!
|
2017-09-05 02:10:02 +02:00
|
|
|
return Attachment(attachmentID: attachmentID, row: row)
|
2017-07-17 04:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 06:19:45 +02:00
|
|
|
|