mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 00:04:25 +00:00
191 lines
7.0 KiB
Swift
191 lines
7.0 KiB
Swift
//
|
||
// SyncStatusTable.swift
|
||
// NetNewsWire
|
||
//
|
||
// Created by Maurice Parker on 5/14/19.
|
||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||
//
|
||
|
||
import Foundation
|
||
import Articles
|
||
import RSDatabase
|
||
import RSDatabaseObjC
|
||
|
||
struct SyncStatusTable {
|
||
static let name = "syncStatus"
|
||
|
||
// Selects first, then marks just the rows being returned. Marking every row would flag rows
|
||
// past the limit as in-flight, and `deleteSelectedForProcessing` with a nil key would then
|
||
// delete a queued status that was never sent.
|
||
static func selectForProcessing(limit: Int?, database: FMDatabase) throws -> Set<SyncStatus> {
|
||
let operation = "Reading statuses to send"
|
||
database.beginTransaction()
|
||
|
||
let selectSQL = {
|
||
var sql = "select * from \(name)"
|
||
if let limit {
|
||
sql = "\(sql) limit \(limit)"
|
||
}
|
||
return sql
|
||
}()
|
||
|
||
guard let resultSet = database.executeQuery(selectSQL, withArgumentsIn: nil) else {
|
||
let error = database.syncDatabaseError(operation: operation)
|
||
database.rollback()
|
||
throw error
|
||
}
|
||
|
||
// Rows are fetched during iteration, not by the query above. A failure partway
|
||
// through stops early and returns the rows read so far, which is indistinguishable
|
||
// from a complete result.
|
||
let statuses = resultSet.mapToSet(statusWithRow)
|
||
guard !database.hadError() else {
|
||
let error = database.syncDatabaseError(operation: operation)
|
||
database.rollback()
|
||
throw error
|
||
}
|
||
|
||
guard !statuses.isEmpty else {
|
||
database.commit()
|
||
return statuses
|
||
}
|
||
|
||
// An articleID can have both a read row and a starred row, so match on the pair —
|
||
// grouping by key means each statement touches only that key’s articleIDs.
|
||
let statusesByKey = Dictionary(grouping: statuses, by: \.key)
|
||
for (key, statusesForKey) in statusesByKey {
|
||
let articleIDs = statusesForKey.map { $0.articleID }
|
||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))
|
||
var parameters = articleIDs.map { $0 as AnyObject }
|
||
parameters.append(key.rawValue as AnyObject)
|
||
let updateSQL = "update \(name) set selected = true where articleID in \(placeholders) and key = ?"
|
||
guard database.executeUpdate(updateSQL, withArgumentsIn: parameters) else {
|
||
let error = database.syncDatabaseError(operation: "Marking \(articleIDs.count) \(key.rawValue) statuses as in flight")
|
||
database.rollback()
|
||
throw error
|
||
}
|
||
}
|
||
|
||
database.commit()
|
||
|
||
return Set(statuses.map { SyncStatus(articleID: $0.articleID, key: $0.key, flag: $0.flag, selected: true) })
|
||
}
|
||
|
||
static func selectPendingCount(database: FMDatabase) throws -> Int {
|
||
let operation = "Counting pending statuses"
|
||
let sql = "select count(*) from \(name)"
|
||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
||
throw database.syncDatabaseError(operation: operation)
|
||
}
|
||
|
||
guard let count = resultSet.intWithCountResult() else {
|
||
throw database.syncDatabaseError(operation: operation)
|
||
}
|
||
return count
|
||
}
|
||
|
||
static func selectPendingReadStatusArticleIDs(database: FMDatabase) throws -> Set<String> {
|
||
try selectPendingArticleIDs(.read, database: database)
|
||
}
|
||
|
||
static func selectPendingStarredStatusArticleIDs(database: FMDatabase) throws -> Set<String> {
|
||
try selectPendingArticleIDs(.starred, database: database)
|
||
}
|
||
|
||
static func resetAllSelectedForProcessing(database: FMDatabase) throws {
|
||
let updateSQL = "update \(name) set selected = false"
|
||
guard database.executeUpdateInTransaction(updateSQL) else {
|
||
throw database.syncDatabaseError(operation: "Returning all in-flight statuses to the queue")
|
||
}
|
||
}
|
||
|
||
// An articleID can have both a read row and a starred row queued. Callers that send
|
||
// one status kind at a time must pass `key` so they don’t touch the other kind’s rows.
|
||
|
||
static func resetSelectedForProcessing(_ articleIDs: Set<String>, key: SyncStatus.Key?, database: FMDatabase) throws {
|
||
guard !articleIDs.isEmpty else {
|
||
return
|
||
}
|
||
|
||
let operation = "Returning \(articleIDs.count) in-flight statuses to the queue"
|
||
var parameters = articleIDs.map { $0 as AnyObject }
|
||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))
|
||
var updateSQL = "update \(name) set selected = false where articleID in \(placeholders)"
|
||
if let key {
|
||
updateSQL += " and key = ?"
|
||
parameters.append(key.rawValue as AnyObject)
|
||
}
|
||
guard database.executeUpdateInTransaction(updateSQL, withArgumentsIn: parameters) else {
|
||
throw database.syncDatabaseError(operation: operation)
|
||
}
|
||
}
|
||
|
||
static func deleteSelectedForProcessing(_ articleIDs: Set<String>, key: SyncStatus.Key?, database: FMDatabase) throws {
|
||
guard !articleIDs.isEmpty else {
|
||
return
|
||
}
|
||
|
||
let operation = "Deleting \(articleIDs.count) sent statuses"
|
||
var parameters = articleIDs.map { $0 as AnyObject }
|
||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))
|
||
var deleteSQL = "delete from \(name) where selected = true and articleID in \(placeholders)"
|
||
if let key {
|
||
deleteSQL += " and key = ?"
|
||
parameters.append(key.rawValue as AnyObject)
|
||
}
|
||
guard database.executeUpdateInTransaction(deleteSQL, withArgumentsIn: parameters) else {
|
||
throw database.syncDatabaseError(operation: operation)
|
||
}
|
||
}
|
||
|
||
static func insertStatuses(_ statuses: Set<SyncStatus>, database: FMDatabase) throws {
|
||
let operation = "Queueing \(statuses.count) statuses to send"
|
||
database.beginTransaction()
|
||
|
||
let statusArray = statuses.map { $0.databaseDictionary() }
|
||
guard database.insertRows(statusArray, insertType: .orReplace, tableName: name) else {
|
||
let error = database.syncDatabaseError(operation: operation)
|
||
database.rollback()
|
||
throw error
|
||
}
|
||
|
||
guard database.commit() else {
|
||
throw database.syncDatabaseError(operation: operation)
|
||
}
|
||
}
|
||
}
|
||
|
||
private extension SyncStatusTable {
|
||
|
||
static func statusWithRow(_ row: FMResultSet) -> SyncStatus? {
|
||
guard let articleID = row.swiftString(forColumn: DatabaseKey.articleID),
|
||
let rawKey = row.swiftString(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)
|
||
}
|
||
|
||
// Includes rows that are selected — a send in flight hasn’t been confirmed by the server yet,
|
||
// and skipping those let a concurrent refresh revert the change the send was carrying.
|
||
// <https://github.com/Ranchero-Software/NetNewsWire/issues/4280>
|
||
static func selectPendingArticleIDs(_ statusKey: ArticleStatus.Key, database: FMDatabase) throws -> Set<String> {
|
||
let operation = "Reading pending \(statusKey.rawValue) statuses"
|
||
let sql = "select articleID from \(name) where key = \"\(statusKey.rawValue)\";"
|
||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
||
throw database.syncDatabaseError(operation: operation)
|
||
}
|
||
|
||
let articleIDs = resultSet.mapToSet { $0.swiftString(forColumnIndex: 0) }
|
||
guard !database.hadError() else {
|
||
throw database.syncDatabaseError(operation: operation)
|
||
}
|
||
|
||
return articleIDs
|
||
}
|
||
}
|