mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Rename databaseEdited to hasPersistentChangedValues
This commit is contained in:
1 parent
1534ac4828
commit
3deedffb77
17 files changed
+140
-121
No files matched your search
+19
-4
@@ -3,7 +3,7 @@ Release Notes
|
||||
|
||||
## Next Release
|
||||
|
||||
The Record class has been refactored so that it gets closer from the RowConvertible and DatabasePersistable protocols. It also makes it easier to write subclasses with non-optional properties.
|
||||
The Record class has been refactored so that it gets closer from the RowConvertible and DatabasePersistable protocols. It also makes it easier to write subclasses that have non-optional properties.
|
||||
|
||||
Methods names that did not match [Swift 3 API Design Guidelines](https://swift.org/documentation/api-design-guidelines.html) have been refactored.
|
||||
|
||||
@@ -13,14 +13,29 @@ Methods names that did not match [Swift 3 API Design Guidelines](https://swift.o
|
||||
|
||||
**Breaking Changes**
|
||||
|
||||
`DatabasePersistable`:
|
||||
|
||||
- `DatabasePersistable.storedDatabaseDictionary` has been renamed `persistedDictionary`.
|
||||
|
||||
`Record`:
|
||||
|
||||
- `Record.reload()` has been removed.
|
||||
- `Record.init(row: Row)` has been renamed `Record.init(_ row: Row)`
|
||||
- `Record.init(row: Row)` has been renamed `Record.init(_ row: Row)` (unlabelled row argument).
|
||||
- `Record.updateFromRow()` has been removed. Override `init(_ row: Row)` instead.
|
||||
- `Record.didInsertWithRowID(_:forColumn:)` should be overriden by Record subclasses that are interested with their inserted rowID.
|
||||
- `Record.didInsertWithRowID(_:forColumn:)` should be overriden by Record subclasses that are interested with their row ids.
|
||||
- `Record.databaseEdited` has been renamed `hasPersistentChangedValues`.
|
||||
- `Record.databaseChanges` now returns `[String: DatabaseValue?]`, the dictionary of old values for changed column names.
|
||||
|
||||
`Row`:
|
||||
|
||||
- `Row.value(named:)` returns nil if no such column exists in the row.
|
||||
- `DatabaseValue` initializers from Int64, Double, String and NSData now have an unlabelled argument.
|
||||
|
||||
`DatabaseValue`:
|
||||
|
||||
- `DatabaseValue` has no public initializers. To create one, use `DatabaseValue.Null`, or the fact that Int, String, etc. adopt the protocol: `1.databaseValue`, `"foo".databaseValue`.
|
||||
|
||||
`DatabaseMigrator`:
|
||||
|
||||
- `DatabaseMigrator.registerMigrationWithoutForeignKeyChecks(_:_:)` has been renamed `DatabaseMigrator.registerMigration(_:withDisabledForeignKeyChecks:migrate:)`.
|
||||
|
||||
|
||||
|
||||
@@ -20,53 +20,53 @@ class DatabaseValueTests: GRDBTestCase {
|
||||
let barData = "bar".dataUsingEncoding(NSUTF8StringEncoding)!
|
||||
|
||||
XCTAssertEqual(DatabaseValue.Null, DatabaseValue.Null)
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue(Int64(1)))
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue(1.0))
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue("foo"))
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue(fooData))
|
||||
XCTAssertNotEqual(DatabaseValue.Null, 1.databaseValue)
|
||||
XCTAssertNotEqual(DatabaseValue.Null, 1.0.databaseValue)
|
||||
XCTAssertNotEqual(DatabaseValue.Null, "foo".databaseValue)
|
||||
XCTAssertNotEqual(DatabaseValue.Null, fooData.databaseValue)
|
||||
|
||||
XCTAssertNotEqual(DatabaseValue(Int64(1)), DatabaseValue.Null)
|
||||
XCTAssertEqual(DatabaseValue(Int64(1)), DatabaseValue(Int64(1)))
|
||||
XCTAssertNotEqual(DatabaseValue(Int64(1)), DatabaseValue(Int64(2)))
|
||||
XCTAssertEqual(DatabaseValue(Int64(1)), DatabaseValue(1.0))
|
||||
XCTAssertNotEqual(DatabaseValue(Int64(1)), DatabaseValue(1.1))
|
||||
XCTAssertNotEqual(DatabaseValue(Int64(1)), DatabaseValue(2.0))
|
||||
XCTAssertEqual(DatabaseValue(1 << 53), DatabaseValue(Double(1 << 53))) // Any integer up to 2^53 has an exact representation as a IEEE-754 double...
|
||||
XCTAssertNotEqual(DatabaseValue(1 << 53 + 1), DatabaseValue(Double(1 << 53 + 1))) // ... 2^53 + 1 does not....
|
||||
XCTAssertEqual(DatabaseValue(1 << 54), DatabaseValue(Double(1 << 54))) // ... but 2^54 does.
|
||||
XCTAssertNotEqual(DatabaseValue(Int64.max), DatabaseValue(Double(Int64.max))) // ... and Int64.max does not.
|
||||
XCTAssertNotEqual(DatabaseValue(Int64(1)), DatabaseValue("foo"))
|
||||
XCTAssertNotEqual(DatabaseValue(Int64(1)), DatabaseValue("1"))
|
||||
XCTAssertNotEqual(DatabaseValue(Int64(1)), DatabaseValue("1.0"))
|
||||
XCTAssertNotEqual(DatabaseValue(Int64(1)), DatabaseValue(fooData))
|
||||
XCTAssertNotEqual(1.databaseValue, DatabaseValue.Null)
|
||||
XCTAssertEqual(1.databaseValue, 1.databaseValue)
|
||||
XCTAssertNotEqual(1.databaseValue, 2.databaseValue)
|
||||
XCTAssertEqual(1.databaseValue, 1.0.databaseValue)
|
||||
XCTAssertNotEqual(1.databaseValue, 1.1.databaseValue)
|
||||
XCTAssertNotEqual(1.databaseValue, 2.0.databaseValue)
|
||||
XCTAssertEqual(Double(1 << 53).databaseValue, Double(1 << 53).databaseValue) // Any integer up to 2^53 has an exact representation as a IEEE-754 double...
|
||||
XCTAssertNotEqual((1 << 53 + 1).databaseValue, Double(1 << 53 + 1).databaseValue) // ... 2^53 + 1 does not....
|
||||
XCTAssertEqual((1 << 54).databaseValue, Double(1 << 54).databaseValue) // ... but 2^54 does.
|
||||
XCTAssertNotEqual(Int64.max.databaseValue, Double(Int64.max).databaseValue) // ... and Int64.max does not.
|
||||
XCTAssertNotEqual(1.databaseValue, "foo".databaseValue)
|
||||
XCTAssertNotEqual(1.databaseValue, "1".databaseValue)
|
||||
XCTAssertNotEqual(1.databaseValue, "1.0".databaseValue)
|
||||
XCTAssertNotEqual(1.databaseValue, fooData.databaseValue)
|
||||
|
||||
XCTAssertNotEqual(DatabaseValue(1.0), DatabaseValue.Null)
|
||||
XCTAssertEqual(DatabaseValue(1.0), DatabaseValue(Int64(1)))
|
||||
XCTAssertNotEqual(DatabaseValue(1.1), DatabaseValue(Int64(1)))
|
||||
XCTAssertNotEqual(DatabaseValue(1.0), DatabaseValue(Int64(2)))
|
||||
XCTAssertEqual(DatabaseValue(1.0), DatabaseValue(1.0))
|
||||
XCTAssertNotEqual(DatabaseValue(1.0), DatabaseValue(2.0))
|
||||
XCTAssertEqual(DatabaseValue(Double(1 << 53)), DatabaseValue(1 << 53)) // Any integer up to 2^53 has an exact representation as a IEEE-754 double...
|
||||
XCTAssertNotEqual(DatabaseValue(Double(1 << 53 + 1)), DatabaseValue(1 << 53 + 1)) // ... 2^53 + 1 does not....
|
||||
XCTAssertEqual(DatabaseValue(Double(1 << 54)), DatabaseValue(1 << 54)) // ... but 2^54 does.
|
||||
XCTAssertNotEqual(DatabaseValue(Double(Int64.max)), DatabaseValue(Int64.max)) // ... and Int64.max does not.
|
||||
XCTAssertNotEqual(DatabaseValue(1.0), DatabaseValue("foo"))
|
||||
XCTAssertNotEqual(DatabaseValue(1.0), DatabaseValue("1"))
|
||||
XCTAssertNotEqual(DatabaseValue(1.0), DatabaseValue("1.0"))
|
||||
XCTAssertNotEqual(DatabaseValue(1.0), DatabaseValue(fooData))
|
||||
XCTAssertNotEqual(1.0.databaseValue, DatabaseValue.Null)
|
||||
XCTAssertEqual(1.0.databaseValue, 1.databaseValue)
|
||||
XCTAssertNotEqual(1.1.databaseValue, 1.databaseValue)
|
||||
XCTAssertNotEqual(1.0.databaseValue, 2.databaseValue)
|
||||
XCTAssertEqual(1.0.databaseValue, 1.0.databaseValue)
|
||||
XCTAssertNotEqual(1.0.databaseValue, 2.0.databaseValue)
|
||||
XCTAssertEqual(Double(1 << 53).databaseValue, Double(1 << 53).databaseValue) // Any integer up to 2^53 has an exact representation as a IEEE-754 double...
|
||||
XCTAssertNotEqual(Double(1 << 53 + 1).databaseValue, (1 << 53 + 1).databaseValue) // ... 2^53 + 1 does not....
|
||||
XCTAssertEqual(Double(1 << 54).databaseValue, (1 << 54).databaseValue) // ... but 2^54 does.
|
||||
XCTAssertNotEqual(Double(Int64.max).databaseValue, Int64.max.databaseValue) // ... and Int64.max does not.
|
||||
XCTAssertNotEqual(1.0.databaseValue, "foo".databaseValue)
|
||||
XCTAssertNotEqual(1.0.databaseValue, "1".databaseValue)
|
||||
XCTAssertNotEqual(1.0.databaseValue, "1.0".databaseValue)
|
||||
XCTAssertNotEqual(1.0.databaseValue, fooData.databaseValue)
|
||||
|
||||
XCTAssertNotEqual(DatabaseValue("foo"), DatabaseValue.Null)
|
||||
XCTAssertNotEqual(DatabaseValue("foo"), DatabaseValue(Int64(1)))
|
||||
XCTAssertNotEqual(DatabaseValue("foo"), DatabaseValue(1.0))
|
||||
XCTAssertEqual(DatabaseValue("foo"), DatabaseValue("foo"))
|
||||
XCTAssertNotEqual(DatabaseValue("foo"), DatabaseValue("bar"))
|
||||
XCTAssertNotEqual(DatabaseValue("foo"), DatabaseValue(fooData))
|
||||
XCTAssertNotEqual("foo".databaseValue, DatabaseValue.Null)
|
||||
XCTAssertNotEqual("foo".databaseValue, 1.databaseValue)
|
||||
XCTAssertNotEqual("foo".databaseValue, 1.0.databaseValue)
|
||||
XCTAssertEqual("foo".databaseValue, "foo".databaseValue)
|
||||
XCTAssertNotEqual("foo".databaseValue, "bar".databaseValue)
|
||||
XCTAssertNotEqual("foo".databaseValue, fooData.databaseValue)
|
||||
|
||||
XCTAssertNotEqual(DatabaseValue(fooData), DatabaseValue.Null)
|
||||
XCTAssertNotEqual(DatabaseValue(fooData), DatabaseValue(Int64(1)))
|
||||
XCTAssertNotEqual(DatabaseValue(fooData), DatabaseValue(1.0))
|
||||
XCTAssertNotEqual(DatabaseValue(fooData), DatabaseValue("foo"))
|
||||
XCTAssertEqual(DatabaseValue(fooData), DatabaseValue(fooData))
|
||||
XCTAssertNotEqual(DatabaseValue(fooData), DatabaseValue(barData))
|
||||
XCTAssertNotEqual(fooData.databaseValue, DatabaseValue.Null)
|
||||
XCTAssertNotEqual(fooData.databaseValue, 1.databaseValue)
|
||||
XCTAssertNotEqual(fooData.databaseValue, 1.0.databaseValue)
|
||||
XCTAssertNotEqual(fooData.databaseValue, "foo".databaseValue)
|
||||
XCTAssertEqual(fooData.databaseValue, fooData.databaseValue)
|
||||
XCTAssertNotEqual(fooData.databaseValue, barData.databaseValue)
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ struct DatabaseTimestamp: DatabaseValueConvertible {
|
||||
|
||||
/// Returns a value that can be stored in the database.
|
||||
var databaseValue: DatabaseValue {
|
||||
return DatabaseValue(date.timeIntervalSince1970)
|
||||
return date.timeIntervalSince1970.databaseValue
|
||||
}
|
||||
|
||||
/// Returns a value initialized from *databaseValue*, if possible.
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ struct WrappedInt: DatabaseValueConvertible {
|
||||
self.int = int
|
||||
}
|
||||
var databaseValue: DatabaseValue {
|
||||
return DatabaseValue(Int64(int))
|
||||
return int.databaseValue
|
||||
}
|
||||
static func fromDatabaseValue(databaseValue: DatabaseValue) -> WrappedInt? {
|
||||
guard let int = Int.fromDatabaseValue(databaseValue) else {
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ import GRDB
|
||||
|
||||
class FetchableParent : DatabaseValueConvertible, CustomStringConvertible {
|
||||
var databaseValue: DatabaseValue {
|
||||
return DatabaseValue("Parent")
|
||||
return "Parent".databaseValue
|
||||
}
|
||||
|
||||
class func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? {
|
||||
@@ -26,7 +26,7 @@ class FetchableParent : DatabaseValueConvertible, CustomStringConvertible {
|
||||
class FetchableChild : FetchableParent {
|
||||
/// Returns a value that can be stored in the database.
|
||||
override var databaseValue: DatabaseValue {
|
||||
return DatabaseValue("Child")
|
||||
return "Child".databaseValue
|
||||
}
|
||||
|
||||
override var description: String { return "Child" }
|
||||
|
||||
@@ -5,7 +5,7 @@ class NSDataTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseValueCanNotStoreEmptyData() {
|
||||
// SQLite can't store zero-length blob.
|
||||
let databaseValue = DatabaseValue(NSData())
|
||||
let databaseValue = NSData().databaseValue
|
||||
XCTAssertEqual(databaseValue, DatabaseValue.Null)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import GRDB
|
||||
|
||||
struct CustomValueType : DatabaseValueConvertible {
|
||||
var databaseValue: DatabaseValue {
|
||||
return DatabaseValue("CustomValueType")
|
||||
return "CustomValueType".databaseValue
|
||||
}
|
||||
static func fromDatabaseValue(databaseValue: DatabaseValue) -> CustomValueType? {
|
||||
guard let string = String.fromDatabaseValue(databaseValue) where string == "CustomValueType" else {
|
||||
|
||||
+2
-3
@@ -16,7 +16,7 @@ struct FastWrappedInt: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
}
|
||||
|
||||
var databaseValue: DatabaseValue {
|
||||
return DatabaseValue(Int64(int))
|
||||
return int.databaseValue
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(databaseValue: DatabaseValue) -> FastWrappedInt? {
|
||||
@@ -30,8 +30,7 @@ struct FastWrappedInt: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
class SQLiteStatementConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testSlowConversion() {
|
||||
let databaseValue = DatabaseValue(Int64(0))
|
||||
let slow = FastWrappedInt.fromDatabaseValue(databaseValue)!
|
||||
let slow = FastWrappedInt.fromDatabaseValue(0.databaseValue)!
|
||||
XCTAssertEqual(slow.int, 0)
|
||||
XCTAssertEqual(slow.fast, false)
|
||||
}
|
||||
|
||||
@@ -365,7 +365,7 @@ class NSDateComponentsTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFromUnparsableString() {
|
||||
let databaseDateComponents = DatabaseDateComponents.fromDatabaseValue(DatabaseValue("foo"))
|
||||
let databaseDateComponents = DatabaseDateComponents.fromDatabaseValue("foo".databaseValue)
|
||||
XCTAssertTrue(databaseDateComponents == nil)
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@ class NSDateTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
func testNSDateFromUnparsableString() {
|
||||
let date = NSDate.fromDatabaseValue(DatabaseValue("foo"))
|
||||
let date = NSDate.fromDatabaseValue("foo".databaseValue)
|
||||
XCTAssertTrue(date == nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
// Create a Record. No fetch has happen, so we don't know if it is
|
||||
// identical to its eventual row in the database. So it is edited.
|
||||
let person = Person(name: "Arthur", age: 41)
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
}
|
||||
|
||||
func testRecordIsEditedAfterInitFromRow() {
|
||||
@@ -46,7 +46,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
// So it is edited.
|
||||
let row = Row(dictionary: ["name": "Arthur", "age": 41])
|
||||
let person = Person(row)
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
}
|
||||
|
||||
func testRecordIsNotEditedAfterFullFetch() {
|
||||
@@ -58,7 +58,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try dbQueue.inDatabase { db in
|
||||
try Person(name: "Arthur", age: 41).insert(db)
|
||||
let person = Person.fetchOne(db, "SELECT * FROM persons")!
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,7 +88,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
XCTFail("Unexpected DatabaseValue")
|
||||
}
|
||||
|
||||
XCTAssertFalse(record.databaseEdited)
|
||||
XCTAssertFalse(record.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try dbQueue.inDatabase { db in
|
||||
try Person(name: "Arthur", age: 41).insert(db)
|
||||
let person = Person.fetchOne(db, "SELECT *, 1 AS foo FROM persons")!
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,7 +116,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try dbQueue.inDatabase { db in
|
||||
try Person(name: "Arthur", age: 41).insert(db)
|
||||
let person = Person.fetchOne(db, "SELECT name FROM persons")!
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,7 +127,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try dbQueue.inDatabase { db in
|
||||
let person = Person(name: "Arthur", age: 41)
|
||||
try person.insert(db)
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,21 +142,21 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.name != nil)
|
||||
person.name = "Bobby" // non-nil vs. non-nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
}
|
||||
do {
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.name != nil)
|
||||
person.name = nil // non-nil vs. nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
}
|
||||
do {
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.age == nil)
|
||||
person.age = 41 // nil vs. non-nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,14 +170,14 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.name != nil)
|
||||
person.name = "Arthur" // non-nil vs. non-nil
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
}
|
||||
do {
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.age == nil)
|
||||
person.age = nil // nil vs. nil
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -191,7 +191,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try person.insert(db)
|
||||
person.name = "Bobby"
|
||||
try person.update(db)
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,11 +202,11 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try dbQueue.inDatabase { db in
|
||||
let person = Person(name: "Arthur", age: 41)
|
||||
try person.save(db)
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
person.name = "Bobby"
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
try person.save(db)
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -218,7 +218,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
let person = Person(name: "Arthur", age: 41)
|
||||
try person.insert(db)
|
||||
person.id = person.id + 1
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,20 +229,20 @@ class RecordEditedTests: GRDBTestCase {
|
||||
let person = Person(name: "Arthur", age: 41)
|
||||
|
||||
try person.insert(db)
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.copy().databaseEdited)
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
XCTAssertFalse(person.copy().hasPersistentChangedValues)
|
||||
|
||||
person.name = "Barbara"
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.copy().databaseEdited)
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
XCTAssertTrue(person.copy().hasPersistentChangedValues)
|
||||
|
||||
person.databaseEdited = false
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
XCTAssertFalse(person.copy().databaseEdited)
|
||||
person.hasPersistentChangedValues = false
|
||||
XCTAssertFalse(person.hasPersistentChangedValues)
|
||||
XCTAssertFalse(person.copy().hasPersistentChangedValues)
|
||||
|
||||
person.databaseEdited = true
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
XCTAssertTrue(person.copy().databaseEdited)
|
||||
person.hasPersistentChangedValues = true
|
||||
XCTAssertTrue(person.hasPersistentChangedValues)
|
||||
XCTAssertTrue(person.copy().hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -442,11 +442,11 @@ class RecordEditedTests: GRDBTestCase {
|
||||
XCTAssertTrue(person.databaseChanges.count > 0) // TODO: compare actual changes
|
||||
XCTAssertEqual(person.databaseChanges.count, person.copy().databaseChanges.count)
|
||||
|
||||
person.databaseEdited = false
|
||||
person.hasPersistentChangedValues = false
|
||||
XCTAssertEqual(person.databaseChanges.count, 0)
|
||||
XCTAssertEqual(person.copy().databaseChanges.count, 0)
|
||||
|
||||
person.databaseEdited = true
|
||||
person.hasPersistentChangedValues = true
|
||||
XCTAssertTrue(person.databaseChanges.count > 0) // TODO: compare actual changes
|
||||
XCTAssertEqual(person.databaseChanges.count, person.copy().databaseChanges.count)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import XCTest
|
||||
import GRDB
|
||||
|
||||
// BadlyMangledStuff.updateFromRow() accepts a row with mangled column names.
|
||||
// Its databaseEdited flag is wrong.
|
||||
// Its hasPersistentChangedValues flag is wrong.
|
||||
class BadlyMangledStuff : Record {
|
||||
var id: Int64?
|
||||
var name: String?
|
||||
@@ -63,7 +63,7 @@ class RecordWithColumnNameManglingTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
// Nothing special here
|
||||
XCTAssertFalse(record.databaseEdited)
|
||||
XCTAssertFalse(record.hasPersistentChangedValues)
|
||||
}
|
||||
do {
|
||||
let record = BadlyMangledStuff.fetchOne(db, "SELECT id AS mangled_id, name AS mangled_name FROM stuffs")!
|
||||
@@ -73,7 +73,7 @@ class RecordWithColumnNameManglingTests: GRDBTestCase {
|
||||
|
||||
// But here lies the problem with BadlyMangledStuff.
|
||||
// It should not be edited:
|
||||
XCTAssertTrue(record.databaseEdited)
|
||||
XCTAssertTrue(record.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,17 +54,17 @@ public struct DatabaseValue : Hashable {
|
||||
public static let Null = DatabaseValue(storage: .Null)
|
||||
|
||||
/// Returns a DatabaseValue storing an Int64.
|
||||
public init(_ int64: Swift.Int64) {
|
||||
init(_ int64: Swift.Int64) {
|
||||
storage = .Int64(int64)
|
||||
}
|
||||
|
||||
/// Returns a DatabaseValue storing a Double.
|
||||
public init(_ double: Swift.Double) {
|
||||
init(_ double: Swift.Double) {
|
||||
storage = .Double(double)
|
||||
}
|
||||
|
||||
/// Returns a DatabaseValue storing a String.
|
||||
public init(_ string: Swift.String) {
|
||||
init(_ string: Swift.String) {
|
||||
storage = .String(string)
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public struct DatabaseValue : Hashable {
|
||||
///
|
||||
/// SQLite cant' store zero-length blobs: if data has zero length, the
|
||||
/// result is NULL.
|
||||
public init(_ data: NSData) {
|
||||
init(_ data: NSData) {
|
||||
if data.length == 0 {
|
||||
// SQLite cant' store zero-length blobs.
|
||||
storage = .Null
|
||||
@@ -82,7 +82,7 @@ public struct DatabaseValue : Hashable {
|
||||
}
|
||||
|
||||
/// Copy initializer
|
||||
public init(_ databaseValue: DatabaseValue) {
|
||||
init(_ databaseValue: DatabaseValue) {
|
||||
// This initializer is used by DatabaseValue.init?(object: AnyObject)
|
||||
self = databaseValue
|
||||
}
|
||||
|
||||
+19
-15
@@ -31,8 +31,8 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
///
|
||||
/// - parameter row: A Row.
|
||||
public func awakeFromFetch(row: Row) {
|
||||
// Take care of the databaseEdited flag. If the row does not contain
|
||||
// all needed columns, the record turns edited.
|
||||
// Take care of the hasPersistentChangedValues flag. If the row does not
|
||||
/// contain ll needed columns, the record turns edited.
|
||||
//
|
||||
// Row may be a metal row which will turn invalid as soon as the SQLite
|
||||
// statement is iterated. We need to store an immutable and safe copy.
|
||||
@@ -107,7 +107,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
/// persistedDictionary.
|
||||
///
|
||||
/// Note that the eventual primary key is copied, as well as the
|
||||
/// databaseEdited flag.
|
||||
/// hasPersistentChangedValues flag.
|
||||
///
|
||||
/// - returns: A copy of self.
|
||||
@warn_unused_result
|
||||
@@ -135,7 +135,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
/// you may set it to true or false when you know better. Setting it to
|
||||
/// false does not prevent it from turning true on subsequent modifications
|
||||
/// of the record.
|
||||
public var databaseEdited: Bool {
|
||||
public var hasPersistentChangedValues: Bool {
|
||||
get {
|
||||
return generateDatabaseChanges().next() != nil
|
||||
}
|
||||
@@ -156,7 +156,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
/// Unless the record has actually been fetched or saved, the old values
|
||||
/// are nil.
|
||||
///
|
||||
/// See `databaseEdited` for more information.
|
||||
/// See `hasPersistentChangedValues` for more information.
|
||||
public var databaseChanges: [String: DatabaseValue?] {
|
||||
var changes: [String: DatabaseValue?] = [:]
|
||||
for (column: column, old: old) in generateDatabaseChanges() {
|
||||
@@ -165,7 +165,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
return changes
|
||||
}
|
||||
|
||||
// A change generator that is used by both databaseEdited and
|
||||
// A change generator that is used by both hasPersistentChangedValues and
|
||||
// databaseChanges properties.
|
||||
private func generateDatabaseChanges() -> AnyGenerator<(column: String, old: DatabaseValue?)> {
|
||||
let oldRow = referenceRow
|
||||
@@ -186,7 +186,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
}
|
||||
|
||||
|
||||
/// Reference row for the *databaseEdited* property.
|
||||
/// Reference row for the *hasPersistentChangedValues* property.
|
||||
var referenceRow: Row?
|
||||
|
||||
|
||||
@@ -194,7 +194,8 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
|
||||
/// Executes an INSERT statement.
|
||||
///
|
||||
/// On success, this method sets the *databaseEdited* flag to false.
|
||||
/// On success, this method sets the *hasPersistentChangedValues* flag
|
||||
/// to false.
|
||||
///
|
||||
/// This method is guaranteed to have inserted a row in the database if it
|
||||
/// returns without error.
|
||||
@@ -207,12 +208,13 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
/// - throws: A DatabaseError whenever a SQLite error occurs.
|
||||
public func insert(db: Database) throws {
|
||||
try performInsert(db)
|
||||
databaseEdited = false
|
||||
hasPersistentChangedValues = false
|
||||
}
|
||||
|
||||
/// Executes an UPDATE statement.
|
||||
///
|
||||
/// On success, this method sets the *databaseEdited* flag to false.
|
||||
/// On success, this method sets the *hasPersistentChangedValues* flag
|
||||
/// to false.
|
||||
///
|
||||
/// This method is guaranteed to have updated a row in the database if it
|
||||
/// returns without error.
|
||||
@@ -223,7 +225,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
/// any row in the database and record could not be updated.
|
||||
public func update(db: Database) throws {
|
||||
try performUpdate(db)
|
||||
databaseEdited = false
|
||||
hasPersistentChangedValues = false
|
||||
}
|
||||
|
||||
/// Executes an INSERT or an UPDATE statement so that `self` is saved in
|
||||
@@ -234,7 +236,8 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
///
|
||||
/// Otherwise, performs an insert.
|
||||
///
|
||||
/// On success, this method sets the *databaseEdited* flag to false.
|
||||
/// On success, this method sets the *hasPersistentChangedValues* flag
|
||||
/// to false.
|
||||
///
|
||||
/// This method is guaranteed to have inserted or updated a row in the
|
||||
/// database if it returns without error.
|
||||
@@ -248,7 +251,8 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
|
||||
/// Executes a DELETE statement.
|
||||
///
|
||||
/// On success, this method sets the *databaseEdited* flag to true.
|
||||
/// On success, this method sets the *hasPersistentChangedValues* flag
|
||||
/// to true.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - returns: Whether a database row was deleted.
|
||||
@@ -257,8 +261,8 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
let deleted = try performDelete(db)
|
||||
// Future calls to update() will throw NotFound. Make the user
|
||||
// a favor and make sure this error is thrown even if she checks the
|
||||
// databaseEdited flag:
|
||||
databaseEdited = true
|
||||
// hasPersistentChangedValues flag:
|
||||
hasPersistentChangedValues = true
|
||||
return deleted
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func synchronizePersonsWithJSON(jsonString: String, inDatabase db: Database) thr
|
||||
case .Common(let person, let jsonPerson):
|
||||
// Matching database and JSON persons:
|
||||
person.updateFromJSON(jsonPerson)
|
||||
if person.databaseEdited {
|
||||
if person.hasPersistentChangedValues {
|
||||
try person.update(db)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -831,7 +831,7 @@ public protocol DatabaseValueConvertible {
|
||||
|
||||
All types that adopt this protocol can be used wherever the built-in types `Int`, `String`, etc. are used. without any limitation or caveat. Those built-in types actually adopt it.
|
||||
|
||||
The `databaseValue` property returns [DatabaseValue](GRDB/Core/DatabaseValue.swift), a type that wraps the five types supported by SQLite: NULL, Int64, Double, String and NSData.
|
||||
The `databaseValue` property returns [DatabaseValue](GRDB/Core/DatabaseValue.swift), a type that wraps the five types supported by SQLite: NULL, Int64, Double, String and NSData. DatabaseValue has no public initializer: to create one, use `DatabaseValue.Null`, or the fact that Int, String, etc. adopt the protocol: `1.databaseValue`, `"foo".databaseValue`.
|
||||
|
||||
The `fromDatabaseValue()` factory method returns an instance of your custom type, if the databaseValue contains a suitable value.
|
||||
|
||||
@@ -860,7 +860,8 @@ struct DatabaseTimestamp: DatabaseValueConvertible {
|
||||
|
||||
/// Returns a value that can be stored in the database.
|
||||
var databaseValue: DatabaseValue {
|
||||
return DatabaseValue(date.timeIntervalSince1970)
|
||||
// Double itself adopts DatabaseValueConvertible:
|
||||
return date.timeIntervalSince1970.databaseValue
|
||||
}
|
||||
|
||||
/// Returns a value initialized from *databaseValue*, if possible.
|
||||
@@ -1339,7 +1340,7 @@ try dbQueue.inDatabase { db in
|
||||
// Changes tracking
|
||||
person.name = "Barbara"
|
||||
person.databaseChanges.keys // ["name"]
|
||||
if person.databaseEdited { // Avoid useless UPDATE statements
|
||||
if person.hasPersistentChangedValues { // Avoid useless UPDATE statements
|
||||
try person.save(db)
|
||||
}
|
||||
}
|
||||
@@ -1751,7 +1752,7 @@ Yet, it does a few things well:
|
||||
person.name = "Barbara"
|
||||
person.age = 41
|
||||
person.databaseChanges.keys // ["age"]
|
||||
if person.databaseEdited {
|
||||
if person.hasPersistentChangedValues {
|
||||
try person.save(db)
|
||||
}
|
||||
```
|
||||
@@ -1785,7 +1786,7 @@ class Person {
|
||||
func copy() -> Self
|
||||
|
||||
// Change Tracking
|
||||
var databaseEdited: Bool
|
||||
var hasPersistentChangedValues: Bool
|
||||
var databaseChanges: [String: DatabaseValue?]
|
||||
|
||||
// Persistence
|
||||
@@ -2040,16 +2041,16 @@ try dbQueue.inDatabase { db in
|
||||
|
||||
The `update()` method always executes an UPDATE statement. When the record has not been edited, this database access is generally useless.
|
||||
|
||||
Avoid it with the `databaseEdited` property, which returns whether the record has changes that have not been saved:
|
||||
Avoid it with the `hasPersistentChangedValues` property, which returns whether the record has changes that have not been saved:
|
||||
|
||||
```swift
|
||||
// Saves the person if it has changes that have not been saved:
|
||||
if person.databaseEdited {
|
||||
if person.hasPersistentChangedValues {
|
||||
try person.save(db)
|
||||
}
|
||||
```
|
||||
|
||||
Note that `databaseEdited` is based on value comparison: **setting a property to the same value does not set the edited flag**.
|
||||
Note that `hasPersistentChangedValues` is based on value comparison: **setting a property to the same value does not set the edited flag**.
|
||||
|
||||
For an efficient algorithm which synchronizes the content of a database table with a JSON payload, check this [sample code](https://gist.github.com/groue/dcdd3784461747874f41).
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
- [ ] `IN (?)` sql snippet, with an array argument.
|
||||
- [ ] Write sample code around NSFetchedResultsController: fetch and output a list of TableView sections & row differences.
|
||||
- [ ] Compare DatabaseCoder with http://mjtsai.com/blog/2015/11/08/the-java-deserialization-bug-and-nssecurecoding/
|
||||
- [ ] databaseEdited -> hasDatabaseChanges, hasNotPersistedChanges, hasPersistentChangedValues (NSManagedObject)? Check if doc makes it clear that the changes are based on last fetch. The method name should make it clear too.
|
||||
- [X] databaseEdited -> hasDatabaseChanges, hasNotPersistedChanges, hasPersistentChangedValues (NSManagedObject)? Check if doc makes it clear that the changes are based on last fetch. The method name should make it clear too.
|
||||
- [X] Check Swift API Guideline https://swift.org/documentation/api-design-guidelines.html
|
||||
- [X] databaseTableName, etc.
|
||||
- [X] didInsertWithRowID(_:forColumn:)
|
||||
|
||||
Reference in new issue
Block a user