Mario Andhika
09/24/2024, 7:35 AMOleg Babichev
09/24/2024, 10:46 AMwithDistinct()
but looks like it adds only DISTINCT
key word without possibility to specify column.
Created the issue on YT to implement it later: EXPOSED-560 Support DISTINCT ON from PostgresChantal Loncle
09/24/2024, 11:58 AMfun Column<*>.distinctOn(vararg extraColumns: Column<*>) =
DistinctOn(this, extraColumns)
class DistinctOn<T>(
val expr: Column<T>,
val columns: Array<out Column<*>>
) : Function<T>(expr.columnType) {
override fun toQueryBuilder(queryBuilder: QueryBuilder) {
queryBuilder {
val txm = TransactionManager.current()
val distinctNames = listOf(expr, *columns).joinToString { txm.fullIdentity(it) }
append("DISTINCT ON ($distinctNames) ${txm.fullIdentity(expr)}")
}
}
}
Sessions
.innerJoin(Users)
.select(Sessions.user.distinctOn(Sessions.name), Users.id)
// SELECT
// DISTINCT ON (sessions."userId", sessions."name") sessions."userId", users.id
// FROM sessions INNER JOIN ...
Mario Andhika
09/24/2024, 12:38 PMthis class does not have a constructor
with that code sample though. I’m on exposed 0.53.0Chantal Loncle
09/24/2024, 12:57 PMFunction
being used is the Exposed one, not the Kotlin one. IDE likes to choose the Kotlin one when code is copy-pasted.Mario Andhika
09/24/2024, 1:20 PM