// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved. import Foundation import GRDB import SessionUtilitiesKit // MARK: - GRDB public extension QueryInterfaceRequest { @discardableResult func updateAllAndConfig( _ db: Database, _ assignments: ColumnAssignment... ) throws -> Int { return try updateAllAndConfig(db, assignments) } @discardableResult func updateAllAndConfig( _ db: Database, _ assignments: [ColumnAssignment] ) throws -> Int { switch self { case let contactRequest as QueryInterfaceRequest: return try contactRequest.updateAndFetchAllAndUpdateConfig(db, assignments).count case let profileRequest as QueryInterfaceRequest: return try profileRequest.updateAndFetchAllAndUpdateConfig(db, assignments).count case let threadRequest as QueryInterfaceRequest: return try threadRequest.updateAndFetchAllAndUpdateConfig(db, assignments).count default: return try self.updateAll(db, assignments) } } } public extension QueryInterfaceRequest where RowDecoder: FetchableRecord & TableRecord { @discardableResult func updateAndFetchAllAndUpdateConfig( _ db: Database, _ assignments: ColumnAssignment... ) throws -> [RowDecoder] { return try updateAndFetchAllAndUpdateConfig(db, assignments) } @discardableResult func updateAndFetchAllAndUpdateConfig( _ db: Database, _ assignments: [ColumnAssignment] ) throws -> [RowDecoder] { defer { // If we change one of these types then we may as well automatically enqueue // a new config sync job once the transaction completes (but only enqueue it // once per transaction - doing it more than once is pointless) if self is QueryInterfaceRequest || self is QueryInterfaceRequest || self is QueryInterfaceRequest || self is QueryInterfaceRequest { db.afterNextTransactionNestedOnce(dedupeIdentifier: "EnqueueConfigurationSyncJob") { db in ConfigurationSyncJob.enqueue(db) } } } // FIXME: Remove this once `useSharedUtilForUserConfig` is permanent guard Features.useSharedUtilForUserConfig else { return try self.updateAndFetchAll(db, assignments) } // Update the config dump state where needed switch self { case is QueryInterfaceRequest: return try SessionUtil.updatingContacts(db, try updateAndFetchAll(db, assignments)) case is QueryInterfaceRequest: return try SessionUtil.updatingProfiles(db, try updateAndFetchAll(db, assignments)) case is QueryInterfaceRequest: return try SessionUtil.updatingThreads(db, try updateAndFetchAll(db, assignments)) default: return try self.updateAndFetchAll(db, assignments) } } }