Skip to content

Commit

Permalink
EVA-PIMP provide jooq context for too many rows exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Rattenkrieg committed Jan 15, 2024
1 parent 675031f commit ce24e83
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/CI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import java.lang.Boolean.parseBoolean
object Ci {

private const val SNAPSHOT_BASE = "0.6.0"
private const val RELEASE_VERSION = "0.5.4"
private const val RELEASE_VERSION = "0.5.5"
private val githubSha = System.getenv("GITHUB_SHA") ?: "latest"

val publishRelease = System.getProperty("release", "true").let(::parseBoolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ abstract class JooqBaseModelRepository<ID, MID, M, ME, R>(
jooqQuery = updateQuery,
table = table
)
}.getSingle(this::fromRecord) { throw IllegalStateException("Too many rows updated") }.second
}.getSingleOrNull(this::fromRecord) {
JooqQueryException(updateQuery, it, "Too many rows updated")
}

@Suppress("UNCHECKED_CAST")
when (updated) {
Expand Down Expand Up @@ -270,7 +272,7 @@ abstract class JooqBaseModelRepository<ID, MID, M, ME, R>(
}

private suspend fun <R : Record> exists(select: Select<R>): Boolean {
return oneRecord(dslContext.selectOne().whereExists(select)) != null
return atMostOneRecord(dslContext.selectOne().whereExists(select)) != null
}

protected suspend fun findOneWhere(condition: Condition): M? {
Expand All @@ -280,7 +282,7 @@ abstract class JooqBaseModelRepository<ID, MID, M, ME, R>(
}

private suspend fun findOne(select: Select<R>): M? {
return oneRecord(select)?.let { fromRecord(it) }
return atMostOneRecord(select)?.let { fromRecord(it) }
}

protected suspend fun findLast(
Expand Down Expand Up @@ -343,11 +345,11 @@ abstract class JooqBaseModelRepository<ID, MID, M, ME, R>(
}

protected suspend fun count(condition: Condition): Long {
return oneRecord(dslContext.select(LONG_COUNT).from(table).where(condition))?.value1() ?: 0
return atMostOneRecord(dslContext.select(LONG_COUNT).from(table).where(condition))?.value1() ?: 0
}

protected suspend fun countGrouped(condition: Condition, groupFields: Set<TableField<R, *>>): Long {
return oneRecord(
return atMostOneRecord(
dslContext
.select(LONG_COUNT)
.from(
Expand All @@ -359,10 +361,11 @@ abstract class JooqBaseModelRepository<ID, MID, M, ME, R>(
)?.value1() ?: 0
}

protected suspend fun <R : Record> oneRecord(select: Select<R>): R? {
protected suspend fun <R : Record> atMostOneRecord(select: Select<R>): R? {
return allRecords(select)
.getSingle({ it }) { throw IllegalStateException("Found more than one record") }
.second
.getSingleOrNull({ it }) {
JooqQueryException(select, it, "Found more than one record")
}
}

protected suspend fun <R : Record> allRecords(select: Select<R>): List<R> {
Expand All @@ -373,12 +376,12 @@ abstract class JooqBaseModelRepository<ID, MID, M, ME, R>(
)
}

private fun <T, K> List<T>.getSingle(mapper: (T) -> K, ex: () -> Exception): Pair<Int, K?> {
return this.fold(Pair<Int, K?>(0, null)) { (i, _), v ->
when (i) {
0 -> i.inc() to mapper(v)
else -> throw ex()
}
@Throws(JooqQueryException::class)
private fun <T, K> List<T>.getSingleOrNull(mapper: (T) -> K, ex: (List<T>) -> JooqQueryException): K? {
return when (size) {
0 -> null
1 -> mapper(first())
else -> throw ex(this)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.razz.eva.repository

import org.jooq.Query
import org.jooq.Record
import java.lang.IllegalStateException

class JooqQueryException(
val query: Query,
val records: List<Record>,
message: String,
) : IllegalStateException(message)

0 comments on commit ce24e83

Please sign in to comment.