From 18eab53f74800a90aee6852faaa1e8fec5997352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 15 Mar 2025 09:51:32 +0100 Subject: [PATCH 1/2] Document aggregate functions --- GRDB/QueryInterface/SQL/SQLFunctions.swift | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/GRDB/QueryInterface/SQL/SQLFunctions.swift b/GRDB/QueryInterface/SQL/SQLFunctions.swift index 8f3ccfea4..a33669218 100644 --- a/GRDB/QueryInterface/SQL/SQLFunctions.swift +++ b/GRDB/QueryInterface/SQL/SQLFunctions.swift @@ -11,7 +11,7 @@ public func abs(_ value: some SQLSpecificExpressible) -> SQLExpression { } #if GRDBCUSTOMSQLITE || GRDBCIPHER -/// The `AVG` SQL function. +/// The `AVG` SQL aggregate function. /// /// For example: /// @@ -26,7 +26,7 @@ public func average( .aggregateFunction("AVG", [value.sqlExpression], filter: filter?.sqlExpression) } #else -/// The `AVG` SQL function. +/// The `AVG` SQL aggregate function. /// /// For example: /// @@ -44,7 +44,7 @@ public func average( filter: filter.sqlExpression) } -/// The `AVG` SQL function. +/// The `AVG` SQL aggregate function. /// /// For example: /// @@ -148,7 +148,7 @@ public func length(_ value: some SQLSpecificExpressible) -> SQLExpression { } #if GRDBCUSTOMSQLITE || GRDBCIPHER -/// The `MAX` SQL function. +/// The `MAX` SQL aggregate function. /// /// For example: /// @@ -163,7 +163,7 @@ public func max( .aggregateFunction("MAX", [value.sqlExpression], filter: filter?.sqlExpression) } #else -/// The `MAX` SQL function. +/// The `MAX` SQL aggregate function. /// /// For example: /// @@ -179,7 +179,7 @@ public func max( .aggregateFunction("MAX", [value.sqlExpression], filter: filter.sqlExpression) } -/// The `MAX` SQL function. +/// The `MAX` SQL aggregate function. /// /// For example: /// @@ -193,7 +193,7 @@ public func max(_ value: some SQLSpecificExpressible) -> SQLExpression { #endif #if GRDBCUSTOMSQLITE || GRDBCIPHER -/// The `MIN` SQL function. +/// The `MIN` SQL aggregate function. /// /// For example: /// @@ -208,7 +208,7 @@ public func min( .aggregateFunction("MIN", [value.sqlExpression], filter: filter?.sqlExpression) } #else -/// The `MIN` SQL function. +/// The `MIN` SQL aggregate function. /// /// For example: /// @@ -224,7 +224,7 @@ public func min( .aggregateFunction("MIN", [value.sqlExpression], filter: filter.sqlExpression) } -/// The `MIN` SQL function. +/// The `MIN` SQL aggregate function. /// /// For example: /// @@ -238,7 +238,7 @@ public func min(_ value: some SQLSpecificExpressible) -> SQLExpression { #endif #if GRDBCUSTOMSQLITE || GRDBCIPHER -/// The `SUM` SQL function. +/// The `SUM` SQL aggregate function. /// /// For example: /// @@ -262,7 +262,7 @@ public func sum( filter: filter?.sqlExpression) } #else -/// The `SUM` SQL function. +/// The `SUM` SQL aggregate function. /// /// For example: /// @@ -284,7 +284,7 @@ public func sum( filter: filter.sqlExpression) } -/// The `SUM` SQL function. +/// The `SUM` SQL aggregate function. /// /// For example: /// @@ -302,7 +302,7 @@ public func sum(_ value: some SQLSpecificExpressible) -> SQLExpression { #endif #if GRDBCUSTOMSQLITE || GRDBCIPHER -/// The `TOTAL` SQL function. +/// The `TOTAL` SQL aggregate function. /// /// For example: /// @@ -326,7 +326,7 @@ public func total( filter: filter?.sqlExpression) } #else -/// The `TOTAL` SQL function. +/// The `TOTAL` SQL aggregate function. /// /// For example: /// @@ -348,7 +348,7 @@ public func total( filter: filter.sqlExpression) } -/// The `TOTAL` SQL function. +/// The `TOTAL` SQL aggregate function. /// /// For example: /// From 4065ff58e83adee4ac6ae8b03e618edbd5554817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 15 Mar 2025 10:02:24 +0100 Subject: [PATCH 2/2] Add the min and max multi-argument functions --- GRDB/QueryInterface/SQL/SQLExpression.swift | 2 ++ GRDB/QueryInterface/SQL/SQLFunctions.swift | 32 +++++++++++++++++++ .../QueryInterfaceExpressionsTests.swift | 30 ++++++++++++++--- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/GRDB/QueryInterface/SQL/SQLExpression.swift b/GRDB/QueryInterface/SQL/SQLExpression.swift index ecc6da2d3..a3c0ea0e2 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression.swift @@ -2209,8 +2209,10 @@ extension SQLExpressible where Self == Column { /// - ``localizedLowercased`` /// - ``localizedUppercased`` /// - ``lowercased`` +/// - ``min(_:_:_:)`` /// - ``min(_:)`` /// - ``min(_:filter:)`` +/// - ``max(_:_:_:)`` /// - ``max(_:)`` /// - ``max(_:filter:)`` /// - ``sum(_:)`` diff --git a/GRDB/QueryInterface/SQL/SQLFunctions.swift b/GRDB/QueryInterface/SQL/SQLFunctions.swift index a33669218..b30fe89b5 100644 --- a/GRDB/QueryInterface/SQL/SQLFunctions.swift +++ b/GRDB/QueryInterface/SQL/SQLFunctions.swift @@ -147,6 +147,22 @@ public func length(_ value: some SQLSpecificExpressible) -> SQLExpression { .function("LENGTH", [value.sqlExpression]) } +/// The `MAX` SQL multi-argument function. +/// +/// For example: +/// +/// ```swift +/// // MAX(score, 1000) +/// max(Column("score"), 1000) +/// ``` +public func max( + _ value1: any SQLSpecificExpressible, + _ value2: any SQLExpressible, + _ values: any SQLExpressible... +) -> SQLExpression { + .simpleFunction("MAX", [value1.sqlExpression, value2.sqlExpression] + values.map(\.sqlExpression)) +} + #if GRDBCUSTOMSQLITE || GRDBCIPHER /// The `MAX` SQL aggregate function. /// @@ -192,6 +208,22 @@ public func max(_ value: some SQLSpecificExpressible) -> SQLExpression { } #endif +/// The `MIN` SQL multi-argument function. +/// +/// For example: +/// +/// ```swift +/// // MIN(score, 1000) +/// min(Column("score"), 1000) +/// ``` +public func min( + _ value1: any SQLSpecificExpressible, + _ value2: any SQLExpressible, + _ values: any SQLExpressible... +) -> SQLExpression { + .simpleFunction("MIN", [value1.sqlExpression, value2.sqlExpression] + values.map(\.sqlExpression)) +} + #if GRDBCUSTOMSQLITE || GRDBCIPHER /// The `MIN` SQL aggregate function. /// diff --git a/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift b/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift index c5855e232..be045623c 100644 --- a/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift +++ b/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift @@ -1559,7 +1559,18 @@ class QueryInterfaceExpressionsTests: GRDBTestCase { "SELECT LENGTH(\"name\") FROM \"readers\"") } - func testMinExpression() throws { + func testMultiArgumentMinExpression() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.select(min(Col.age, 1000))), + "SELECT MIN(\"age\", 1000) FROM \"readers\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.select(min(Col.age, 1000, Col.id))), + "SELECT MIN(\"age\", 1000, \"id\") FROM \"readers\"") + } + + func testAggregateMinExpression() throws { let dbQueue = try makeDatabaseQueue() XCTAssertEqual( @@ -1570,7 +1581,7 @@ class QueryInterfaceExpressionsTests: GRDBTestCase { "SELECT MIN(\"age\" / 2) FROM \"readers\"") } - func testMinExpression_filter() throws { + func testAggregateMinExpression_filter() throws { #if GRDBCUSTOMSQLITE || GRDBCIPHER // Prevent SQLCipher failures guard Database.sqliteLibVersionNumber >= 3030000 else { @@ -1592,7 +1603,18 @@ class QueryInterfaceExpressionsTests: GRDBTestCase { "SELECT MIN(\"age\" / 2) FILTER (WHERE \"age\" > 0) FROM \"readers\"") } - func testMaxExpression() throws { + func testMultiArgumentMaxExpression() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.select(max(Col.age, 1000))), + "SELECT MAX(\"age\", 1000) FROM \"readers\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.select(max(Col.age, 1000, Col.id))), + "SELECT MAX(\"age\", 1000, \"id\") FROM \"readers\"") + } + + func testAggregateMaxExpression() throws { let dbQueue = try makeDatabaseQueue() XCTAssertEqual( @@ -1603,7 +1625,7 @@ class QueryInterfaceExpressionsTests: GRDBTestCase { "SELECT MAX(\"age\" / 2) FROM \"readers\"") } - func testMaxExpression_filter() throws { + func testAggregateMaxExpression_filter() throws { #if GRDBCUSTOMSQLITE || GRDBCIPHER // Prevent SQLCipher failures guard Database.sqliteLibVersionNumber >= 3030000 else {