Skip to content

Commit 2316fb3

Browse files
committed
Prefer key paths when possible
1 parent 98e2812 commit 2316fb3

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

Documentation/AssociationsBasics.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ struct BookInfo: Decodable, FetchableRecord {
939939
var country: String
940940
}
941941
let bookInfos = try Book
942-
.annotated(withRequired: Book.author.select { $0.country })
942+
.annotated(withRequired: Book.author.select(\.country)
943943
.asRequest(of: BookInfo.self)
944944
.fetchAll(db)
945945
```
@@ -1000,7 +1000,7 @@ struct BookInfo: Decodable, FetchableRecord {
10001000
var country: String?
10011001
}
10021002
let bookInfos = try Book
1003-
.annotated(withOptional: Book.author.select { $0.country })
1003+
.annotated(withOptional: Book.author.select(\.country))
10041004
.asRequest(of: BookInfo.self)
10051005
.fetchAll(db)
10061006
```
@@ -1083,7 +1083,7 @@ struct AuthorInfo: Decodable, FetchableRecord {
10831083
}
10841084
let authorInfos = try Author
10851085
.including(all: Author.books
1086-
.select { $0.title }
1086+
.select(\.title)
10871087
.forKey("bookTitles"))
10881088
.asRequest(of: AuthorInfo.self)
10891089
.fetchAll(db)
@@ -1350,7 +1350,7 @@ In this chapter, we take the reversed perspective. We list various shapes of dec
13501350

13511351
let authorInfos = try Author
13521352
.including(all: Author.books
1353-
.select { $0.title }
1353+
.select(\.title)
13541354
.forKey("bookTitles"))
13551355
.asRequest(of: AuthorInfo.self)
13561356
.fetchAll(db)
@@ -1677,7 +1677,7 @@ Associations support more refinements:
16771677
}
16781678

16791679
let distinctBookKinds = Author.books
1680-
.select { $0.kind }
1680+
.select(\.kind)
16811681
.distinct()
16821682
.forKey("bookKinds")
16831683

Documentation/WhyAdoptGRDB.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ try player.updateChanges {
207207

208208
// SELECT * FROM player ORDER BY score DESC LIMIT 10
209209
let bestPlayers: [Player] = try Player
210-
.order { $0.score.desc }
210+
.order(\.score.desc)
211211
.limit(10)
212212
.fetchAll(db)
213213

GRDB/Documentation.docc/RecordRecommendedPractices.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ Extensions to the `DerivableRequest` protocol can not change the type of request
395395
extension QueryInterfaceRequest<Author> {
396396
// Selects authors' name
397397
func selectName() -> QueryInterfaceRequest<String> {
398-
select { $0.name }
398+
select(\.name)
399399
}
400400
}
401401

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,7 +3302,7 @@ So don't miss the [SQL API](#sqlite-api).
33023302
**The query interface requests** let you fetch values from the database:
33033303

33043304
```swift
3305-
let request = Player.filter { $0.email != nil }.order { $0.name }
3305+
let request = Player.filter { $0.email != nil }.order(\.name)
33063306
let players = try request.fetchAll(db) // [Player]
33073307
let count = try request.fetchCount(db) // Int
33083308
```
@@ -3837,7 +3837,7 @@ GRDB comes with a Swift version of many SQLite [built-in operators](https://sqli
38373837

38383838
```swift
38393839
// SELECT * FROM player WHERE id IN (SELECT playerId FROM playerSelection)
3840-
let selectedPlayerIds = PlayerSelection.select { $0.playerId }
3840+
let selectedPlayerIds = PlayerSelection.select(\.playerId)
38413841
Player.filter { selectedPlayerIds.contains($0.id) }
38423842

38433843
// SELECT * FROM player WHERE id IN (SELECT playerId FROM playerSelection)

Tests/GRDBTests/AssociationAggregateTests.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,26 @@ class AssociationAggregateTests: GRDBTestCase {
121121
""")
122122
}
123123
}
124-
124+
125+
#if compiler(>=6.1)
126+
func testAggregateWithGroup_swift61() throws {
127+
let dbQueue = try makeDatabaseQueue()
128+
try dbQueue.read { db in
129+
let request = Team
130+
.select(\.name)
131+
.annotated(with: Team.players.count)
132+
.group(\.name)
133+
134+
try assertEqualSQL(db, request, """
135+
SELECT "team"."name", COUNT(DISTINCT "player"."id") AS "playerCount" \
136+
FROM "team" \
137+
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
138+
GROUP BY "team"."name"
139+
""")
140+
}
141+
}
142+
#endif
143+
125144
func testAnnotatedWithHasManyDefaultAverage() throws {
126145
let dbQueue = try makeDatabaseQueue()
127146
try dbQueue.read { db in

0 commit comments

Comments
 (0)