Make progress on getting StatusTable to compile.

This commit is contained in:
Brent Simmons 2017-07-30 11:55:46 -07:00
parent 031617809f
commit c76d42b42f
3 changed files with 21 additions and 24 deletions

View File

@ -16,7 +16,7 @@ final class StatusesTable: DatabaseTable {
let name: String let name: String
let queue: RSDatabaseQueue let queue: RSDatabaseQueue
let cache = ObjectCache<ArticleStatus>(keyPathForID: \ArticleStatus.articleID) private let cache = ObjectCache<ArticleStatus>(keyPathForID: \ArticleStatus.articleID)
init(name: String, queue: RSDatabaseQueue) { init(name: String, queue: RSDatabaseQueue) {
@ -35,7 +35,7 @@ final class StatusesTable: DatabaseTable {
articles.forEach { (oneArticle) in articles.forEach { (oneArticle) in
if let cachedStatus = cache[oneArticle.articleID] { if let cachedStatus = cache[oneArticle.databaseID] {
oneArticle.status = cachedStatus oneArticle.status = cachedStatus
} }
else if let oneArticleStatus = oneArticle.status { else if let oneArticleStatus = oneArticle.status {
@ -59,7 +59,7 @@ final class StatusesTable: DatabaseTable {
DispatchQueue.main.async { DispatchQueue.main.async {
cache.addObjectsNotCached(Array(statuses)) self.cache.addObjectsNotCached(Array(statuses))
let newArticleIDs = self.articleIDsMissingStatuses(articleIDs) let newArticleIDs = self.articleIDsMissingStatuses(articleIDs)
self.createStatusForNewArticleIDs(newArticleIDs) self.createStatusForNewArticleIDs(newArticleIDs)
@ -79,11 +79,7 @@ final class StatusesTable: DatabaseTable {
} }
} }
// MARK: - Private private extension StatusesTable {
private let statusesTableName = "statuses"
private extension StatusesManager {
// MARK: Marking // MARK: Marking
@ -110,15 +106,11 @@ private extension StatusesManager {
func fetchStatusesForArticleIDs(_ articleIDs: Set<String>, database: FMDatabase) -> Set<ArticleStatus> { func fetchStatusesForArticleIDs(_ articleIDs: Set<String>, database: FMDatabase) -> Set<ArticleStatus> {
guard !articleIDs.isEmpty else { if !articleIDs.isEmpty, let resultSet = selectRowsWhere(key: DatabaseKey.articleID, inValues: Array(articleIDs), in: database) {
return Set<ArticleStatus>() return articleStatusesWithResultSet(resultSet)
} }
guard let resultSet = database.rs_selectRowsWhereKey(DatabaseKey.articleID, inValues: Array(articleIDs), tableName: statusesTableName) else { return Set<ArticleStatus>()
return Set<ArticleStatus>()
}
return articleStatusesWithResultSet(resultSet)
} }
func articleStatusesWithResultSet(_ resultSet: FMResultSet) -> Set<ArticleStatus> { func articleStatusesWithResultSet(_ resultSet: FMResultSet) -> Set<ArticleStatus> {
@ -139,12 +131,12 @@ private extension StatusesManager {
func saveStatuses(_ statuses: Set<ArticleStatus>) { func saveStatuses(_ statuses: Set<ArticleStatus>) {
let statusArray = statuses.map { $0.databaseDictionary() } let statusArray = statuses.map { $0.databaseDictionary() }
insertRows(statusArray, insertType: .insertOrIgnore) insertRows(statusArray, insertType: .orIgnore)
} }
private func updateArticleStatusesInDatabase(_ articleIDs: Set<String>, statusKey: ArticleStatusKey, flag: Bool) { private func updateArticleStatusesInDatabase(_ articleIDs: Set<String>, statusKey: String, flag: Bool) {
updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey.rawValue, whereKey: DatabaseKey.articleID, matches: Array(articleIDs)) updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey, whereKey: DatabaseKey.articleID, matches: Array(articleIDs))
} }
// MARK: Creating // MARK: Creating
@ -152,7 +144,7 @@ private extension StatusesManager {
func createStatusForNewArticleIDs(_ articleIDs: Set<String>) { func createStatusForNewArticleIDs(_ articleIDs: Set<String>) {
let now = Date() let now = Date()
let statuses = articleIDs.map { (oneArticleID) -> LocalArticleStatus in let statuses = articleIDs.map { (oneArticleID) -> ArticleStatus in
return ArticleStatus(articleID: oneArticleID, read: false, starred: false, userDeleted: false, dateArrived: now) return ArticleStatus(articleID: oneArticleID, read: false, starred: false, userDeleted: false, dateArrived: now)
} }
cache.addObjectsNotCached(statuses) cache.addObjectsNotCached(statuses)

View File

@ -16,7 +16,7 @@ public protocol DatabaseTable {
init(name: String, queue: RSDatabaseQueue) init(name: String, queue: RSDatabaseQueue)
} }
extension DatabaseTable { public extension DatabaseTable {
// MARK: Fetching // MARK: Fetching
@ -25,6 +25,11 @@ extension DatabaseTable {
return database.rs_selectRowsWhereKey(key, equalsValue: value, tableName: name) return database.rs_selectRowsWhereKey(key, equalsValue: value, tableName: name)
} }
public func selectRowsWhere(key: String, inValues values: [Any], in database: FMDatabase) -> FMResultSet? {
return database.rs_selectRowsWhereKey(key, inValues: values, tableName: name)
}
// MARK: Deleting // MARK: Deleting
public func deleteRowsWhere(key: String, equalsAnyValue values: [Any], in database: FMDatabase) { public func deleteRowsWhere(key: String, equalsAnyValue values: [Any], in database: FMDatabase) {

View File

@ -10,11 +10,11 @@
@import Foundation; @import Foundation;
typedef enum _RSDatabaseInsertType { typedef NS_ENUM(NSInteger, RSDatabaseInsertType) {
RSDatabaseInsert, RSDatabaseInsertNormal,
RSDatabaseInsertOrReplace, RSDatabaseInsertOrReplace,
RSDatabaseInsertOrIgnore RSDatabaseInsertOrIgnore
} RSDatabaseInsertType; };
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN