Start moving common functions to DatabaseTable.

This commit is contained in:
Brent Simmons 2017-07-29 12:29:05 -07:00
parent da9a974dff
commit 77ba434878
4 changed files with 62 additions and 58 deletions

View File

@ -1,6 +1,6 @@
// //
// Article.swift // Article.swift
// DataModel // Data
// //
// Created by Brent Simmons on 7/1/17. // Created by Brent Simmons on 7/1/17.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved. // Copyright © 2017 Ranchero Software, LLC. All rights reserved.

View File

@ -7,22 +7,22 @@
// //
import Foundation import Foundation
import RSDatabase
import Data import Data
final class ArticlesTable: DatabaseTable { final class ArticlesTable: DatabaseTable {
let name: String let name: String
let queue: RSDatabaseQueue let queue: RSDatabaseQueue
private let cachedArticles: NSMapTable<NSString, Article> = NSMapTable.weakToWeakObjects()
init(name: String, queue: RSDatabaseQueue) { init(name: String, queue: RSDatabaseQueue) {
self.name = name self.name = name
self.queue = queue self.queue = queue
} }
private let cachedArticles: NSMapTable<NSString, Article> = NSMapTable.weakToWeakObjects() func uniquedArticles(_ fetchedArticles: Set<Article>, statusesTable: StatusesTable) -> Set<Article> {
func uniquedArticles(_ fetchedArticles: Set<Article>, statusesManager: StatusesManager) -> Set<Article> {
var articles = Set<Article>() var articles = Set<Article>()
@ -30,7 +30,7 @@ final class ArticlesTable: DatabaseTable {
assert(oneArticle.status != nil) assert(oneArticle.status != nil)
if let existingArticle = cachedArticle(oneArticle.articleID) { if let existingArticle = cachedArticle(oneArticle.databaseID) {
articles.insert(existingArticle) articles.insert(existingArticle)
} }
else { else {
@ -39,23 +39,51 @@ final class ArticlesTable: DatabaseTable {
} }
} }
statusesManager.attachCachedStatuses(articles) statusesTable.attachCachedStatuses(articles)
return articles return articles
} }
typealias FeedCountCallback = (Int) -> Void
func numberOfArticlesWithFeedID(_ feedID: String, callback: @escaping FeedCountCallback) {
queue.fetch { (database: FMDatabase!)
let sql = "select count(*) from articles where feedID = ?;"
var numberOfArticles = -1
if let resultSet = database.executeQuery(sql, withArgumentsIn: [feedID]) {
while (resultSet.next()) {
numberOfArticles = resultSet.long(forColumnIndex: 0)
break
}
}
DispatchQueue.main.async() {
callback(numberOfArticles)
}
}
}
}
private extension ArticlesTable {
func cachedArticle(_ articleID: String) -> Article? { func cachedArticle(_ articleID: String) -> Article? {
return cachedArticles.object(forKey: articleID as NSString) return cachedArticles.object(forKey: articleID as NSString)
} }
func cacheArticle(_ article: Article) { func cacheArticle(_ article: Article) {
cachedArticles.setObject(article, forKey: article.articleID as NSString) cachedArticles.setObject(article, forKey: article.databaseID as NSString)
} }
func cacheArticles(_ articles: Set<Article>) { func cacheArticles(_ articles: Set<Article>) {
articles.forEach { cacheArticle($0) } articles.forEach { cacheArticle($0) }
} }
} }

View File

@ -26,7 +26,7 @@ final class Database {
fileprivate let queue: RSDatabaseQueue fileprivate let queue: RSDatabaseQueue
private let databaseFile: String private let databaseFile: String
private let attachmentsTable = AttachmentsTable(DatabaseTableName.attachments) private let attachmentsTable: AttachmentsTable
fileprivate let statusesManager: StatusesManager fileprivate let statusesManager: StatusesManager
fileprivate let articleCache = ArticlesManager() fileprivate let articleCache = ArticlesManager()
fileprivate var articleArrivalCutoffDate = NSDate.rs_dateWithNumberOfDays(inThePast: 3 * 31)! fileprivate var articleArrivalCutoffDate = NSDate.rs_dateWithNumberOfDays(inThePast: 3 * 31)!
@ -38,6 +38,7 @@ final class Database {
self.delegate = delegate self.delegate = delegate
self.databaseFile = databaseFile self.databaseFile = databaseFile
self.queue = RSDatabaseQueue(filepath: databaseFile, excludeFromBackup: false) self.queue = RSDatabaseQueue(filepath: databaseFile, excludeFromBackup: false)
self.attachmentsTable = AttachmentsTable(name: DatabaseTableName.attachments, queue: queue)
self.statusesManager = StatusesManager(queue: self.queue) self.statusesManager = StatusesManager(queue: self.queue)
let createStatementsPath = Bundle(for: type(of: self)).path(forResource: "CreateStatements", ofType: "sql")! let createStatementsPath = Bundle(for: type(of: self)).path(forResource: "CreateStatements", ofType: "sql")!
@ -358,23 +359,6 @@ private extension Database {
// MARK: Unread counts // MARK: Unread counts
func numberWithCountResultSet(_ resultSet: FMResultSet?) -> Int {
guard let resultSet = resultSet else {
return 0
}
if resultSet.next() {
return Int(resultSet.int(forColumnIndex: 0))
}
return 0
}
func numberWithSQLAndParameters(_ sql: String, parameters: [Any], _ database: FMDatabase) -> Int {
let resultSet = database.executeQuery(sql, withArgumentsIn: parameters)
return numberWithCountResultSet(resultSet)
}
func numberOfArticles(_ feedID: String, _ database: FMDatabase) -> Int { func numberOfArticles(_ feedID: String, _ database: FMDatabase) -> Int {
let sql = "select count(*) from articles where feedID = ?;" let sql = "select count(*) from articles where feedID = ?;"
@ -448,8 +432,7 @@ private extension Database {
return articlesSet return articlesSet
} }
typealias FeedCountCallback = (Int) -> Void
func feedIDsFromArticles(_ articles: Set<Article>) -> Set<String> { func feedIDsFromArticles(_ articles: Set<Article>) -> Set<String> {
return Set(articles.map { $0.feedID }) return Set(articles.map { $0.feedID })
@ -462,28 +445,4 @@ private extension Database {
return return
} }
} }
func numberOfArticlesInFeedID(_ feedID: String, callback: @escaping FeedCountCallback) {
queue.fetch { (database: FMDatabase!) -> Void in
let sql = "select count(*) from articles where feedID = ?;"
logSQL(sql)
var numberOfArticles = -1
if let resultSet = database.executeQuery(sql, withArgumentsIn: [feedID]) {
while (resultSet.next()) {
numberOfArticles = resultSet.long(forColumnIndex: 0)
break
}
}
DispatchQueue.main.async() {
callback(numberOfArticles)
}
}
}
} }

View File

@ -31,4 +31,21 @@ extension DatabaseTable {
database.rs_deleteRowsWhereKey(key, inValues: values, tableName: name) database.rs_deleteRowsWhereKey(key, inValues: values, tableName: name)
} }
// MARK: Counts
func numberWithCountResultSet(_ resultSet: FMResultSet?) -> Int {
if let resultSet = resultSet, resultSet.next() {
return Int(resultSet.int(forColumnIndex: 0))
}
return 0
}
func numberWithSQLAndParameters(_ sql: String, _ parameters: [Any], in database: FMDatabase) -> Int {
let resultSet = database.executeQuery(sql, withArgumentsIn: parameters)
return numberWithCountResultSet(resultSet)
}
} }