Skip to content
New issue

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

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

Already on GitHub? # to your account

Add cursor type for more safety. #647

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: 8 additions & 0 deletions Sources/SQLite/Core/Statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ public final class Statement {

}

extension Statement {

func rowCursorNext() throws -> [Binding?]? {
return try step() ? Array(row) : nil
}

}

extension Statement : Sequence {

public func makeIterator() -> Statement {
Expand Down
44 changes: 37 additions & 7 deletions Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -890,13 +890,48 @@ public struct Delete : ExpressionType {

}

public struct RowCursor {
let statement: Statement
let columnNames: [String: Int]

public func next() throws -> Row? {
return try statement.rowCursorNext().flatMap { Row(columnNames, $0) }
}

public func map<T>(_ transform: (Row) throws -> T) throws -> [T] {
var elements = [T]()
while true {
if let row = try next() {
elements.append(try transform(row))
} else {
break
}
}

return elements
}
}

extension Connection {

public func prepareCursor(_ query: QueryType) throws -> RowCursor {
let expression = query.expression
let statement = try prepare(expression.template, expression.bindings)
return RowCursor(statement: statement, columnNames: try columnNamesForQuery(query))
}

public func prepare(_ query: QueryType) throws -> AnySequence<Row> {
let expression = query.expression
let statement = try prepare(expression.template, expression.bindings)

let columnNames: [String: Int] = try {
let columnNames = try columnNamesForQuery(query)

return AnySequence {
AnyIterator { statement.next().map { Row(columnNames, $0) } }
}
}

private func columnNamesForQuery(_ query: QueryType) throws -> [String: Int] {
var (columnNames, idx) = ([String: Int](), 0)
column: for each in query.clauses.select.columns {
var names = each.expression.template.characters.split { $0 == "." }.map(String.init)
Expand Down Expand Up @@ -937,11 +972,6 @@ extension Connection {
idx += 1
}
return columnNames
}()

return AnySequence {
AnyIterator { statement.next().map { Row(columnNames, $0) } }
}
}

public func scalar<V : Value>(_ query: ScalarQuery<V>) throws -> V {
Expand All @@ -967,7 +997,7 @@ extension Connection {
}

public func pluck(_ query: QueryType) throws -> Row? {
return try prepare(query.limit(1, query.clauses.limit?.offset)).makeIterator().next()
return try prepareCursor(query.limit(1, query.clauses.limit?.offset)).next()
}

/// Runs an `Insert` query.
Expand Down
10 changes: 10 additions & 0 deletions Tests/SQLiteTests/QueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ class QueryIntegrationTests : SQLiteTestCase {
}
}

func test_prepareCursor() {
let names = ["a", "b", "c"]
try! InsertUsers(names)

let emailColumn = Expression<String>("email")
let emails = try! db.prepareCursor(users).map { $0[emailColumn] }

XCTAssertEqual(names.map({ "\($0)@example.com" }), emails.sorted())
}

func test_scalar() {
XCTAssertEqual(0, try! db.scalar(users.count))
XCTAssertEqual(false, try! db.scalar(users.exists))
Expand Down