From 068c0ef5ba280e919ab7d8918e6925f1aeea8c45 Mon Sep 17 00:00:00 2001 From: Aaron L Bratcher Date: Thu, 28 Apr 2022 14:42:17 -0400 Subject: [PATCH] Fix setUnsyncedTables signature. Add hasTable method. --- README.md | 13 ++++----- Sources/AgileDB/AgileDB.swift | 38 ++++++++++++++++++-------- Tests/AgileDBTests/AsyncTests.swift | 3 ++ Tests/AgileDBTests/BasicTests.swift | 2 ++ Tests/AgileDBTests/DBObjectTests.swift | 9 ++++++ Tests/AgileDBTests/SyncTests.swift | 8 ++++++ 6 files changed, 54 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 84a24a8..38e8721 100644 --- a/README.md +++ b/README.md @@ -94,15 +94,12 @@ category.save(to: db) // save to database, automatically delete after designated date category.save(to: db, autoDeleteAfter: deletionDate) -// instantiate synchronously -guard let category = Category(db: db, key: categoryKey) else { return } +// instantiate and pull from DB asynchronously +guard let category = await Category(db: db, key: categoryKey) else { return } + +// delete from DB +category.delete(from: db) -// instantiate asynchronously -do { - let category = try await Category.load(from: db, for: categoryKey) - // use category -} catch { -} ``` ## DBResults Class diff --git a/Sources/AgileDB/AgileDB.swift b/Sources/AgileDB/AgileDB.swift index 8752eb1..a942a22 100644 --- a/Sources/AgileDB/AgileDB.swift +++ b/Sources/AgileDB/AgileDB.swift @@ -48,7 +48,7 @@ public final class AgileDB { /** Read-only array of unsynced tables. Any tables not in this array will be synced. */ - private(set) public var unsyncedTables: [String] = [] + private(set) public var unsyncedTables: [DBTable] = [] public static var dateFormatter: DateFormatter = { let dateFormatter = DateFormatter() @@ -131,6 +131,7 @@ public final class AgileDB { - returns: Bool Returns if the database could be successfully opened. */ + @discardableResult public func open(_ location: URL? = nil) -> Bool { let dbFileLocation = location ?? self.dbFileLocation ?? URL(fileURLWithPath: defaultFileLocation()) // if we already have a db file open at a different location, close it first @@ -889,7 +890,7 @@ public final class AgileDB { tables.dropTable(table) - if syncingEnabled && unsyncedTables.doesNotContain(table.name) { + if syncingEnabled && unsyncedTables.doesNotContain(table) { let now = AgileDB.stringValueForDate(Date()) if !sqlExecute("insert into __synclog(timestamp, sourceDB, originalDB, tableName, activity, key) values('\(now)','\(dbInstanceKey)','\(dbInstanceKey)','\(table)','X',NULL)") { return false @@ -1011,7 +1012,8 @@ public final class AgileDB { - returns: Bool If list was set successfully. */ - public func setUnsyncedTables(_ tables: [String]) -> Bool { + @discardableResult + public func setUnsyncedTables(_ tables: [DBTable]) -> Bool { let openResults = openDB() if case .failure(_) = openResults { return false @@ -1022,10 +1024,10 @@ public final class AgileDB { return false } - unsyncedTables = [String]() - for tableName in tables { - sqlExecute("delete from __synclog where tableName = '\(tableName)'") - unsyncedTables.append(tableName) + unsyncedTables = [DBTable]() + for table in tables { + sqlExecute("delete from __synclog where tableName = '\(table)'") + unsyncedTables.append(table) } return true @@ -1214,6 +1216,17 @@ public final class AgileDB { } // MARK: - Misc + /** + Check for the existance of a given table + - parameter table: The table to check the existence of + + - returns: the existence of a specified table + */ + public func hasTable(_ table: DBTable) -> Bool { + return tables.hasTable(table) + } + + /** The instanceKey for this database instance. Each AgileDB database is created with a unique instanceKey. Is nil when database could not be opened. */ @@ -1337,10 +1350,10 @@ public final class AgileDB { } if syncingEnabled { - unsyncedTables = [String]() + unsyncedTables = [DBTable]() let unsyncedTables = sqlSelect("select tableName from __unsyncedTables") if let unsyncedTables = unsyncedTables { - self.unsyncedTables = unsyncedTables.map({ $0.values[0] as! String }) + self.unsyncedTables = unsyncedTables.map({ $0.values[0] as! DBTable }) } } @@ -1653,7 +1666,7 @@ extension AgileDB { } } - if syncingEnabled && unsyncedTables.doesNotContain(table.name) { + if syncingEnabled && unsyncedTables.doesNotContain(table) { let now = AgileDB.stringValueForDate(Date()) sql = "insert into __synclog(timestamp, sourceDB, originalDB, tableName, activity, key) values('\(now)','\(sourceDB)','\(originalDB)','\(table)','U','\(esc(key))')" @@ -1728,7 +1741,7 @@ extension AgileDB { } let now = AgileDB.stringValueForDate(Date()) - if syncingEnabled && unsyncedTables.doesNotContain(table.name) { + if syncingEnabled && unsyncedTables.doesNotContain(table) { var sql = "" // auto-deleted entries will be automatically removed from any other databases too. Don't need to log this deletion. if !autoDelete { @@ -2189,6 +2202,9 @@ private extension AgileDB { func openDBFile(_ dbFilePath: String, autoCloseTimeout: Int, completion: @escaping (_ successful: BoolResults, _ openedFromOtherThread: Bool, _ fileExists: Bool) -> Void) { self.autoCloseTimeout = TimeInterval(exactly: autoCloseTimeout) ?? 0.0 self.dbFilePath = dbFilePath + if isDebugging { + print(dbFilePath) + } let block = { [unowned self] in let fileExists = FileManager.default.fileExists(atPath: dbFilePath) diff --git a/Tests/AgileDBTests/AsyncTests.swift b/Tests/AgileDBTests/AsyncTests.swift index 9ea3b8b..8027a8d 100644 --- a/Tests/AgileDBTests/AsyncTests.swift +++ b/Tests/AgileDBTests/AsyncTests.swift @@ -188,6 +188,9 @@ class AsyncTests: XCTestCase { do { let hasKey = try await db.tableHasKey(table: table, key: "testKey4") XCTAssertTrue(hasKey) + + let hasKeys = try await db.tableHasAllKeys(table: table, keys: ["testKey1", "testKey2", "testKey3"]) + XCTAssertTrue(hasKeys) } catch { XCTFail() } diff --git a/Tests/AgileDBTests/BasicTests.swift b/Tests/AgileDBTests/BasicTests.swift index 2c0f268..eff7bfa 100644 --- a/Tests/AgileDBTests/BasicTests.swift +++ b/Tests/AgileDBTests/BasicTests.swift @@ -162,6 +162,8 @@ class BasicTests: XCTestCase { XCTAssert(false, "bool not returned") } db.dropTable(table) + + XCTAssertFalse(db.hasTable(table)) } func testTableHasAllKeys() { diff --git a/Tests/AgileDBTests/DBObjectTests.swift b/Tests/AgileDBTests/DBObjectTests.swift index b8655c1..9dfd044 100644 --- a/Tests/AgileDBTests/DBObjectTests.swift +++ b/Tests/AgileDBTests/DBObjectTests.swift @@ -191,6 +191,15 @@ class DBObjectTests: XCTestCase { XCTAssertEqual(transaction2.accountKey, TransactionValue.accountKey) } + func testDelete() async throws { + let transaction = Transaction(key: TransactionValue.key, date: Date(), accountKey: TransactionValue.accountKey, notes: TransactionValue.notes, amount: TransactionValue.amount, isNew: TransactionValue.isNew) + await transaction.save(to: db) + await transaction.delete(from: db) + let hasKey = try await db.tableHasKey(table: Transaction.table, key: TransactionValue.key) + + XCTAssertFalse(hasKey) + } + func testLoadFromDB() async throws { let transaction = Transaction(key: TransactionValue.key, date: Date(), accountKey: TransactionValue.accountKey, notes: TransactionValue.notes, amount: TransactionValue.amount, isNew: TransactionValue.isNew) await transaction.save(to: db) diff --git a/Tests/AgileDBTests/SyncTests.swift b/Tests/AgileDBTests/SyncTests.swift index 3be19e8..183a3e9 100644 --- a/Tests/AgileDBTests/SyncTests.swift +++ b/Tests/AgileDBTests/SyncTests.swift @@ -117,6 +117,14 @@ class SyncTests: XCTestCase { XCTFail() } + let table11: DBTable = "table11" + db.setUnsyncedTables([table11]) + db.close() + db.open() + XCTAssert(db.unsyncedTables.count == 1) + + let unsyncedTables = db.unsyncedTables + // will be deleted db.setValueInTable(DBTable(name: "table8"), for: "testKey1", to: "{\"numValue\":10,\"account\":\"ACCT1\",\"dateValue\":\"2014-8-19T18:23:42.434-05:00\",\"arrayValue\":[1,2,3,4,5]}", autoDeleteAfter: nil)