From b2eafd4a13eac543bc52f0434127b0f5600f757b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 30 Jul 2022 16:34:21 +0200 Subject: [PATCH] SE-0361: Extensions on bound generic types https://github.com/apple/swift-evolution/blob/main/proposals/0361-bound-generic-extensions.md --- Documentation/GRDB6MigrationGuide.md | 11 +++++++++++ Documentation/GoodPracticesForDesigningRecordTypes.md | 4 ++-- GRDB/Core/Row.swift | 4 ++-- GRDB/Core/SQL.swift | 4 ++-- GRDB/Core/SQLRequest.swift | 2 +- GRDB/Fixits.swift | 2 +- .../Request/CommonTableExpression.swift | 2 +- .../Request/QueryInterfaceRequest.swift | 2 +- GRDB/QueryInterface/SQL/SQLExpression.swift | 2 +- GRDB/QueryInterface/SQL/SQLOperators.swift | 4 ++-- GRDB/QueryInterface/SQL/SQLSelection.swift | 2 +- .../SQLGeneration/SQLGenerationContext.swift | 2 +- .../AssociationBelongsToDecodableRecordTests.swift | 2 +- Tests/GRDBTests/TableRecordUpdateTests.swift | 2 +- 14 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Documentation/GRDB6MigrationGuide.md b/Documentation/GRDB6MigrationGuide.md index 5f86719c8..c28a0a52f 100644 --- a/Documentation/GRDB6MigrationGuide.md +++ b/Documentation/GRDB6MigrationGuide.md @@ -42,6 +42,17 @@ Request protocols now come with a primary associated type, enabled by [SE-0346]( } ``` +Your extensions to `QueryInterfaceRequest` can be streamlined as well, thanks to [SE-0361](https://github.com/apple/swift-evolution/blob/main/proposals/0361-bound-generic-extensions.md): + +```diff +-extension QueryInterfaceRequest where RowDecoder == Player { ++extension QueryInterfaceRequest { + func selectID() -> QueryInterfaceRequest { + selectPrimaryKey() + } + } +``` + The `Cursor` protocol has also gained a primary associated type (the type of its elements). ## Record Changes diff --git a/Documentation/GoodPracticesForDesigningRecordTypes.md b/Documentation/GoodPracticesForDesigningRecordTypes.md index 8eef81786..284ce79af 100644 --- a/Documentation/GoodPracticesForDesigningRecordTypes.md +++ b/Documentation/GoodPracticesForDesigningRecordTypes.md @@ -368,8 +368,8 @@ try dbQueue.read { db in Extensions to the `DerivableRequest` protocol can not change the type of requests. This has to be expressed in an extension to `QueryInterfaceRequest`. For example: ```swift -// Author requests ~~~~~~~~~~~~~~~~~~~~~~~~~~ -extension QueryInterfaceRequest where RowDecoder == Author { +// Author requests +extension QueryInterfaceRequest { // Selects author ids func selectId() -> QueryInterfaceRequest { selectPrimaryKey(as: Int64.self) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index b2f56c2a0..d5aa1fd45 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -1433,7 +1433,7 @@ extension Row { } } -extension FetchRequest where RowDecoder == Row { +extension FetchRequest { // MARK: Fetching Rows @@ -1925,7 +1925,7 @@ extension Row { } } -extension OrderedDictionary where Key == String, Value == Row.Prefetch { +extension OrderedDictionary { fileprivate mutating func setRows(_ rows: [Row], forKeyPath keyPath: [String]) { var keyPath = keyPath let key = keyPath.removeFirst() diff --git a/GRDB/Core/SQL.swift b/GRDB/Core/SQL.swift index 3f9ae00d9..0418b8a02 100644 --- a/GRDB/Core/SQL.swift +++ b/GRDB/Core/SQL.swift @@ -196,7 +196,7 @@ extension SQL: SQLOrderingTerm { } } -extension Sequence where Element == SQL { +extension Sequence { /// Returns the concatenated `SQL` literal of this sequence of literals, /// inserting the given separator between each element. /// @@ -215,7 +215,7 @@ extension Sequence where Element == SQL { } } -extension Collection where Element == SQL { +extension Collection { /// Returns the concatenated `SQL` literal of this collection of literals, /// inserting the given SQL separator between each element. /// diff --git a/GRDB/Core/SQLRequest.swift b/GRDB/Core/SQLRequest.swift index ae6bf582a..1d97599e2 100644 --- a/GRDB/Core/SQLRequest.swift +++ b/GRDB/Core/SQLRequest.swift @@ -86,7 +86,7 @@ extension SQLRequest { } } -extension SQLRequest where RowDecoder == Row { +extension SQLRequest { /// Creates a request from an SQL string, optional arguments, and /// optional row adapter. /// diff --git a/GRDB/Fixits.swift b/GRDB/Fixits.swift index 74af3ae20..31c2f55e8 100644 --- a/GRDB/Fixits.swift +++ b/GRDB/Fixits.swift @@ -89,7 +89,7 @@ extension TableRecord where Self: Identifiable, ID: DatabaseValueConvertible { @available(*, unavailable, renamed: "Statement") public typealias UpdateStatement = Statement -extension ValueObservation where Reducer == ValueReducers.Auto { +extension ValueObservation { @available(*, unavailable, renamed: "tracking(_:)") public static func trackingVaryingRegion( _ fetch: @escaping (Database) throws -> Value) diff --git a/GRDB/QueryInterface/Request/CommonTableExpression.swift b/GRDB/QueryInterface/Request/CommonTableExpression.swift index b5e07e947..26392b01e 100644 --- a/GRDB/QueryInterface/Request/CommonTableExpression.swift +++ b/GRDB/QueryInterface/Request/CommonTableExpression.swift @@ -151,7 +151,7 @@ extension CommonTableExpression { } } -extension CommonTableExpression where RowDecoder == Row { +extension CommonTableExpression { /// Creates a common table expression from a request. /// /// For example: diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 904d42487..e94aa77b4 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -805,7 +805,7 @@ throws -> DatabaseRegion .databaseRegion // contains region of nested associations } -extension Array where Element == Row { +extension [Row] { /// - precondition: Columns all exist in all rows. All rows have the same /// columnns, in the same order. fileprivate func grouped(byDatabaseValuesOnColumns columns: [String]) -> [[DatabaseValue]: [Row]] { diff --git a/GRDB/QueryInterface/SQL/SQLExpression.swift b/GRDB/QueryInterface/SQL/SQLExpression.swift index bc9de466e..04fe96e7c 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression.swift @@ -1734,7 +1734,7 @@ extension Sequence where Element: SQLSpecificExpressible { } } -extension Sequence where Element == any SQLSpecificExpressible { +extension Sequence { /// Returns an expression by joining all elements with an associative SQL /// binary operator. /// diff --git a/GRDB/QueryInterface/SQL/SQLOperators.swift b/GRDB/QueryInterface/SQL/SQLOperators.swift index e8ec42407..af367909c 100644 --- a/GRDB/QueryInterface/SQL/SQLOperators.swift +++ b/GRDB/QueryInterface/SQL/SQLOperators.swift @@ -326,7 +326,7 @@ extension CountableClosedRange where Bound: SQLExpressible { } } -extension Sequence where Self.Iterator.Element: SQLExpressible { +extension Sequence where Element: SQLExpressible { /// An SQL expression that checks the inclusion of an expression in /// a sequence. /// @@ -337,7 +337,7 @@ extension Sequence where Self.Iterator.Element: SQLExpressible { } } -extension Sequence where Self.Iterator.Element == SQLExpressible { +extension Sequence { /// An SQL expression that checks the inclusion of an expression in /// a sequence. /// diff --git a/GRDB/QueryInterface/SQL/SQLSelection.swift b/GRDB/QueryInterface/SQL/SQLSelection.swift index f2da3af91..e8f64b038 100644 --- a/GRDB/QueryInterface/SQL/SQLSelection.swift +++ b/GRDB/QueryInterface/SQL/SQLSelection.swift @@ -231,7 +231,7 @@ extension SQLSelection { } } -extension Array where Element == SQLSelection { +extension [SQLSelection] { /// Returns the number of columns in the selection. /// /// This method raises a fatal error if the selection contains a literal, diff --git a/GRDB/QueryInterface/SQLGeneration/SQLGenerationContext.swift b/GRDB/QueryInterface/SQLGeneration/SQLGenerationContext.swift index a761c8de8..397a750d3 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLGenerationContext.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLGenerationContext.swift @@ -451,7 +451,7 @@ public class TableAlias: Hashable { } } -extension Array where Element == TableAlias { +extension [TableAlias] { /// Resolve ambiguities in aliases' names. fileprivate var resolvedNames: [TableAlias: String] { // It is a programmer error to reuse the same TableAlias for diff --git a/Tests/GRDBTests/AssociationBelongsToDecodableRecordTests.swift b/Tests/GRDBTests/AssociationBelongsToDecodableRecordTests.swift index 9431170df..a61ae6401 100644 --- a/Tests/GRDBTests/AssociationBelongsToDecodableRecordTests.swift +++ b/Tests/GRDBTests/AssociationBelongsToDecodableRecordTests.swift @@ -32,7 +32,7 @@ private struct PlayerWithTeamName: Decodable, FetchableRecord { var teamName: String? } -extension QueryInterfaceRequest where RowDecoder == Player { +private extension QueryInterfaceRequest { func filter(teamName: String) -> QueryInterfaceRequest { joining(required: PlayerWithOptionalTeam.team.filter(Column("name") == teamName)) } diff --git a/Tests/GRDBTests/TableRecordUpdateTests.swift b/Tests/GRDBTests/TableRecordUpdateTests.swift index 4154bdf30..c61353e6a 100644 --- a/Tests/GRDBTests/TableRecordUpdateTests.swift +++ b/Tests/GRDBTests/TableRecordUpdateTests.swift @@ -24,7 +24,7 @@ private enum Columns: String, ColumnExpression { case id, name, score, bonus } -private extension QueryInterfaceRequest where RowDecoder == Player { +private extension QueryInterfaceRequest { func incrementScore(_ db: Database) throws { try updateAll(db, Columns.score += 1) }