2019-05-15 01:24:19 +02:00
|
|
|
//
|
|
|
|
// SyncStatusTable.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 5/14/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2019-11-30 08:42:11 +01:00
|
|
|
import RSCore
|
2019-05-15 18:52:56 +02:00
|
|
|
import Articles
|
2019-05-15 01:24:19 +02:00
|
|
|
import RSDatabase
|
|
|
|
|
2019-11-30 06:49:44 +01:00
|
|
|
struct SyncStatusTable: DatabaseTable {
|
|
|
|
|
2019-05-15 01:24:19 +02:00
|
|
|
let name = DatabaseTableName.syncStatus
|
2019-11-30 06:49:44 +01:00
|
|
|
private let queue: DatabaseQueue
|
|
|
|
|
|
|
|
init(queue: DatabaseQueue) {
|
2019-05-15 01:24:19 +02:00
|
|
|
self.queue = queue
|
|
|
|
}
|
|
|
|
|
2019-12-16 20:08:47 +01:00
|
|
|
func selectForProcessing(_ completion: @escaping SyncStatusesCompletionBlock) {
|
|
|
|
queue.runInTransaction { databaseResult in
|
|
|
|
var statuses = Set<SyncStatus>()
|
|
|
|
var error: DatabaseError?
|
2019-12-16 08:07:28 +01:00
|
|
|
|
|
|
|
func makeDatabaseCall(_ database: FMDatabase) {
|
|
|
|
let updateSQL = "update syncStatus set selected = true"
|
|
|
|
database.executeUpdate(updateSQL, withArgumentsIn: nil)
|
|
|
|
|
|
|
|
let selectSQL = "select * from syncStatus where selected == true"
|
|
|
|
if let resultSet = database.executeQuery(selectSQL, withArgumentsIn: nil) {
|
|
|
|
statuses = resultSet.mapToSet(self.statusWithRow)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch databaseResult {
|
|
|
|
case .success(let database):
|
|
|
|
makeDatabaseCall(database)
|
|
|
|
case .failure(let databaseError):
|
|
|
|
error = databaseError
|
2019-05-15 18:52:56 +02:00
|
|
|
}
|
|
|
|
|
2019-12-16 20:08:47 +01:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
if let error = error {
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
completion(.success(Array(statuses)))
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 08:07:28 +01:00
|
|
|
}
|
2019-05-15 18:52:56 +02:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:50:13 +01:00
|
|
|
func selectPendingCount(_ completion: @escaping DatabaseIntCompletionBlock) {
|
|
|
|
queue.runInDatabase { databaseResult in
|
|
|
|
var count: Int = 0
|
|
|
|
var error: DatabaseError?
|
2019-12-16 08:07:28 +01:00
|
|
|
|
|
|
|
func makeDatabaseCall(_ database: FMDatabase) {
|
|
|
|
let sql = "select count(*) from syncStatus"
|
|
|
|
if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) {
|
2019-12-16 19:50:13 +01:00
|
|
|
count = self.numberWithCountResultSet(resultSet)
|
2019-12-16 08:07:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch databaseResult {
|
|
|
|
case .success(let database):
|
|
|
|
makeDatabaseCall(database)
|
|
|
|
case .failure(let databaseError):
|
|
|
|
error = databaseError
|
2019-05-27 19:03:12 +02:00
|
|
|
}
|
2019-12-16 08:07:28 +01:00
|
|
|
|
2019-12-16 19:50:13 +01:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
if let error = error {
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
completion(.success(count))
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 08:07:28 +01:00
|
|
|
}
|
2019-05-27 19:03:12 +02:00
|
|
|
}
|
2019-12-16 08:07:28 +01:00
|
|
|
|
2020-01-03 02:21:18 +01:00
|
|
|
func selectPendingReadStatusArticleIDs(completion: @escaping SyncStatusArticleIDsCompletionBlock) {
|
|
|
|
selectPendingArticleIDsAsync(.read, completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectPendingStarredStatusArticleIDs(completion: @escaping SyncStatusArticleIDsCompletionBlock) {
|
|
|
|
selectPendingArticleIDsAsync(.starred, completion)
|
|
|
|
}
|
|
|
|
|
2019-12-16 08:07:28 +01:00
|
|
|
func resetSelectedForProcessing(_ articleIDs: [String], completion: DatabaseCompletionBlock? = nil) {
|
|
|
|
queue.runInTransaction { databaseResult in
|
|
|
|
|
|
|
|
func makeDatabaseCall(_ database: FMDatabase) {
|
|
|
|
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.executeUpdate(updateSQL, withArgumentsIn: parameters)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch databaseResult {
|
|
|
|
case .success(let database):
|
|
|
|
makeDatabaseCall(database)
|
|
|
|
callCompletion(completion, nil)
|
|
|
|
case .failure(let databaseError):
|
|
|
|
callCompletion(completion, databaseError)
|
|
|
|
}
|
2019-05-15 18:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 08:07:28 +01:00
|
|
|
func deleteSelectedForProcessing(_ articleIDs: [String], completion: DatabaseCompletionBlock? = nil) {
|
|
|
|
queue.runInTransaction { databaseResult in
|
|
|
|
|
|
|
|
func makeDatabaseCall(_ database: FMDatabase) {
|
|
|
|
let parameters = articleIDs.map { $0 as AnyObject }
|
|
|
|
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))!
|
|
|
|
let deleteSQL = "delete from syncStatus where articleID in \(placeholders)"
|
|
|
|
database.executeUpdate(deleteSQL, withArgumentsIn: parameters)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch databaseResult {
|
|
|
|
case .success(let database):
|
|
|
|
makeDatabaseCall(database)
|
|
|
|
callCompletion(completion, nil)
|
|
|
|
case .failure(let databaseError):
|
|
|
|
callCompletion(completion, databaseError)
|
|
|
|
}
|
2019-05-15 18:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 08:07:28 +01:00
|
|
|
func insertStatuses(_ statuses: [SyncStatus], completion: DatabaseCompletionBlock? = nil) {
|
|
|
|
queue.runInTransaction { databaseResult in
|
|
|
|
|
|
|
|
func makeDatabaseCall(_ database: FMDatabase) {
|
|
|
|
let statusArray = statuses.map { $0.databaseDictionary() }
|
|
|
|
self.insertRows(statusArray, insertType: .orReplace, in: database)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch databaseResult {
|
|
|
|
case .success(let database):
|
|
|
|
makeDatabaseCall(database)
|
|
|
|
callCompletion(completion, nil)
|
|
|
|
case .failure(let databaseError):
|
|
|
|
callCompletion(completion, databaseError)
|
|
|
|
}
|
2019-05-15 01:24:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 18:52:56 +02:00
|
|
|
|
|
|
|
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 = ArticleStatus.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)
|
|
|
|
}
|
2020-01-03 02:21:18 +01:00
|
|
|
|
|
|
|
func selectPendingArticleIDsAsync(_ statusKey: ArticleStatus.Key, _ completion: @escaping SyncStatusArticleIDsCompletionBlock) {
|
|
|
|
|
|
|
|
queue.runInDatabase { databaseResult in
|
|
|
|
|
|
|
|
func makeDatabaseCall(_ database: FMDatabase) {
|
|
|
|
let sql = "select articleID from syncStatus where selected == false and key = \"\(statusKey.rawValue)\";"
|
|
|
|
|
|
|
|
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(.success(Set<String>()))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let articleIDs = resultSet.mapToSet{ $0.string(forColumnIndex: 0) }
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(.success(articleIDs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch databaseResult {
|
|
|
|
case .success(let database):
|
|
|
|
makeDatabaseCall(database)
|
|
|
|
case .failure(let databaseError):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(.failure(databaseError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
}
|
2019-12-16 08:07:28 +01:00
|
|
|
|
|
|
|
private func callCompletion(_ completion: DatabaseCompletionBlock?, _ databaseError: DatabaseError?) {
|
|
|
|
guard let completion = completion else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion(databaseError)
|
|
|
|
}
|
|
|
|
}
|