mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Turn initializers of DatabaseValue, Row, RowConvertible and StatementArguments from AnyObject, NSArray and NSDictionary to failable initializers.
This commit is contained in:
1 parent
b81a1eb2d7
commit
561ae52422
9 files changed
+95
-37
No files matched your search
@@ -1,6 +1,21 @@
|
||||
Release Notes
|
||||
=============
|
||||
|
||||
## Next Releases
|
||||
|
||||
**New**
|
||||
|
||||
- `DatabaseValue.init?(object: AnyObject)` initializer.
|
||||
|
||||
|
||||
**Breaking Changes**
|
||||
|
||||
- `Row.init?(dictionary: NSDictionary)` is now a failable initializer which returns nil if the NSDictionary contains invalid values.
|
||||
- `RowConvertible.init?(dictionary: NSDictionary)` is now a failable initializer which returns nil if the NSDictionary contains invalid values.
|
||||
- `StatementArguments.init?(_ array: NSArray)` is now a failable initializer which returns nil if the NSArray contains invalid values.
|
||||
- `StatementArguments.init?(_ dictionary: NSDictionary)` is now a failable initializer which returns nil if the NSDictionary contains invalid values.
|
||||
|
||||
|
||||
## 0.28.0
|
||||
|
||||
Released November 13, 2015
|
||||
|
||||
@@ -3,9 +3,15 @@ import GRDB
|
||||
|
||||
class RowFoundationTests: GRDBTestCase {
|
||||
|
||||
func testRowFromInvalidNSDictionary() {
|
||||
let dictionary: NSDictionary = ["a": NSObject()]
|
||||
let row = Row(dictionary: dictionary)
|
||||
XCTAssertTrue(row == nil)
|
||||
}
|
||||
|
||||
func testRowFromNSDictionary() {
|
||||
let dictionary: NSDictionary = ["a": "foo", "b": 1, "c": NSNull(), "d": NSDate(timeIntervalSince1970: 1443642439)]
|
||||
let row = Row(dictionary: dictionary)
|
||||
let row = Row(dictionary: dictionary)!
|
||||
|
||||
XCTAssertEqual(row.count, 4)
|
||||
XCTAssertEqual(row.value(named: "a") as String, "foo")
|
||||
|
||||
@@ -15,11 +15,17 @@ struct PointOfInterest : RowConvertible {
|
||||
|
||||
class RowConvertibleFoundationTests: GRDBTestCase {
|
||||
|
||||
func testInvalidNSDictionaryInitializer() {
|
||||
let dictionary: NSDictionary = ["a": NSObject()]
|
||||
let s = PointOfInterest(dictionary: dictionary)
|
||||
XCTAssertTrue(s == nil)
|
||||
}
|
||||
|
||||
func testNSDictionaryInitializer() {
|
||||
let latitude = 41.8919300
|
||||
let longitude = 12.5113300
|
||||
let dictionary: NSDictionary = ["latitude": latitude, "longitude": longitude, "title": NSNull()]
|
||||
let s = PointOfInterest(dictionary: dictionary)
|
||||
let s = PointOfInterest(dictionary: dictionary)!
|
||||
XCTAssertEqual(s.latitude, latitude)
|
||||
XCTAssertEqual(s.longitude, longitude)
|
||||
XCTAssertTrue(s.title == nil)
|
||||
|
||||
@@ -160,6 +160,8 @@
|
||||
56BB862C1BA988F2001F9168 /* FMResultSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 56BB86221BA988F2001F9168 /* FMResultSet.m */; };
|
||||
56BB862F1BA98AA9001F9168 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 56BB862E1BA98AA9001F9168 /* libsqlite3.tbd */; };
|
||||
56BB86311BA98AB0001F9168 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 56BB86301BA98AB0001F9168 /* libsqlite3.tbd */; };
|
||||
56CA0D411BF5D8E20019C023 /* DatabaseValue+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA0D401BF5D8E20019C023 /* DatabaseValue+Foundation.swift */; };
|
||||
56CA0D421BF5D8E20019C023 /* DatabaseValue+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA0D401BF5D8E20019C023 /* DatabaseValue+Foundation.swift */; };
|
||||
56CA21BA1BB3FCF7009A04C5 /* NSDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA21B91BB3FCF7009A04C5 /* NSDataTests.swift */; };
|
||||
56CA21BB1BB3FCF7009A04C5 /* NSDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA21B91BB3FCF7009A04C5 /* NSDataTests.swift */; };
|
||||
56CA221D1BB41533009A04C5 /* FMDBPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA221A1BB41533009A04C5 /* FMDBPerformanceTests.swift */; };
|
||||
@@ -382,6 +384,7 @@
|
||||
56BB862D1BA98933001F9168 /* GRDBTests-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDBTests-Bridging.h"; sourceTree = "<group>"; };
|
||||
56BB862E1BA98AA9001F9168 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk/usr/lib/libsqlite3.tbd; sourceTree = DEVELOPER_DIR; };
|
||||
56BB86301BA98AB0001F9168 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = usr/lib/libsqlite3.tbd; sourceTree = SDKROOT; };
|
||||
56CA0D401BF5D8E20019C023 /* DatabaseValue+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DatabaseValue+Foundation.swift"; sourceTree = "<group>"; };
|
||||
56CA21B91BB3FCF7009A04C5 /* NSDataTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataTests.swift; sourceTree = "<group>"; };
|
||||
56CA21FE1BB414FE009A04C5 /* SQLite.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = SQLite.xcodeproj; path = SQLite.swift/SQLite.xcodeproj; sourceTree = "<group>"; };
|
||||
56CA221A1BB41533009A04C5 /* FMDBPerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FMDBPerformanceTests.swift; sourceTree = "<group>"; };
|
||||
@@ -497,6 +500,7 @@
|
||||
children = (
|
||||
56A238011B9C6A650082EB20 /* DatabaseDateComponents.swift */,
|
||||
56B7F4331BE4BF1600E39BBF /* DatabaseCoder.swift */,
|
||||
56CA0D401BF5D8E20019C023 /* DatabaseValue+Foundation.swift */,
|
||||
56A2380B1B9C6E440082EB20 /* NSData.swift */,
|
||||
56A238041B9C6D670082EB20 /* NSDate.swift */,
|
||||
56A238AC1B9C90090082EB20 /* NSNull.swift */,
|
||||
@@ -1136,6 +1140,7 @@
|
||||
56A238101B9C74460082EB20 /* RawRepresentable.swift in Sources */,
|
||||
56A238061B9C6D670082EB20 /* NSDate.swift in Sources */,
|
||||
56A2380D1B9C6E440082EB20 /* NSData.swift in Sources */,
|
||||
56CA0D421BF5D8E20019C023 /* DatabaseValue+Foundation.swift in Sources */,
|
||||
56A2387C1B9C75030082EB20 /* Configuration.swift in Sources */,
|
||||
56A238B11B9C90BB0082EB20 /* NSString.swift in Sources */,
|
||||
56A238B41B9C919D0082EB20 /* NSNumber.swift in Sources */,
|
||||
@@ -1306,6 +1311,7 @@
|
||||
56A238891B9C75030082EB20 /* SelectStatement.swift in Sources */,
|
||||
56A238811B9C75030082EB20 /* DatabaseError.swift in Sources */,
|
||||
56A238991B9C752B0082EB20 /* DatabaseTableMapping.swift in Sources */,
|
||||
56CA0D411BF5D8E20019C023 /* DatabaseValue+Foundation.swift in Sources */,
|
||||
56A238851B9C75030082EB20 /* DatabaseValue.swift in Sources */,
|
||||
56A238A41B9C753B0082EB20 /* Record.swift in Sources */,
|
||||
56A2388F1B9C75030082EB20 /* UpdateStatement.swift in Sources */,
|
||||
|
||||
@@ -36,6 +36,11 @@ public struct DatabaseValue : Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy initializer
|
||||
public init(_ databaseValue: DatabaseValue) {
|
||||
self.storage = databaseValue.storage
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Extracting Swift Value
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import Foundation
|
||||
|
||||
/// Foundation support for DatabaseValue
|
||||
extension DatabaseValue {
|
||||
|
||||
/// Builds a DatabaseValue from AnyObject.
|
||||
///
|
||||
/// The result is nil unless object adopts DatabaseValueConvertible (NSData,
|
||||
/// NSDate, NSNull, NSNumber, NSString, NSURL).
|
||||
///
|
||||
/// - parameter object: An AnyObject.
|
||||
public init?(object: AnyObject) {
|
||||
guard let convertible = object as? DatabaseValueConvertible else {
|
||||
return nil
|
||||
}
|
||||
self.init(convertible.databaseValue)
|
||||
}
|
||||
}
|
||||
@@ -5,21 +5,21 @@ extension Row {
|
||||
|
||||
/// Builds a row from an NSDictionary.
|
||||
///
|
||||
/// All keys must be strings, and values must adopt
|
||||
/// DatabaseValueConvertible: NSData, NSDate, NSNull, NSNumber, NSString,
|
||||
/// NSURL.
|
||||
/// The result is nil unless all dictionary keys are strings, and values
|
||||
/// adopt DatabaseValueConvertible (NSData, NSDate, NSNull, NSNumber,
|
||||
/// NSString, NSURL).
|
||||
///
|
||||
/// - parameter dictionary: An NSDictionary.
|
||||
public convenience init(dictionary: NSDictionary) {
|
||||
public convenience init?(dictionary: NSDictionary) {
|
||||
var initDictionary = [String: DatabaseValueConvertible?]()
|
||||
for (key, value) in dictionary {
|
||||
guard let columnName = key as? String else {
|
||||
fatalError("Dictionary key is not a string: \(key)")
|
||||
return nil
|
||||
}
|
||||
guard let convertible = value as? DatabaseValueConvertible else {
|
||||
fatalError("Dictionary value is not a string: \(key)")
|
||||
guard let databaseValue = DatabaseValue(object: value) else {
|
||||
return nil
|
||||
}
|
||||
initDictionary[columnName] = convertible.databaseValue // Because databaseValue adopts DatabaseValueConvertible?
|
||||
initDictionary[columnName] = databaseValue
|
||||
}
|
||||
self.init(dictionary: initDictionary)
|
||||
}
|
||||
|
||||
@@ -4,9 +4,15 @@ extension RowConvertible {
|
||||
|
||||
/// NSDictionary initializer.
|
||||
///
|
||||
/// The result is nil unless all keys are strings, and values adopt
|
||||
/// DatabaseValueConvertible (NSData, NSDate, NSNull, NSNumber, NSString,
|
||||
/// NSURL).
|
||||
///
|
||||
/// - parameter dictionary: An NSDictionary.
|
||||
public init(dictionary: NSDictionary) {
|
||||
let row = Row(dictionary: dictionary)
|
||||
public init?(dictionary: NSDictionary) {
|
||||
guard let row = Row(dictionary: dictionary) else {
|
||||
return nil
|
||||
}
|
||||
self.init(row: row)
|
||||
}
|
||||
}
|
||||
@@ -4,15 +4,15 @@ extension StatementArguments {
|
||||
|
||||
/// Initializes arguments from an NSArray.
|
||||
///
|
||||
/// The array must contain objects that adopt the DatabaseValueConvertible
|
||||
/// protocol. A fatal error is thrown otherwise.
|
||||
/// The result is nil unless all objects adopt DatabaseValueConvertible
|
||||
/// (NSData, NSDate, NSNull, NSNumber, NSString, NSURL).
|
||||
///
|
||||
/// let values: NSArray = ["foo", "bar", "baz"]
|
||||
/// db.execute("INSERT ... (?,?,?)", arguments: StatementArguments(values))
|
||||
/// db.execute("INSERT ... (?,?,?)", arguments: StatementArguments(values)!)
|
||||
///
|
||||
/// - parameter array: An NSArray
|
||||
/// - returns: A StatementArguments.
|
||||
public init(_ array: NSArray) {
|
||||
public init?(_ array: NSArray) {
|
||||
// IMPLEMENTATION NOTE
|
||||
//
|
||||
// This initializer is required for the following code to compile:
|
||||
@@ -27,23 +27,22 @@ extension StatementArguments {
|
||||
// }
|
||||
var values = [DatabaseValueConvertible?]()
|
||||
for object in array {
|
||||
switch object {
|
||||
case let value as DatabaseValueConvertible:
|
||||
values.append(value)
|
||||
default:
|
||||
fatalError("Not DatabaseValueConvertible: \(object)")
|
||||
guard let databaseValue = DatabaseValue(object: object) else {
|
||||
return nil
|
||||
}
|
||||
values.append(databaseValue)
|
||||
}
|
||||
self.init(values)
|
||||
}
|
||||
|
||||
/// Initializes arguments from an NSDictionary.
|
||||
///
|
||||
/// The dictionary must contain objects that adopt the
|
||||
/// DatabaseValueConvertible protocol. A fatal error is thrown otherwise.
|
||||
/// The result is nil unless all dictionary keys are strings, and values
|
||||
/// adopt DatabaseValueConvertible (NSData, NSDate, NSNull, NSNumber,
|
||||
/// NSString, NSURL).
|
||||
///
|
||||
/// let values: NSDictionary = ["firstName": "Arthur", "lastName": "Miller"]
|
||||
/// db.execute("INSERT ... (?,?,?)", arguments: StatementArguments(values))
|
||||
/// db.execute("INSERT ... (?,?,?)", arguments: StatementArguments(values)!)
|
||||
///
|
||||
/// GRDB.swift only supports colon-prefixed named arguments, even though
|
||||
/// SQLite supports other syntaxes. See https://www.sqlite.org/lang_expr.html#varparam
|
||||
@@ -51,7 +50,7 @@ extension StatementArguments {
|
||||
///
|
||||
/// - parameter dictionary: An NSDictionary
|
||||
/// - returns: A StatementArguments.
|
||||
public init(_ dictionary: NSDictionary) {
|
||||
public init?(_ dictionary: NSDictionary) {
|
||||
// IMPLEMENTATION NOTE
|
||||
//
|
||||
// This initializer is required for the following code to compile:
|
||||
@@ -64,19 +63,16 @@ extension StatementArguments {
|
||||
// for person in persons {
|
||||
// try statement.execute(StatementArguments(person)) // Avoid an error here
|
||||
// }
|
||||
var values = [String: DatabaseValueConvertible?]()
|
||||
for (key, object) in dictionary {
|
||||
if let key = key as? String {
|
||||
switch object {
|
||||
case let value as DatabaseValueConvertible:
|
||||
values[key] = value
|
||||
default:
|
||||
fatalError("Not DatabaseValueConvertible: \(object)")
|
||||
}
|
||||
} else {
|
||||
fatalError("Not a String key: \(key)")
|
||||
var initDictionary = [String: DatabaseValueConvertible?]()
|
||||
for (key, value) in dictionary {
|
||||
guard let columnName = key as? String else {
|
||||
return nil
|
||||
}
|
||||
guard let databaseValue = DatabaseValue(object: value) else {
|
||||
return nil
|
||||
}
|
||||
initDictionary[columnName] = databaseValue
|
||||
}
|
||||
self.init(values)
|
||||
self.init(initDictionary)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user