Add ABS and LENGTH SQL functions on association aggregates

This commit is contained in:
Gwendal Roué committed 2021-12-12 14:27:41 +01:00
1 parent 95b3f447e4
commit 8ba89a398a
4 files changed
+87 -2

No files matched your search

+1
View File
@@ -86,6 +86,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
## Next Release
- **New**: `DatabaseRegion(table: tableName)` is deprecated. Use `Table(tableName)` instead.
- **New**: The `ABS` and `LENGTH` SQL functions are now available on association aggregates, through `abs(aggregate)` and `length(aggregate)`.
## 5.16.0
+17
View File
@@ -2653,6 +2653,23 @@ Aggregates can be modified and combined with Swift operators:
let request = Team.annotated(with: Team.players.min(Column("score")) ?? 0)
```
- SQL functions `ABS` and `LENGTH` are available as the `abs` and `length` Swift functions:
<details>
<summary>SQL</summary>
```sql
SELECT "team".*, ABS(MAX("player"."score"))
FROM "team"
LEFT JOIN "player" ON ("player"."teamId" = "team"."id")
GROUP BY "team"."id"
```
</details>
```swift
let request = Team.annotated(with: abs(Team.players.max(Column("score"))))
```
### Isolation of Multiple Aggregates
@@ -1095,5 +1095,22 @@ public func ?? <RowDecoder>(
.with { $0.key = lhs.key } // Preserve key
}
// TODO: add support for ABS(aggregate)
// TODO: add support for LENGTH(aggregate)
// MARK: - ABS(...)
/// Returns an aggregate that evaluates to the absolute value of the
/// input aggregate.
public func abs<RowDecoder>(_ aggregate: AssociationAggregate<RowDecoder>)
-> AssociationAggregate<RowDecoder>
{
aggregate.map(abs)
}
// MARK: - LENGTH(...)
/// Returns an aggregate that evaluates the `LENGTH` SQL function on the
/// input aggregate.
public func length<RowDecoder>(_ aggregate: AssociationAggregate<RowDecoder>)
-> AssociationAggregate<RowDecoder>
{
aggregate.map(length)
}
@@ -1412,4 +1412,54 @@ class AssociationAggregateTests: GRDBTestCase {
XCTAssertEqual(teamInfos[3].minPlayerScore, 0)
}
}
func testAbs() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
do {
let request = Team.annotated(with: abs(Team.players.max(Column("score"))))
try assertEqualSQL(db, request, """
SELECT "team".*, ABS(MAX("player"."score")) \
FROM "team" \
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
GROUP BY "team"."id"
""")
}
do {
let request = Team.annotated(with: abs(Team.players.max(Column("score"))).forKey("foo"))
try assertEqualSQL(db, request, """
SELECT "team".*, ABS(MAX("player"."score")) AS "foo" \
FROM "team" \
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
GROUP BY "team"."id"
""")
}
}
}
func testLength() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
do {
// This is a meaningless request, but this is enough for this test
let request = Team.annotated(with: length(Team.players.max(Column("score"))))
try assertEqualSQL(db, request, """
SELECT "team".*, LENGTH(MAX("player"."score")) \
FROM "team" \
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
GROUP BY "team"."id"
""")
}
do {
// This is a meaningless request, but this is enough for this test
let request = Team.annotated(with: length(Team.players.max(Column("score"))).forKey("foo"))
try assertEqualSQL(db, request, """
SELECT "team".*, LENGTH(MAX("player"."score")) AS "foo" \
FROM "team" \
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
GROUP BY "team"."id"
""")
}
}
}
}