Skip to content

Add a new method to capture errors during the iteration #1027

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,8 @@ public struct RowIterator: FailableIterator {

extension Connection {

/// Return an iterator that will lazily load the query's results as they are iterated over.
/// - Throws: Any error that happened during the preparation. Errors thrown during the iteration are not captured and will cause a crash.
public func prepare(_ query: QueryType) throws -> AnySequence<Row> {
let expression = query.expression
let statement = try prepare(expression.template, expression.bindings)
Expand All @@ -927,6 +929,24 @@ extension Connection {
AnyIterator { statement.next().map { Row(columnNames, $0) } }
}
}

/// Load and return all the results of the query.
/// - Throws: Any error that happened during the preparation or the execution.
public func prepareAndExecute(_ query: QueryType) throws -> AnySequence<Row> {
let expression = query.expression
let statement = try prepare(expression.template, expression.bindings)

let columnNames = try columnNamesForQuery(query)

var rows = [Row]()
var bindings = try statement.failableNext()
while bindings != nil {
bindings.flatMap { rows.append(Row(columnNames, $0)) }
bindings = try statement.failableNext()
}

return AnySequence(rows)
}


public func prepareRowIterator(_ query: QueryType) throws -> RowIterator {
Expand Down