Fix column distinct

This commit is contained in:
Mikołaj Pich 2023-10-29 20:15:25 +01:00
parent 8dd7c7816f
commit df7c57d9d9
2 changed files with 28 additions and 1 deletions

View file

@ -0,0 +1,24 @@
package io.github.wulkanowy.schools.dao
import org.jetbrains.exposed.sql.BooleanColumnType
import org.jetbrains.exposed.sql.CustomFunction
import org.jetbrains.exposed.sql.Expression
import org.jetbrains.exposed.sql.QueryBuilder
fun customDistinctOn(vararg expressions: Expression<*>): CustomFunction<Boolean?> = customBooleanFunction(
functionName = "DISTINCT ON",
postfix = " TRUE",
params = expressions
)
fun customBooleanFunction(
functionName: String, postfix: String = "", vararg params: Expression<*>
): CustomFunction<Boolean?> =
object : CustomFunction<Boolean?>(functionName, BooleanColumnType(), *params) {
override fun toQueryBuilder(queryBuilder: QueryBuilder) {
super.toQueryBuilder(queryBuilder)
if (postfix.isNotEmpty()) {
queryBuilder.append(postfix)
}
}
}

View file

@ -29,8 +29,11 @@ class LoginEventDao {
order: SortOrder?, order: SortOrder?,
): List<LoginEvent> = dbQuery { ): List<LoginEvent> = dbQuery {
LoginEvents LoginEvents
.slice(
customDistinctOn(LoginEvents.schoolId, LoginEvents.symbol, LoginEvents.scraperBaseUrl),
*(LoginEvents.columns).toTypedArray()
)
.selectAll() .selectAll()
.groupBy(LoginEvents.schoolId, LoginEvents.symbol, LoginEvents.scraperBaseUrl)
.limit(pageSize, page * pageSize) .limit(pageSize, page * pageSize)
.let { .let {
if (orderBy != null && order != null) { if (orderBy != null && order != null) {