mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Add the min and max multi-argument functions
This commit is contained in:
1 parent
18eab53f74
commit
4065ff58e8
3 files changed
+60
-4
No files matched your search
@@ -2209,8 +2209,10 @@ extension SQLExpressible where Self == Column {
|
||||
/// - ``localizedLowercased``
|
||||
/// - ``localizedUppercased``
|
||||
/// - ``lowercased``
|
||||
/// - ``min(_:_:_:)``
|
||||
/// - ``min(_:)``
|
||||
/// - ``min(_:filter:)``
|
||||
/// - ``max(_:_:_:)``
|
||||
/// - ``max(_:)``
|
||||
/// - ``max(_:filter:)``
|
||||
/// - ``sum(_:)``
|
||||
|
||||
@@ -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.
|
||||
///
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user