Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only warn of schema/db mismatch if its the case. It isnt on left join. #541

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions exposed/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@ class ResultRow(internal val fieldIndex: Map<Expression<*>, Int>) {
*
* @see [getOrNull] to get null in the cases an exception would be thrown
*/
@Suppress("UNCHECKED_CAST")
operator fun <T> get(c: Expression<T>): T {
val d = getRaw(c)
return when {
d == null && c is Column<*> && c.dbDefaultValue != null && !c.columnType.nullable -> {
exposedLogger.warn("Column ${TransactionManager.current().identity(c)} is marked as not null, " +
"has default db value, but returns null. Possible have to re-read it from DB.")
null
}
d == null -> null
d == NotInitializedValue -> error("${c.toSQL(QueryBuilder(false))} is not initialized yet")
c is ExpressionAlias<T> && c.delegate is ExpressionWithColumnType<T> -> c.delegate.columnType.valueFromDB(d)
c is ExpressionWithColumnType<T> -> c.columnType.valueFromDB(d)
else -> d
} as T

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

return rawToColumnValue(d, c)
}

operator fun <T> set(c: Expression<out T>, value: T) {
Expand All @@ -44,11 +38,22 @@ class ResultRow(internal val fieldIndex: Map<Expression<*>, Int>) {

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

fun <T> getOrNull(c: Expression<T>): T? = if (hasValue(c)) get(c) else null
fun <T> getOrNull(c: Expression<T>): T? = if (hasValue(c)) rawToColumnValue(getRaw(c), c) else null

@Deprecated("Replaced with getOrNull to be more kotlinish", replaceWith = ReplaceWith("getOrNull(c)"))
fun <T> tryGet(c: Expression<T>): T? = getOrNull(c)

@Suppress("UNCHECKED_CAST")
private fun <T> rawToColumnValue(raw: T?, c: Expression<T>): T {
return when {
raw == null -> null
raw == NotInitializedValue -> error("${c.toSQL(QueryBuilder(false))} is not initialized yet")
c is ExpressionAlias<T> && c.delegate is ExpressionWithColumnType<T> -> c.delegate.columnType.valueFromDB(raw)
c is ExpressionWithColumnType<T> -> c.columnType.valueFromDB(raw)
else -> raw
} as T
}

@Suppress("UNCHECKED_CAST")
private fun <T> getRaw(c: Expression<T>): T? =
data[fieldIndex[c] ?: error("${c.toSQL(QueryBuilder(false))} is not in record set")] as T?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,28 @@ class DMLTests : DatabaseTestsBase() {
assertEquals(allSities.size, cities.select { Op.TRUE }.count())
}
}

@Test fun testNoWarningsOnLeftJoinRegression(){
val MainTable = object : Table("maintable"){
val id = integer("idCol")
}
val JoinTable = object : Table("jointable"){
val id = integer("idCol")
val data = integer("dataCol").default(42)
}

withTables(MainTable, JoinTable){
MainTable.insert { it[id] = 2 }

MainTable.join(JoinTable, JoinType.LEFT, JoinTable.id, MainTable.id)
.slice(JoinTable.data)
.selectAll()
.single()
.getOrNull(JoinTable.data)

// Assert no logging took place. No idea how to.
}
}
}

private val today: DateTime = DateTime.now().withTimeAtStartOfDay()
Expand Down