diff --git a/CHANGELOG.md b/CHANGELOG.md index bd4c59381..ea3cf1e7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ Release Notes ============= +## Next Version + +**Breaking Changes** + +- `DatabaseIntRepresentable`, `DatabaseInt32Representable`, `DatabaseInt64Representable` and `DatabaseStringRepresentable` have been replaced by the single protocol `DatabaseRawRepresentable` ([documentation](https://github.com/groue/GRDB.swift#swift-enums)) + + ## 0.45.1 Released February 11, 2016 diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 07d4df835..7841df2cf 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -31,8 +31,8 @@ 5605F16E1C672E4000235C62 /* Row+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1541C672E4000235C62 /* Row+Foundation.swift */; }; 5605F16F1C672E4000235C62 /* StatementArguments+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1551C672E4000235C62 /* StatementArguments+Foundation.swift */; }; 5605F1701C672E4000235C62 /* StatementArguments+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1551C672E4000235C62 /* StatementArguments+Foundation.swift */; }; - 5605F1711C672E4000235C62 /* RawRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1571C672E4000235C62 /* RawRepresentable.swift */; }; - 5605F1721C672E4000235C62 /* RawRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1571C672E4000235C62 /* RawRepresentable.swift */; }; + 5605F1711C672E4000235C62 /* DatabaseRawRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1571C672E4000235C62 /* DatabaseRawRepresentable.swift */; }; + 5605F1721C672E4000235C62 /* DatabaseRawRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1571C672E4000235C62 /* DatabaseRawRepresentable.swift */; }; 5605F1731C672E4000235C62 /* StandardLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1581C672E4000235C62 /* StandardLibrary.swift */; }; 5605F1741C672E4000235C62 /* StandardLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1581C672E4000235C62 /* StandardLibrary.swift */; }; 5605F1871C69111300235C62 /* SelectStatementSourceTablesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1861C69111300235C62 /* SelectStatementSourceTablesTests.swift */; }; @@ -114,8 +114,8 @@ 56A2383F1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381A1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift */; }; 56A238401B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381A1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift */; }; 56A238421B9C74A90082EB20 /* DatabaseValueConversionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381B1B9C74A90082EB20 /* DatabaseValueConversionTests.swift */; }; - 56A238431B9C74A90082EB20 /* RawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381C1B9C74A90082EB20 /* RawRepresentableTests.swift */; }; - 56A238441B9C74A90082EB20 /* RawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381C1B9C74A90082EB20 /* RawRepresentableTests.swift */; }; + 56A238431B9C74A90082EB20 /* DatabaseRawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381C1B9C74A90082EB20 /* DatabaseRawRepresentableTests.swift */; }; + 56A238441B9C74A90082EB20 /* DatabaseRawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381C1B9C74A90082EB20 /* DatabaseRawRepresentableTests.swift */; }; 56A238451B9C74A90082EB20 /* DictionaryRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381E1B9C74A90082EB20 /* DictionaryRowTests.swift */; }; 56A238461B9C74A90082EB20 /* DictionaryRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381E1B9C74A90082EB20 /* DictionaryRowTests.swift */; }; 56A238471B9C74A90082EB20 /* DetachedRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381F1B9C74A90082EB20 /* DetachedRowTests.swift */; }; @@ -313,7 +313,7 @@ 5605F1531C672E4000235C62 /* NSURL.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSURL.swift; sourceTree = ""; }; 5605F1541C672E4000235C62 /* Row+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Row+Foundation.swift"; sourceTree = ""; }; 5605F1551C672E4000235C62 /* StatementArguments+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "StatementArguments+Foundation.swift"; sourceTree = ""; }; - 5605F1571C672E4000235C62 /* RawRepresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentable.swift; sourceTree = ""; }; + 5605F1571C672E4000235C62 /* DatabaseRawRepresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseRawRepresentable.swift; sourceTree = ""; }; 5605F1581C672E4000235C62 /* StandardLibrary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StandardLibrary.swift; sourceTree = ""; }; 5605F1861C69111300235C62 /* SelectStatementSourceTablesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectStatementSourceTablesTests.swift; sourceTree = ""; }; 5605F1891C6B1A8700235C62 /* SQLCollation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLCollation.swift; sourceTree = ""; }; @@ -350,7 +350,7 @@ 56A238181B9C74A90082EB20 /* DatabaseValueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueTests.swift; sourceTree = ""; }; 56A2381A1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConvertibleSubclassTests.swift; sourceTree = ""; }; 56A2381B1B9C74A90082EB20 /* DatabaseValueConversionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversionTests.swift; sourceTree = ""; }; - 56A2381C1B9C74A90082EB20 /* RawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentableTests.swift; sourceTree = ""; }; + 56A2381C1B9C74A90082EB20 /* DatabaseRawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseRawRepresentableTests.swift; sourceTree = ""; }; 56A2381E1B9C74A90082EB20 /* DictionaryRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DictionaryRowTests.swift; sourceTree = ""; }; 56A2381F1B9C74A90082EB20 /* DetachedRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DetachedRowTests.swift; sourceTree = ""; }; 56A238211B9C74A90082EB20 /* SelectStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectStatementTests.swift; sourceTree = ""; }; @@ -537,7 +537,7 @@ 5605F1561C672E4000235C62 /* StandardLibrary */ = { isa = PBXGroup; children = ( - 5605F1571C672E4000235C62 /* RawRepresentable.swift */, + 5605F1571C672E4000235C62 /* DatabaseRawRepresentable.swift */, 5605F1581C672E4000235C62 /* StandardLibrary.swift */, ); path = StandardLibrary; @@ -680,12 +680,12 @@ 56A238191B9C74A90082EB20 /* DatabaseValueConvertible */ = { isa = PBXGroup; children = ( + 56A2381C1B9C74A90082EB20 /* DatabaseRawRepresentableTests.swift */, 56A238B51B9CA2590082EB20 /* DatabaseTimestampTests.swift */, 56A2381B1B9C74A90082EB20 /* DatabaseValueConversionTests.swift */, 56E8CE0C1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift */, 56A2381A1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift */, 56CA21B91BB3FCF7009A04C5 /* NSDataTests.swift */, - 56A2381C1B9C74A90082EB20 /* RawRepresentableTests.swift */, ); path = DatabaseValueConvertible; sourceTree = ""; @@ -1282,7 +1282,7 @@ 5605F15A1C672E4000235C62 /* CGFloat.swift in Sources */, 5605F1641C672E4000235C62 /* NSDate.swift in Sources */, 5605F1901C6B1A8700235C62 /* SQLFunction.swift in Sources */, - 5605F1721C672E4000235C62 /* RawRepresentable.swift in Sources */, + 5605F1721C672E4000235C62 /* DatabaseRawRepresentable.swift in Sources */, 563445061C4A7FD7003D3DC6 /* Utils.swift in Sources */, 56A238941B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */, 5605F16A1C672E4000235C62 /* NSString.swift in Sources */, @@ -1346,7 +1346,7 @@ 56A238421B9C74A90082EB20 /* DatabaseValueConversionTests.swift in Sources */, 5658420F1C1A1C820099FE88 /* MutablePersistableTests.swift in Sources */, 56FC78671BBAEB8C00CA1285 /* MappingTests.swift in Sources */, - 56A238441B9C74A90082EB20 /* RawRepresentableTests.swift in Sources */, + 56A238441B9C74A90082EB20 /* DatabaseRawRepresentableTests.swift in Sources */, 5607EFD41BB827FD00605DE3 /* TransactionObserverTests.swift in Sources */, 565D5D721BBC694D00DC9BD4 /* Row+FoundationTests.swift in Sources */, 56A2386A1B9C74A90082EB20 /* RecordSubClassTests.swift in Sources */, @@ -1405,7 +1405,7 @@ 56A238671B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */, 56300B611C53C42C005A543B /* RowConvertible+FetchRequestTests.swift in Sources */, 56A2385D1B9C74A90082EB20 /* RecordCopyTests.swift in Sources */, - 56A238431B9C74A90082EB20 /* RawRepresentableTests.swift in Sources */, + 56A238431B9C74A90082EB20 /* DatabaseRawRepresentableTests.swift in Sources */, 56EB0AB21BCD787300A3DC55 /* NSDataMemoryTests.swift in Sources */, 56FC78681BBAEB8C00CA1285 /* ManagedDataControllerTests.swift in Sources */, 5658420E1C1A1C820099FE88 /* MutablePersistableTests.swift in Sources */, @@ -1452,7 +1452,7 @@ 5605F1591C672E4000235C62 /* CGFloat.swift in Sources */, 5605F1631C672E4000235C62 /* NSDate.swift in Sources */, 5605F18F1C6B1A8700235C62 /* SQLFunction.swift in Sources */, - 5605F1711C672E4000235C62 /* RawRepresentable.swift in Sources */, + 5605F1711C672E4000235C62 /* DatabaseRawRepresentable.swift in Sources */, 56A2387B1B9C75030082EB20 /* Configuration.swift in Sources */, 5605F1691C672E4000235C62 /* NSString.swift in Sources */, 5605F16D1C672E4000235C62 /* Row+Foundation.swift in Sources */, diff --git a/GRDB/Core/Support/StandardLibrary/DatabaseRawRepresentable.swift b/GRDB/Core/Support/StandardLibrary/DatabaseRawRepresentable.swift new file mode 100644 index 000000000..e170e7bc0 --- /dev/null +++ b/GRDB/Core/Support/StandardLibrary/DatabaseRawRepresentable.swift @@ -0,0 +1,32 @@ +/// Have your RawRepresentable type adopt DatabaseRawRepresentable and it +/// automatically gains DatabaseValueConvertible adoption. +/// +/// enum Color : Int { +/// case Red +/// case White +/// case Rose +/// } +/// +/// // Declare DatabaseRawRepresentable adoption: +/// extension Color : DatabaseRawRepresentable { } +/// +/// // Gain full GRDB.swift support: +/// db.execute("INSERT INTO colors (color) VALUES (?)", [Color.Red]) +/// let color: Color? = Color.fetchOne(db, "SELECT ...") +public protocol DatabaseRawRepresentable: RawRepresentable, DatabaseValueConvertible { } + +public extension DatabaseRawRepresentable where RawValue: DatabaseValueConvertible { + + /// Returns a value that can be stored in the database. + var databaseValue: DatabaseValue { + return rawValue.databaseValue + } + + /// Returns a value initialized from *databaseValue*, if possible. + static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { + guard let rawValue = RawValue.fromDatabaseValue(databaseValue) else { + return nil + } + return self.init(rawValue: rawValue) + } +} diff --git a/GRDB/Core/Support/StandardLibrary/RawRepresentable.swift b/GRDB/Core/Support/StandardLibrary/RawRepresentable.swift deleted file mode 100644 index 5c53c3e41..000000000 --- a/GRDB/Core/Support/StandardLibrary/RawRepresentable.swift +++ /dev/null @@ -1,158 +0,0 @@ -// MARK: - DatabaseIntRepresentable - -/// Have your Int enum adopt DatabaseIntRepresentable and it automatically gains -/// DatabaseValueConvertible adoption. -/// -/// // An Int enum: -/// enum Color : Int { -/// case Red -/// case White -/// case Rose -/// } -/// -/// // Declare DatabaseIntRepresentable adoption: -/// extension Color : DatabaseIntRepresentable { } -/// -/// // Gain full GRDB.swift support: -/// db.execute("INSERT INTO colors (color) VALUES (?)", [Color.Red]) -/// let color: Color? = Color.fetchOne(db, "SELECT ...") -public protocol DatabaseIntRepresentable : DatabaseValueConvertible { - var rawValue: Int { get } - init?(rawValue: Int) -} - -extension DatabaseIntRepresentable { - - /// Returns a value that can be stored in the database. - public var databaseValue: DatabaseValue { - return rawValue.databaseValue - } - - /// Returns a value initialized from *databaseValue*, if possible. - public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { - guard let int = Int.fromDatabaseValue(databaseValue) else { - return nil - } - return self.init(rawValue: int) - } -} - - -// MARK: - DatabaseInt32Representable - -/// Have your Int32 enum adopt DatabaseInt32Representable and it automatically -/// gains DatabaseValueConvertible adoption. -/// -/// // An Int enum: -/// enum Color : Int32 { -/// case Red -/// case White -/// case Rose -/// } -/// -/// // Declare DatabaseInt32Representable adoption: -/// extension Color : DatabaseInt32Representable { } -/// -/// // Gain full GRDB.swift support: -/// db.execute("INSERT INTO colors (color) VALUES (?)", [Color.Red]) -/// let color: Color? = Color.fetchOne(db, "SELECT ...") -public protocol DatabaseInt32Representable : DatabaseValueConvertible { - var rawValue: Int32 { get } - init?(rawValue: Int32) -} - -extension DatabaseInt32Representable { - - /// Returns a value that can be stored in the database. - public var databaseValue: DatabaseValue { - return rawValue.databaseValue - } - - /// Returns a value initialized from *databaseValue*, if possible. - public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { - guard let int = Int32.fromDatabaseValue(databaseValue) else { - return nil - } - return self.init(rawValue: int) - } -} - - -// MARK: - DatabaseInt64Representable - -/// Have your Int64 enum adopt DatabaseInt64Representable and it automatically -/// gains DatabaseValueConvertible adoption. -/// -/// // An Int enum: -/// enum Color : Int64 { -/// case Red -/// case White -/// case Rose -/// } -/// -/// // Declare DatabaseInt64Representable adoption: -/// extension Color : DatabaseInt64Representable { } -/// -/// // Gain full GRDB.swift support: -/// db.execute("INSERT INTO colors (color) VALUES (?)", [Color.Red]) -/// let color: Color? = Color.fetchOne(db, "SELECT ...") -public protocol DatabaseInt64Representable : DatabaseValueConvertible { - var rawValue: Int64 { get } - init?(rawValue: Int64) -} - -extension DatabaseInt64Representable { - - /// Returns a value that can be stored in the database. - public var databaseValue: DatabaseValue { - return rawValue.databaseValue - } - - /// Returns a value initialized from *databaseValue*, if possible. - public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { - guard let int = Int64.fromDatabaseValue(databaseValue) else { - return nil - } - return self.init(rawValue: int) - } -} - - -// MARK: - DatabaseStringRepresentable - -/// Have your String enum adopt DatabaseStringRepresentable and it automatically -/// gains DatabaseValueConvertible adoption. -/// -/// // A String enum: -/// enum Color : String { -/// case Red -/// case White -/// case Rose -/// } -/// -/// // Declare DatabaseIntRepresentable adoption: -/// extension Color : DatabaseStringRepresentable { } -/// -/// // Gain full GRDB.swift support: -/// db.execute("INSERT StringO colors (color) VALUES (?)", [Color.Red]) -/// let color: Color? = Color.fetchOne(db, "SELECT ...") -public protocol DatabaseStringRepresentable : DatabaseValueConvertible { - var rawValue: String { get } - init?(rawValue: String) -} - -extension DatabaseStringRepresentable { - - /// Returns a value that can be stored in the database. - public var databaseValue: DatabaseValue { - return rawValue.databaseValue - } - - /// Returns a value initialized from *databaseValue*, if possible. - public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { - guard let string = String.fromDatabaseValue(databaseValue) else { - return nil - } - return self.init(rawValue: string) - } -} diff --git a/README.md b/README.md index 4ba3ef873..af9d2cbea 100644 --- a/README.md +++ b/README.md @@ -826,11 +826,11 @@ enum Grape : String { } ``` -Add conformance to a protocol, and the enum type can be stored and fetched from the database just like other [value types](#values): +Add conformance to the `DatabaseRawRepresentable` protocol, and the enum type can be stored and fetched from the database just like other [value types](#values): ```swift -extension Color : DatabaseIntRepresentable { } // DatabaseInt32Representable for Int32, DatabaseInt64Representable for Int64 -extension Grape : DatabaseStringRepresentable { } +extension Color : DatabaseRawRepresentable { } +extension Grape : DatabaseRawRepresentable { } // Store try db.execute("INSERT INTO wines (grape, color) VALUES (?, ?)", diff --git a/Tests/Public/Core/DatabaseValueConvertible/RawRepresentableTests.swift b/Tests/Public/Core/DatabaseValueConvertible/DatabaseRawRepresentableTests.swift similarity index 97% rename from Tests/Public/Core/DatabaseValueConvertible/RawRepresentableTests.swift rename to Tests/Public/Core/DatabaseValueConvertible/DatabaseRawRepresentableTests.swift index b2519a9d9..ca18bb7b2 100644 --- a/Tests/Public/Core/DatabaseValueConvertible/RawRepresentableTests.swift +++ b/Tests/Public/Core/DatabaseValueConvertible/DatabaseRawRepresentableTests.swift @@ -25,10 +25,10 @@ enum Grape : String { case Riesling } -extension Color32 : DatabaseInt32Representable { } -extension Color64 : DatabaseInt64Representable { } -extension Color : DatabaseIntRepresentable { } -extension Grape : DatabaseStringRepresentable { } +extension Color32 : DatabaseRawRepresentable { } +extension Color64 : DatabaseRawRepresentable { } +extension Color : DatabaseRawRepresentable { } +extension Grape : DatabaseRawRepresentable { } class RawRepresentableTests: GRDBTestCase {