-
Notifications
You must be signed in to change notification settings - Fork 695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to specify nullable column ordering behaviour #478
Comments
When will this enhancement be released? |
Okay, I found a way to archive class ColumnNullsLast(private val col: Column<*>) : Expression<String>() {
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder {
append(col, " IS NULL, ", col)
}
}
fun Column<*>.nullsLast() = ColumnNullsLast(this) Usage: query.orderBy(Users.age.nullsLast() to SortOrder.DESC) |
instead of |
NULLS FIRST/LAST use index in postgres. This solution for null first enum class NullOrder {
FIRST,
LAST
}
fun Query.orderBy(vararg expressions: Pair<NullsExpression, SortOrder>): Query {
return orderBy(
*expressions.map { (nullExpression, sortOrder) ->
Pair(OrderWithNull(nullExpression.expression, sortOrder, nullExpression.nullOrder), sortOrder)
}.toTypedArray()
)
}
fun Expression<*>.nullsFirst(): NullsExpression = NullsExpression(this, NullOrder.FIRST)
fun Expression<*>.nullsLast(): NullsExpression = NullsExpression(this, NullOrder.LAST)
class NullsExpression(val expression: Expression<*>, val nullOrder: NullOrder)
private class OrderWithNull(
private val expression: Expression<*>,
private val sortOrder: SortOrder,
private val nullOrder: NullOrder
) : Expression<String>() {
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder {
append(expression)
append(" ")
append(sortOrder.name)
when (nullOrder) {
NullOrder.FIRST -> append(" NULLS FIRST ")
NullOrder.LAST -> append(" NULLS LAST ")
}
append(", TRUE ")
}
}``` |
Hi, is there any progress on that? Regarding last comment it produces 'order by' query part as e.g |
When sorting a table in descending order while using PostgreSQL,
NULL
values appear on top. However, I would likeNULL
values to appear last. To solve this issue PostgreSQL hasNULLS LAST
andNULLS FIRST
options:However when using Exposed, there doesn't seem a way to specify this as I'm only able to supply a
vararg
ofPair<Column<*>, Boolean>
when ordering my queries:The text was updated successfully, but these errors were encountered: