Skip to content

Commit 0a3e37d

Browse files
committed
feat: Apply suggested changes
- Inherit from Function instead of Op - Make both functions accept varargs - Don't use an infix function for the expression builder Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
1 parent 6bd5445 commit 0a3e37d

5 files changed

Lines changed: 100 additions & 44 deletions

File tree

exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Function.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,44 @@ class Random(
6565
}
6666
}
6767

68+
/**
69+
* Represents an SQL operator that returns the greatest (maximum, largest
70+
* value) of [expr1], [expr2], and [others].
71+
*
72+
* **Note:** The semantics of this function when applied to null values
73+
* or values of multiple types depends on the vendor. Please check the
74+
* documentation for your respective database.
75+
*/
76+
class Greatest<T>(
77+
val expr1: Expression<in T>,
78+
val expr2: Expression<in T>,
79+
columnType: IColumnType<T & Any>,
80+
vararg val others: Expression<in T>
81+
) : Function<T>(columnType) {
82+
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder {
83+
currentDialect.functionProvider.greatest(queryBuilder, expr1, expr2, *others)
84+
}
85+
}
86+
87+
/**
88+
* Represents an SQL operator that returns the least (minimum, smallest
89+
* value) of [expr1], [expr2], and [others].
90+
*
91+
* **Note:** The semantics of this function when applied to null values
92+
* or values of multiple types depends on the vendor. Please check the
93+
* documentation for your respective database.
94+
*/
95+
class Least<T>(
96+
val expr1: Expression<in T>,
97+
val expr2: Expression<in T>,
98+
columnType: IColumnType<T & Any>,
99+
vararg val others: Expression<in T>
100+
) : Function<T>(columnType) {
101+
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder {
102+
currentDialect.functionProvider.least(queryBuilder, expr1, expr2, *others)
103+
}
104+
}
105+
68106
// String Functions
69107

70108
/**

exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Op.kt

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -299,30 +299,6 @@ class IsDistinctFromOp(
299299
}
300300
}
301301

302-
/**
303-
* Represents an SQL operator that returns the greatest (maximum) of [expr1] and [expr2].
304-
*/
305-
class GreatestOp<T, S : T>(
306-
val expr1: Expression<T>,
307-
val expr2: Expression<S>,
308-
) : Op<T>() {
309-
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder {
310-
currentDialect.functionProvider.greatest(expr1, expr2, queryBuilder)
311-
}
312-
}
313-
314-
/**
315-
* Represents an SQL operator that returns the least (minimum) of [expr1] and [expr2].
316-
*/
317-
class LeastOp<T, S : T>(
318-
val expr1: Expression<T>,
319-
val expr2: Expression<S>,
320-
) : Op<T>() {
321-
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder {
322-
currentDialect.functionProvider.least(expr1, expr2, queryBuilder)
323-
}
324-
}
325-
326302
// Mathematical Operators
327303

328304
/**

exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,24 +580,60 @@ interface ISqlExpressionBuilder {
580580
): IsDistinctFromOp = IsDistinctFromOp(this, other)
581581

582582
/**
583-
* The greatest (maximum) of this expression and [t].
583+
* The greatest (maximum, largest value) of [expr1], [expr2], and [others].
584+
*
585+
* **Note:** The semantics of this function when applied to null values
586+
* or values of multiple types depends on the vendor. Please check the
587+
* documentation for your respective database.
584588
*/
585-
infix fun <T> ExpressionWithColumnType<T>.greatest(t: T): GreatestOp<T, T> = GreatestOp(this, wrap(t))
589+
fun <T> greatest(
590+
expr1: ExpressionWithColumnType<T>,
591+
expr2: Expression<in T>,
592+
vararg others: Expression<in T>,
593+
): Greatest<T> = Greatest(expr1, expr2, expr1.columnType, *others)
586594

587595
/**
588-
* The greatest (maximum) of this expression and [other].
596+
* The greatest (maximum, largest value) of [literal], [expr1], [expr2],
597+
* and [others].
598+
*
599+
* **Note:** The semantics of this function when applied to null values
600+
* or values of multiple types depends on the vendor. Please check the
601+
* documentation for your respective database.
589602
*/
590-
infix fun <T, S : T> ExpressionWithColumnType<T>.greatest(other: Expression<S>): GreatestOp<T, S> = GreatestOp(this, other)
603+
fun <T> greatest(
604+
literal: T,
605+
expr1: ExpressionWithColumnType<T>,
606+
expr2: Expression<in T>,
607+
vararg others: Expression<in T>,
608+
): Greatest<T> = Greatest(expr1, expr2, expr1.columnType, *others, expr1.wrap(literal))
591609

592610
/**
593-
* The least (minimum) of this expression and [t].
611+
* The least (minimum, smallest value) of [expr1], [expr2], and [others].
612+
*
613+
* **Note:** The semantics of this function when applied to null values
614+
* or values of multiple types depends on the vendor. Please check the
615+
* documentation for your respective database.
594616
*/
595-
infix fun <T> ExpressionWithColumnType<T>.least(t: T): LeastOp<T, T> = LeastOp(this, wrap(t))
617+
fun <T> least(
618+
expr1: ExpressionWithColumnType<T>,
619+
expr2: Expression<in T>,
620+
vararg others: Expression<in T>,
621+
): Least<T> = Least(expr1, expr2, expr1.columnType, *others)
596622

597623
/**
598-
* The least (minimum) of this expression and [other].
624+
* The least (minimum, smallest value) of [literal], [expr1], [expr2], and
625+
* [others].
626+
*
627+
* **Note:** The semantics of this function when applied to null values
628+
* or values of multiple types depends on the vendor. Please check the
629+
* documentation for your respective database.
599630
*/
600-
infix fun <T, S : T> ExpressionWithColumnType<T>.least(other: Expression<S>): LeastOp<T, S> = LeastOp(this, other)
631+
fun <T> least(
632+
literal: T,
633+
expr1: ExpressionWithColumnType<T>,
634+
expr2: Expression<in T>,
635+
vararg others: Expression<in T>,
636+
): Least<T> = Least(expr1, expr2, expr1.columnType, *others, expr1.wrap(literal))
601637

602638
// Mathematical Operators
603639

exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/FunctionProvider.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,25 @@ abstract class FunctionProvider {
3737
/**
3838
* SQL function that returns the greatest (maximum) of two values.
3939
*
40-
* @param expr1 The first value.
41-
* @param expr2 The second value.
4240
* @param queryBuilder Query builder to append the SQL function to.
41+
* @param expr The expressions to compare.
4342
*/
44-
open fun <T, S : T> greatest(expr1: Expression<T>, expr2: Expression<S>, queryBuilder: QueryBuilder): Unit = queryBuilder {
45-
append("GREATEST(", expr1, ", ", expr2, ")")
43+
open fun <T> greatest(queryBuilder: QueryBuilder, vararg expr: Expression<in T>): Unit = queryBuilder {
44+
append("GREATEST(")
45+
expr.appendTo { +it }
46+
append(")")
4647
}
4748

4849
/**
4950
* SQL function that returns the least (minimum) of two values.
5051
*
51-
* @param expr1 The first value.
52-
* @param expr2 The second value.
5352
* @param queryBuilder Query builder to append the SQL function to.
53+
* @param expr The expressions to compare.
5454
*/
55-
open fun <T, S : T> least(expr1: Expression<T>, expr2: Expression<S>, queryBuilder: QueryBuilder): Unit = queryBuilder {
56-
append("LEAST(", expr1, ", ", expr2, ")")
55+
open fun <T> least(queryBuilder: QueryBuilder, vararg expr: Expression<in T>): Unit = queryBuilder {
56+
append("LEAST(")
57+
expr.appendTo { +it }
58+
append(")")
5759
}
5860

5961
// String functions

exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/SQLiteDialect.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ internal object SQLiteDataTypeProvider : DataTypeProvider() {
2525

2626
@Suppress("TooManyFunctions")
2727
internal object SQLiteFunctionProvider : FunctionProvider() {
28-
override fun <T, S : T> greatest(expr1: Expression<T>, expr2: Expression<S>, queryBuilder: QueryBuilder) = queryBuilder {
29-
append("MAX(", expr1, ", ", expr2, ")")
28+
override fun <T> greatest(queryBuilder: QueryBuilder, vararg expr: Expression<in T>) = queryBuilder {
29+
append("MAX(")
30+
expr.appendTo { +it }
31+
append(")")
3032
}
3133

32-
override fun <T, S : T> least(expr1: Expression<T>, expr2: Expression<S>, queryBuilder: QueryBuilder) = queryBuilder {
33-
append("MIN(", expr1, ", ", expr2, ")")
34+
override fun <T> least(queryBuilder: QueryBuilder, vararg expr: Expression<in T>) = queryBuilder {
35+
append("MIN(")
36+
expr.appendTo { +it }
37+
append(")")
3438
}
3539

3640
override fun <T : String?> charLength(expr: Expression<T>, queryBuilder: QueryBuilder) = queryBuilder {

0 commit comments

Comments
 (0)