From 80ec31f5881856f88bcdd30ce692253aebd85187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 30 Jul 2022 15:37:22 +0200 Subject: [PATCH] BREAKING: remove DerivableRequest.limit(_:offset:) --- CHANGELOG.md | 1 + Documentation/GRDB6MigrationGuide.md | 15 ++++++++ GRDB/Fixits.swift | 5 +++ .../Request/Association/Association.swift | 14 ------- .../Request/QueryInterfaceRequest.swift | 2 +- .../Request/RequestProtocols.swift | 30 --------------- .../AssociationPrefetchingSQLTests.swift | 37 ------------------- 7 files changed, 22 insertions(+), 82 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d745930e6..88962a6e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Documentation/GRDB6MigrationGuide.md b/Documentation/GRDB6MigrationGuide.md index a37188fba..dcf2d49ef 100644 --- a/Documentation/GRDB6MigrationGuide.md +++ b/Documentation/GRDB6MigrationGuide.md @@ -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)) + ``` diff --git a/GRDB/Fixits.swift b/GRDB/Fixits.swift index dcd78178e..74af3ae20 100644 --- a/GRDB/Fixits.swift +++ b/GRDB/Fixits.swift @@ -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() } diff --git a/GRDB/QueryInterface/Request/Association/Association.swift b/GRDB/QueryInterface/Request/Association/Association.swift index a78763445..f89a9e13d 100644 --- a/GRDB/QueryInterface/Request/Association/Association.swift +++ b/GRDB/QueryInterface/Request/Association/Association.swift @@ -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. diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 42e9eba0f..d3cd991e5 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -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) } diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index e70216489..e1439c5d6 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -762,20 +762,6 @@ public protocol DerivableRequest: 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: 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) -> Self { var request = self let expression = aggregate.prepare(&request) diff --git a/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift b/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift index 140520cc4..5a96828c6 100644 --- a/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift @@ -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 {