diff --git a/Frameworks/Database/StatusesTable.swift b/Frameworks/Database/StatusesTable.swift index a8da9c4d0..79360ab81 100644 --- a/Frameworks/Database/StatusesTable.swift +++ b/Frameworks/Database/StatusesTable.swift @@ -138,25 +138,13 @@ private extension StatusesManager { func saveStatuses(_ statuses: Set) { - let statusArray = statuses.map { (oneStatus) -> NSDictionary in - return oneStatus.databaseDictionary() - } - - queue.update { (database: FMDatabase!) -> Void in - - statusArray.forEach { (oneStatusDictionary) in - - let _ = database.rs_insertRow(with: oneStatusDictionary as [NSObject: AnyObject], insertType: RSDatabaseInsertOrIgnore, tableName: "statuses") - } - } + let statusArray = statuses.map { $0.databaseDictionary() } + insertRows(statusArray, insertType: .insertOrIgnore) } private func updateArticleStatusesInDatabase(_ articleIDs: Set, statusKey: ArticleStatusKey, flag: Bool) { - - queue.update { (database: FMDatabase!) -> Void in - - let _ = database.rs_updateRows(withValue: NSNumber(value: flag), valueKey: statusKey.rawValue, whereKey: articleIDKey, inValues: Array(articleIDs), tableName: statusesTableName) - } + + updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey.rawValue, whereKey: DatabaseKey.articleID, matches: Array(articleIDs)) } // MARK: Creating diff --git a/Frameworks/RSDatabase/DatabaseTable.swift b/Frameworks/RSDatabase/DatabaseTable.swift index b67fb714c..ce4f29585 100644 --- a/Frameworks/RSDatabase/DatabaseTable.swift +++ b/Frameworks/RSDatabase/DatabaseTable.swift @@ -17,12 +17,16 @@ public protocol DatabaseTable { } extension DatabaseTable { - + + // MARK: Fetching + public func selectRowsWhere(key: String, equals value: Any, in database: FMDatabase) -> FMResultSet? { - return database.rs_selectRowsWhereKey(key, equalsValue: value, tableName: self.name) + return database.rs_selectRowsWhereKey(key, equalsValue: value, tableName: name) } - + + // MARK: Deleting + public func deleteRowsWhere(key: String, equalsAnyValue values: [Any], in database: FMDatabase) { if values.isEmpty { @@ -32,7 +36,29 @@ extension DatabaseTable { database.rs_deleteRowsWhereKey(key, inValues: values, tableName: name) } - // MARK: Counts + // MARK: Updating + + public func updateRowsWithValue(_ value: Any, valueKey: String, whereKey: String, matches: [Any]) { + + queue.update { (database: FMDatabase!) in + + let _ = database.rs_updateRows(withValue: value, valueKey: valueKey, whereKey: whereKey, inValues: matches, tableName: self.name) + } + } + + // MARK: Saving + + public func insertRows(_ dictionaries: [NSDictionary], insertType: RSDatabaseInsertType) { + + queue.update { (database: FMDatabase!) -> Void in + + dictionaries.forEach { (oneDictionary) in + let _ = database.rs_insertRow(with: oneDictionary as [NSObject: AnyObject], insertType: insertType, tableName: self.name) + } + } + } + + // MARK: Counting func numberWithCountResultSet(_ resultSet: FMResultSet?) -> Int {