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-05-15 18:52:56 +02:00
|
|
|
import Articles
|
2019-05-15 01:24:19 +02:00
|
|
|
import RSDatabase
|
|
|
|
|
|
|
|
final class SyncStatusTable: DatabaseTable {
|
|
|
|
|
|
|
|
let name = DatabaseTableName.syncStatus
|
|
|
|
private let queue: RSDatabaseQueue
|
|
|
|
|
|
|
|
init(queue: RSDatabaseQueue) {
|
|
|
|
self.queue = queue
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func selectForProcessing() -> [SyncStatus] {
|
|
|
|
|
|
|
|
var statuses: Set<SyncStatus>? = nil
|
|
|
|
|
|
|
|
self.queue.updateSync { database in
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return statuses != nil ? Array(statuses!) : [SyncStatus]()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-27 19:03:12 +02:00
|
|
|
func selectPendingCount() -> Int {
|
|
|
|
|
|
|
|
var count: Int = 0
|
|
|
|
|
|
|
|
self.queue.fetchSync { (database) in
|
|
|
|
let sql = "select count(*) from syncStatus"
|
|
|
|
if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) {
|
|
|
|
resultSet.next()
|
|
|
|
count = Int(resultSet.int(forColumnIndex: 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func resetSelectedForProcessing(_ articleIDs: [String]) {
|
|
|
|
self.queue.update { database in
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSelectedForProcessing(_ articleIDs: [String]) {
|
|
|
|
self.queue.update { database in
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:24:19 +02:00
|
|
|
func insertStatuses(_ statuses: [SyncStatus]) {
|
|
|
|
self.queue.update { database in
|
2019-05-15 18:52:56 +02:00
|
|
|
let statusArray = statuses.map { $0.databaseDictionary() }
|
2019-05-15 01:24:19 +02:00
|
|
|
self.insertRows(statusArray, insertType: .orReplace, in: database)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|