DatabaseValue adopts StatementColumnConvertible. Stronger tests for DatabaseValue adoption of StatementColumnConvertible and DatabaseValueConvertible.

This commit is contained in:
Gwendal Roué committed 2016-02-07 12:44:09 +01:00
1 parent 0ae37db4ec
commit 94e4fa42bf
5 files changed
+86 -50

No files matched your search

+4
View File
@@ -3,6 +3,10 @@ import Foundation
/// A raw SQLite connection, suitable for the SQLite C API.
public typealias SQLiteConnection = COpaquePointer
/// A raw SQLite function argument.
typealias SQLiteValue = COpaquePointer
/// A Database connection.
///
/// You don't create a database directly. Instead, you use a DatabaseQueue:
+56 -44
View File
@@ -1,13 +1,11 @@
import Foundation
typealias SQLiteValue = COpaquePointer
// MARK: - DatabaseValue
/// DatabaseValue is the intermediate type between SQLite and your values.
///
/// See https://www.sqlite.org/datatype3.html
public struct DatabaseValue : Hashable {
public struct DatabaseValue {
/// An SQLite storage (NULL, INTEGER, REAL, TEXT, BLOB).
public enum Storage {
@@ -30,23 +28,6 @@ public struct DatabaseValue : Hashable {
/// The SQLite storage
public let storage: Storage
/// The hash value
public var hashValue: Int {
switch storage {
case .Null:
return 0
case .Int64(let int64):
// 1 == 1.0, hence 1 and 1.0 must have the same hash:
return Double(int64).hashValue
case .Double(let double):
return double.hashValue
case .String(let string):
return string.hashValue
case .Blob(let data):
return data.hashValue
}
}
/// The NULL DatabaseValue.
public static let Null = DatabaseValue(storage: .Null)
@@ -130,27 +111,6 @@ public struct DatabaseValue : Hashable {
self.storage = storage
}
// SQLite column value
init(sqliteStatement: SQLiteStatement, index: Int) {
switch sqlite3_column_type(sqliteStatement, Int32(index)) {
case SQLITE_NULL:
storage = .Null
case SQLITE_INTEGER:
storage = .Int64(sqlite3_column_int64(sqliteStatement, Int32(index)))
case SQLITE_FLOAT:
storage = .Double(sqlite3_column_double(sqliteStatement, Int32(index)))
case SQLITE_TEXT:
let cString = UnsafePointer<Int8>(sqlite3_column_text(sqliteStatement, Int32(index)))
storage = .String(String.fromCString(cString)!)
case SQLITE_BLOB:
let bytes = sqlite3_column_blob(sqliteStatement, Int32(index))
let length = sqlite3_column_bytes(sqliteStatement, Int32(index))
storage = .Blob(NSData(bytes: bytes, length: Int(length))) // copy bytes
case let type:
fatalError("Unexpected SQLite column type: \(type)")
}
}
// SQLite function argument
init(sqliteValue: SQLiteValue) {
switch sqlite3_value_type(sqliteValue) {
@@ -174,7 +134,28 @@ public struct DatabaseValue : Hashable {
}
// MARK: - Equatable
// MARK: - Hashable & Equatable
/// DatabaseValue adopts Hashable.
extension DatabaseValue : Hashable {
/// The hash value
public var hashValue: Int {
switch storage {
case .Null:
return 0
case .Int64(let int64):
// 1 == 1.0, hence 1 and 1.0 must have the same hash:
return Double(int64).hashValue
case .Double(let double):
return double.hashValue
case .String(let string):
return string.hashValue
case .Blob(let data):
return data.hashValue
}
}
}
/// DatabaseValue adopts Equatable.
public func ==(lhs: DatabaseValue, rhs: DatabaseValue) -> Bool {
@@ -218,9 +199,40 @@ extension DatabaseValue : DatabaseValueConvertible {
return self
}
/// Returns *databaseValue*.
/// Returns *databaseValue*, or nil for NULL input.
public static func fromDatabaseValue(databaseValue: DatabaseValue) -> DatabaseValue? {
return databaseValue
// Follow protocol semantics: DatabaseValueConvertible types turn NULL
// into nil:
//
// DatabaseValue.fetchOne(db, "SELECT NULL") // nil
return databaseValue.isNull ? nil : databaseValue
}
}
// MARK: - StatementColumnConvertible
extension DatabaseValue : StatementColumnConvertible {
/// Returns a DatabaseValue initialized from a raw SQLite statement pointer.
public init(sqliteStatement: SQLiteStatement, index: Int32) {
switch sqlite3_column_type(sqliteStatement, Int32(index)) {
case SQLITE_NULL:
storage = .Null
case SQLITE_INTEGER:
storage = .Int64(sqlite3_column_int64(sqliteStatement, Int32(index)))
case SQLITE_FLOAT:
storage = .Double(sqlite3_column_double(sqliteStatement, Int32(index)))
case SQLITE_TEXT:
let cString = UnsafePointer<Int8>(sqlite3_column_text(sqliteStatement, Int32(index)))
storage = .String(String.fromCString(cString)!)
case SQLITE_BLOB:
let bytes = sqlite3_column_blob(sqliteStatement, Int32(index))
let length = sqlite3_column_bytes(sqliteStatement, Int32(index))
storage = .Blob(NSData(bytes: bytes, length: Int(length))) // copy bytes
case let type:
fatalError("Unexpected SQLite column type: \(type)")
}
}
}
+2 -2
View File
@@ -664,7 +664,7 @@ private struct StatementCopyRowImpl : RowImpl {
init(statement: SelectStatement) {
let sqliteStatement = statement.sqliteStatement
self.databaseValues = (0..<statement.columnCount).map { DatabaseValue(sqliteStatement: sqliteStatement, index: $0) }
self.databaseValues = (0..<Int32(statement.columnCount)).map { DatabaseValue(sqliteStatement: sqliteStatement, index: $0) }
self.columnNames = statement.columnNames
}
@@ -723,7 +723,7 @@ private struct StatementRowImpl : RowImpl {
}
func databaseValue(atIndex index: Int) -> DatabaseValue {
return DatabaseValue(sqliteStatement: sqliteStatement, index: index)
return DatabaseValue(sqliteStatement: sqliteStatement, index: Int32(index))
}
func columnName(atIndex index: Int) -> String {
@@ -3,13 +3,33 @@ import GRDB
class DatabaseValueTests: GRDBTestCase {
func testDatabaseValueAdoptsDatabaseValueConvertible() {
func testDatabaseValueAsDatabaseValueConvertible() {
dbQueue.inDatabase { db in
XCTAssertTrue((Row.fetchOne(db, "SELECT 1")!.value(atIndex: 0) as DatabaseValue).value() is Int64)
XCTAssertTrue((Row.fetchOne(db, "SELECT 1.0")!.value(atIndex: 0) as DatabaseValue).value() is Double)
XCTAssertTrue((Row.fetchOne(db, "SELECT 'foo'")!.value(atIndex: 0) as DatabaseValue).value() is String)
XCTAssertTrue((Row.fetchOne(db, "SELECT x'53514C697465'")!.value(atIndex: 0) as DatabaseValue).value() is NSData)
XCTAssertTrue((Row.fetchOne(db, "SELECT NULL")!.value(atIndex: 0) as DatabaseValue?) == nil)
}
}
func testDatabaseValueAsStatementColumnConvertible() {
dbQueue.inDatabase { db in
XCTAssertTrue(DatabaseValue.fetchOne(db, "SELECT 1")!.value() is Int64)
XCTAssertTrue(DatabaseValue.fetchOne(db, "SELECT 1.0")!.value() is Double)
XCTAssertTrue(DatabaseValue.fetchOne(db, "SELECT 'foo'")!.value() is String)
XCTAssertTrue(DatabaseValue.fetchOne(db, "SELECT x'53514C697465'")!.value() is NSData)
XCTAssertTrue(DatabaseValue.fetchOne(db, "SELECT NULL") == nil)
}
}
func testDatabaseValueCanBeUsedAsStatementArgument() {
assertNoError {
try dbQueue.inDatabase { db in
try db.execute("CREATE TABLE integers (integer INTEGER)")
try db.execute("INSERT INTO integers (integer) VALUES (1)")
let databaseValue: DatabaseValue = Row.fetchOne(db, "SELECT * FROM integers")!.value(named: "integer")! // Triggers DatabaseValue.init?(databaseValue: DatabaseValue)
let count = Int.fetchOne(db, "SELECT COUNT(*) FROM integers WHERE integer = ?", arguments: [databaseValue])! // Triggers DatabaseValue.databaseValue
let databaseValue: DatabaseValue = 1.databaseValue
let count = Int.fetchOne(db, "SELECT COUNT(*) FROM integers WHERE integer = ?", arguments: [databaseValue])!
XCTAssertEqual(count, 1)
}
}
@@ -24,7 +24,7 @@ class FunctionTests: GRDBTestCase {
return nil
}
db.addFunction(fn)
XCTAssertTrue(DatabaseValue.fetchOne(db, "SELECT f()")!.isNull)
XCTAssertTrue(DatabaseValue.fetchOne(db, "SELECT f()") == nil)
}
}
}