From df7c57d9d944de7ed6508b1f6e668d3872fe93af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 29 Oct 2023 20:15:25 +0100 Subject: [PATCH] Fix column distinct --- .../wulkanowy/schools/dao/DistinctOn.kt | 24 +++++++++++++++++++ .../wulkanowy/schools/dao/LoginEventDao.kt | 5 +++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/io/github/wulkanowy/schools/dao/DistinctOn.kt 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) {