2017-07-03 19:40:48 +02:00
//
2018-07-24 03:29:08 +02:00
// A r t i c l e s D a t a b a s e . s w i f t
2018-08-29 07:18:24 +02:00
// N e t N e w s W i r e
2017-07-03 19:40:48 +02:00
//
// C r e a t e d b y B r e n t S i m m o n s o n 7 / 2 0 / 1 5 .
// C o p y r i g h t © 2 0 1 5 R a n c h e r o S o f t w a r e , L L C . A l l r i g h t s r e s e r v e d .
//
import Foundation
import RSCore
import RSDatabase
2017-07-03 20:20:14 +02:00
import RSParser
2018-07-24 03:29:08 +02:00
import Articles
2017-07-03 19:40:48 +02:00
2019-07-06 05:06:31 +02:00
// T h i s f i l e i s t h e e n t i r e t y o f t h e p u b l i c A P I f o r A r t i c l e s D a t a b a s e . f r a m e w o r k .
2017-09-16 19:38:54 +02:00
// E v e r y t h i n g e l s e i s i m p l e m e n t a t i o n .
2019-07-06 05:06:31 +02:00
// M a i n t h r e a d o n l y .
public typealias UnreadCountDictionary = [ String : Int ] // f e e d I D : u n r e a d C o u n t
2017-09-16 19:21:39 +02:00
public typealias UnreadCountCompletionBlock = ( UnreadCountDictionary ) -> Void
2017-09-16 19:38:54 +02:00
public typealias UpdateArticlesWithFeedCompletionBlock = ( Set < Article > ? , Set < Article > ? ) -> Void // n e w A r t i c l e s , u p d a t e d A r t i c l e s
2017-07-03 19:40:48 +02:00
2018-07-24 03:29:08 +02:00
public final class ArticlesDatabase {
2017-07-03 19:40:48 +02:00
2017-09-10 03:46:58 +02:00
private let accountID : String
2017-08-21 22:31:14 +02:00
private let articlesTable : ArticlesTable
2017-08-27 00:37:15 +02:00
2017-09-18 02:03:58 +02:00
public init ( databaseFilePath : String , accountID : String ) {
2017-09-10 03:46:58 +02:00
self . accountID = accountID
2017-09-16 19:38:54 +02:00
2017-09-18 02:03:58 +02:00
let queue = RSDatabaseQueue ( filepath : databaseFilePath , excludeFromBackup : false )
2017-09-10 03:46:58 +02:00
self . articlesTable = ArticlesTable ( name : DatabaseTableName . articles , accountID : accountID , queue : queue )
2017-08-21 00:56:58 +02:00
2019-02-23 07:17:05 +01:00
queue . createTables ( usingStatements : ArticlesDatabase . tableCreationStatements )
2017-12-19 03:20:13 +01:00
queue . update { ( database ) in
2019-02-23 07:17:05 +01:00
if ! self . articlesTable . containsColumn ( " searchRowID " , in : database ) {
database . executeStatements ( " ALTER TABLE articles add column searchRowID INTEGER; " )
}
2019-03-03 21:30:58 +01:00
database . executeStatements ( " CREATE INDEX if not EXISTS articles_searchRowID on articles(searchRowID); " )
2018-09-26 07:48:47 +02:00
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; " )
2017-12-19 03:20:13 +01:00
}
2017-07-03 19:40:48 +02:00
queue . vacuumIfNeeded ( )
2019-02-25 00:34:10 +01:00
DispatchQueue . main . async {
self . articlesTable . indexUnindexedArticles ( )
}
2017-07-03 19:40:48 +02:00
}
2017-08-21 06:23:17 +02:00
// MARK: - F e t c h i n g A r t i c l e s
2017-07-03 19:40:48 +02:00
2019-07-06 05:06:31 +02:00
public func fetchArticles ( _ feedID : String ) -> Set < Article > {
2018-07-28 21:16:14 +02:00
return articlesTable . fetchArticles ( feedID )
2017-07-03 19:40:48 +02:00
}
2019-05-14 13:20:53 +02:00
2019-07-06 05:06:31 +02:00
public func fetchArticles ( articleIDs : Set < String > ) -> Set < Article > {
return articlesTable . fetchArticles ( articleIDs : articleIDs )
}
public func fetchUnreadArticles ( _ feedIDs : Set < String > ) -> Set < Article > {
return articlesTable . fetchUnreadArticles ( feedIDs )
}
public func fetchTodayArticles ( _ feedIDs : Set < String > ) -> Set < Article > {
return articlesTable . fetchTodayArticles ( feedIDs )
}
public func fetchStarredArticles ( _ feedIDs : Set < String > ) -> Set < Article > {
return articlesTable . fetchStarredArticles ( feedIDs )
}
public func fetchArticlesMatching ( _ searchString : String , _ feedIDs : Set < String > ) -> Set < Article > {
return articlesTable . fetchArticlesMatching ( searchString , feedIDs )
}
// MARK: - F e t c h i n g A r t i c l e s A s y n c
public func fetchArticlesAsync ( _ feedID : String , _ callback : @ escaping ArticleSetBlock ) {
articlesTable . fetchArticlesAsync ( feedID , callback )
2019-05-14 13:20:53 +02:00
}
2017-07-03 19:40:48 +02:00
2019-07-06 05:06:31 +02:00
public func fetchArticlesAsync ( articleIDs : Set < String > , _ callback : @ escaping ArticleSetBlock ) {
articlesTable . fetchArticlesAsync ( articleIDs : articleIDs , callback )
2017-07-03 19:40:48 +02:00
}
2019-07-06 05:06:31 +02:00
public func fetchUnreadArticlesAsync ( _ feedIDs : Set < String > , _ callback : @ escaping ArticleSetBlock ) {
articlesTable . fetchUnreadArticlesAsync ( feedIDs , callback )
2017-07-03 19:40:48 +02:00
}
2017-08-21 06:23:17 +02:00
2019-07-06 05:06:31 +02:00
public func fetchTodayArticlesAsync ( _ feedIDs : Set < String > , _ callback : @ escaping ArticleSetBlock ) {
articlesTable . fetchTodayArticlesAsync ( feedIDs , callback )
2018-02-11 02:37:47 +01:00
}
2019-07-06 05:06:31 +02:00
public func fetchedStarredArticlesAsync ( _ feedIDs : Set < String > , _ callback : @ escaping ArticleSetBlock ) {
articlesTable . fetchStarredArticlesAsync ( feedIDs , callback )
2018-02-11 21:07:55 +01:00
}
2019-07-06 05:06:31 +02:00
public func fetchArticlesMatchingAsync ( _ searchString : String , _ feedIDs : Set < String > , _ callback : @ escaping ArticleSetBlock ) {
articlesTable . fetchArticlesMatchingAsync ( searchString , feedIDs , callback )
2019-02-19 07:29:43 +01:00
}
2017-08-21 06:23:17 +02:00
// MARK: - U n r e a d C o u n t s
2017-07-03 19:40:48 +02:00
2019-07-06 05:06:31 +02:00
public func fetchUnreadCounts ( for feedIDs : Set < String > , _ callback : @ escaping UnreadCountCompletionBlock ) {
articlesTable . fetchUnreadCounts ( feedIDs , callback )
2017-07-03 19:40:48 +02:00
}
2018-07-28 21:16:14 +02:00
public func fetchUnreadCount ( for feedIDs : Set < String > , since : Date , callback : @ escaping ( Int ) -> Void ) {
articlesTable . fetchUnreadCount ( feedIDs , since , callback )
2017-11-19 21:44:17 +01:00
}
2018-07-28 21:16:14 +02:00
public func fetchStarredAndUnreadCount ( for feedIDs : Set < String > , callback : @ escaping ( Int ) -> Void ) {
articlesTable . fetchStarredAndUnreadCount ( feedIDs , callback )
2017-11-20 00:40:02 +01:00
}
2019-07-06 05:06:31 +02:00
public func fetchAllNonZeroUnreadCounts ( _ callback : @ escaping UnreadCountCompletionBlock ) {
articlesTable . fetchAllUnreadCounts ( callback )
2017-12-03 20:57:53 +01:00
}
2017-09-16 19:38:54 +02:00
// MARK: - S a v i n g a n d U p d a t i n g A r t i c l e s
2017-07-03 19:40:48 +02:00
2019-05-13 15:32:03 +02:00
public func update ( feedID : String , parsedItems : Set < ParsedItem > , defaultRead : Bool , completion : @ escaping UpdateArticlesWithFeedCompletionBlock ) {
return articlesTable . update ( feedID , parsedItems , defaultRead , completion )
2017-07-03 19:40:48 +02:00
}
2019-05-14 17:06:29 +02:00
public func ensureStatuses ( _ articleIDs : Set < String > , _ statusKey : ArticleStatus . Key , _ flag : Bool ) {
articlesTable . ensureStatuses ( articleIDs , statusKey , flag )
}
2017-08-21 06:23:17 +02:00
// MARK: - S t a t u s
2017-07-03 19:40:48 +02:00
2019-07-08 00:05:36 +02:00
public func fetchUnreadArticleIDs ( _ callback : @ escaping ( Set < String > ) -> Void ) {
articlesTable . fetchUnreadArticleIDs ( callback )
2019-05-14 13:20:53 +02:00
}
2019-07-08 00:05:36 +02:00
public func fetchStarredArticleIDs ( _ callback : @ escaping ( Set < String > ) -> Void ) {
articlesTable . fetchStarredArticleIDs ( callback )
2019-05-14 13:20:53 +02:00
}
2019-07-08 00:05:36 +02:00
public func fetchArticleIDsForStatusesWithoutArticles ( _ callback : @ escaping ( Set < String > ) -> Void ) {
articlesTable . fetchArticleIDsForStatusesWithoutArticles ( callback )
2019-05-17 21:56:27 +02:00
}
2017-10-09 06:06:25 +02:00
public func mark ( _ articles : Set < Article > , statusKey : ArticleStatus . Key , flag : Bool ) -> Set < ArticleStatus > ? {
return articlesTable . mark ( articles , statusKey , flag )
2017-07-03 19:40:48 +02:00
}
}
2019-02-23 07:17:05 +01:00
// MARK: - P r i v a t e
private extension ArticlesDatabase {
static let tableCreationStatements = " " "
CREATE TABLE if not EXISTS articles ( articleID TEXT NOT NULL PRIMARY KEY , feedID TEXT NOT NULL , uniqueID TEXT NOT NULL , title TEXT , contentHTML TEXT , contentText TEXT , url TEXT , externalURL TEXT , summary TEXT , imageURL TEXT , bannerImageURL TEXT , datePublished DATE , dateModified DATE , searchRowID INTEGER ) ;
CREATE TABLE if not EXISTS statuses ( articleID TEXT NOT NULL PRIMARY KEY , read BOOL NOT NULL DEFAULT 0 , starred BOOL NOT NULL DEFAULT 0 , userDeleted BOOL NOT NULL DEFAULT 0 , dateArrived DATE NOT NULL DEFAULT 0 ) ;
CREATE TABLE if not EXISTS authors ( authorID TEXT NOT NULL PRIMARY KEY , name TEXT , url TEXT , avatarURL TEXT , emailAddress TEXT ) ;
CREATE TABLE if not EXISTS authorsLookup ( authorID TEXT NOT NULL , articleID TEXT NOT NULL , PRIMARY KEY ( authorID , articleID ) ) ;
CREATE TABLE if not EXISTS attachments ( attachmentID TEXT NOT NULL PRIMARY KEY , url TEXT NOT NULL , mimeType TEXT , title TEXT , sizeInBytes INTEGER , durationInSeconds INTEGER ) ;
CREATE TABLE if not EXISTS attachmentsLookup ( attachmentID TEXT NOT NULL , articleID TEXT NOT NULL , PRIMARY KEY ( attachmentID , articleID ) ) ;
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 ) ;
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 ;
" " "
}