Fix setUnsyncedTables signature. Add hasTable method.
This commit is contained in:
parent
cd6c612602
commit
068c0ef5ba
13
README.md
13
README.md
|
@ -94,15 +94,12 @@ category.save(to: db)
|
||||||
// save to database, automatically delete after designated date
|
// save to database, automatically delete after designated date
|
||||||
category.save(to: db, autoDeleteAfter: deletionDate)
|
category.save(to: db, autoDeleteAfter: deletionDate)
|
||||||
|
|
||||||
// instantiate synchronously
|
// instantiate and pull from DB asynchronously
|
||||||
guard let category = Category(db: db, key: categoryKey) else { return }
|
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
|
## DBResults Class
|
||||||
|
|
|
@ -48,7 +48,7 @@ public final class AgileDB {
|
||||||
/**
|
/**
|
||||||
Read-only array of unsynced tables. Any tables not in this array will be synced.
|
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 = {
|
public static var dateFormatter: DateFormatter = {
|
||||||
let dateFormatter = DateFormatter()
|
let dateFormatter = DateFormatter()
|
||||||
|
@ -131,6 +131,7 @@ public final class AgileDB {
|
||||||
|
|
||||||
- returns: Bool Returns if the database could be successfully opened.
|
- returns: Bool Returns if the database could be successfully opened.
|
||||||
*/
|
*/
|
||||||
|
@discardableResult
|
||||||
public func open(_ location: URL? = nil) -> Bool {
|
public func open(_ location: URL? = nil) -> Bool {
|
||||||
let dbFileLocation = location ?? self.dbFileLocation ?? URL(fileURLWithPath: defaultFileLocation())
|
let dbFileLocation = location ?? self.dbFileLocation ?? URL(fileURLWithPath: defaultFileLocation())
|
||||||
// if we already have a db file open at a different location, close it first
|
// 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)
|
tables.dropTable(table)
|
||||||
|
|
||||||
if syncingEnabled && unsyncedTables.doesNotContain(table.name) {
|
if syncingEnabled && unsyncedTables.doesNotContain(table) {
|
||||||
let now = AgileDB.stringValueForDate(Date())
|
let now = AgileDB.stringValueForDate(Date())
|
||||||
if !sqlExecute("insert into __synclog(timestamp, sourceDB, originalDB, tableName, activity, key) values('\(now)','\(dbInstanceKey)','\(dbInstanceKey)','\(table)','X',NULL)") {
|
if !sqlExecute("insert into __synclog(timestamp, sourceDB, originalDB, tableName, activity, key) values('\(now)','\(dbInstanceKey)','\(dbInstanceKey)','\(table)','X',NULL)") {
|
||||||
return false
|
return false
|
||||||
|
@ -1011,7 +1012,8 @@ public final class AgileDB {
|
||||||
|
|
||||||
- returns: Bool If list was set successfully.
|
- returns: Bool If list was set successfully.
|
||||||
*/
|
*/
|
||||||
public func setUnsyncedTables(_ tables: [String]) -> Bool {
|
@discardableResult
|
||||||
|
public func setUnsyncedTables(_ tables: [DBTable]) -> Bool {
|
||||||
let openResults = openDB()
|
let openResults = openDB()
|
||||||
if case .failure(_) = openResults {
|
if case .failure(_) = openResults {
|
||||||
return false
|
return false
|
||||||
|
@ -1022,10 +1024,10 @@ public final class AgileDB {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
unsyncedTables = [String]()
|
unsyncedTables = [DBTable]()
|
||||||
for tableName in tables {
|
for table in tables {
|
||||||
sqlExecute("delete from __synclog where tableName = '\(tableName)'")
|
sqlExecute("delete from __synclog where tableName = '\(table)'")
|
||||||
unsyncedTables.append(tableName)
|
unsyncedTables.append(table)
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -1214,6 +1216,17 @@ public final class AgileDB {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Misc
|
// 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.
|
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 {
|
if syncingEnabled {
|
||||||
unsyncedTables = [String]()
|
unsyncedTables = [DBTable]()
|
||||||
let unsyncedTables = sqlSelect("select tableName from __unsyncedTables")
|
let unsyncedTables = sqlSelect("select tableName from __unsyncedTables")
|
||||||
if let unsyncedTables = 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())
|
let now = AgileDB.stringValueForDate(Date())
|
||||||
sql = "insert into __synclog(timestamp, sourceDB, originalDB, tableName, activity, key) values('\(now)','\(sourceDB)','\(originalDB)','\(table)','U','\(esc(key))')"
|
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())
|
let now = AgileDB.stringValueForDate(Date())
|
||||||
if syncingEnabled && unsyncedTables.doesNotContain(table.name) {
|
if syncingEnabled && unsyncedTables.doesNotContain(table) {
|
||||||
var sql = ""
|
var sql = ""
|
||||||
// auto-deleted entries will be automatically removed from any other databases too. Don't need to log this deletion.
|
// auto-deleted entries will be automatically removed from any other databases too. Don't need to log this deletion.
|
||||||
if !autoDelete {
|
if !autoDelete {
|
||||||
|
@ -2189,6 +2202,9 @@ private extension AgileDB {
|
||||||
func openDBFile(_ dbFilePath: String, autoCloseTimeout: Int, completion: @escaping (_ successful: BoolResults, _ openedFromOtherThread: Bool, _ fileExists: Bool) -> Void) {
|
func openDBFile(_ dbFilePath: String, autoCloseTimeout: Int, completion: @escaping (_ successful: BoolResults, _ openedFromOtherThread: Bool, _ fileExists: Bool) -> Void) {
|
||||||
self.autoCloseTimeout = TimeInterval(exactly: autoCloseTimeout) ?? 0.0
|
self.autoCloseTimeout = TimeInterval(exactly: autoCloseTimeout) ?? 0.0
|
||||||
self.dbFilePath = dbFilePath
|
self.dbFilePath = dbFilePath
|
||||||
|
if isDebugging {
|
||||||
|
print(dbFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
let block = { [unowned self] in
|
let block = { [unowned self] in
|
||||||
let fileExists = FileManager.default.fileExists(atPath: dbFilePath)
|
let fileExists = FileManager.default.fileExists(atPath: dbFilePath)
|
||||||
|
|
|
@ -188,6 +188,9 @@ class AsyncTests: XCTestCase {
|
||||||
do {
|
do {
|
||||||
let hasKey = try await db.tableHasKey(table: table, key: "testKey4")
|
let hasKey = try await db.tableHasKey(table: table, key: "testKey4")
|
||||||
XCTAssertTrue(hasKey)
|
XCTAssertTrue(hasKey)
|
||||||
|
|
||||||
|
let hasKeys = try await db.tableHasAllKeys(table: table, keys: ["testKey1", "testKey2", "testKey3"])
|
||||||
|
XCTAssertTrue(hasKeys)
|
||||||
} catch {
|
} catch {
|
||||||
XCTFail()
|
XCTFail()
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,6 +162,8 @@ class BasicTests: XCTestCase {
|
||||||
XCTAssert(false, "bool not returned")
|
XCTAssert(false, "bool not returned")
|
||||||
}
|
}
|
||||||
db.dropTable(table)
|
db.dropTable(table)
|
||||||
|
|
||||||
|
XCTAssertFalse(db.hasTable(table))
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTableHasAllKeys() {
|
func testTableHasAllKeys() {
|
||||||
|
|
|
@ -191,6 +191,15 @@ class DBObjectTests: XCTestCase {
|
||||||
XCTAssertEqual(transaction2.accountKey, TransactionValue.accountKey)
|
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 {
|
func testLoadFromDB() async throws {
|
||||||
let transaction = Transaction(key: TransactionValue.key, date: Date(), accountKey: TransactionValue.accountKey, notes: TransactionValue.notes, amount: TransactionValue.amount, isNew: TransactionValue.isNew)
|
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.save(to: db)
|
||||||
|
|
|
@ -117,6 +117,14 @@ class SyncTests: XCTestCase {
|
||||||
XCTFail()
|
XCTFail()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let table11: DBTable = "table11"
|
||||||
|
db.setUnsyncedTables([table11])
|
||||||
|
db.close()
|
||||||
|
db.open()
|
||||||
|
XCTAssert(db.unsyncedTables.count == 1)
|
||||||
|
|
||||||
|
let unsyncedTables = db.unsyncedTables
|
||||||
|
|
||||||
// will be deleted
|
// 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)
|
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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue