Continue moving functions to DatabaseTable.

This commit is contained in:
Brent Simmons 2017-07-29 12:50:23 -07:00
parent 77ba434878
commit bf11f88ea7
2 changed files with 34 additions and 20 deletions

View File

@ -138,25 +138,13 @@ private extension StatusesManager {
func saveStatuses(_ statuses: Set<ArticleStatus>) { func saveStatuses(_ statuses: Set<ArticleStatus>) {
let statusArray = statuses.map { (oneStatus) -> NSDictionary in let statusArray = statuses.map { $0.databaseDictionary() }
return oneStatus.databaseDictionary() insertRows(statusArray, insertType: .insertOrIgnore)
}
queue.update { (database: FMDatabase!) -> Void in
statusArray.forEach { (oneStatusDictionary) in
let _ = database.rs_insertRow(with: oneStatusDictionary as [NSObject: AnyObject], insertType: RSDatabaseInsertOrIgnore, tableName: "statuses")
}
}
} }
private func updateArticleStatusesInDatabase(_ articleIDs: Set<String>, statusKey: ArticleStatusKey, flag: Bool) { private func updateArticleStatusesInDatabase(_ articleIDs: Set<String>, statusKey: ArticleStatusKey, flag: Bool) {
queue.update { (database: FMDatabase!) -> Void in updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey.rawValue, whereKey: DatabaseKey.articleID, matches: Array(articleIDs))
let _ = database.rs_updateRows(withValue: NSNumber(value: flag), valueKey: statusKey.rawValue, whereKey: articleIDKey, inValues: Array(articleIDs), tableName: statusesTableName)
}
} }
// MARK: Creating // MARK: Creating

View File

@ -18,11 +18,15 @@ public protocol DatabaseTable {
extension DatabaseTable { extension DatabaseTable {
// MARK: Fetching
public func selectRowsWhere(key: String, equals value: Any, in database: FMDatabase) -> FMResultSet? { 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) { public func deleteRowsWhere(key: String, equalsAnyValue values: [Any], in database: FMDatabase) {
if values.isEmpty { if values.isEmpty {
@ -32,7 +36,29 @@ extension DatabaseTable {
database.rs_deleteRowsWhereKey(key, inValues: values, tableName: name) 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 { func numberWithCountResultSet(_ resultSet: FMResultSet?) -> Int {