Skip to content

Commit

Permalink
feat: EXPOSED-681 Add support for GREATEST and LEAST functions
Browse files Browse the repository at this point in the history
Adds support for GREATEST(X, Y) and LEAST(X, Y) functions.
In SQLite, these are named MAX(X, Y) and MIN(X, Y), respectively.
(because you just had to be different, huh sqlite?)
  • Loading branch information
solonovamax committed Jan 9, 2025
1 parent e7ad4df commit 4c4cfff
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
24 changes: 24 additions & 0 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Op.kt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,30 @@ class IsDistinctFromOp(
}
}

/**
* Represents an SQL operator that returns the greatest (maximum) of [expr1] and [expr2].
*/
class GreatestOp<T, S : T>(
val expr1: Expression<T>,
val expr2: Expression<S>,
) : Op<T>() {
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder {
currentDialect.functionProvider.greatest(expr1, expr2, queryBuilder)
}
}

/**
* Represents an SQL operator that returns the least (minimum) of [expr1] and [expr2].
*/
class LeastOp<T, S : T>(
val expr1: Expression<T>,
val expr2: Expression<S>,
) : Op<T>() {
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder {
currentDialect.functionProvider.least(expr1, expr2, queryBuilder)
}
}

// Mathematical Operators

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,26 @@ interface ISqlExpressionBuilder {
other: ExpressionWithColumnType<E>
): IsDistinctFromOp = IsDistinctFromOp(this, other)

/**
* The greatest (maximum) of this expression and [t].
*/
infix fun <T> ExpressionWithColumnType<T>.greatest(t: T): GreatestOp<T, T> = GreatestOp(this, wrap(t))

/**
* The greatest (maximum) of this expression and [other].
*/
infix fun <T, S : T> ExpressionWithColumnType<T>.greatest(other: Expression<S>): GreatestOp<T, S> = GreatestOp(this, other)

/**
* The least (minimum) of this expression and [t].
*/
infix fun <T> ExpressionWithColumnType<T>.least(t: T): LeastOp<T, T> = LeastOp(this, wrap(t))

/**
* The least (minimum) of this expression and [other].
*/
infix fun <T, S : T> ExpressionWithColumnType<T>.least(other: Expression<S>): LeastOp<T, S> = LeastOp(this, other)

// Mathematical Operators

/** Adds the [t] value to this expression. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ abstract class FunctionProvider {
*/
open fun random(seed: Int?): String = "RANDOM(${seed?.toString().orEmpty()})"

/**
* SQL function that returns the greatest (maximum) of two values.
*
* @param expr1 The first value.
* @param expr2 The second value.
* @param queryBuilder Query builder to append the SQL function to.
*/
open fun <T, S : T> greatest(expr1: Expression<T>, expr2: Expression<S>, queryBuilder: QueryBuilder): Unit = queryBuilder {
append("GREATEST(", expr1, ", ", expr2, ")")
}

/**
* SQL function that returns the least (minimum) of two values.
*
* @param expr1 The first value.
* @param expr2 The second value.
* @param queryBuilder Query builder to append the SQL function to.
*/
open fun <T, S : T> least(expr1: Expression<T>, expr2: Expression<S>, queryBuilder: QueryBuilder): Unit = queryBuilder {
append("LEAST(", expr1, ", ", expr2, ")")
}

// String functions

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ internal object SQLiteDataTypeProvider : DataTypeProvider() {

@Suppress("TooManyFunctions")
internal object SQLiteFunctionProvider : FunctionProvider() {
override fun <T, S : T> greatest(expr1: Expression<T>, expr2: Expression<S>, queryBuilder: QueryBuilder) = queryBuilder {
append("MAX(", expr1, ", ", expr2, ")")
}

override fun <T, S : T> least(expr1: Expression<T>, expr2: Expression<S>, queryBuilder: QueryBuilder) = queryBuilder {
append("MIN(", expr1, ", ", expr2, ")")
}

override fun <T : String?> charLength(expr: Expression<T>, queryBuilder: QueryBuilder) = queryBuilder {
append("LENGTH(", expr, ")")
}
Expand Down

0 comments on commit 4c4cfff

Please sign in to comment.