From 82141f7701bbe48a036feff8469cfbcec2bca426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 10 Jul 2015 08:48:37 +0200 Subject: [PATCH] RowModel.copyDatabaseValuesFrom(_:) --- GRDB Tests/RowModel/RowModelCopyTests.swift | 45 +++++++++++++++++++++ GRDB.xcodeproj/project.pbxproj | 6 +++ GRDB/RowModel.swift | 26 ++++++++++-- RELEASE_NOTES.md | 3 +- TODO.md | 2 +- 5 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 GRDB Tests/RowModel/RowModelCopyTests.swift diff --git a/GRDB Tests/RowModel/RowModelCopyTests.swift b/GRDB Tests/RowModel/RowModelCopyTests.swift new file mode 100644 index 000000000..7870a4e47 --- /dev/null +++ b/GRDB Tests/RowModel/RowModelCopyTests.swift @@ -0,0 +1,45 @@ +// +// GRDB.swift +// https://github.com/groue/GRDB.swift +// Copyright (c) 2015 Gwendal Roué +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + + +import XCTest +import GRDB + +class RowModelCopyTests: RowModelTestCase { + + func testRowModelCopyDatabaseValuesFrom() { + assertNoError { + try dbQueue.inDatabase { db in + let person = Person(name: "Arthur", age: 41) + try person.insert(db) + + let clone = Person() + clone.copyDatabaseValuesFrom(person) + XCTAssertEqual(clone.id, person.id) + XCTAssertEqual(clone.name, person.name) + XCTAssertEqual(clone.age, person.age) + XCTAssertTrue(abs(clone.creationDate.timeIntervalSinceDate(person.creationDate)) < 1) + } + } + } +} diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index a38f4ef1b..18742e1a5 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ 56105B121B4F18CC002CD224 /* RowModelDirtyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56105B111B4F18CC002CD224 /* RowModelDirtyTests.swift */; }; 56105B131B4F18CC002CD224 /* RowModelDirtyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56105B111B4F18CC002CD224 /* RowModelDirtyTests.swift */; }; + 56105B151B4F9D86002CD224 /* RowModelCopyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56105B141B4F9D86002CD224 /* RowModelCopyTests.swift */; }; + 56105B161B4F9D86002CD224 /* RowModelCopyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56105B141B4F9D86002CD224 /* RowModelCopyTests.swift */; }; 564E0B141B4D8FC100EBC9F6 /* DatabaseError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E0B131B4D8FC100EBC9F6 /* DatabaseError.swift */; }; 564E0B161B4D901300EBC9F6 /* DatabaseValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E0B151B4D901300EBC9F6 /* DatabaseValue.swift */; }; 565E191E1B42B2B600D32814 /* Database.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565E191D1B42B2B600D32814 /* Database.swift */; }; @@ -105,6 +107,7 @@ /* Begin PBXFileReference section */ 560DB28F1B4AD49300B14958 /* RowModelInitializersTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelInitializersTests.swift; sourceTree = ""; }; 56105B111B4F18CC002CD224 /* RowModelDirtyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelDirtyTests.swift; sourceTree = ""; }; + 56105B141B4F9D86002CD224 /* RowModelCopyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelCopyTests.swift; sourceTree = ""; }; 5610C2081B483AA500F8B4EC /* InMemoryDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InMemoryDatabaseTests.swift; sourceTree = ""; }; 5610C20D1B4852D600F8B4EC /* RowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowTests.swift; sourceTree = ""; }; 5623E0891B494A8A00B20B7F /* RawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentableTests.swift; sourceTree = ""; }; @@ -239,6 +242,7 @@ 56C9E6541B468EB200DDC541 /* PrimaryKeyNoneTests.swift */, 56C9E6561B468EB200DDC541 /* PrimaryKeyRowIDTests.swift */, 56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */, + 56105B141B4F9D86002CD224 /* RowModelCopyTests.swift */, 5623E08B1B49970E00B20B7F /* RowModelDescriptionTests.swift */, 56105B111B4F18CC002CD224 /* RowModelDirtyTests.swift */, 56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */, @@ -560,6 +564,7 @@ 56E5D81A1B4D435D00430942 /* InMemoryDatabaseTests.swift in Sources */, 56E5D81F1B4D435D00430942 /* PrimaryKeyRowIDTests.swift in Sources */, 56E5D81D1B4D435D00430942 /* PrimaryKeyMultipleTests.swift in Sources */, + 56105B161B4F9D86002CD224 /* RowModelCopyTests.swift in Sources */, 56E5D8241B4D435D00430942 /* RowModelSubClassTests.swift in Sources */, 56E5D81E1B4D435D00430942 /* PrimaryKeyNoneTests.swift in Sources */, 56E5D8261B4D435D00430942 /* CustomDatabaseValueConvertibleTests.swift in Sources */, @@ -588,6 +593,7 @@ 56E5D8091B4D424400430942 /* PrimaryKeyMultipleTests.swift in Sources */, 56E5D8041B4D424400430942 /* GRDBTestCase.swift in Sources */, 56E5D80A1B4D424400430942 /* PrimaryKeyNoneTests.swift in Sources */, + 56105B151B4F9D86002CD224 /* RowModelCopyTests.swift in Sources */, 56E5D8071B4D424400430942 /* DatabaseMigratorTests.swift in Sources */, 56E5D8101B4D424400430942 /* RowModelSubClassTests.swift in Sources */, 56E5D8061B4D424400430942 /* InMemoryDatabaseTests.swift in Sources */, diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index a5446134c..911c6d941 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -100,10 +100,27 @@ public class RowModel { } - // MARK: - CRUD + // MARK: - Copy - /// Return true if the stored database dictionary has been modified since - /// last synchronization with the database. + /// Updates a RowModel from another one. + public func copyDatabaseValuesFrom(other: RowModel) { + for (column, value) in other.storedDatabaseDictionary { + if let value = value { + setDatabaseValue(value.databaseValue, forColumn: column) + } else { + setDatabaseValue(.Null, forColumn: column) + } + } + + // Primary key may have been updated: set dirty. + cleanRow = nil + } + + + /// MARK: - Dirty + + /// Return false if the stored database dictionary is known to be not been + /// modified since last synchronization with the database (save or reload). public var isDirty: Bool { guard let cleanRow = cleanRow else { // No known clean row => dirty @@ -113,6 +130,9 @@ public class RowModel { return cleanRow.containsSameColumnsAndValuesAsRow(Row(dictionary: storedDatabaseDictionary)) } + + // MARK: - CRUD + /// An enum that specifies an alternative constraint conflict resolution /// algorithm to use during INSERT and UPDATE commands. /// See https://www.sqlite.org/lang_insert.html & https://www.sqlite.org/lang_update.html diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index b71f2b207..56fec3f6a 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -5,8 +5,9 @@ Release Notes **New** -- `Blob.init?(NSData?)`. It returns nil for empty data, because SQLite can't store zero-length blobs. +- `Blob.init?(NSData?)`. - `RowModel.isDirty`. Updating non dirty models does not touch the database. +- `RowModel.copyDatabaseValuesFrom(_:)` copies a RowModel into another. ## v0.2.0 diff --git a/TODO.md b/TODO.md index 20a2fe720..2b46de1bf 100644 --- a/TODO.md +++ b/TODO.md @@ -8,4 +8,4 @@ - [X] When one forgets to set the primaryKey in setDatabaseValue(_:column:), he has issues later deleting or updating. It's difficult to know why the delete fails. - [ ] Test for the item above - [X] Check the behavior of Blob regarding NSData of size zero which are stored as NULL if I'm not mistaken. -- [ ] How to copy a RowModel? +- [ ] Document RowModel.isDirty