mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2024-12-24 16:51:19 +01:00
Start moving common functions to DatabaseTable.
This commit is contained in:
parent
da9a974dff
commit
77ba434878
@ -1,6 +1,6 @@
|
||||
//
|
||||
// Article.swift
|
||||
// DataModel
|
||||
// Data
|
||||
//
|
||||
// Created by Brent Simmons on 7/1/17.
|
||||
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
||||
|
@ -7,22 +7,22 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RSDatabase
|
||||
import Data
|
||||
|
||||
final class ArticlesTable: DatabaseTable {
|
||||
|
||||
let name: String
|
||||
let queue: RSDatabaseQueue
|
||||
private let cachedArticles: NSMapTable<NSString, Article> = NSMapTable.weakToWeakObjects()
|
||||
|
||||
init(name: String, queue: RSDatabaseQueue) {
|
||||
|
||||
self.name = name
|
||||
self.queue = queue
|
||||
}
|
||||
|
||||
private let cachedArticles: NSMapTable<NSString, Article> = NSMapTable.weakToWeakObjects()
|
||||
|
||||
func uniquedArticles(_ fetchedArticles: Set<Article>, statusesManager: StatusesManager) -> Set<Article> {
|
||||
|
||||
func uniquedArticles(_ fetchedArticles: Set<Article>, statusesTable: StatusesTable) -> Set<Article> {
|
||||
|
||||
var articles = Set<Article>()
|
||||
|
||||
@ -30,7 +30,7 @@ final class ArticlesTable: DatabaseTable {
|
||||
|
||||
assert(oneArticle.status != nil)
|
||||
|
||||
if let existingArticle = cachedArticle(oneArticle.articleID) {
|
||||
if let existingArticle = cachedArticle(oneArticle.databaseID) {
|
||||
articles.insert(existingArticle)
|
||||
}
|
||||
else {
|
||||
@ -39,23 +39,51 @@ final class ArticlesTable: DatabaseTable {
|
||||
}
|
||||
}
|
||||
|
||||
statusesManager.attachCachedStatuses(articles)
|
||||
statusesTable.attachCachedStatuses(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? {
|
||||
|
||||
|
||||
return cachedArticles.object(forKey: articleID as NSString)
|
||||
}
|
||||
|
||||
|
||||
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>) {
|
||||
|
||||
|
||||
articles.forEach { cacheArticle($0) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ final class Database {
|
||||
|
||||
fileprivate let queue: RSDatabaseQueue
|
||||
private let databaseFile: String
|
||||
private let attachmentsTable = AttachmentsTable(DatabaseTableName.attachments)
|
||||
private let attachmentsTable: AttachmentsTable
|
||||
fileprivate let statusesManager: StatusesManager
|
||||
fileprivate let articleCache = ArticlesManager()
|
||||
fileprivate var articleArrivalCutoffDate = NSDate.rs_dateWithNumberOfDays(inThePast: 3 * 31)!
|
||||
@ -38,6 +38,7 @@ final class Database {
|
||||
self.delegate = delegate
|
||||
self.databaseFile = databaseFile
|
||||
self.queue = RSDatabaseQueue(filepath: databaseFile, excludeFromBackup: false)
|
||||
self.attachmentsTable = AttachmentsTable(name: DatabaseTableName.attachments, queue: queue)
|
||||
self.statusesManager = StatusesManager(queue: self.queue)
|
||||
|
||||
let createStatementsPath = Bundle(for: type(of: self)).path(forResource: "CreateStatements", ofType: "sql")!
|
||||
@ -358,23 +359,6 @@ private extension Database {
|
||||
|
||||
// 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 {
|
||||
|
||||
let sql = "select count(*) from articles where feedID = ?;"
|
||||
@ -448,8 +432,7 @@ private extension Database {
|
||||
return articlesSet
|
||||
}
|
||||
|
||||
typealias FeedCountCallback = (Int) -> Void
|
||||
|
||||
|
||||
func feedIDsFromArticles(_ articles: Set<Article>) -> Set<String> {
|
||||
|
||||
return Set(articles.map { $0.feedID })
|
||||
@ -462,28 +445,4 @@ private extension Database {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,4 +31,21 @@ extension DatabaseTable {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user