diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index df9bc4e4e..df2aeb5cc 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -146,8 +146,8 @@ isa = PBXGroup; children = ( 56C9E6511B468EB200DDC541 /* CustomSQLiteValueConvertibleTests.swift */, - 56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */, 5623E0891B494A8A00B20B7F /* RawRepresentableTests.swift */, + 56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */, ); path = SQLiteValueConvertible; sourceTree = ""; diff --git a/GRDB/SQLiteValueConvertible.swift b/GRDB/SQLiteValueConvertible.swift index cff354d82..c8b6f0ea8 100644 --- a/GRDB/SQLiteValueConvertible.swift +++ b/GRDB/SQLiteValueConvertible.swift @@ -33,6 +33,95 @@ public protocol SQLiteValueConvertible { init?(sqliteValue: SQLiteValue) } + +// MARK: - Int Enum Support + +/** +Have your Int enum adopt SQLiteIntRepresentable and it automatically gains +SQLiteValueConvertible adoption. + + // An Int enum: + enum Color : Int { + case Red + case White + case Rose + } + + // Declare SQLiteIntRepresentable adoption: + extension Color : SQLiteIntRepresentable { } + + // Gain full GRDB.swift support: + db.execute("INSERT INTO colors (color) VALUES (?)", [Color.Red]) + let color: Color? = db.fetchOne(Color.self, "SELECT ...") +*/ +public protocol SQLiteIntRepresentable : SQLiteValueConvertible { + var rawValue: Int { get } + init?(rawValue: Int) +} + +extension SQLiteIntRepresentable { + + /// Returns a SQLite value that can be stored in the database. + public var sqliteValue: SQLiteValue { + return .Integer(Int64(rawValue)) + } + + /// Create an instance initialized to `sqliteValue`. + public init?(sqliteValue: SQLiteValue) { + if let int = Int(sqliteValue: sqliteValue) { + self.init(rawValue: int) + } else { + return nil + } + } +} + + +// MARK: - String Enum Support + +/** +Have your String enum adopt SQLiteStringRepresentable and it automatically gains +SQLiteValueConvertible adoption. + + // A String enum: + enum Color : String { + case Red = "red" + case White = "white" + case Rose = "rose" + } + + // Declare SQLiteStringRepresentable adoption: + extension Color : SQLiteStringRepresentable { } + + // Gain full GRDB.swift support: + db.execute("INSERT StringO colors (color) VALUES (?)", [Color.Red]) + let color: Color? = db.fetchOne(Color.self, "SELECT ...") +*/ +public protocol SQLiteStringRepresentable : SQLiteValueConvertible { + var rawValue: String { get } + init?(rawValue: String) +} + +extension SQLiteStringRepresentable { + + /// Returns a SQLite value that can be stored in the database. + public var sqliteValue: SQLiteValue { + return .Text(rawValue) + } + + /// Create an instance initialized to `sqliteValue`. + public init?(sqliteValue: SQLiteValue) { + if let string = String(sqliteValue: sqliteValue) { + self.init(rawValue: string) + } else { + return nil + } + } +} + + +// MARK: - Built-in Types + /// Bool is convertible to and from SQLiteValue. extension Bool: SQLiteValueConvertible { diff --git a/RawRepresentableTests.swift b/RawRepresentableTests.swift index 1399dac73..e29641461 100644 --- a/RawRepresentableTests.swift +++ b/RawRepresentableTests.swift @@ -9,32 +9,20 @@ import XCTest import GRDB -protocol IntRawRepresentableSQLiteValueConvertible : SQLiteValueConvertible { - typealias RawValue = Int - var rawValue: Int { get } - init?(rawValue: Int) -} - -extension IntRawRepresentableSQLiteValueConvertible { - var sqliteValue: SQLiteValue { - return .Integer(Int64(rawValue)) - } - init?(sqliteValue: SQLiteValue) { - if let int = Int(sqliteValue: sqliteValue) { - self.init(rawValue: int) - } else { - return nil - } - } -} - enum Color : Int { case Red case White case Rose } -extension Color : IntRawRepresentableSQLiteValueConvertible { } +enum Grape : String { + case Chardonnay = "Chardonnay" + case Merlot = "Merlot" + case Riesling = "Riesling" +} + +extension Color : SQLiteIntRepresentable { } +extension Grape : SQLiteStringRepresentable { } class RawRepresentableTests: GRDBTestCase { @@ -43,7 +31,7 @@ class RawRepresentableTests: GRDBTestCase { var migrator = DatabaseMigrator() migrator.registerMigration("createPersons") { db in - try db.execute("CREATE TABLE wines (color INTEGER)") + try db.execute("CREATE TABLE wines (grape TEXT, color INTEGER)") } assertNoError { try migrator.migrate(dbQueue) @@ -82,4 +70,37 @@ class RawRepresentableTests: GRDBTestCase { } } } + + func testGrape() { + assertNoError { + try dbQueue.inTransaction { db in + + do { + for grape in [Grape.Chardonnay, Grape.Merlot, Grape.Riesling] { + try db.execute("INSERT INTO wines (grape) VALUES (?)", bindings: [grape]) + } + try db.execute("INSERT INTO wines (grape) VALUES (?)", bindings: ["Syrah"]) + } + + do { + let rows = db.fetchAllRows("SELECT grape FROM wines ORDER BY grape") + let grapes = rows.map { $0.value(atIndex: 0) as Grape? } + XCTAssertEqual(grapes[0]!, Grape.Chardonnay) + XCTAssertEqual(grapes[1]!, Grape.Merlot) + XCTAssertEqual(grapes[2]!, Grape.Riesling) + XCTAssertTrue(grapes[3] == nil) + } + + do { + let grapes = db.fetchAll(Grape.self, "SELECT grape FROM wines ORDER BY grape") + XCTAssertEqual(grapes[0]!, Grape.Chardonnay) + XCTAssertEqual(grapes[1]!, Grape.Merlot) + XCTAssertEqual(grapes[2]!, Grape.Riesling) + XCTAssertTrue(grapes[3] == nil) + } + + return .Rollback + } + } + } }