mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Remove useless support for enums
This commit is contained in:
1 parent
2ee3320889
commit
e9bf3c2eac
3 files changed
-89
No files matched your search
@@ -1,61 +0,0 @@
|
||||
//
|
||||
// DatabaseEnumTests.swift
|
||||
// GRDB
|
||||
//
|
||||
// Created by Gwendal Roué on 03/07/2015.
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
enum Color: Int { // A raw underlying type is required
|
||||
case Red
|
||||
case White
|
||||
case Rose
|
||||
}
|
||||
|
||||
class DatabaseEnumTests: GRDBTests {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createWines") { db in
|
||||
try db.execute(
|
||||
"CREATE TABLE wines (color INTEGER)")
|
||||
}
|
||||
assertNoError {
|
||||
try migrator.migrate(dbQueue)
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseEnum() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
|
||||
try db.execute("INSERT INTO wines (color) VALUES (?)", bindings: [DatabaseEnum(Color.Red)])
|
||||
try db.execute("INSERT INTO wines (color) VALUES (?)", bindings: [DatabaseEnum(Color.White)])
|
||||
try db.execute("INSERT INTO wines (color) VALUES (?)", bindings: [DatabaseEnum(Color.Rose)])
|
||||
try db.execute("INSERT INTO wines (color) VALUES (?)", bindings: [3])
|
||||
|
||||
return .Commit
|
||||
}
|
||||
|
||||
dbQueue.inDatabase { db in
|
||||
let row = db.fetchOneRow("SELECT color FROM wines ORDER BY color LIMIT 1")!
|
||||
let dbColor: DatabaseEnum<Color> = row.value(named: "color")!
|
||||
let color = dbColor.value
|
||||
XCTAssertEqual(color, Color.Red)
|
||||
|
||||
let colors = db.fetchAll(DatabaseEnum<Color>.self, "SELECT color FROM wines ORDER BY color").map { $0?.value }
|
||||
XCTAssertEqual(colors.count, 4)
|
||||
XCTAssertEqual(colors[0]!, Color.Red)
|
||||
XCTAssertEqual(colors[1]!, Color.White)
|
||||
XCTAssertEqual(colors[2]!, Color.Rose)
|
||||
XCTAssertTrue(colors[3] == nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
56CC3C951B43C0E600A9A534 /* DatabaseMigrator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C941B43C0E600A9A534 /* DatabaseMigrator.swift */; };
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C981B445DDB00A9A534 /* GRDBTests.swift */; };
|
||||
56CC3C9B1B4464AE00A9A534 /* RowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C9A1B4464AE00A9A534 /* RowModel.swift */; };
|
||||
56CC3CA11B47106900A9A534 /* DatabaseEnumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3CA01B47106900A9A534 /* DatabaseEnumTests.swift */; };
|
||||
56E4BB7F1B452A6200FD324B /* SQLite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4BB7E1B452A6200FD324B /* SQLite.swift */; };
|
||||
56E4BB811B45373600FD324B /* Bindings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4BB801B45373600FD324B /* Bindings.swift */; };
|
||||
DC2393C81ABE35F8003FF113 /* GRDB-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
@@ -72,7 +71,6 @@
|
||||
56CC3C941B43C0E600A9A534 /* DatabaseMigrator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseMigrator.swift; sourceTree = "<group>"; };
|
||||
56CC3C981B445DDB00A9A534 /* GRDBTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTests.swift; sourceTree = "<group>"; };
|
||||
56CC3C9A1B4464AE00A9A534 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = "<group>"; };
|
||||
56CC3CA01B47106900A9A534 /* DatabaseEnumTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseEnumTests.swift; sourceTree = "<group>"; };
|
||||
56E1524B1B43FD5B00EE27AA /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = "<group>"; };
|
||||
56E4BB7E1B452A6200FD324B /* SQLite.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLite.swift; sourceTree = "<group>"; };
|
||||
56E4BB801B45373600FD324B /* Bindings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bindings.swift; sourceTree = "<group>"; };
|
||||
@@ -129,7 +127,6 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56C9E6511B468EB200DDC541 /* CustomDatabaseValueTypeTests.swift */,
|
||||
56CC3CA01B47106900A9A534 /* DatabaseEnumTests.swift */,
|
||||
);
|
||||
path = DatabaseValue;
|
||||
sourceTree = "<group>";
|
||||
@@ -374,7 +371,6 @@
|
||||
56C9E6621B468EB200DDC541 /* RowModelSubClassTests.swift in Sources */,
|
||||
56C9E6611B468EB200DDC541 /* PrimaryKeySQLiteRowIDTests.swift in Sources */,
|
||||
56C9E65F1B468EB200DDC541 /* PrimaryKeyNoneTests.swift in Sources */,
|
||||
56CC3CA11B47106900A9A534 /* DatabaseEnumTests.swift in Sources */,
|
||||
56C9E6681B4696E100DDC541 /* SelectStatementTests.swift in Sources */,
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTests.swift in Sources */,
|
||||
56C9E6601B468EB200DDC541 /* PrimaryKeySingleTests.swift in Sources */,
|
||||
|
||||
@@ -13,30 +13,6 @@ public protocol DatabaseValueType {
|
||||
static func fromSQLiteValue(value: SQLiteValue) -> Self?
|
||||
}
|
||||
|
||||
public struct DatabaseEnum<T: RawRepresentable where T.RawValue: DatabaseValueType> : DatabaseValueType {
|
||||
public let value: T
|
||||
|
||||
public init?(_ value: T?) {
|
||||
if let value = value {
|
||||
self.value = value
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
public func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
return value.rawValue.bindInSQLiteStatement(statement, atIndex: index)
|
||||
}
|
||||
|
||||
public static func fromSQLiteValue(value: SQLiteValue) -> DatabaseEnum<T>? {
|
||||
if let rawValue = T.RawValue.fromSQLiteValue(value), value = T.init(rawValue: rawValue) {
|
||||
return self.init(value)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Bool: DatabaseValueType {
|
||||
public func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
return sqlite3_bind_int(statement, Int32(index), Int32(self ? 1 : 0))
|
||||
|
||||
Reference in new issue
Block a user