mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-02-02 03:56:55 +01:00
Clean code
- Extract statements to create `search` table so it can be reused during database migration - Rename `authors` as `authorsNames` for consistency - Extract code to improve readability
This commit is contained in:
parent
cf1be33044
commit
408e5d0ba0
@ -76,6 +76,7 @@ public final class ArticlesDatabase {
|
||||
self.retentionStyle = retentionStyle
|
||||
|
||||
try! queue.runCreateStatements(ArticlesDatabase.tableCreationStatements)
|
||||
try! queue.runCreateStatements(ArticlesDatabase.searchTableCreationStatements)
|
||||
queue.runInDatabase { databaseResult in
|
||||
let database = databaseResult.database!
|
||||
if !self.articlesTable.containsColumn("searchRowID", in: database) {
|
||||
@ -84,7 +85,8 @@ public final class ArticlesDatabase {
|
||||
database.executeStatements("CREATE INDEX if not EXISTS articles_searchRowID on articles(searchRowID);")
|
||||
database.executeStatements("DROP TABLE if EXISTS tags;DROP INDEX if EXISTS tags_tagName_index;DROP INDEX if EXISTS articles_feedID_index;DROP INDEX if EXISTS statuses_read_index;DROP TABLE if EXISTS attachments;DROP TABLE if EXISTS attachmentsLookup;")
|
||||
if !self.searchTable.containsColumn("authors", in: database) {
|
||||
database.executeStatements("DROP TABLE if EXISTS search;CREATE VIRTUAL TABLE if not EXISTS search using fts4(title, body, authors);UPDATE articles SET searchRowID = null;")
|
||||
database.executeStatements("DROP TABLE if EXISTS search;UPDATE articles SET searchRowID = null;")
|
||||
database.executeStatements(ArticlesDatabase.searchTableCreationStatements)
|
||||
}
|
||||
}
|
||||
|
||||
@ -336,12 +338,14 @@ private extension ArticlesDatabase {
|
||||
CREATE INDEX if not EXISTS articles_feedID_datePublished_articleID on articles (feedID, datePublished, articleID);
|
||||
|
||||
CREATE INDEX if not EXISTS statuses_starred_index on statuses (starred);
|
||||
|
||||
CREATE VIRTUAL TABLE if not EXISTS search using fts4(title, body, authors);
|
||||
|
||||
CREATE TRIGGER if not EXISTS articles_after_delete_trigger_delete_search_text after delete on articles begin delete from search where rowid = OLD.searchRowID; end;
|
||||
"""
|
||||
|
||||
static let searchTableCreationStatements = """
|
||||
CREATE VIRTUAL TABLE if not EXISTS search using fts4(title, body, authors);
|
||||
|
||||
CREATE TRIGGER if not EXISTS articles_after_delete_trigger_delete_search_text after delete on articles begin delete from search where rowid = OLD.searchRowID; end;
|
||||
"""
|
||||
|
||||
func todayCutoffDate() -> Date {
|
||||
// 24 hours previous. This is used by the Today smart feed, which should not actually empty out at midnight.
|
||||
return Date(timeIntervalSinceNow: -(60 * 60 * 24)) // This does not need to be more precise.
|
||||
|
@ -145,12 +145,8 @@ final class ArticlesTable: DatabaseTable {
|
||||
}
|
||||
|
||||
// MARK: - Fetching Articles for Indexer
|
||||
|
||||
func fetchArticleSearchInfos(_ articleIDs: Set<String>, in database: FMDatabase) -> Set<ArticleSearchInfo>? {
|
||||
let parameters = articleIDs.map { $0 as AnyObject }
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))!
|
||||
// Aggregating authors names in SQL using a subquery with GROUP_CONCAT
|
||||
let sql = """
|
||||
private func articleSearchInfosQuery(with placeholders: String) -> String {
|
||||
return """
|
||||
SELECT
|
||||
art.articleID,
|
||||
art.title,
|
||||
@ -165,16 +161,20 @@ final class ArticlesTable: DatabaseTable {
|
||||
GROUP BY autl.articleID) as authors
|
||||
FROM articles as art
|
||||
WHERE articleID in \(placeholders);
|
||||
""";
|
||||
"""
|
||||
}
|
||||
|
||||
if let resultSet = database.executeQuery(sql, withArgumentsIn: parameters) {
|
||||
func fetchArticleSearchInfos(_ articleIDs: Set<String>, in database: FMDatabase) -> Set<ArticleSearchInfo>? {
|
||||
let parameters = articleIDs.map { $0 as AnyObject }
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))!
|
||||
if let resultSet = database.executeQuery(self.articleSearchInfosQuery(with: placeholders), withArgumentsIn: parameters) {
|
||||
return resultSet.mapToSet { (row) -> ArticleSearchInfo? in
|
||||
let articleID = row.string(forColumn: DatabaseKey.articleID)!
|
||||
let title = row.string(forColumn: DatabaseKey.title)
|
||||
let contentHTML = row.string(forColumn: DatabaseKey.contentHTML)
|
||||
let contentText = row.string(forColumn: DatabaseKey.contentText)
|
||||
let summary = row.string(forColumn: DatabaseKey.summary)
|
||||
let authors = row.string(forColumn: DatabaseKey.authors)
|
||||
let authorsNames = row.string(forColumn: DatabaseKey.authors)
|
||||
|
||||
let searchRowIDObject = row.object(forColumnName: DatabaseKey.searchRowID)
|
||||
var searchRowID: Int? = nil
|
||||
@ -182,7 +182,7 @@ final class ArticlesTable: DatabaseTable {
|
||||
searchRowID = Int(row.longLongInt(forColumn: DatabaseKey.searchRowID))
|
||||
}
|
||||
|
||||
return ArticleSearchInfo(articleID: articleID, title: title, authorsNames: authors, contentHTML: contentHTML, contentText: contentText, summary: summary, searchRowID: searchRowID)
|
||||
return ArticleSearchInfo(articleID: articleID, title: title, authorsNames: authorsNames, contentHTML: contentHTML, contentText: contentText, summary: summary, searchRowID: searchRowID)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -49,7 +49,8 @@ final class ArticleSearchInfo: Hashable {
|
||||
}
|
||||
|
||||
convenience init(article: Article) {
|
||||
self.init(articleID: article.articleID, title: article.title, authorsNames: article.authors?.map({ $0.name }).reduce("", { $0.appending("").appending($1 ?? "") }), contentHTML: article.contentHTML, contentText: article.contentText, summary: article.summary, searchRowID: nil)
|
||||
let authorsNames = article.authors?.map({ $0.name }).reduce("", { $0.appending("").appending($1 ?? "") })
|
||||
self.init(articleID: article.articleID, title: article.title, authorsNames: authorsNames, contentHTML: article.contentHTML, contentText: article.contentText, summary: article.summary, searchRowID: nil)
|
||||
}
|
||||
|
||||
// MARK: Hashable
|
||||
|
Loading…
x
Reference in New Issue
Block a user