Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct SQLiteFunctionDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: [UInt8].Type) throws -> [UInt8]? {
mutating func decode(_ columnType: [UInt8].Type) throws(QueryDecodingError) -> [UInt8]? {
precondition(argumentCount > currentIndex)
let value = arguments?[Int(currentIndex)]
switch sqlite3_value_type(value) {
Expand All @@ -57,18 +57,22 @@ struct SQLiteFunctionDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: Bool.Type) throws -> Bool? {
mutating func decode(_ columnType: Bool.Type) throws(QueryDecodingError) -> Bool? {
try decode(Int64.self).map { $0 != 0 }
}

@usableFromInline
mutating func decode(_ columnType: Date.Type) throws -> Date? {
guard let iso8601String = try decode(String.self) else { return nil }
return try Date(iso8601String: iso8601String)
mutating func decode(_ columnType: Date.Type) throws(QueryDecodingError) -> Date? {
do {
guard let iso8601String = try decode(String.self) else { return nil }
return try Date(iso8601String: iso8601String)
} catch {
throw .other(error)
}
}

@inlinable
mutating func decode(_ columnType: Double.Type) throws -> Double? {
mutating func decode(_ columnType: Double.Type) throws(QueryDecodingError) -> Double? {
precondition(argumentCount > currentIndex)
let value = arguments?[Int(currentIndex)]
switch sqlite3_value_type(value) {
Expand All @@ -85,12 +89,12 @@ struct SQLiteFunctionDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: Int.Type) throws -> Int? {
mutating func decode(_ columnType: Int.Type) throws(QueryDecodingError) -> Int? {
try decode(Int64.self).map(Int.init)
}

@inlinable
mutating func decode(_ columnType: Int64.Type) throws -> Int64? {
mutating func decode(_ columnType: Int64.Type) throws(QueryDecodingError) -> Int64? {
precondition(argumentCount > currentIndex)
let value = arguments?[Int(currentIndex)]
switch sqlite3_value_type(value) {
Expand All @@ -107,7 +111,7 @@ struct SQLiteFunctionDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: String.Type) throws -> String? {
mutating func decode(_ columnType: String.Type) throws(QueryDecodingError) -> String? {
precondition(argumentCount > currentIndex)
let value = arguments?[Int(currentIndex)]
switch sqlite3_value_type(value) {
Expand All @@ -124,20 +128,20 @@ struct SQLiteFunctionDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: UInt64.Type) throws -> UInt64? {
mutating func decode(_ columnType: UInt64.Type) throws(QueryDecodingError) -> UInt64? {
guard let n = try decode(Int64.self) else { return nil }
guard n >= 0 else { throw UInt64OverflowError(signedInteger: n) }
guard n >= 0 else { throw .other(UInt64OverflowError(signedInteger: n)) }
return UInt64(n)
}

@usableFromInline
mutating func decode(_ columnType: UUID.Type) throws -> UUID? {
mutating func decode(_ columnType: UUID.Type) throws(QueryDecodingError) -> UUID? {
guard let uuidString = try decode(String.self) else { return nil }
return UUID(uuidString: uuidString)
}

@usableFromInline
func reportTypeMismatch(_ columnType: Any.Type) throws {
func reportTypeMismatch(_ columnType: Any.Type) throws(QueryDecodingError) {
#if StrictDecoding
throw QueryDecodingError.typeMismatch(columnType)
#else
Expand Down
30 changes: 17 additions & 13 deletions Sources/SQLiteData/StructuredQueries+GRDB/SQLiteQueryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct SQLiteQueryDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: [UInt8].Type) throws -> [UInt8]? {
mutating func decode(_ columnType: [UInt8].Type) throws(QueryDecodingError) -> [UInt8]? {
switch sqlite3_column_type(statement, currentIndex) {
case SQLITE_NULL:
currentIndex += 1
Expand All @@ -46,17 +46,21 @@ struct SQLiteQueryDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: Bool.Type) throws -> Bool? {
mutating func decode(_ columnType: Bool.Type) throws(QueryDecodingError) -> Bool? {
try decode(Int64.self).map { $0 != 0 }
}

@inlinable
mutating func decode(_ columnType: Date.Type) throws -> Date? {
try decode(String.self).map { try Date(iso8601String: $0) }
mutating func decode(_ columnType: Date.Type) throws(QueryDecodingError) -> Date? {
do {
return try decode(String.self).map { try Date(iso8601String: $0) }
} catch {
throw .other(error)
}
}

@inlinable
mutating func decode(_ columnType: Double.Type) throws -> Double? {
mutating func decode(_ columnType: Double.Type) throws(QueryDecodingError) -> Double? {
switch sqlite3_column_type(statement, currentIndex) {
case SQLITE_NULL:
currentIndex += 1
Expand All @@ -71,12 +75,12 @@ struct SQLiteQueryDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: Int.Type) throws -> Int? {
mutating func decode(_ columnType: Int.Type) throws(QueryDecodingError) -> Int? {
try decode(Int64.self).map(Int.init)
}

@inlinable
mutating func decode(_ columnType: Int64.Type) throws -> Int64? {
mutating func decode(_ columnType: Int64.Type) throws(QueryDecodingError) -> Int64? {
switch sqlite3_column_type(statement, currentIndex) {
case SQLITE_NULL:
currentIndex += 1
Expand All @@ -91,7 +95,7 @@ struct SQLiteQueryDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: String.Type) throws -> String? {
mutating func decode(_ columnType: String.Type) throws(QueryDecodingError) -> String? {
switch sqlite3_column_type(statement, currentIndex) {
case SQLITE_NULL:
currentIndex += 1
Expand All @@ -106,21 +110,21 @@ struct SQLiteQueryDecoder: QueryDecoder {
}

@inlinable
mutating func decode(_ columnType: UInt64.Type) throws -> UInt64? {
mutating func decode(_ columnType: UInt64.Type) throws(QueryDecodingError) -> UInt64? {
guard let n = try decode(Int64.self) else { return nil }
guard n >= 0 else { throw UInt64OverflowError(signedInteger: n) }
guard n >= 0 else { throw .other(UInt64OverflowError(signedInteger: n)) }
return UInt64(n)
}

@inlinable
mutating func decode(_ columnType: UUID.Type) throws -> UUID? {
mutating func decode(_ columnType: UUID.Type) throws(QueryDecodingError) -> UUID? {
guard let uuidString = try decode(String.self) else { return nil }
guard let uuid = UUID(uuidString: uuidString) else { throw InvalidUUID() }
guard let uuid = UUID(uuidString: uuidString) else { throw .other(InvalidUUID()) }
return uuid
}

@usableFromInline
func reportTypeMismatch(_ columnType: Any.Type) throws {
func reportTypeMismatch(_ columnType: Any.Type) throws(QueryDecodingError) {
#if StrictDecoding
throw QueryDecodingError.typeMismatch(columnType)
#else
Expand Down