File tree Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -200,10 +200,33 @@ extension Statement : Sequence {
200
200
201
201
}
202
202
203
+ public enum ResultOrError < ResultType> {
204
+ case success( ResultType )
205
+ case error( Error )
206
+
207
+ public func map< T> ( _ transform: ( ResultType ) throws -> T ) rethrows -> ResultOrError < T > {
208
+ switch self {
209
+ case let . success( result) : return try . success( transform ( result) )
210
+ case let . error( error) : return . error( error)
211
+ }
212
+ }
213
+
214
+ public func unwrapOrThrow( ) throws -> ResultType {
215
+ switch self {
216
+ case let . success( result) : return result
217
+ case let . error( error) : throw error
218
+ }
219
+ }
220
+ }
221
+
203
222
extension Statement : IteratorProtocol {
204
223
205
- public func next( ) -> [ Binding ? ] ? {
206
- return try ! step ( ) ? Array ( row) : nil
224
+ public func next( ) -> ResultOrError < [ Binding ? ] > ? {
225
+ do {
226
+ return try step ( ) ? . success( Array ( row) ) : nil
227
+ } catch {
228
+ return . error( error)
229
+ }
207
230
}
208
231
209
232
}
Original file line number Diff line number Diff line change @@ -892,7 +892,7 @@ public struct Delete : ExpressionType {
892
892
893
893
extension Connection {
894
894
895
- public func prepare( _ query: QueryType ) throws -> AnySequence < Row > {
895
+ public func prepare( _ query: QueryType ) throws -> AnySequence < ResultOrError < Row > > {
896
896
let expression = query. expression
897
897
let statement = try prepare ( expression. template, expression. bindings)
898
898
@@ -940,7 +940,7 @@ extension Connection {
940
940
} ( )
941
941
942
942
return AnySequence {
943
- AnyIterator { statement. next ( ) . map { Row ( columnNames, $0) } }
943
+ AnyIterator { statement. next ( ) . map { $0 . map { Row ( columnNames, $0) } } }
944
944
}
945
945
}
946
946
@@ -967,7 +967,7 @@ extension Connection {
967
967
}
968
968
969
969
public func pluck( _ query: QueryType ) throws -> Row ? {
970
- return try prepare ( query. limit ( 1 , query. clauses. limit? . offset) ) . makeIterator ( ) . next ( )
970
+ return try prepare ( query. limit ( 1 , query. clauses. limit? . offset) ) . makeIterator ( ) . next ( ) ? . unwrapOrThrow ( )
971
971
}
972
972
973
973
/// Runs an `Insert` query.
You can’t perform that action at this time.
0 commit comments