From b686e1fe91f020b09264d2b3305166155a3807d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 6 Sep 2015 17:33:39 +0200 Subject: [PATCH] NSNull, NSNumber and NSString adopt DatabaseValueConvertible. --- GRDB.xcodeproj/project.pbxproj | 32 ++++++-- ...nts.swift => DatabaseDateComponents.swift} | 0 GRDB/Foundation/NSNull.swift | 13 ++++ GRDB/Foundation/NSNumber.swift | 50 +++++++++++++ GRDB/Foundation/NSString.swift | 17 +++++ ...ementArgumentsFoundationInitializers.swift | 74 ++++--------------- 6 files changed, 119 insertions(+), 67 deletions(-) rename GRDB/Foundation/{NSDateComponents.swift => DatabaseDateComponents.swift} (100%) create mode 100644 GRDB/Foundation/NSNull.swift create mode 100644 GRDB/Foundation/NSNumber.swift create mode 100644 GRDB/Foundation/NSString.swift diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 580bc780c..1c02bf252 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -11,8 +11,8 @@ 56997A8E1B5390D4005EBEED /* DatabaseErrorFromSelectStatementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56997A8D1B5390D4005EBEED /* DatabaseErrorFromSelectStatementTests.swift */; }; 56997A8F1B5390D4005EBEED /* DatabaseErrorFromSelectStatementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56997A8D1B5390D4005EBEED /* DatabaseErrorFromSelectStatementTests.swift */; }; 56A237FF1B9C69E60082EB20 /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A237FE1B9C69E60082EB20 /* UIImage.swift */; settings = {ASSET_TAGS = (); }; }; - 56A238021B9C6A650082EB20 /* NSDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238011B9C6A650082EB20 /* NSDateComponents.swift */; settings = {ASSET_TAGS = (); }; }; - 56A238031B9C6A650082EB20 /* NSDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238011B9C6A650082EB20 /* NSDateComponents.swift */; settings = {ASSET_TAGS = (); }; }; + 56A238021B9C6A650082EB20 /* DatabaseDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238011B9C6A650082EB20 /* DatabaseDateComponents.swift */; settings = {ASSET_TAGS = (); }; }; + 56A238031B9C6A650082EB20 /* DatabaseDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238011B9C6A650082EB20 /* DatabaseDateComponents.swift */; settings = {ASSET_TAGS = (); }; }; 56A238051B9C6D670082EB20 /* NSDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238041B9C6D670082EB20 /* NSDate.swift */; settings = {ASSET_TAGS = (); }; }; 56A238061B9C6D670082EB20 /* NSDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238041B9C6D670082EB20 /* NSDate.swift */; settings = {ASSET_TAGS = (); }; }; 56A238091B9C6DD20082EB20 /* StandardLibraryTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238081B9C6DD20082EB20 /* StandardLibraryTypes.swift */; settings = {ASSET_TAGS = (); }; }; @@ -115,6 +115,12 @@ 56A238A81B9C760C0082EB20 /* Blob.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A61B9C760C0082EB20 /* Blob.swift */; settings = {ASSET_TAGS = (); }; }; 56A238AA1B9C7E540082EB20 /* StatementArgumentsFoundationInitializers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A91B9C7E540082EB20 /* StatementArgumentsFoundationInitializers.swift */; settings = {ASSET_TAGS = (); }; }; 56A238AB1B9C7E540082EB20 /* StatementArgumentsFoundationInitializers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A91B9C7E540082EB20 /* StatementArgumentsFoundationInitializers.swift */; settings = {ASSET_TAGS = (); }; }; + 56A238AD1B9C90090082EB20 /* NSNull.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238AC1B9C90090082EB20 /* NSNull.swift */; settings = {ASSET_TAGS = (); }; }; + 56A238AE1B9C90090082EB20 /* NSNull.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238AC1B9C90090082EB20 /* NSNull.swift */; settings = {ASSET_TAGS = (); }; }; + 56A238B01B9C90BB0082EB20 /* NSString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238AF1B9C90BB0082EB20 /* NSString.swift */; settings = {ASSET_TAGS = (); }; }; + 56A238B11B9C90BB0082EB20 /* NSString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238AF1B9C90BB0082EB20 /* NSString.swift */; settings = {ASSET_TAGS = (); }; }; + 56A238B31B9C919D0082EB20 /* NSNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238B21B9C919D0082EB20 /* NSNumber.swift */; settings = {ASSET_TAGS = (); }; }; + 56A238B41B9C919D0082EB20 /* NSNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238B21B9C919D0082EB20 /* NSNumber.swift */; settings = {ASSET_TAGS = (); }; }; 56E5D7D41B4D3FEE00430942 /* GRDB.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 56E5D7CA1B4D3FED00430942 /* GRDB.framework */; }; 56E5D7FE1B4D422E00430942 /* GRDB.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC3773F319C8CBB3004FCF85 /* GRDB.framework */; }; 56E5D8041B4D424400430942 /* GRDBTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */; }; @@ -152,7 +158,7 @@ 5683C2681B4D445E00296494 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/lib/libsqlite3.dylib; sourceTree = DEVELOPER_DIR; }; 56997A8D1B5390D4005EBEED /* DatabaseErrorFromSelectStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseErrorFromSelectStatementTests.swift; sourceTree = ""; }; 56A237FE1B9C69E60082EB20 /* UIImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImage.swift; sourceTree = ""; }; - 56A238011B9C6A650082EB20 /* NSDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateComponents.swift; sourceTree = ""; }; + 56A238011B9C6A650082EB20 /* DatabaseDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateComponents.swift; sourceTree = ""; }; 56A238041B9C6D670082EB20 /* NSDate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDate.swift; sourceTree = ""; }; 56A238081B9C6DD20082EB20 /* StandardLibraryTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StandardLibraryTypes.swift; sourceTree = ""; }; 56A2380B1B9C6E440082EB20 /* NSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSData.swift; sourceTree = ""; }; @@ -204,6 +210,9 @@ 56A238A11B9C753B0082EB20 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = ""; }; 56A238A61B9C760C0082EB20 /* Blob.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Blob.swift; sourceTree = ""; }; 56A238A91B9C7E540082EB20 /* StatementArgumentsFoundationInitializers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementArgumentsFoundationInitializers.swift; sourceTree = ""; }; + 56A238AC1B9C90090082EB20 /* NSNull.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSNull.swift; sourceTree = ""; }; + 56A238AF1B9C90BB0082EB20 /* NSString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSString.swift; sourceTree = ""; }; + 56A238B21B9C919D0082EB20 /* NSNumber.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSNumber.swift; sourceTree = ""; }; 56B8F49A1B4E2F3600C24296 /* GRDB.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = GRDB.xcconfig; sourceTree = ""; }; 56B8F49B1B4E2F3600C24296 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; 56E1524B1B43FD5B00EE27AA /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = ""; }; @@ -296,9 +305,12 @@ 56A238001B9C6A1E0082EB20 /* Foundation */ = { isa = PBXGroup; children = ( - 56A238011B9C6A650082EB20 /* NSDateComponents.swift */, - 56A238041B9C6D670082EB20 /* NSDate.swift */, + 56A238011B9C6A650082EB20 /* DatabaseDateComponents.swift */, 56A2380B1B9C6E440082EB20 /* NSData.swift */, + 56A238041B9C6D670082EB20 /* NSDate.swift */, + 56A238AC1B9C90090082EB20 /* NSNull.swift */, + 56A238B21B9C919D0082EB20 /* NSNumber.swift */, + 56A238AF1B9C90BB0082EB20 /* NSString.swift */, 56A238A91B9C7E540082EB20 /* StatementArgumentsFoundationInitializers.swift */, ); path = Foundation; @@ -746,18 +758,21 @@ 56A238941B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */, 56A238AB1B9C7E540082EB20 /* StatementArgumentsFoundationInitializers.swift in Sources */, 56A2389E1B9C752B0082EB20 /* RowConvertible.swift in Sources */, - 56A238031B9C6A650082EB20 /* NSDateComponents.swift in Sources */, + 56A238031B9C6A650082EB20 /* DatabaseDateComponents.swift in Sources */, 56A238A51B9C753B0082EB20 /* RowModel.swift in Sources */, 56A238101B9C74460082EB20 /* RawRepresentable.swift in Sources */, 56A238061B9C6D670082EB20 /* NSDate.swift in Sources */, 56A2380D1B9C6E440082EB20 /* NSData.swift in Sources */, 56A2387C1B9C75030082EB20 /* Configuration.swift in Sources */, + 56A238B11B9C90BB0082EB20 /* NSString.swift in Sources */, 56A238A81B9C760C0082EB20 /* Blob.swift in Sources */, + 56A238B41B9C919D0082EB20 /* NSNumber.swift in Sources */, 56A2388C1B9C75030082EB20 /* Statement.swift in Sources */, 56A238A31B9C753B0082EB20 /* DatabaseStorable.swift in Sources */, 56A2388A1B9C75030082EB20 /* SelectStatement.swift in Sources */, 56A2389C1B9C752B0082EB20 /* DatabaseValueConvertible.swift in Sources */, 56A238801B9C75030082EB20 /* DatabaseChanges.swift in Sources */, + 56A238AE1B9C90090082EB20 /* NSNull.swift in Sources */, 56A2389A1B9C752B0082EB20 /* DatabaseTableMapping.swift in Sources */, 56A237FF1B9C69E60082EB20 /* UIImage.swift in Sources */, 56A238881B9C75030082EB20 /* Row.swift in Sources */, @@ -849,6 +864,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 56A238AD1B9C90090082EB20 /* NSNull.swift in Sources */, 56A2389D1B9C752B0082EB20 /* RowConvertible.swift in Sources */, 56A2388D1B9C75030082EB20 /* StatementArguments.swift in Sources */, 56A238051B9C6D670082EB20 /* NSDate.swift in Sources */, @@ -864,9 +880,11 @@ 56A238A41B9C753B0082EB20 /* RowModel.swift in Sources */, 56A2388F1B9C75030082EB20 /* UpdateStatement.swift in Sources */, 56A238091B9C6DD20082EB20 /* StandardLibraryTypes.swift in Sources */, - 56A238021B9C6A650082EB20 /* NSDateComponents.swift in Sources */, + 56A238021B9C6A650082EB20 /* DatabaseDateComponents.swift in Sources */, 56A2388B1B9C75030082EB20 /* Statement.swift in Sources */, 56A2387F1B9C75030082EB20 /* DatabaseChanges.swift in Sources */, + 56A238B31B9C919D0082EB20 /* NSNumber.swift in Sources */, + 56A238B01B9C90BB0082EB20 /* NSString.swift in Sources */, 56A238931B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */, 56A2387D1B9C75030082EB20 /* Database.swift in Sources */, 56A238831B9C75030082EB20 /* DatabaseQueue.swift in Sources */, diff --git a/GRDB/Foundation/NSDateComponents.swift b/GRDB/Foundation/DatabaseDateComponents.swift similarity index 100% rename from GRDB/Foundation/NSDateComponents.swift rename to GRDB/Foundation/DatabaseDateComponents.swift diff --git a/GRDB/Foundation/NSNull.swift b/GRDB/Foundation/NSNull.swift new file mode 100644 index 000000000..9032de869 --- /dev/null +++ b/GRDB/Foundation/NSNull.swift @@ -0,0 +1,13 @@ +/// NSNull adopts DatabaseValueConvertible +extension NSNull: DatabaseValueConvertible { + + /// Returns DatabaseValue.Null. + public var databaseValue: DatabaseValue { + return .Null + } + + /// Returns nil. + public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { + return nil + } +} diff --git a/GRDB/Foundation/NSNumber.swift b/GRDB/Foundation/NSNumber.swift new file mode 100644 index 000000000..42ec3fb8e --- /dev/null +++ b/GRDB/Foundation/NSNumber.swift @@ -0,0 +1,50 @@ +/// NSNumber adopts DatabaseValueConvertible +extension NSNumber: DatabaseValueConvertible { + + /// Returns a value that can be stored in the database. + public var databaseValue: DatabaseValue { + let objCType = String.fromCString(self.objCType)! + switch objCType { + case "c": + return Int64(charValue).databaseValue + case "C": + return Int64(unsignedCharValue).databaseValue + case "s": + return Int64(shortValue).databaseValue + case "S": + return Int64(unsignedShortValue).databaseValue + case "i": + return Int64(intValue).databaseValue + case "I": + return Int64(unsignedIntValue).databaseValue + case "l": + return Int64(longValue).databaseValue + case "L": + return Int64(unsignedLongValue).databaseValue + case "q": + return Int64(longLongValue).databaseValue + case "Q": + return Int64(unsignedLongLongValue).databaseValue + case "f": + return Double(floatValue).databaseValue + case "d": + return doubleValue.databaseValue + case "B": + return boolValue.databaseValue + default: + fatalError("DatabaseValueConvertible: Unsupported NSNumber type: \(objCType)") + } + } + + /// Create an instance initialized to `databaseValue`. + public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { + switch databaseValue { + case .Integer(let int64): + return self.init(longLong: int64) + case .Real(let double): + return self.init(double: double) + default: + return nil + } + } +} diff --git a/GRDB/Foundation/NSString.swift b/GRDB/Foundation/NSString.swift new file mode 100644 index 000000000..c042fec86 --- /dev/null +++ b/GRDB/Foundation/NSString.swift @@ -0,0 +1,17 @@ +/// NSString adopts DatabaseValueConvertible +extension NSString: DatabaseValueConvertible { + + /// Returns a value that can be stored in the database. + public var databaseValue: DatabaseValue { + return .Text(self as String) + } + + /// Create an instance initialized to `databaseValue`. + public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { + if let string = String.fromDatabaseValue(databaseValue) { + return self.init(string: string) + } else { + return nil + } + } +} diff --git a/GRDB/Foundation/StatementArgumentsFoundationInitializers.swift b/GRDB/Foundation/StatementArgumentsFoundationInitializers.swift index d3a96052a..776060536 100644 --- a/GRDB/Foundation/StatementArgumentsFoundationInitializers.swift +++ b/GRDB/Foundation/StatementArgumentsFoundationInitializers.swift @@ -26,8 +26,13 @@ extension StatementArguments { // try statement.execute(StatementArguments(person)) // Avoid an error here // } var values = [DatabaseValueConvertible?]() - for item in array { - values.append(StatementArguments.valueFromAnyObject(item)) + for object in array { + switch object { + case let value as DatabaseValueConvertible: + values.append(value) + default: + fatalError("Not DatabaseValueConvertible: \(object)") + } } self.init(values) } @@ -62,69 +67,18 @@ extension StatementArguments { // try statement.execute(StatementArguments(person)) // Avoid an error here // } var values = [String: DatabaseValueConvertible?]() - for (key, item) in dictionary { + for (key, object) in dictionary { if let key = key as? String { - values[key] = StatementArguments.valueFromAnyObject(item) + switch object { + case let value as DatabaseValueConvertible: + values[key] = value + default: + fatalError("Not DatabaseValueConvertible: \(object)") + } } else { fatalError("Not a String key: \(key)") } } self.init(values) } - - // IMPLEMENTATION NOTE: - // - // NSNumber, NSString, NSNull can't adopt DatabaseValueConvertible because - // DatabaseValueConvertible has a Self reference which prevents non-final - // classes to adopt it. - // - // This is why this method exists. As a convenience for init(NSArray) - // and init(NSDictionary), themselves conveniences for the library user. - private static func valueFromAnyObject(object: AnyObject) -> DatabaseValueConvertible? { - - switch object { - case let value as DatabaseValueConvertible: - return value - case _ as NSNull: - return nil - case let data as NSData: - return data - case let string as NSString: - return string as String - case let number as NSNumber: - let objCType = String.fromCString(number.objCType)! - switch objCType { - case "c": - return Int64(number.charValue) - case "C": - return Int64(number.unsignedCharValue) - case "s": - return Int64(number.shortValue) - case "S": - return Int64(number.unsignedShortValue) - case "i": - return Int64(number.intValue) - case "I": - return Int64(number.unsignedIntValue) - case "l": - return Int64(number.longValue) - case "L": - return Int64(number.unsignedLongValue) - case "q": - return Int64(number.longLongValue) - case "Q": - return Int64(number.unsignedLongLongValue) - case "f": - return Double(number.floatValue) - case "d": - return number.doubleValue - case "B": - return number.boolValue - default: - fatalError("Not a DatabaseValueConvertible: \(object)") - } - default: - fatalError("Not a DatabaseValueConvertible: \(object)") - } - } } \ No newline at end of file