File tree 3 files changed +13
-3
lines changed 3 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import SQLite3
12
12
public protocol SQLDataType {
13
13
func sqlBind( statement: OpaquePointer , index: Int32 )
14
14
static func sqlColumn( statement: OpaquePointer , index: Int32 ) -> Self
15
+ static func convert( from value: Any ) -> Self ?
15
16
}
16
17
17
18
public extension SQLDataType {
@@ -26,6 +27,11 @@ extension Int: SQLDataType {
26
27
public static func sqlColumn( statement: OpaquePointer , index: Int32 ) -> Int {
27
28
Int ( sqlite3_column_int64 ( statement, index) )
28
29
}
30
+
31
+ public static func convert( from value: Any ) -> Int ? {
32
+ guard let int64 = value as? Int64 else { return nil }
33
+ return Int ( int64)
34
+ }
29
35
}
30
36
31
37
extension Int32 : SQLDataType {
Original file line number Diff line number Diff line change @@ -71,7 +71,11 @@ public struct SQLRow {
71
71
///
72
72
/// - parameter index: The leftmost column of the result set has the index 0.
73
73
public subscript< T: SQLDataType > ( index: Int ) -> T {
74
- T . convert ( from: values [ index] !) !
74
+ let value = values [ index] !
75
+ guard let convertedValue = T . convert ( from: value) else {
76
+ fatalError ( " Could not convert \( type ( of: value) ) . Make sure target type ( \( T . self) ) correctly implements convert(from:). " )
77
+ }
78
+ return convertedValue
75
79
}
76
80
77
81
/// Returns a single column of the current result row of a query. If the
Original file line number Diff line number Diff line change @@ -170,9 +170,9 @@ private extension SQLConnection {
170
170
171
171
private struct User : Hashable , SQLRowDecodable {
172
172
let name : String
173
- let level : Int64
173
+ let level : Int
174
174
175
- init ( name: String , level: Int64 ) {
175
+ init ( name: String , level: Int ) {
176
176
self . name = name
177
177
self . level = level
178
178
}
You can’t perform that action at this time.
0 commit comments