diff --git a/src/main/kotlin/io/github/wulkanowy/schools/dao/DistinctOn.kt b/src/main/kotlin/io/github/wulkanowy/schools/dao/DistinctOn.kt new file mode 100644 index 0000000..8131242 --- /dev/null +++ b/src/main/kotlin/io/github/wulkanowy/schools/dao/DistinctOn.kt @@ -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 = customBooleanFunction( + functionName = "DISTINCT ON", + postfix = " TRUE", + params = expressions +) + +fun customBooleanFunction( + functionName: String, postfix: String = "", vararg params: Expression<*> +): CustomFunction = + object : CustomFunction(functionName, BooleanColumnType(), *params) { + override fun toQueryBuilder(queryBuilder: QueryBuilder) { + super.toQueryBuilder(queryBuilder) + if (postfix.isNotEmpty()) { + queryBuilder.append(postfix) + } + } + } diff --git a/src/main/kotlin/io/github/wulkanowy/schools/dao/LoginEventDao.kt b/src/main/kotlin/io/github/wulkanowy/schools/dao/LoginEventDao.kt index 59438b7..44f15bd 100644 --- a/src/main/kotlin/io/github/wulkanowy/schools/dao/LoginEventDao.kt +++ b/src/main/kotlin/io/github/wulkanowy/schools/dao/LoginEventDao.kt @@ -29,8 +29,11 @@ class LoginEventDao { order: SortOrder?, ): List = dbQuery { LoginEvents + .slice( + customDistinctOn(LoginEvents.schoolId, LoginEvents.symbol, LoginEvents.scraperBaseUrl), + *(LoginEvents.columns).toTypedArray() + ) .selectAll() - .groupBy(LoginEvents.schoolId, LoginEvents.symbol, LoginEvents.scraperBaseUrl) .limit(pageSize, page * pageSize) .let { if (orderBy != null && order != null) {