Skip to content

Commit

Permalink
fix: Remove false warning log
Browse files Browse the repository at this point in the history
-This warning was first eliminated here https://github.com/JetBrains/Exposed/pull/541/files. Then it was accidentally introduced again here 77f11a9 because of the change made to the getOrNull function.
-A new dependency was added (io.github.hakky54:logcaptor) to assert that no logging took place
  • Loading branch information
joc-a committed Aug 25, 2023
1 parent b00f1d8 commit eeda917
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
44 changes: 24 additions & 20 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ResultRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,34 @@ class ResultRow(
*
* @see [getOrNull] to get null in the cases an exception would be thrown
*/
operator fun <T> get(expression: Expression<T>): T {
operator fun <T> get(expression: Expression<T>): T = getInternal(expression, checkNullability = true)

operator fun <T> set(expression: Expression<out T>, value: T) {
setInternal(expression, value)
lookUpCache.remove(expression)
}

private fun <T> setInternal(expression: Expression<out T>, value: T) {
val index = getExpressionIndex(expression)
data[index] = value
}

fun <T> hasValue(expression: Expression<T>): Boolean = fieldIndex[expression]?.let { data[it] != NotInitializedValue } ?: false

fun <T> getOrNull(expression: Expression<T>): T? = if (hasValue(expression)) getInternal(expression, checkNullability = false) else null

private fun <T> getInternal(expression: Expression<T>, checkNullability: Boolean): T {
if (expression in lookUpCache) return lookUpCache[expression] as T

val d = getRaw(expression)

if (d == null && expression is Column<*> && expression.dbDefaultValue != null && !expression.columnType.nullable) {
exposedLogger.warn(
"Column ${TransactionManager.current().fullIdentity(expression)} is marked as not null, " +
"has default db value, but returns null. Possible have to re-read it from DB."
)
if (checkNullability) {
if (d == null && expression is Column<*> && expression.dbDefaultValue != null && !expression.columnType.nullable) {
exposedLogger.warn(
"Column ${TransactionManager.current().fullIdentity(expression)} is marked as not null, " +
"has default db value, but returns null. Possible have to re-read it from DB."
)
}
}

val result = database?.dialect?.let {
Expand All @@ -40,20 +58,6 @@ class ResultRow(
return result
}

operator fun <T> set(expression: Expression<out T>, value: T) {
setInternal(expression, value)
lookUpCache.remove(expression)
}

private fun <T> setInternal(expression: Expression<out T>, value: T) {
val index = getExpressionIndex(expression)
data[index] = value
}

fun <T> hasValue(expression: Expression<T>): Boolean = fieldIndex[expression]?.let { data[it] != NotInitializedValue } ?: false

fun <T> getOrNull(expression: Expression<T>): T? = if (hasValue(expression)) get(expression) else null

@Suppress("UNCHECKED_CAST")
private fun <T> rawToColumnValue(raw: T?, expression: Expression<T>): T {
return when {
Expand Down
1 change: 1 addition & 0 deletions exposed-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
testCompileOnly("com.impossibl.pgjdbc-ng", "pgjdbc-ng", Versions.postgreNG)
compileOnly("com.h2database", "h2", Versions.h2)
testCompileOnly("org.xerial", "sqlite-jdbc", Versions.sqlLite3)
testImplementation("io.github.hakky54:logcaptor:2.9.0")
}

tasks.withType<Test>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.exposed.sql.tests.shared.dml

import nl.altindag.log.LogCaptor
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
Expand Down Expand Up @@ -190,7 +191,10 @@ class JoinTests : DatabaseTestsBase() {
}
}

@Test fun testNoWarningsOnLeftJoinRegression() {
@Test
fun testNoWarningsOnLeftJoinRegression() {
val logCaptor = LogCaptor.forName(exposedLogger.name)

val MainTable = object : Table("maintable") {
val id = integer("idCol")
}
Expand All @@ -208,7 +212,9 @@ class JoinTests : DatabaseTestsBase() {
.single()
.getOrNull(JoinTable.data)

// Assert no logging took place. No idea how to.
// Assert no logging took place
assertTrue(logCaptor.warnLogs.isEmpty())
assertTrue(logCaptor.errorLogs.isEmpty())
}
}
}

0 comments on commit eeda917

Please sign in to comment.