From 37aa7feee9f672048f39555aab191170188bbacb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 9 Apr 2020 18:50:36 +0200 Subject: [PATCH] Rename KeyPathRefining to Refinable --- GRDB.xcodeproj/project.pbxproj | 20 ++--- GRDB/Core/DatabaseValueConversion.swift | 6 +- .../Association/AssociationAggregate.swift | 6 +- .../Request/QueryInterfaceRequest.swift | 36 ++++----- GRDB/QueryInterface/SQL/SQLAssociation.swift | 12 +-- GRDB/QueryInterface/SQL/SQLQuery.swift | 62 +++++++------- GRDB/QueryInterface/SQL/SQLRelation.swift | 60 +++++++------- .../SQLGeneration/SQLQueryGenerator.swift | 4 +- GRDB/Utils/KeyPathRefining.swift | 38 --------- GRDB/Utils/Refinable.swift | 81 +++++++++++++++++++ GRDBCustom.xcodeproj/project.pbxproj | 12 +-- 11 files changed, 190 insertions(+), 147 deletions(-) delete mode 100644 GRDB/Utils/KeyPathRefining.swift create mode 100644 GRDB/Utils/Refinable.swift diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 1489ac5b7..363612181 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -433,6 +433,10 @@ 5674A72D1F30A9090095F066 /* FetchableRecordDecodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5674A7261F30A9090095F066 /* FetchableRecordDecodableTests.swift */; }; 5676FBA622F5CAD9004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5676FB9F22F5CAD9004717D9 /* ValueObservationRegionRecordingTests.swift */; }; 5676FBA722F5CAD9004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5676FB9F22F5CAD9004717D9 /* ValueObservationRegionRecordingTests.swift */; }; + 56781B0B243F86E600650A83 /* Refinable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56781B0A243F86E600650A83 /* Refinable.swift */; }; + 56781B0C243F86E600650A83 /* Refinable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56781B0A243F86E600650A83 /* Refinable.swift */; }; + 56781B0D243F86E600650A83 /* Refinable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56781B0A243F86E600650A83 /* Refinable.swift */; }; + 56781B0E243F86E600650A83 /* Refinable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56781B0A243F86E600650A83 /* Refinable.swift */; }; 567A80571D41350C00C7DCEC /* IndexInfoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567A80521D41350C00C7DCEC /* IndexInfoTests.swift */; }; 567DAF1C1EAB61ED00FC0928 /* grdb_config.h in Headers */ = {isa = PBXBuildFile; fileRef = 567DAF141EAB61ED00FC0928 /* grdb_config.h */; settings = {ATTRIBUTES = (Private, ); }; }; 567DAF1F1EAB61ED00FC0928 /* grdb_config.h in Headers */ = {isa = PBXBuildFile; fileRef = 567DAF141EAB61ED00FC0928 /* grdb_config.h */; settings = {ATTRIBUTES = (Private, ); }; }; @@ -762,10 +766,6 @@ 56DF001C228DDBA300D611F3 /* AssociationPrefetchingRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF0019228DDBA200D611F3 /* AssociationPrefetchingRowTests.swift */; }; 56DF001D228DDBA300D611F3 /* AssociationPrefetchingCodableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF001A228DDBA300D611F3 /* AssociationPrefetchingCodableRecordTests.swift */; }; 56DF001E228DDBA300D611F3 /* AssociationPrefetchingCodableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF001A228DDBA300D611F3 /* AssociationPrefetchingCodableRecordTests.swift */; }; - 56DF37AB23D77AB4009AAA05 /* KeyPathRefining.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF37AA23D77AB4009AAA05 /* KeyPathRefining.swift */; }; - 56DF37AC23D77AB4009AAA05 /* KeyPathRefining.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF37AA23D77AB4009AAA05 /* KeyPathRefining.swift */; }; - 56DF37AD23D77AB4009AAA05 /* KeyPathRefining.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF37AA23D77AB4009AAA05 /* KeyPathRefining.swift */; }; - 56DF37AE23D77AB4009AAA05 /* KeyPathRefining.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF37AA23D77AB4009AAA05 /* KeyPathRefining.swift */; }; 56E4F7EE2392E2D000A611F6 /* DatabaseAbortedTransactionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4F7ED2392E2D000A611F6 /* DatabaseAbortedTransactionTests.swift */; }; 56E4F7EF2392E2D000A611F6 /* DatabaseAbortedTransactionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4F7ED2392E2D000A611F6 /* DatabaseAbortedTransactionTests.swift */; }; 56E4F7F02392E2D000A611F6 /* DatabaseAbortedTransactionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4F7ED2392E2D000A611F6 /* DatabaseAbortedTransactionTests.swift */; }; @@ -1333,6 +1333,7 @@ 5674A71C1F30A8DF0095F066 /* MutablePersistableRecordEncodableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutablePersistableRecordEncodableTests.swift; sourceTree = ""; }; 5674A7261F30A9090095F066 /* FetchableRecordDecodableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchableRecordDecodableTests.swift; sourceTree = ""; }; 5676FB9F22F5CAD9004717D9 /* ValueObservationRegionRecordingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRegionRecordingTests.swift; sourceTree = ""; }; + 56781B0A243F86E600650A83 /* Refinable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Refinable.swift; sourceTree = ""; }; 567A80521D41350C00C7DCEC /* IndexInfoTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexInfoTests.swift; sourceTree = ""; }; 567DAF141EAB61ED00FC0928 /* grdb_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = grdb_config.h; sourceTree = ""; }; 567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLogErrorTests.swift; sourceTree = ""; }; @@ -1473,7 +1474,6 @@ 56DE7B101C3D93ED00861EB8 /* StatementArgumentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementArgumentsTests.swift; sourceTree = ""; }; 56DF0019228DDBA200D611F3 /* AssociationPrefetchingRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingRowTests.swift; sourceTree = ""; }; 56DF001A228DDBA300D611F3 /* AssociationPrefetchingCodableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingCodableRecordTests.swift; sourceTree = ""; }; - 56DF37AA23D77AB4009AAA05 /* KeyPathRefining.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyPathRefining.swift; sourceTree = ""; }; 56E4F7ED2392E2D000A611F6 /* DatabaseAbortedTransactionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseAbortedTransactionTests.swift; sourceTree = ""; }; 56E5D7CA1B4D3FED00430942 /* GRDB.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GRDB.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 56E5D7D31B4D3FEE00430942 /* GRDBiOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBiOSTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1925,11 +1925,11 @@ children = ( 563EF4492161F179007DAACD /* Inflections.swift */, 569BBA482291707D00478429 /* Inflections+English.swift */, - 56DF37AA23D77AB4009AAA05 /* KeyPathRefining.swift */, 566BE7172342542F00A8254B /* LockedBox.swift */, 563EF414215F87EB007DAACD /* OrderedDictionary.swift */, 5659F4971EA8D989004A4992 /* Pool.swift */, 5659F48F1EA8D964004A4992 /* ReadWriteBox.swift */, + 56781B0A243F86E600650A83 /* Refinable.swift */, 5659F4871EA8D94E004A4992 /* Utils.swift */, ); path = Utils; @@ -2735,7 +2735,6 @@ 56CEB5671EAA359A00BFAF62 /* SQLSelectable.swift in Sources */, 5653EB272094A14400F46237 /* QueryInterfaceRequest+Association.swift in Sources */, 565490B91D5AE236005622CB /* DatabasePool.swift in Sources */, - 56DF37AD23D77AB4009AAA05 /* KeyPathRefining.swift in Sources */, 563B06AD217EF0CC00B38F35 /* ValueObservation.swift in Sources */, 5674A6EF1F307F0E0095F066 /* DatabaseValueConvertible+Encodable.swift in Sources */, 565490D81D5AE252005622CB /* DatabaseMigrator.swift in Sources */, @@ -2824,6 +2823,7 @@ 565490BC1D5AE236005622CB /* DatabaseSchemaCache.swift in Sources */, 563EF42F2161180D007DAACD /* AssociationAggregate.swift in Sources */, 565490BA1D5AE236005622CB /* DatabaseQueue.swift in Sources */, + 56781B0D243F86E600650A83 /* Refinable.swift in Sources */, 565490CD1D5AE252005622CB /* Date.swift in Sources */, 5653EB0820944C7C00F46237 /* SQLForeignKeyRequest.swift in Sources */, 565490BE1D5AE236005622CB /* DatabaseValue.swift in Sources */, @@ -2911,7 +2911,6 @@ 56A2387C1B9C75030082EB20 /* Configuration.swift in Sources */, 56DAA2DE1DE9C827006E10C8 /* Cursor.swift in Sources */, 560A37A51C8F625000949E71 /* DatabasePool.swift in Sources */, - 56DF37AC23D77AB4009AAA05 /* KeyPathRefining.swift in Sources */, 5674A7081F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift in Sources */, 56AE64132229A53700AD1B0B /* HasOneThroughAssociation.swift in Sources */, 56A2388C1B9C75030082EB20 /* Statement.swift in Sources */, @@ -2959,6 +2958,7 @@ 5613ED3621A95A5C00DC7A68 /* Map.swift in Sources */, 566475A51D9810A400FF74B8 /* SQLSelectable+QueryInterface.swift in Sources */, 56E9FAD8221053DD00C703A8 /* SQLLiteral.swift in Sources */, + 56781B0C243F86E600650A83 /* Refinable.swift in Sources */, 5653EB0720944C7C00F46237 /* SQLForeignKeyRequest.swift in Sources */, 5657AABC1D107001006283EF /* NSData.swift in Sources */, 31A778841C6A4E0600F507F6 /* FetchedRecordsController.swift in Sources */, @@ -3451,7 +3451,6 @@ AAA4DCB0230F1E0600C74B15 /* Configuration.swift in Sources */, AAA4DCB1230F1E0600C74B15 /* Cursor.swift in Sources */, AAA4DCB2230F1E0600C74B15 /* DatabasePool.swift in Sources */, - 56DF37AE23D77AB4009AAA05 /* KeyPathRefining.swift in Sources */, AAA4DCB3230F1E0600C74B15 /* DatabaseValueConvertible+ReferenceConvertible.swift in Sources */, AAA4DCB4230F1E0600C74B15 /* HasOneThroughAssociation.swift in Sources */, AAA4DCB5230F1E0600C74B15 /* Statement.swift in Sources */, @@ -3499,6 +3498,7 @@ AAA4DCE3230F1E0600C74B15 /* Map.swift in Sources */, AAA4DCE4230F1E0600C74B15 /* SQLSelectable+QueryInterface.swift in Sources */, AAA4DCE5230F1E0600C74B15 /* SQLLiteral.swift in Sources */, + 56781B0E243F86E600650A83 /* Refinable.swift in Sources */, AAA4DCE6230F1E0600C74B15 /* SQLForeignKeyRequest.swift in Sources */, AAA4DCE7230F1E0600C74B15 /* NSData.swift in Sources */, AAA4DCE8230F1E0600C74B15 /* FetchedRecordsController.swift in Sources */, @@ -3781,7 +3781,6 @@ 5605F1691C672E4000235C62 /* NSString.swift in Sources */, 560D92401C672C3E00F4F92B /* DatabaseValueConvertible.swift in Sources */, 56A8C2301D1914540096E9D4 /* UUID.swift in Sources */, - 56DF37AB23D77AB4009AAA05 /* KeyPathRefining.swift in Sources */, 56CEB5011EAA2F4D00BFAF62 /* FTS4.swift in Sources */, 56AE64122229A53700AD1B0B /* HasOneThroughAssociation.swift in Sources */, 56CEB55A1EAA359A00BFAF62 /* SQLOrdering.swift in Sources */, @@ -3833,6 +3832,7 @@ 5605F1671C672E4000235C62 /* NSNumber.swift in Sources */, 56E9FADA221053DD00C703A8 /* SQLLiteral.swift in Sources */, C96C0F2B2084A442006B2981 /* SQLiteDateParser.swift in Sources */, + 56781B0B243F86E600650A83 /* Refinable.swift in Sources */, 56A238871B9C75030082EB20 /* Row.swift in Sources */, 5653EB2120944C7C00F46237 /* HasOneAssociation.swift in Sources */, 5605F1731C672E4000235C62 /* StandardLibrary.swift in Sources */, diff --git a/GRDB/Core/DatabaseValueConversion.swift b/GRDB/Core/DatabaseValueConversion.swift index cbc79bc31..f88661d4f 100644 --- a/GRDB/Core/DatabaseValueConversion.swift +++ b/GRDB/Core/DatabaseValueConversion.swift @@ -9,7 +9,7 @@ import SQLite3 // MARK: - Conversion Context and Errors /// A type that helps the user understanding value conversion errors -struct ValueConversionContext: KeyPathRefining { +struct ValueConversionContext: Refinable { private enum Column { case columnIndex(Int) case columnName(String) @@ -20,11 +20,11 @@ struct ValueConversionContext: KeyPathRefining { var arguments: StatementArguments? private var column: Column? - func atColumn(_ columnIndex: Int) -> ValueConversionContext { + func atColumn(_ columnIndex: Int) -> Self { with(\.column, .columnIndex(columnIndex)) } - func atColumn(_ columnName: String) -> ValueConversionContext { + func atColumn(_ columnName: String) -> Self { with(\.column, .columnName(columnName)) } diff --git a/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift b/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift index 028de11fc..aafa8de13 100644 --- a/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift +++ b/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift @@ -66,7 +66,7 @@ public struct AssociationAggregate { var key: String? = nil } -extension AssociationAggregate: KeyPathRefining { +extension AssociationAggregate: Refinable { /// Returns an aggregate that is selected in a column with the given name. /// /// For example: @@ -76,7 +76,7 @@ extension AssociationAggregate: KeyPathRefining { /// if let row = try Row.fetchOne(db, request) { /// let numberOfBooks: Int = row["numberOfBooks"] /// } - public func forKey(_ key: String) -> AssociationAggregate { + public func forKey(_ key: String) -> Self { with(\.key, key) } @@ -95,7 +95,7 @@ extension AssociationAggregate: KeyPathRefining { /// return try AuthorInfo.fetchAll(db, request) /// } /// } - public func forKey(_ key: CodingKey) -> AssociationAggregate { + public func forKey(_ key: CodingKey) -> Self { forKey(key.stringValue) } } diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index a12d1ac84..834f7e38d 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -130,7 +130,7 @@ extension QueryInterfaceRequest: SelectionRequest { /// .select([Column("id")]) /// .select([Column("email")]) public func select(_ selection: [SQLSelectable]) -> QueryInterfaceRequest { - map(\.query, { $0.select(selection) }) + map(\.query) { $0.select(selection) } } /// Creates a request which selects *selection*, and fetches values of @@ -221,7 +221,7 @@ extension QueryInterfaceRequest: SelectionRequest { /// .select([Column("id"), Column("email")]) /// .annotated(with: [Column("name")]) public func annotated(with selection: [SQLSelectable]) -> QueryInterfaceRequest { - map(\.query, { $0.annotated(with: selection) }) + map(\.query) { $0.annotated(with: selection) } } } @@ -235,7 +235,7 @@ extension QueryInterfaceRequest: FilteredRequest { /// var request = Player.all() /// request = request.filter { db in true } public func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> QueryInterfaceRequest { - map(\.query, { $0.filter(predicate) }) + map(\.query) { $0.filter(predicate) } } } @@ -256,7 +256,7 @@ extension QueryInterfaceRequest: OrderedRequest { /// .reversed() /// .order{ _ in [Column("name")] } public func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> QueryInterfaceRequest { - map(\.query, { $0.order(orderings) }) + map(\.query) { $0.order(orderings) } } /// Creates a request that reverses applied orderings. @@ -271,7 +271,7 @@ extension QueryInterfaceRequest: OrderedRequest { /// var request = Player.all() /// request = request.reversed() public func reversed() -> QueryInterfaceRequest { - map(\.query, { $0.reversed() }) + map(\.query) { $0.reversed() } } /// Creates a request without any ordering. @@ -280,7 +280,7 @@ extension QueryInterfaceRequest: OrderedRequest { /// var request = Player.all().order(Column("name")) /// request = request.unordered() public func unordered() -> QueryInterfaceRequest { - map(\.query, { $0.unordered() }) + map(\.query) { $0.unordered() } } } @@ -289,46 +289,46 @@ extension QueryInterfaceRequest: AggregatingRequest { /// Creates a request grouped according to *expressions promise*. public func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> QueryInterfaceRequest { - map(\.query, { $0.group(expressions) }) + map(\.query) { $0.group(expressions) } } /// Creates a request with the provided *predicate* added to the /// eventual set of already applied predicates. public func having(_ predicate: SQLExpressible) -> QueryInterfaceRequest { - map(\.query, { $0.having(predicate) }) + map(\.query) { $0.having(predicate) } } } extension QueryInterfaceRequest: _JoinableRequest { /// :nodoc: public func _including(all association: SQLAssociation) -> QueryInterfaceRequest { - map(\.query, { $0._including(all: association) }) + map(\.query) { $0._including(all: association) } } /// :nodoc: public func _including(optional association: SQLAssociation) -> QueryInterfaceRequest { - map(\.query, { $0._including(optional: association) }) + map(\.query) { $0._including(optional: association) } } /// :nodoc: public func _including(required association: SQLAssociation) -> QueryInterfaceRequest { - map(\.query, { $0._including(required: association) }) + map(\.query) { $0._including(required: association) } } /// :nodoc: public func _joining(optional association: SQLAssociation) -> QueryInterfaceRequest { - map(\.query, { $0._joining(optional: association) }) + map(\.query) { $0._joining(optional: association) } } /// :nodoc: public func _joining(required association: SQLAssociation) -> QueryInterfaceRequest { - map(\.query, { $0._joining(required: association) }) + map(\.query) { $0._joining(required: association) } } } extension QueryInterfaceRequest: JoinableRequest where T: TableRecord { } -extension QueryInterfaceRequest: KeyPathRefining { +extension QueryInterfaceRequest: Refinable { // MARK: Request Derivation @@ -342,7 +342,7 @@ extension QueryInterfaceRequest: KeyPathRefining { /// var request = Player.select(Column("name")) /// request = request.distinct() public func distinct() -> QueryInterfaceRequest { - map(\.query, { $0.distinct() }) + map(\.query) { $0.distinct() } } /// Creates a request which expects a single result. @@ -353,7 +353,7 @@ extension QueryInterfaceRequest: KeyPathRefining { /// /// :nodoc: public func expectingSingleResult() -> QueryInterfaceRequest { - map(\.query, { $0.expectingSingleResult() }) + map(\.query) { $0.expectingSingleResult() } } @@ -365,7 +365,7 @@ extension QueryInterfaceRequest: KeyPathRefining { /// /// Any previous limit is replaced. public func limit(_ limit: Int, offset: Int? = nil) -> QueryInterfaceRequest { - map(\.query, { $0.limit(limit, offset: offset) }) + map(\.query) { $0.limit(limit, offset: offset) } } /// Creates a request that allows you to define expressions that target @@ -386,7 +386,7 @@ extension QueryInterfaceRequest: KeyPathRefining { /// .aliased(playerAlias) /// .including(required: Player.team.filter(Column("avgScore") < playerAlias[Column("score")]) public func aliased(_ alias: TableAlias) -> QueryInterfaceRequest { - map(\.query, { $0.qualified(with: alias) }) + map(\.query) { $0.qualified(with: alias) } } /// Creates a request bound to type Target. diff --git a/GRDB/QueryInterface/SQL/SQLAssociation.swift b/GRDB/QueryInterface/SQL/SQLAssociation.swift index 635647af6..8a84642db 100644 --- a/GRDB/QueryInterface/SQL/SQLAssociation.swift +++ b/GRDB/QueryInterface/SQL/SQLAssociation.swift @@ -86,12 +86,12 @@ public /* TODO: internal */ struct SQLAssociation { } /// Changes the destination key - func forDestinationKey(_ key: SQLAssociationKey) -> SQLAssociation { + func forDestinationKey(_ key: SQLAssociationKey) -> Self { with(\.destination.key, key) } /// Returns a new association - func through(_ other: SQLAssociation) -> SQLAssociation { + func through(_ other: SQLAssociation) -> Self { SQLAssociation(steps: other.steps + steps) } @@ -181,7 +181,7 @@ public /* TODO: internal */ struct SQLAssociation { .filteringChildren { $0.kind.cardinality == .toOne } // Don't interfere with user-defined keys that could be added later - let key = step.key.map(\.baseName, { "grdb_\($0)" }) + let key = step.key.map(\.baseName) { "grdb_\($0)" } return SQLAssociationStep( key: key, @@ -195,9 +195,9 @@ public /* TODO: internal */ struct SQLAssociation { } } -extension SQLAssociation: KeyPathRefining { } +extension SQLAssociation: Refinable { } -struct SQLAssociationStep: KeyPathRefining { +struct SQLAssociationStep: Refinable { var key: SQLAssociationKey var condition: SQLAssociationCondition var relation: SQLRelation @@ -256,7 +256,7 @@ enum SQLAssociationCardinality { /// /// The SQLAssociationKey type aims at providing the necessary support for /// those various inflections. -enum SQLAssociationKey: KeyPathRefining { +enum SQLAssociationKey: Refinable { /// A key that is inflected in singular and plural contexts. /// /// For example: diff --git a/GRDB/QueryInterface/SQL/SQLQuery.swift b/GRDB/QueryInterface/SQL/SQLQuery.swift index 80890e010..e507a6957 100644 --- a/GRDB/QueryInterface/SQL/SQLQuery.swift +++ b/GRDB/QueryInterface/SQL/SQLQuery.swift @@ -13,85 +13,85 @@ struct SQLQuery { var limit: SQLLimit? } -extension SQLQuery: KeyPathRefining { - func distinct() -> SQLQuery { +extension SQLQuery: Refinable { + func distinct() -> Self { with(\.isDistinct, true) } - func expectingSingleResult() -> SQLQuery { + func expectingSingleResult() -> Self { with(\.expectsSingleResult, true) } - func limit(_ limit: Int, offset: Int? = nil) -> SQLQuery { + func limit(_ limit: Int, offset: Int? = nil) -> Self { with(\.limit, SQLLimit(limit: limit, offset: offset)) } - func qualified(with alias: TableAlias) -> SQLQuery { + func qualified(with alias: TableAlias) -> Self { // We do not need to qualify group and having clauses. They will be // in SQLQueryGenerator.init() - map(\.relation, { $0.qualified(with: alias) }) + map(\.relation) { $0.qualified(with: alias) } } } extension SQLQuery: SelectionRequest { - func select(_ selection: [SQLSelectable]) -> SQLQuery { - map(\.relation, { $0.select(selection) }) + func select(_ selection: [SQLSelectable]) -> Self { + map(\.relation) { $0.select(selection) } } - func annotated(with selection: [SQLSelectable]) -> SQLQuery { - map(\.relation, { $0.annotated(with: selection) }) + func annotated(with selection: [SQLSelectable]) -> Self { + map(\.relation) { $0.annotated(with: selection) } } } extension SQLQuery: FilteredRequest { - func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> SQLQuery { - map(\.relation, { $0.filter(predicate) }) + func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> Self { + map(\.relation) { $0.filter(predicate) } } } extension SQLQuery: OrderedRequest { - func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> SQLQuery { - map(\.relation, { $0.order(orderings) }) + func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> Self { + map(\.relation) { $0.order(orderings) } } - func reversed() -> SQLQuery { - map(\.relation, { $0.reversed() }) + func reversed() -> Self { + map(\.relation) { $0.reversed() } } - func unordered() -> SQLQuery { - map(\.relation, { $0.unordered() }) + func unordered() -> Self { + map(\.relation) { $0.unordered() } } } extension SQLQuery: AggregatingRequest { - func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> SQLQuery { + func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> Self { with(\.groupPromise, DatabasePromise { db in try expressions(db).map(\.sqlExpression) }) } - func having(_ predicate: SQLExpressible) -> SQLQuery { - mapInto(\.havingExpressions, { $0.append(predicate.sqlExpression) }) + func having(_ predicate: SQLExpressible) -> Self { + mapInto(\.havingExpressions) { $0.append(predicate.sqlExpression) } } } extension SQLQuery: _JoinableRequest { - func _including(all association: SQLAssociation) -> SQLQuery { - map(\.relation, { $0._including(all: association) }) + func _including(all association: SQLAssociation) -> Self { + map(\.relation) { $0._including(all: association) } } - func _including(optional association: SQLAssociation) -> SQLQuery { - map(\.relation, { $0._including(optional: association) }) + func _including(optional association: SQLAssociation) -> Self { + map(\.relation) { $0._including(optional: association) } } - func _including(required association: SQLAssociation) -> SQLQuery { - map(\.relation, { $0._including(required: association) }) + func _including(required association: SQLAssociation) -> Self { + map(\.relation) { $0._including(required: association) } } - func _joining(optional association: SQLAssociation) -> SQLQuery { - map(\.relation, { $0._joining(optional: association) }) + func _joining(optional association: SQLAssociation) -> Self { + map(\.relation) { $0._joining(optional: association) } } - func _joining(required association: SQLAssociation) -> SQLQuery { - map(\.relation, { $0._joining(required: association) }) + func _joining(required association: SQLAssociation) -> Self { + map(\.relation) { $0._joining(required: association) } } } diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index 3ef8e3b1d..7a70bf8b6 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -100,7 +100,7 @@ /// .filter(Column("continent") == "EU") /// .including(all: Country.citizens) struct SQLRelation { - struct Child: KeyPathRefining { + struct Child: Refinable { enum Kind { // Record.including(optional: association) case oneOptional @@ -166,10 +166,10 @@ struct SQLRelation { return child.relation.prefetchedAssociations.map { association in // Remove redundant pivot child let pivotKey = association.pivot.keyName - let child = child.map(\.relation, { relation in + let child = child.map(\.relation) { relation in assert(relation.children[pivotKey] != nil) return relation.removingChild(forKey: pivotKey) - }) + } return association.through(child.makeAssociationForKey(key)) } } @@ -177,44 +177,44 @@ struct SQLRelation { } } -extension SQLRelation: KeyPathRefining { - func select(_ selection: [SQLSelectable]) -> SQLRelation { +extension SQLRelation: Refinable { + func select(_ selection: [SQLSelectable]) -> Self { with(\.selection, selection) } /// Removes all selections from chidren - func selectOnly(_ selection: [SQLSelectable]) -> SQLRelation { + func selectOnly(_ selection: [SQLSelectable]) -> Self { self.with(\.selection, selection) .map(\.children, { $0.mapValues { $0.map(\.relation, { $0.selectOnly([]) }) } }) } - func annotated(with selection: [SQLSelectable]) -> SQLRelation { - mapInto(\.selection, { $0.append(contentsOf: selection) }) + func annotated(with selection: [SQLSelectable]) -> Self { + mapInto(\.selection) { $0.append(contentsOf: selection) } } - func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> SQLRelation { - map(\.filtersPromise, { filtersPromise in + func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> Self { + map(\.filtersPromise) { filtersPromise in filtersPromise.flatMap { filters in DatabasePromise { try filters + [predicate($0).sqlExpression] } } - }) + } } - func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> SQLRelation { + func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> Self { with(\.ordering, SQLRelation.Ordering(orderings: orderings)) } - func reversed() -> SQLRelation { + func reversed() -> Self { map(\.ordering, \.reversed) } - func unordered() -> SQLRelation { + func unordered() -> Self { self.with(\.ordering, SQLRelation.Ordering()) .map(\.children, { $0.mapValues { $0.map(\.relation, { $0.unordered() }) } }) } - func qualified(with alias: TableAlias) -> SQLRelation { - map(\.source, { $0.qualified(with: alias) }) + func qualified(with alias: TableAlias) -> Self { + map(\.source) { $0.qualified(with: alias) } } } @@ -251,7 +251,7 @@ extension SQLRelation { /// HasMany in the above examples, but also for indirect associations such /// as HasManyThrough, which have any number of pivot relations between the /// origin and the destination. - func appendingChild(for association: SQLAssociation, kind: SQLRelation.Child.Kind) -> SQLRelation { + func appendingChild(for association: SQLAssociation, kind: SQLRelation.Child.Kind) -> Self { // Preserve association cardinality in intermediate steps of // including(all:), and force desired cardinality otherwize let childCardinality = (kind == .allNotPrefetched) @@ -317,7 +317,7 @@ extension SQLRelation { } } - private func appendingChild(_ child: SQLRelation.Child, forKey key: String) -> SQLRelation { + private func appendingChild(_ child: SQLRelation.Child, forKey key: String) -> Self { var relation = self if let existingChild = relation.children.removeValue(forKey: key) { guard let mergedChild = existingChild.merged(with: child) else { @@ -334,33 +334,33 @@ extension SQLRelation { return relation } - func removingChild(forKey key: String) -> SQLRelation { - mapInto(\.children, { $0.removeValue(forKey: key) }) + func removingChild(forKey key: String) -> Self { + mapInto(\.children) { $0.removeValue(forKey: key) } } - func filteringChildren(_ included: (Child) throws -> Bool) rethrows -> SQLRelation { - try map(\.children, { try $0.filter { try included($1) } }) + func filteringChildren(_ included: (Child) throws -> Bool) rethrows -> Self { + try map(\.children) { try $0.filter { try included($1) } } } } extension SQLRelation: _JoinableRequest { - func _including(all association: SQLAssociation) -> SQLRelation { + func _including(all association: SQLAssociation) -> Self { appendingChild(for: association, kind: .allPrefetched) } - func _including(optional association: SQLAssociation) -> SQLRelation { + func _including(optional association: SQLAssociation) -> Self { appendingChild(for: association, kind: .oneOptional) } - func _including(required association: SQLAssociation) -> SQLRelation { + func _including(required association: SQLAssociation) -> Self { appendingChild(for: association, kind: .oneRequired) } - func _joining(optional association: SQLAssociation) -> SQLRelation { + func _joining(optional association: SQLAssociation) -> Self { appendingChild(for: association.map(\.destination.relation, { $0.select([]) }), kind: .oneOptional) } - func _joining(required association: SQLAssociation) -> SQLRelation { + func _joining(required association: SQLAssociation) -> Self { appendingChild(for: association.map(\.destination.relation, { $0.select([]) }), kind: .oneRequired) } } @@ -628,7 +628,7 @@ struct SQLAssociationCondition: Equatable { extension SQLRelation { /// Returns nil if relations can't be merged (conflict in source, joins...) - func merged(with other: SQLRelation) -> SQLRelation? { + func merged(with other: SQLRelation) -> Self? { guard let mergedSource = source.merged(with: other.source) else { // can't merge return nil @@ -699,7 +699,7 @@ extension SQLSource { extension SQLRelation.Child { /// Returns nil if joins can't be merged (conflict in condition, relation...) - func merged(with other: SQLRelation.Child) -> SQLRelation.Child? { + func merged(with other: SQLRelation.Child) -> Self? { guard condition == other.condition else { // can't merge return nil @@ -724,7 +724,7 @@ extension SQLRelation.Child { extension SQLRelation.Child.Kind { /// Returns nil if kinds can't be merged - func merged(with other: SQLRelation.Child.Kind) -> SQLRelation.Child.Kind? { + func merged(with other: SQLRelation.Child.Kind) -> Self? { switch (self, other) { case (.oneRequired, .oneRequired), (.oneRequired, .oneOptional), diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 67a3e8394..aaa1f88f8 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -565,13 +565,13 @@ private struct SQLQualifiedRelation { } /// Removes all selections from joins - func selectOnly(_ selection: [SQLSelectable]) -> SQLQualifiedRelation { + func selectOnly(_ selection: [SQLSelectable]) -> Self { self.with(\.sourceSelection, selection.map { $0.qualifiedSelectable(with: sourceAlias) }) .map(\.joins, { $0.mapValues { $0.selectOnly([]) } }) } } -extension SQLQualifiedRelation: KeyPathRefining { } +extension SQLQualifiedRelation: Refinable { } /// A "qualified" source, where all tables are identified with a table alias. private enum SQLQualifiedSource { diff --git a/GRDB/Utils/KeyPathRefining.swift b/GRDB/Utils/KeyPathRefining.swift deleted file mode 100644 index beda0ab41..000000000 --- a/GRDB/Utils/KeyPathRefining.swift +++ /dev/null @@ -1,38 +0,0 @@ -/// A marker protocol for types that allow keyPath refining. -/// -/// For example: -/// -/// struct Player: KeyPathRefining { -/// var name: String -/// var score: Int -/// } -/// -/// let player = Player(name: "Arthur", score: 1000) -/// player.with(\.score, 100) // Player(name: "Arthur", score: 100) -/// player.map(\.score, { $0 + 100 }) // Player(name: "Arthur", score: 1100) -protocol KeyPathRefining { } - -extension KeyPathRefining { - @inlinable - func with(_ keyPath: WritableKeyPath, _ value: T) -> Self { - var result = self - result[keyPath: keyPath] = value - return result - } - - @inlinable - func map(_ keyPath: WritableKeyPath, _ transform: (T) throws -> T) rethrows -> Self { - var result = self - result[keyPath: keyPath] = try transform(result[keyPath: keyPath]) - return result - } - - @inlinable - func mapInto(_ keyPath: WritableKeyPath, _ update: (inout T) throws -> Void) rethrows -> Self { - var result = self - try update(&result[keyPath: keyPath]) - return result - } -} - -extension Array: KeyPathRefining where Element: KeyPathRefining { } diff --git a/GRDB/Utils/Refinable.swift b/GRDB/Utils/Refinable.swift new file mode 100644 index 000000000..b5fe7ff02 --- /dev/null +++ b/GRDB/Utils/Refinable.swift @@ -0,0 +1,81 @@ +/// A marker protocol for refinable types. +/// +/// For example: +/// +/// struct Player { +/// var name: String +/// var score: Int +/// } +/// +/// extension Player: Refinable { } +/// +/// let player = Player(name: "Arthur", score: 1000) +/// player.with { $0.score = 100 } // Player(name: "Arthur", score: 100) +/// player.with(\.score, 100) // Player(name: "Arthur", score: 100) +/// player.map(\.score) { $0 + 100 } // Player(name: "Arthur", score: 1100) +protocol Refinable { } + +extension Refinable { + + /// Returns self modified with the *update* function. + /// + /// For example: + /// + /// let player = Player(name: "Arthur", score: 1000) + /// let newPlayer = player.with { + /// $0.score = 100 + /// } + /// newPlayer.name // "Arthur" + /// newPlayer.score // 100 + @inline(__always) + func with(_ update: (inout Self) throws -> Void) rethrows -> Self { + var result = self + try update(&result) + return result + } + + /// Returns self with the given value set on the given key path. + /// + /// For example: + /// + /// let player = Player(name: "Arthur", score: 1000) + /// let newPlayer = player.with(\.name, 1100) + /// newPlayer.name // "Arthur" + /// newPlayer.score // 1100 + @inline(__always) + func with(_ keyPath: WritableKeyPath, _ value: T) -> Self { + with { $0[keyPath: keyPath] = value } + } + + /// Returns self with the given transform function applied to the given key path. + /// + /// For example: + /// + /// let player = Player(name: "Arthur", score: 1000) + /// let newPlayer = player.map(\.name) { $0 + 100 } + /// newPlayer.name // "Arthur" + /// newPlayer.score // 1100 + @inline(__always) + func map(_ keyPath: WritableKeyPath, _ transform: (T) throws -> T) rethrows -> Self { + try with { + $0[keyPath: keyPath] = try transform($0[keyPath: keyPath]) + } + } + + /// Returns self with the given update function applied to the given key path. + /// + /// For example: + /// + /// let player = Player(name: "Arthur", scores: [100, 10]) + /// let newPlayer = player.mapInto(\.scores) { $0.append(1000) } + /// newPlayer.name // "Arthur" + /// newPlayer.scores // [100, 10, 1000] + @inline(__always) + func mapInto(_ keyPath: WritableKeyPath, _ update: (inout T) throws -> Void) rethrows -> Self { + try with { + try update(&$0[keyPath: keyPath]) + } + } +} + +extension Array: Refinable where Element: Refinable { } diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index 310265141..a5013d3d5 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -448,8 +448,8 @@ 56DF0016228DDB8300D611F3 /* AssociationPrefetchingCodableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF0013228DDB8200D611F3 /* AssociationPrefetchingCodableRecordTests.swift */; }; 56DF0017228DDB8300D611F3 /* AssociationPrefetchingRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF0014228DDB8200D611F3 /* AssociationPrefetchingRowTests.swift */; }; 56DF0018228DDB8300D611F3 /* AssociationPrefetchingRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF0014228DDB8200D611F3 /* AssociationPrefetchingRowTests.swift */; }; - 56DF37A723D77AA0009AAA05 /* KeyPathRefining.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF37A623D77AA0009AAA05 /* KeyPathRefining.swift */; }; - 56DF37A823D77AA0009AAA05 /* KeyPathRefining.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF37A623D77AA0009AAA05 /* KeyPathRefining.swift */; }; + 56DF37A723D77AA0009AAA05 /* Refinable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF37A623D77AA0009AAA05 /* Refinable.swift */; }; + 56DF37A823D77AA0009AAA05 /* Refinable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DF37A623D77AA0009AAA05 /* Refinable.swift */; }; 56E4F7F92392E2EE00A611F6 /* DatabaseAbortedTransactionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4F7F72392E2EE00A611F6 /* DatabaseAbortedTransactionTests.swift */; }; 56E4F7FA2392E2EE00A611F6 /* DatabaseAbortedTransactionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4F7F72392E2EE00A611F6 /* DatabaseAbortedTransactionTests.swift */; }; 56E9FAC42210468500C703A8 /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E9FAC32210468500C703A8 /* SQLInterpolation.swift */; }; @@ -1015,7 +1015,7 @@ 56DE7B101C3D93ED00861EB8 /* StatementArgumentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementArgumentsTests.swift; sourceTree = ""; }; 56DF0013228DDB8200D611F3 /* AssociationPrefetchingCodableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingCodableRecordTests.swift; sourceTree = ""; }; 56DF0014228DDB8200D611F3 /* AssociationPrefetchingRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingRowTests.swift; sourceTree = ""; }; - 56DF37A623D77AA0009AAA05 /* KeyPathRefining.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyPathRefining.swift; sourceTree = ""; }; + 56DF37A623D77AA0009AAA05 /* Refinable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Refinable.swift; sourceTree = ""; }; 56E4F7F72392E2EE00A611F6 /* DatabaseAbortedTransactionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAbortedTransactionTests.swift; sourceTree = ""; }; 56E8CE0C1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConvertibleFetchTests.swift; sourceTree = ""; }; 56E8CE0F1BB4FE5B00828BEC /* StatementColumnConvertibleFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementColumnConvertibleFetchTests.swift; sourceTree = ""; }; @@ -1451,11 +1451,11 @@ children = ( 563EF44C2161F196007DAACD /* Inflections.swift */, 569BBA4B229170B300478429 /* Inflections+English.swift */, - 56DF37A623D77AA0009AAA05 /* KeyPathRefining.swift */, 566BE7132342541F00A8254B /* LockedBox.swift */, 563EF41E215F8A76007DAACD /* OrderedDictionary.swift */, 5659F4971EA8D989004A4992 /* Pool.swift */, 5659F48F1EA8D964004A4992 /* ReadWriteBox.swift */, + 56DF37A623D77AA0009AAA05 /* Refinable.swift */, 5659F4871EA8D94E004A4992 /* Utils.swift */, ); path = Utils; @@ -2194,7 +2194,7 @@ 56A8C2351D1914540096E9D4 /* UUID.swift in Sources */, 5656A8822295BD56001FF3FF /* SQLCollection.swift in Sources */, 56D51D051EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */, - 56DF37A823D77AA0009AAA05 /* KeyPathRefining.swift in Sources */, + 56DF37A823D77AA0009AAA05 /* Refinable.swift in Sources */, F3BA80151CFB2876003DC1BA /* DatabaseWriter.swift in Sources */, F3BA800A1CFB286A003DC1BA /* Configuration.swift in Sources */, 5656A86C2295BD56001FF3FF /* HasManyAssociation.swift in Sources */, @@ -2528,7 +2528,7 @@ 56A8C2321D1914540096E9D4 /* UUID.swift in Sources */, 5656A8812295BD56001FF3FF /* SQLCollection.swift in Sources */, 56D51D021EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */, - 56DF37A723D77AA0009AAA05 /* KeyPathRefining.swift in Sources */, + 56DF37A723D77AA0009AAA05 /* Refinable.swift in Sources */, F3BA80711CFB2E55003DC1BA /* DatabaseWriter.swift in Sources */, F3BA80661CFB2E55003DC1BA /* Configuration.swift in Sources */, 5656A86B2295BD56001FF3FF /* HasManyAssociation.swift in Sources */,