Skip to content

Commit 78d3f17

Browse files
authored
Merge pull request #7 from ahmedk92/int_conversion
Int type tolerance, and better SQLDataType conversion requirements and conveniences
2 parents 81b97d9 + c431a4c commit 78d3f17

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

Sources/SwiftSQL/SQLDataType.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import SQLite3
1212
public protocol SQLDataType {
1313
func sqlBind(statement: OpaquePointer, index: Int32)
1414
static func sqlColumn(statement: OpaquePointer, index: Int32) -> Self
15-
}
16-
17-
public extension SQLDataType {
18-
static func convert(from value: Any) -> Self? { value as? Self }
15+
static func convert(from value: Any) -> Self?
1916
}
2017

2118
extension Int: SQLDataType {
@@ -26,6 +23,11 @@ extension Int: SQLDataType {
2623
public static func sqlColumn(statement: OpaquePointer, index: Int32) -> Int {
2724
Int(sqlite3_column_int64(statement, index))
2825
}
26+
27+
public static func convert(from value: Any) -> Int? {
28+
guard let int64 = value as? Int64 else { return nil }
29+
return Int(int64)
30+
}
2931
}
3032

3133
extension Int32: SQLDataType {
@@ -36,6 +38,11 @@ extension Int32: SQLDataType {
3638
public static func sqlColumn(statement: OpaquePointer, index: Int32) -> Int32 {
3739
sqlite3_column_int(statement, index)
3840
}
41+
42+
public static func convert(from value: Any) -> Self? {
43+
guard let int64 = value as? Int64 else { return nil }
44+
return Int32(int64)
45+
}
3946
}
4047

4148
extension Int64: SQLDataType {
@@ -46,6 +53,8 @@ extension Int64: SQLDataType {
4653
public static func sqlColumn(statement: OpaquePointer, index: Int32) -> Int64 {
4754
sqlite3_column_int64(statement, index)
4855
}
56+
57+
public static func convert(from value: Any) -> Self? { value as? Self }
4958
}
5059

5160
extension Double: SQLDataType {
@@ -56,6 +65,8 @@ extension Double: SQLDataType {
5665
public static func sqlColumn(statement: OpaquePointer, index: Int32) -> Double {
5766
sqlite3_column_double(statement, index)
5867
}
68+
69+
public static func convert(from value: Any) -> Self? { value as? Self }
5970
}
6071

6172
extension String: SQLDataType {
@@ -67,6 +78,8 @@ extension String: SQLDataType {
6778
guard let pointer = sqlite3_column_text(statement, index) else { return "" }
6879
return String(cString: pointer)
6980
}
81+
82+
public static func convert(from value: Any) -> Self? { value as? Self }
7083
}
7184

7285
extension Data: SQLDataType {
@@ -81,6 +94,8 @@ extension Data: SQLDataType {
8194
let count = Int(sqlite3_column_bytes(statement, Int32(index)))
8295
return Data(bytes: pointer, count: count)
8396
}
97+
98+
public static func convert(from value: Any) -> Self? { value as? Self }
8499
}
85100

86101
private let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)

Sources/SwiftSQLExt/SwiftSQLExt.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ public struct SQLRow {
7171
///
7272
/// - parameter index: The leftmost column of the result set has the index 0.
7373
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
7579
}
7680

7781
/// Returns a single column of the current result row of a query. If the

Tests/SwiftSQLExtTests/SwiftSQLExtTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ private extension SQLConnection {
141141
""")
142142

143143
try insertUsersStatement
144-
.bind("Alice", Int64(80))
144+
.bind("Alice", 80)
145145
.execute()
146146

147147
try insertUsersStatement.reset()
148148

149149
try insertUsersStatement
150-
.bind("Bob", Int64(90))
150+
.bind("Bob", 90)
151151
.execute()
152152

153153
let insertPersonsStatement = try self.prepare("""
@@ -170,9 +170,9 @@ private extension SQLConnection {
170170

171171
private struct User: Hashable, SQLRowDecodable {
172172
let name: String
173-
let level: Int64
173+
let level: Int
174174

175-
init(name: String, level: Int64) {
175+
init(name: String, level: Int) {
176176
self.name = name
177177
self.level = level
178178
}

0 commit comments

Comments
 (0)