mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
BREAKING: DatabaseCursor no longer exposes its prepared statement
This commit is contained in:
1 parent
eda403bd31
commit
0ed9367b26
11 files changed
+82
-29
No files matched your search
@@ -106,6 +106,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
|
||||
- **Breaking**: The `DerivableRequest.limit(_:offset_:)` method was removed, without replacement.
|
||||
- **Breaking**: `DatabaseRegionObservation.start(in:onError:onChange:)` now returns a cancellable.
|
||||
- **Breaking**: The `DatabaseRegionObservation.extent` property was removed.
|
||||
- **Breaking**: The `statement` property of database cursors was replaced with read-only properties such as `sql` or `columnNames`.
|
||||
- **New**: Request protocols and cursors now define primary associated types, enabled by [SE-0346](https://github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md).
|
||||
- **New**: You can append the contents of a cursor to a collection with `RangeReplaceableCollection.append(contentsOf:)`.
|
||||
- **New**: `ValueObservation.map` now accepts a throwing closure argument.
|
||||
|
||||
@@ -178,3 +178,12 @@ The record protocols have been refactored. We tried to keep the amount of modifi
|
||||
```
|
||||
|
||||
The `DatabaseRegionObservation.extent` property was removed. You now control the duration of the observation with the cancellable returned from the `start` method.
|
||||
|
||||
- Database cursors no longer have a `statement` property. When you want information about the database statement used by a cursor, use dedicated cursor properties. For example:
|
||||
|
||||
```diff
|
||||
-let sql = cursor.statement.sql
|
||||
-let columns = cursor.statement.columnNames
|
||||
+let sql = cursor.sql
|
||||
+let columns = cursor.columnNames
|
||||
```
|
||||
@@ -340,7 +340,7 @@ private struct TableRegion: Equatable {
|
||||
/// `DatabaseRegionConvertible` is the protocol for values that can be turned
|
||||
/// into a `DatabaseRegion`.
|
||||
///
|
||||
/// Such values specify the region obserbed by `DatabaseRegionObservation`.
|
||||
/// Such values specify the region observed by `DatabaseRegionObservation`.
|
||||
public protocol DatabaseRegionConvertible {
|
||||
/// Returns a database region.
|
||||
///
|
||||
|
||||
@@ -105,13 +105,14 @@ extension DatabaseValueConvertible {
|
||||
/// }
|
||||
public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: DatabaseCursor {
|
||||
public typealias Element = Value
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public let _statement: Statement
|
||||
/// :nodoc:
|
||||
public var _isDone = false
|
||||
private let columnIndex: CInt
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws {
|
||||
self.statement = statement
|
||||
self._statement = statement
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
columnIndex = try CInt(adapter.baseColumnIndex(atIndex: 0, layout: statement))
|
||||
@@ -126,7 +127,7 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: Databas
|
||||
deinit {
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? statement.reset()
|
||||
try? _statement.reset()
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
@@ -134,7 +135,7 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: Databas
|
||||
try Value.decode(
|
||||
fromStatement: sqliteStatement,
|
||||
atUncheckedIndex: columnIndex,
|
||||
context: RowDecodingContext(statement: statement, index: Int(columnIndex)))
|
||||
context: RowDecodingContext(statement: _statement, index: Int(columnIndex)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -1087,13 +1087,14 @@ extension Row {
|
||||
/// }
|
||||
public final class RowCursor: DatabaseCursor {
|
||||
public typealias Element = Row
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public let _statement: Statement
|
||||
/// :nodoc:
|
||||
public var _isDone = false
|
||||
@usableFromInline let _row: Row // Reused for performance
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws {
|
||||
self.statement = statement
|
||||
self._statement = statement
|
||||
self._row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
|
||||
// Assume cursor is created for immediate iteration: reset and set arguments
|
||||
@@ -1103,7 +1104,7 @@ public final class RowCursor: DatabaseCursor {
|
||||
deinit {
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? statement.reset()
|
||||
try? _statement.reset()
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
|
||||
@@ -420,15 +420,34 @@ public protocol _DatabaseCursor: Cursor {
|
||||
/// Must be initialized to false.
|
||||
var _isDone: Bool { get set }
|
||||
|
||||
/// The statement iterated by the cursor
|
||||
var _statement: Statement { get }
|
||||
|
||||
/// Called after one successful call to `sqlite3_step()`. Returns the
|
||||
/// element for the current statement step.
|
||||
func _element(sqliteStatement: SQLiteStatement) throws -> Element
|
||||
}
|
||||
|
||||
/// A protocol for cursors that iterate a database statement.
|
||||
public protocol DatabaseCursor: _DatabaseCursor {
|
||||
/// The statement iterated by the cursor
|
||||
var statement: Statement { get }
|
||||
public protocol DatabaseCursor: _DatabaseCursor { }
|
||||
|
||||
// Read-only access to statement information. We don't want the user to modify
|
||||
// a statement through a cursor, in case this would mess with the cursor state.
|
||||
extension DatabaseCursor {
|
||||
/// The SQL query
|
||||
public var sql: String { _statement.sql }
|
||||
|
||||
/// The SQL statement arguments.
|
||||
public var arguments: StatementArguments { _statement.arguments }
|
||||
|
||||
/// The column names, ordered from left to right.
|
||||
public var columnNames: [String] { _statement.columnNames }
|
||||
|
||||
/// The number of columns in the resulting rows.
|
||||
public var columnCount: Int { _statement.columnCount }
|
||||
|
||||
/// The database region that the cursor looks into.
|
||||
public var databaseRegion: DatabaseRegion { _statement.databaseRegion }
|
||||
}
|
||||
|
||||
extension DatabaseCursor {
|
||||
@@ -437,7 +456,7 @@ extension DatabaseCursor {
|
||||
if _isDone {
|
||||
return nil
|
||||
}
|
||||
if let element = try statement.step(_element) {
|
||||
if let element = try _statement.step(_element) {
|
||||
return element
|
||||
}
|
||||
_isDone = true
|
||||
@@ -449,7 +468,7 @@ extension DatabaseCursor {
|
||||
@inlinable
|
||||
public func forEach(_ body: (Element) throws -> Void) throws {
|
||||
if _isDone { return }
|
||||
try statement.forEachStep {
|
||||
try _statement.forEachStep {
|
||||
try body(_element(sqliteStatement: $0))
|
||||
}
|
||||
_isDone = true
|
||||
@@ -468,12 +487,12 @@ extension DatabaseCursor {
|
||||
/// }
|
||||
final class StatementCursor: DatabaseCursor {
|
||||
typealias Element = Void
|
||||
let statement: Statement
|
||||
let _statement: Statement
|
||||
var _isDone = false
|
||||
|
||||
// Use Statement.makeCursor() instead
|
||||
init(statement: Statement, arguments: StatementArguments? = nil) throws {
|
||||
self.statement = statement
|
||||
self._statement = statement
|
||||
|
||||
// Assume cursor is created for immediate iteration: reset and set arguments
|
||||
try statement.reset(withArguments: arguments)
|
||||
@@ -482,7 +501,7 @@ final class StatementCursor: DatabaseCursor {
|
||||
deinit {
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? statement.reset()
|
||||
try? _statement.reset()
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
|
||||
@@ -144,13 +144,14 @@ public final class FastDatabaseValueCursor<Value>: DatabaseCursor
|
||||
where Value: DatabaseValueConvertible & StatementColumnConvertible
|
||||
{
|
||||
public typealias Element = Value
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public let _statement: Statement
|
||||
/// :nodoc:
|
||||
public var _isDone = false
|
||||
@usableFromInline let columnIndex: CInt
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws {
|
||||
self.statement = statement
|
||||
self._statement = statement
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
columnIndex = try CInt(adapter.baseColumnIndex(atIndex: 0, layout: statement))
|
||||
@@ -165,7 +166,7 @@ where Value: DatabaseValueConvertible & StatementColumnConvertible
|
||||
deinit {
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? statement.reset()
|
||||
try? _statement.reset()
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
@@ -174,7 +175,7 @@ where Value: DatabaseValueConvertible & StatementColumnConvertible
|
||||
try Value.fastDecode(
|
||||
fromStatement: sqliteStatement,
|
||||
atUncheckedIndex: columnIndex,
|
||||
context: RowDecodingContext(statement: statement, index: Int(columnIndex)))
|
||||
context: RowDecodingContext(statement: _statement, index: Int(columnIndex)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,11 @@ extension Database {
|
||||
public func makeUpdateStatement(literal sqlLiteral: SQL) throws -> Statement { preconditionFailure() }
|
||||
}
|
||||
|
||||
extension DatabaseCursor {
|
||||
@available(*, unavailable, message: "statement has been removed. You may use other cursor properties instead.")
|
||||
public var statement: Statement { preconditionFailure() }
|
||||
}
|
||||
|
||||
extension DatabaseMigrator {
|
||||
@available(*, unavailable, message: "The completion function now accepts one Result<Database, Error> argument")
|
||||
public func asyncMigrate(
|
||||
|
||||
@@ -517,13 +517,14 @@ extension FetchRequest where RowDecoder: FetchableRecord & Hashable {
|
||||
/// }
|
||||
public final class RecordCursor<Record: FetchableRecord>: DatabaseCursor {
|
||||
public typealias Element = Record
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public let _statement: Statement
|
||||
/// :nodoc:
|
||||
public var _isDone = false
|
||||
private let row: Row // Instantiated once, reused for performance
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws {
|
||||
self.statement = statement
|
||||
self._statement = statement
|
||||
row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
try statement.reset(withArguments: arguments)
|
||||
}
|
||||
@@ -531,7 +532,7 @@ public final class RecordCursor<Record: FetchableRecord>: DatabaseCursor {
|
||||
deinit {
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? statement.reset()
|
||||
try? _statement.reset()
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
|
||||
@@ -3,13 +3,24 @@ import GRDB
|
||||
|
||||
class RowFetchTests: GRDBTestCase {
|
||||
|
||||
func testStatementInformationFromCursor() throws {
|
||||
// Test that cursor provides statement information
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let request: SQLRequest<Row> = "SELECT \("Arthur") AS firstName, \("Martin") AS lastName"
|
||||
let cursor = try request.fetchCursor(db)
|
||||
XCTAssertEqual(cursor.sql, "SELECT ? AS firstName, ? AS lastName")
|
||||
XCTAssertEqual(cursor.arguments, ["Arthur", "Martin"])
|
||||
XCTAssertEqual(cursor.columnCount, 2)
|
||||
XCTAssertEqual(cursor.columnNames, ["firstName", "lastName"])
|
||||
XCTAssertEqual(cursor.databaseRegion.description, "empty")
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchCursor() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: RowCursor) throws {
|
||||
// Check that RowCursor gives access to the raw SQLite API
|
||||
XCTAssertEqual(String(cString: sqlite3_column_name(cursor.statement.sqliteStatement, 0)), "firstName")
|
||||
|
||||
var row = try cursor.next()!
|
||||
XCTAssertEqual(row["firstName"] as String, "Arthur")
|
||||
XCTAssertEqual(row["lastName"] as String, "Martin")
|
||||
|
||||
@@ -28,8 +28,12 @@ class SelectStatementTests : GRDBTestCase {
|
||||
let statement = try db.makeStatement(sql: sql)
|
||||
let cursor = try statement.makeCursor()
|
||||
|
||||
// Check that StatementCursor gives access to the raw SQLite API
|
||||
XCTAssertEqual(String(cString: sqlite3_column_name(cursor.statement.sqliteStatement, 0)), "firstName")
|
||||
// Test that cursor provides statement information
|
||||
XCTAssertEqual(cursor.sql, sql)
|
||||
XCTAssertEqual(cursor.arguments, [])
|
||||
XCTAssertEqual(cursor.columnCount, 2)
|
||||
XCTAssertEqual(cursor.columnNames, ["firstName", "lastName"])
|
||||
XCTAssertEqual(cursor.databaseRegion.description, "empty")
|
||||
|
||||
XCTAssertFalse(try cursor.next() == nil)
|
||||
XCTAssertFalse(try cursor.next() == nil)
|
||||
@@ -44,7 +48,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ cursor: StatementCursor) throws {
|
||||
let sql = cursor.statement.sql
|
||||
let sql = cursor.sql
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
XCTFail()
|
||||
|
||||
Reference in new issue
Block a user