mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Move local modules into a folder named Modules.
This commit is contained in:
23
Modules/SyncDatabase/Sources/SyncDatabase/Constants.swift
Normal file
23
Modules/SyncDatabase/Sources/SyncDatabase/Constants.swift
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Constants.swift
|
||||
// SyncDatabase
|
||||
//
|
||||
// Created by Maurice Parker on 5/14/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct DatabaseTableName {
|
||||
|
||||
static let syncStatus = "syncStatus"
|
||||
}
|
||||
|
||||
struct DatabaseKey {
|
||||
|
||||
// Sync Status
|
||||
static let articleID = "articleID"
|
||||
static let key = "key"
|
||||
static let flag = "flag"
|
||||
static let selected = "selected"
|
||||
}
|
||||
119
Modules/SyncDatabase/Sources/SyncDatabase/SyncDatabase.swift
Normal file
119
Modules/SyncDatabase/Sources/SyncDatabase/SyncDatabase.swift
Normal file
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// SyncDatabase.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 5/14/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Database
|
||||
import FMDB
|
||||
|
||||
public actor SyncDatabase {
|
||||
|
||||
private var database: FMDatabase?
|
||||
private var databasePath: String
|
||||
private let syncStatusTable = SyncStatusTable()
|
||||
|
||||
public init(databasePath: String) {
|
||||
|
||||
let database = FMDatabase.openAndSetUpDatabase(path: databasePath)
|
||||
database.runCreateStatements(Self.creationStatements)
|
||||
database.vacuum()
|
||||
|
||||
self.database = database
|
||||
self.databasePath = databasePath
|
||||
}
|
||||
|
||||
// MARK: - API
|
||||
|
||||
public func insertStatuses(_ statuses: Set<SyncStatus>) throws {
|
||||
|
||||
guard let database else {
|
||||
throw DatabaseError.suspended
|
||||
}
|
||||
syncStatusTable.insertStatuses(statuses, database: database)
|
||||
}
|
||||
|
||||
public func selectForProcessing(limit: Int? = nil) throws -> Set<SyncStatus>? {
|
||||
|
||||
guard let database else {
|
||||
throw DatabaseError.suspended
|
||||
}
|
||||
return syncStatusTable.selectForProcessing(limit: limit, database: database)
|
||||
}
|
||||
|
||||
public func selectPendingCount() throws -> Int? {
|
||||
|
||||
guard let database else {
|
||||
throw DatabaseError.suspended
|
||||
}
|
||||
return syncStatusTable.selectPendingCount(database: database)
|
||||
}
|
||||
|
||||
public func selectPendingReadStatusArticleIDs() throws -> Set<String>? {
|
||||
|
||||
guard let database else {
|
||||
throw DatabaseError.suspended
|
||||
}
|
||||
return syncStatusTable.selectPendingReadStatusArticleIDs(database: database)
|
||||
}
|
||||
|
||||
public func selectPendingStarredStatusArticleIDs() throws -> Set<String>? {
|
||||
|
||||
guard let database else {
|
||||
throw DatabaseError.suspended
|
||||
}
|
||||
return syncStatusTable.selectPendingStarredStatusArticleIDs(database: database)
|
||||
}
|
||||
|
||||
public func resetAllSelectedForProcessing() throws {
|
||||
|
||||
guard let database else {
|
||||
throw DatabaseError.suspended
|
||||
}
|
||||
syncStatusTable.resetAllSelectedForProcessing(database: database)
|
||||
}
|
||||
|
||||
public func resetSelectedForProcessing(_ articleIDs: Set<String>) throws {
|
||||
|
||||
guard let database else {
|
||||
throw DatabaseError.suspended
|
||||
}
|
||||
syncStatusTable.resetSelectedForProcessing(articleIDs, database: database)
|
||||
}
|
||||
|
||||
public func deleteSelectedForProcessing(_ articleIDs: Set<String>) throws {
|
||||
|
||||
guard let database else {
|
||||
throw DatabaseError.suspended
|
||||
}
|
||||
syncStatusTable.deleteSelectedForProcessing(articleIDs, database: database)
|
||||
}
|
||||
|
||||
// MARK: - Suspend and Resume (for iOS)
|
||||
|
||||
public func suspend() {
|
||||
#if os(iOS)
|
||||
database?.close()
|
||||
database = nil
|
||||
#endif
|
||||
}
|
||||
|
||||
public func resume() {
|
||||
#if os(iOS)
|
||||
if database == nil {
|
||||
self.database = FMDatabase.openAndSetUpDatabase(path: databasePath)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private extension SyncDatabase {
|
||||
|
||||
static let creationStatements = """
|
||||
CREATE TABLE if not EXISTS syncStatus (articleID TEXT NOT NULL, key TEXT NOT NULL, flag BOOL NOT NULL DEFAULT 0, selected BOOL NOT NULL DEFAULT 0, PRIMARY KEY (articleID, key));
|
||||
"""
|
||||
|
||||
}
|
||||
52
Modules/SyncDatabase/Sources/SyncDatabase/SyncStatus.swift
Normal file
52
Modules/SyncDatabase/Sources/SyncDatabase/SyncStatus.swift
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// SyncStatus.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 5/14/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Articles
|
||||
import Database
|
||||
|
||||
public struct SyncStatus: Hashable, Equatable, Sendable {
|
||||
|
||||
public enum Key: String, Sendable {
|
||||
case read = "read"
|
||||
case starred = "starred"
|
||||
case deleted = "deleted"
|
||||
case new = "new"
|
||||
|
||||
public init(_ articleStatusKey: ArticleStatus.Key) {
|
||||
switch articleStatusKey {
|
||||
case .read:
|
||||
self = Self.read
|
||||
case .starred:
|
||||
self = Self.starred
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public let articleID: String
|
||||
public let key: SyncStatus.Key
|
||||
public let flag: Bool
|
||||
public let selected: Bool
|
||||
|
||||
public init(articleID: String, key: SyncStatus.Key, flag: Bool, selected: Bool = false) {
|
||||
self.articleID = articleID
|
||||
self.key = key
|
||||
self.flag = flag
|
||||
self.selected = selected
|
||||
}
|
||||
|
||||
public func databaseDictionary() -> DatabaseDictionary {
|
||||
return [DatabaseKey.articleID: articleID, DatabaseKey.key: key.rawValue, DatabaseKey.flag: flag, DatabaseKey.selected: selected]
|
||||
}
|
||||
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(articleID)
|
||||
hasher.combine(key)
|
||||
}
|
||||
}
|
||||
128
Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift
Normal file
128
Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift
Normal file
@@ -0,0 +1,128 @@
|
||||
//
|
||||
// SyncStatusTable.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 5/14/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Articles
|
||||
import Database
|
||||
import FMDB
|
||||
|
||||
struct SyncStatusTable {
|
||||
|
||||
static private let name = "syncStatus"
|
||||
|
||||
func selectForProcessing(limit: Int?, database: FMDatabase) -> Set<SyncStatus>? {
|
||||
|
||||
let updateSQL = "update syncStatus set selected = true"
|
||||
database.executeUpdateInTransaction(updateSQL, withArgumentsIn: nil)
|
||||
|
||||
let selectSQL = {
|
||||
var sql = "select * from syncStatus where selected == true"
|
||||
if let limit {
|
||||
sql = "\(sql) limit \(limit)"
|
||||
}
|
||||
return sql
|
||||
}()
|
||||
|
||||
guard let resultSet = database.executeQuery(selectSQL, withArgumentsIn: nil) else {
|
||||
return nil
|
||||
}
|
||||
let statuses = resultSet.mapToSet(self.statusWithRow)
|
||||
return statuses
|
||||
}
|
||||
|
||||
func selectPendingCount(database: FMDatabase) -> Int? {
|
||||
|
||||
let sql = "select count(*) from syncStatus"
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let count = resultSet.intWithCountResult()
|
||||
return count
|
||||
}
|
||||
|
||||
func selectPendingReadStatusArticleIDs(database: FMDatabase) -> Set<String>? {
|
||||
|
||||
selectPendingArticleIDs(.read, database: database)
|
||||
}
|
||||
|
||||
func selectPendingStarredStatusArticleIDs(database: FMDatabase) -> Set<String>? {
|
||||
|
||||
selectPendingArticleIDs(.starred, database: database)
|
||||
}
|
||||
|
||||
func resetAllSelectedForProcessing(database: FMDatabase) {
|
||||
|
||||
let updateSQL = "update syncStatus set selected = false"
|
||||
database.executeUpdateInTransaction(updateSQL)
|
||||
}
|
||||
|
||||
func resetSelectedForProcessing(_ articleIDs: Set<String>, database: FMDatabase) {
|
||||
|
||||
guard !articleIDs.isEmpty else {
|
||||
return
|
||||
}
|
||||
|
||||
let parameters = articleIDs.map { $0 as AnyObject }
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))!
|
||||
let updateSQL = "update syncStatus set selected = false where articleID in \(placeholders)"
|
||||
|
||||
database.executeUpdateInTransaction(updateSQL, withArgumentsIn: parameters)
|
||||
}
|
||||
|
||||
func deleteSelectedForProcessing(_ articleIDs: Set<String>, database: FMDatabase) {
|
||||
|
||||
guard !articleIDs.isEmpty else {
|
||||
return
|
||||
}
|
||||
|
||||
let parameters = articleIDs.map { $0 as AnyObject }
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))!
|
||||
let deleteSQL = "delete from syncStatus where selected = true and articleID in \(placeholders)"
|
||||
|
||||
database.executeUpdateInTransaction(deleteSQL, withArgumentsIn: parameters)
|
||||
}
|
||||
|
||||
func insertStatuses(_ statuses: Set<SyncStatus>, database: FMDatabase) {
|
||||
|
||||
database.beginTransaction()
|
||||
|
||||
let statusArray = statuses.map { $0.databaseDictionary() }
|
||||
database.insertRows(statusArray, insertType: .orReplace, tableName: Self.name)
|
||||
|
||||
database.commit()
|
||||
}
|
||||
}
|
||||
|
||||
private extension SyncStatusTable {
|
||||
|
||||
func statusWithRow(_ row: FMResultSet) -> SyncStatus? {
|
||||
|
||||
guard let articleID = row.string(forColumn: DatabaseKey.articleID),
|
||||
let rawKey = row.string(forColumn: DatabaseKey.key),
|
||||
let key = SyncStatus.Key(rawValue: rawKey) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let flag = row.bool(forColumn: DatabaseKey.flag)
|
||||
let selected = row.bool(forColumn: DatabaseKey.selected)
|
||||
|
||||
return SyncStatus(articleID: articleID, key: key, flag: flag, selected: selected)
|
||||
}
|
||||
|
||||
func selectPendingArticleIDs(_ statusKey: ArticleStatus.Key, database: FMDatabase) -> Set<String>? {
|
||||
|
||||
let sql = "select articleID from syncStatus where selected == false and key = \"\(statusKey.rawValue)\";"
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let articleIDs = resultSet.mapToSet{ $0.string(forColumnIndex: 0) }
|
||||
return articleIDs
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user