From c96c6b960c7bfbe4ad2cfcd2fd6f3cdf65e4ce2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 2 Sep 2015 13:36:20 +0200 Subject: [PATCH] DatabaseValue adopts DatabaseValueConvertible --- .../DatabaseValueTests.swift | 41 +++++++++++++++++++ GRDB.xcodeproj/project.pbxproj | 6 +++ GRDB/DatabaseValue.swift | 13 ++++++ RELEASE_NOTES.md | 7 ++++ TODO.md | 1 - 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 GRDB Tests/Public/DatabaseValueConvertible/DatabaseValueTests.swift diff --git a/GRDB Tests/Public/DatabaseValueConvertible/DatabaseValueTests.swift b/GRDB Tests/Public/DatabaseValueConvertible/DatabaseValueTests.swift new file mode 100644 index 000000000..3d553780f --- /dev/null +++ b/GRDB Tests/Public/DatabaseValueConvertible/DatabaseValueTests.swift @@ -0,0 +1,41 @@ +// +// 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 DatabaseValueTests: GRDBTestCase { + + func testDatabaseValueAdoptsDatabaseValueConvertible() { + assertNoError { + try dbQueue.inDatabase { db in + try db.execute("CREATE TABLE integers (integer INTEGER)") + try db.execute("INSERT INTO integers (integer) VALUES (1)") + let databaseValue: DatabaseValue = db.fetchOneRow("SELECT * FROM integers")!.value(named: "integer")! // Triggers DatabaseValue.init?(databaseValue: DatabaseValue) + let count = db.fetchOne(Int.self, "SELECT COUNT(*) FROM integers WHERE integer = ?", arguments: [databaseValue])! // Triggers DatabaseValue.databaseValue + XCTAssertEqual(count, 1) + } + } + } +} diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index ce48ba48e..be2aea6e5 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -96,6 +96,8 @@ 56F0B9901B6001C600A2F135 /* DatabaseDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98D1B6001C600A2F135 /* DatabaseDateComponentsTests.swift */; }; 56F0B9911B6001C600A2F135 /* DatabaseDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* DatabaseDateTests.swift */; }; 56F0B9921B6001C600A2F135 /* DatabaseDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* DatabaseDateTests.swift */; }; + 56F691D31B97136500423970 /* DatabaseValueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F691D21B97136500423970 /* DatabaseValueTests.swift */; settings = {ASSET_TAGS = (); }; }; + 56F691D41B97136500423970 /* DatabaseValueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F691D21B97136500423970 /* DatabaseValueTests.swift */; settings = {ASSET_TAGS = (); }; }; 9E903FA81B82729400A63DA0 /* DatabaseChanges.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E903FA71B82729400A63DA0 /* DatabaseChanges.swift */; }; 9E903FA91B82741C00A63DA0 /* DatabaseChanges.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E903FA71B82729400A63DA0 /* DatabaseChanges.swift */; }; DC2393C81ABE35F8003FF113 /* GRDB-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -170,6 +172,7 @@ 56E5D7F91B4D422D00430942 /* GRDBTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 56F0B98D1B6001C600A2F135 /* DatabaseDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateComponentsTests.swift; sourceTree = ""; }; 56F0B98E1B6001C600A2F135 /* DatabaseDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateTests.swift; sourceTree = ""; }; + 56F691D21B97136500423970 /* DatabaseValueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueTests.swift; sourceTree = ""; }; 9E903FA71B82729400A63DA0 /* DatabaseChanges.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseChanges.swift; sourceTree = ""; }; DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDB-Bridging.h"; sourceTree = ""; }; DC3773F319C8CBB3004FCF85 /* GRDB.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GRDB.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -270,6 +273,7 @@ children = ( 5699796F1B539038005EBEED /* DatabaseValueConvertibleTests.swift */, 569979701B539038005EBEED /* RawRepresentableTests.swift */, + 56F691D21B97136500423970 /* DatabaseValueTests.swift */, ); path = DatabaseValueConvertible; sourceTree = ""; @@ -642,6 +646,7 @@ 56997A361B539038005EBEED /* DatabaseTests.swift in Sources */, 56997A4E1B539038005EBEED /* PrimaryKeyNoneTests.swift in Sources */, 56997A5A1B539038005EBEED /* RowModelInitializersTests.swift in Sources */, + 56F691D41B97136500423970 /* DatabaseValueTests.swift in Sources */, 56997A401B539038005EBEED /* DatabaseValueConvertibleTests.swift in Sources */, 56997A4C1B539038005EBEED /* PrimaryKeyMultipleTests.swift in Sources */, 56F0B9921B6001C600A2F135 /* DatabaseDateTests.swift in Sources */, @@ -677,6 +682,7 @@ 56997A351B539038005EBEED /* DatabaseTests.swift in Sources */, 56997A4D1B539038005EBEED /* PrimaryKeyNoneTests.swift in Sources */, 56997A591B539038005EBEED /* RowModelInitializersTests.swift in Sources */, + 56F691D31B97136500423970 /* DatabaseValueTests.swift in Sources */, 56997A3F1B539038005EBEED /* DatabaseValueConvertibleTests.swift in Sources */, 56997A4B1B539038005EBEED /* PrimaryKeyMultipleTests.swift in Sources */, 56F0B9911B6001C600A2F135 /* DatabaseDateTests.swift in Sources */, diff --git a/GRDB/DatabaseValue.swift b/GRDB/DatabaseValue.swift index 566353a95..454b7f4db 100644 --- a/GRDB/DatabaseValue.swift +++ b/GRDB/DatabaseValue.swift @@ -130,3 +130,16 @@ public func ==(lhs: DatabaseValue, rhs: DatabaseValue) -> Bool { return false } } + +/// DatabaseValue adopts DatabaseValueConvertible. +extension DatabaseValue : DatabaseValueConvertible { + /// Returns self + public var databaseValue: DatabaseValue { + return self + } + + /// Create a copy of `databaseValue`. + public init?(databaseValue: DatabaseValue) { + self = databaseValue + } +} diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index aa48c867a..17ae32205 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,6 +1,13 @@ Release Notes ============= +## Next version + +**New** + +- `DatabaseValue` adopts `DatabaseValueConvertible`, which allows the injection of a fetched value in another query without having to convert the raw database value into a regular Swift type. + + ## 0.9.0 Released August 25, 2015 diff --git a/TODO.md b/TODO.md index 547dfb5ab..170ebf3da 100644 --- a/TODO.md +++ b/TODO.md @@ -4,4 +4,3 @@ - [ ] How can RowModel wrap two columns in a single CLLocationCoordinate2D property? - [ ] Find a purpose for methods that return a DatabaseValueConvertible? (Row.value(atIndex:), Row.value(named:), DatabaseValue.value()), or remove them. - [ ] Tests for DatabaseValue as Equatable -- [ ] Make sure DatabaseValue is DatabaseValueConvertible, and write a test that chains two requests, the second one using a databaseValue from the first as an argument.