Skip to content

Commit 4e73309

Browse files
Generic min max (pointfreeco#76)
* updated to QueryValue so for example we can return a date in an @selection * removed foundation import * used the optionalised functions from optional-helpers * wip --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent 51a9fde commit 4e73309

File tree

3 files changed

+49
-47
lines changed

3 files changed

+49
-47
lines changed

Sources/StructuredQueriesCore/AggregateFunctions.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ where QueryValue: _OptionalPromotable, QueryValue._Optionalized.Wrapped == Strin
8484
}
8585
}
8686

87-
extension QueryExpression where QueryValue: QueryBindable {
87+
extension QueryExpression where QueryValue: QueryBindable & _OptionalPromotable {
8888
/// A maximum aggregate of this expression.
8989
///
9090
/// ```swift
@@ -96,10 +96,11 @@ extension QueryExpression where QueryValue: QueryBindable {
9696
/// - Returns: A maximum aggregate of this expression.
9797
public func max(
9898
filter: (some QueryExpression<Bool>)? = Bool?.none
99-
) -> some QueryExpression<Int?> {
99+
) -> some QueryExpression<QueryValue._Optionalized.Wrapped?> {
100100
AggregateFunction("max", [queryFragment], filter: filter?.queryFragment)
101101
}
102102

103+
103104
/// A minimum aggregate of this expression.
104105
///
105106
/// ```swift
@@ -111,7 +112,7 @@ extension QueryExpression where QueryValue: QueryBindable {
111112
/// - Returns: A minimum aggregate of this expression.
112113
public func min(
113114
filter: (some QueryExpression<Bool>)? = Bool?.none
114-
) -> some QueryExpression<Int?> {
115+
) -> some QueryExpression<QueryValue._Optionalized.Wrapped?> {
115116
AggregateFunction("min", [queryFragment], filter: filter?.queryFragment)
116117
}
117118
}
@@ -170,7 +171,7 @@ where QueryValue: _OptionalPromotable, QueryValue._Optionalized.Wrapped: Numeric
170171
///
171172
/// ```swift
172173
/// Item.select { $0.price.total() }
173-
/// // SELECT sum("items"."price") FROM "items"
174+
/// // SELECT total("items"."price") FROM "items"
174175
/// ```
175176
///
176177
/// - Parameters:

Tests/StructuredQueriesTests/AggregateFunctionsTests.swift

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extension SnapshotTests {
1313
var name: String
1414
var isAdmin: Bool
1515
var age: Int?
16+
var birthDate: Date?
1617
}
1718

1819
@Test func average() {
@@ -93,45 +94,45 @@ extension SnapshotTests {
9394
}
9495
}
9596

96-
@Test func max() {
97-
assertInlineSnapshot(of: User.columns.id.max(), as: .sql) {
98-
"""
99-
max("users"."id")
100-
"""
101-
}
102-
assertQuery(Reminder.select { $0.id.max() }) {
103-
"""
104-
SELECT max("reminders"."id")
105-
FROM "reminders"
106-
"""
107-
} results: {
108-
"""
109-
────┐
110-
│ 10
111-
────┘
112-
"""
113-
}
114-
}
115-
116-
@Test func min() {
117-
assertInlineSnapshot(of: User.columns.id.min(), as: .sql) {
118-
"""
119-
min("users"."id")
120-
"""
121-
}
122-
assertQuery(Reminder.select { $0.priority.min() }) {
123-
"""
124-
SELECT min("reminders"."priority")
125-
FROM "reminders"
126-
"""
127-
} results: {
128-
"""
129-
───┐
130-
│ 1
131-
───┘
132-
"""
133-
}
134-
}
97+
@Test func max() {
98+
assertInlineSnapshot(of: User.columns.birthDate.max(), as: .sql) {
99+
"""
100+
max("users"."birthDate")
101+
"""
102+
}
103+
assertQuery(Reminder.select { $0.dueDate.max() }) {
104+
"""
105+
SELECT max("reminders"."dueDate")
106+
FROM "reminders"
107+
"""
108+
}results: {
109+
"""
110+
┌────────────────────────────────┐
111+
│ Date(2001-01-05T00:00:00.000Z)
112+
└────────────────────────────────┘
113+
"""
114+
}
115+
}
116+
117+
@Test func min() {
118+
assertInlineSnapshot(of: User.columns.birthDate.min(), as: .sql) {
119+
"""
120+
min("users"."birthDate")
121+
"""
122+
}
123+
assertQuery(Reminder.select { $0.dueDate.min() }) {
124+
"""
125+
SELECT min("reminders"."dueDate")
126+
FROM "reminders"
127+
"""
128+
} results: {
129+
"""
130+
┌────────────────────────────────┐
131+
│ Date(2000-06-25T00:00:00.000Z)
132+
└────────────────────────────────┘
133+
"""
134+
}
135+
}
135136

136137
@Test func sum() {
137138
assertInlineSnapshot(of: User.columns.id.sum(), as: .sql) {

Tests/StructuredQueriesTests/SelectTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,17 +1312,17 @@ extension SnapshotTests {
13121312
}
13131313
}
13141314
do {
1315-
let query: some Statement<Int?> = Reminder.select { $0.priority.flatMap { $0.max() } }
1315+
let query: some Statement<Priority?> = Reminder.select { $0.priority.flatMap { $0.max() } }
13161316
assertQuery(query) {
13171317
"""
13181318
SELECT max("reminders"."priority")
13191319
FROM "reminders"
13201320
"""
13211321
} results: {
13221322
"""
1323-
┌───┐
1324-
3
1325-
└───┘
1323+
┌───────
1324+
.high
1325+
└───────
13261326
"""
13271327
}
13281328
}

0 commit comments

Comments
 (0)