Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Row.get throw instead of crash. #649

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/SQLite/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ extension QueryType {
extension Row {

public subscript(column: Expression<Data>) -> Data {
return get(column)
return try! get(column)
}
public subscript(column: Expression<Data?>) -> Data? {
return get(column)
return try! get(column)
}

public subscript(column: Expression<Date>) -> Date {
return get(column)
return try! get(column)
}
public subscript(column: Expression<Date?>) -> Date? {
return get(column)
return try! get(column)
}

}
52 changes: 34 additions & 18 deletions Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,17 @@ extension Connection {

}

public enum SQLiteErrorType {
case unexpectedNullValue
case noSuchColumn
case ambiguousColumn
}

public struct SQLiteError: Error {
let type: SQLiteErrorType
let message: String
}

public struct Row {

fileprivate let columnNames: [String: Int]
Expand All @@ -1037,25 +1048,30 @@ public struct Row {
/// - Parameter column: An expression representing a column selected in a Query.
///
/// - Returns: The value for the given column.
public func get<V: Value>(_ column: Expression<V>) -> V {
return get(Expression<V?>(column))!
public func get<V: Value>(_ column: Expression<V>) throws -> V {
if let value = try get(Expression<V?>(column)) {
return value
} else {
throw SQLiteError(type: .unexpectedNullValue, message: "Unexpected NULL value in column \(column.template)")
}
}
public func get<V: Value>(_ column: Expression<V?>) -> V? {

public func get<V: Value>(_ column: Expression<V?>) throws -> V? {
func valueAtIndex(_ idx: Int) -> V? {
guard let value = values[idx] as? V.Datatype else { return nil }
return (V.fromDatatypeValue(value) as? V)!
return V.fromDatatypeValue(value) as? V
}

guard let idx = columnNames[column.template] else {
let similar = Array(columnNames.keys).filter { $0.hasSuffix(".\(column.template)") }

switch similar.count {
case 0:
fatalError("no such column '\(column.template)' in columns: \(columnNames.keys.sorted())")
throw SQLiteError(type: .noSuchColumn, message: "No such column '\(column.template)' in columns: \(columnNames.keys.sorted())")
case 1:
return valueAtIndex(columnNames[similar[0]]!)
default:
fatalError("ambiguous column '\(column.template)' (please disambiguate: \(similar))")
throw SQLiteError(type: .ambiguousColumn, message: "Ambiguous column '\(column.template)' (please disambiguate: \(similar))")
}
}

Expand All @@ -1065,45 +1081,45 @@ public struct Row {
// FIXME: rdar://problem/18673897 // subscript<T>…

public subscript(column: Expression<Blob>) -> Blob {
return get(column)
return try! get(column)
}
public subscript(column: Expression<Blob?>) -> Blob? {
return get(column)
return try! get(column)
}

public subscript(column: Expression<Bool>) -> Bool {
return get(column)
return try! get(column)
}
public subscript(column: Expression<Bool?>) -> Bool? {
return get(column)
return try! get(column)
}

public subscript(column: Expression<Double>) -> Double {
return get(column)
return try! get(column)
}
public subscript(column: Expression<Double?>) -> Double? {
return get(column)
return try! get(column)
}

public subscript(column: Expression<Int>) -> Int {
return get(column)
return try! get(column)
}
public subscript(column: Expression<Int?>) -> Int? {
return get(column)
return try! get(column)
}

public subscript(column: Expression<Int64>) -> Int64 {
return get(column)
return try! get(column)
}
public subscript(column: Expression<Int64?>) -> Int64? {
return get(column)
return try! get(column)
}

public subscript(column: Expression<String>) -> String {
return get(column)
return try! get(column)
}
public subscript(column: Expression<String?>) -> String? {
return get(column)
return try! get(column)
}

}
Expand Down