BREAKING: remove DerivableRequest.limit(_:offset:)

This commit is contained in:
Gwendal Roué committed 2022-08-21 21:22:34 +02:00
1 parent c26ed6c280
commit 80ec31f588
7 files changed
+22 -82

No files matched your search

+1
View File
@@ -103,6 +103,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
- **Breaking**: `Cursor.isEmpty` is now a throwing property, instead of a method.
- **Breaking**: The `Record.copy()` method was removed, without replacement.
- **Breaking**: The `FetchableRecord.init(row:)` initializer can now throw errors.
- **Breaking**: The `DerivableRequest.limit(_:offset_:)` method was removed, without replacement.
- **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.
+15
View File
@@ -126,3 +126,18 @@ The record protocols have been refactored. We tried to keep the amount of modifi
```
- The `Record.copy()` method was removed, without replacement.
- The `DerivableRequest.limit(_:offset_:)` method was removed, without replacement.
You can still limit `QueryInterfaceRequest`, but associations can no longer be limited:
```swift
// Still OK: a limited request of authors
let request = Author.limit(10)
// Still OK: a limited request of books
let request = author.request(for: Author.books).limit(10)
// No longer possible: including a limited association
let request = Author.including(all: Author.books.limit(10))
```
+5
View File
@@ -1,6 +1,11 @@
// Fixits for changes introduced by GRDB 6.0.0
// swiftlint:disable all
extension Association {
@available(*, unavailable, message: "limit(_:offset:) was not working properly, and was removed.")
public func limit(_ limit: Int, offset: Int? = nil) -> Self { preconditionFailure() }
}
extension Database {
@available(*, unavailable, renamed: "cachedStatement(sql:)")
public func cachedSelectStatement(sql: String) throws -> Statement { preconditionFailure() }
@@ -377,20 +377,6 @@ extension Association {
}
}
/// Creates an association that fetches *limit* rows, starting at *offset*.
///
/// Any previous limit is replaced.
///
/// - warning: Avoid this method: it is unlikely it does what you expect it
/// to do. It will be removed in a future GRDB version.
///
/// :nodoc:
public func limit(_ limit: Int, offset: Int? = nil) -> Self {
withDestinationRelation { relation in
relation.limit = SQLLimit(limit: limit, offset: offset)
}
}
/// Returns an association that embeds the common table expression.
///
/// See `QueryInterfaceRequest.with(_:)` for more information.
@@ -361,7 +361,7 @@ extension QueryInterfaceRequest: DerivableRequest {
/// request = request.limit(10, offset: 20)
///
/// Any previous limit is replaced.
public func limit(_ limit: Int, offset: Int?) -> QueryInterfaceRequest {
public func limit(_ limit: Int, offset: Int? = nil) -> QueryInterfaceRequest {
with {
$0.relation.limit = SQLLimit(limit: limit, offset: offset)
}
@@ -762,20 +762,6 @@ public protocol DerivableRequest<RowDecoder>: AggregatingRequest, FilteredReques
/// request = request.distinct()
func distinct() -> Self
/// Creates a request which fetches *limit* rows, starting at *offset*.
///
/// // SELECT * FROM player LIMIT 10 OFFSET 20
/// var request = Player.all()
/// request = request.limit(10, offset: 20)
///
/// Any previous limit is replaced.
///
/// - warning: Avoid to call this method on associations: it is unlikely it
/// does what you expect it to do. Only call it on requests.
///
/// :nodoc:
func limit(_ limit: Int, offset: Int?) -> Self
/// Returns a request which embeds the common table expression.
///
/// If a common table expression with the same table name had already been
@@ -810,22 +796,6 @@ public protocol DerivableRequest<RowDecoder>: AggregatingRequest, FilteredReques
}
extension DerivableRequest {
/// Creates a request which fetches *limit* rows.
///
/// // SELECT * FROM player LIMIT 1
/// var request = Player.all()
/// request = request.limit(1)
///
/// Any previous limit is replaced.
///
/// - warning: Avoid to call this method on associations: it is unlikely it
/// does what you expect it to do. Only call it on requests.
///
/// :nodoc:
public func limit(_ limit: Int) -> Self {
self.limit(limit, offset: nil)
}
private func annotated(with aggregate: AssociationAggregate<RowDecoder>) -> Self {
var request = self
let expression = aggregate.prepare(&request)
@@ -1898,43 +1898,6 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
}
}
func testLimit() throws {
struct Player: TableRecord { }
struct Team: TableRecord {
static let players = hasMany(Player.self)
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "team") { t in
t.autoIncrementedPrimaryKey("teamId")
}
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("playerId")
t.column("parentId", .integer).references("team")
}
try db.execute(sql: "INSERT INTO team DEFAULT VALUES")
do {
sqlQueries.removeAll()
let association = Team.players.limit(10, offset: 5)
let request = Team.including(all: association)
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter(isSelectQuery)
XCTAssertEqual(selectQueries, [
"""
SELECT * FROM "team"
""",
"""
SELECT *, "parentId" AS "grdb_parentId" \
FROM "player" \
WHERE "parentId" = 1 \
LIMIT 10 OFFSET 5
"""])
}
}
}
func testCTE() throws {
struct Player: TableRecord { }
struct Team: TableRecord {