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

Bug for SqlType.transform when transforming database null values of JVM primitive types. #400

Closed
vincentlauvlwj opened this issue Jun 23, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@vincentlauvlwj
Copy link
Member

Code to reproduce this issue:

enum class Status(val code: Int) {
    ONE(1), TWO(2), THREE(3);

    companion object {
        fun forCode(code: Int): Status {
            return values().first { it.code == code }
        }
    }
}

@Test
fun testTransformingNullValues() {
    val t = object : Table<Nothing>("T_TEST_TRANSFORMING_NULL_VALUES") {
        val status = int("STATUS").transform({ Status.forCode(it) }, { it.code })
    }

    database.useConnection { conn ->
        conn.createStatement().use { statement ->
            val sql = """CREATE TABLE T_TEST_TRANSFORMING_NULL_VALUES(STATUS INT)"""
            statement.executeUpdate(sql)
        }
    }

    database.insert(t) {
        set(it.status, null)
    }

    val query = database.from(t).select(t.status)
    assert(query.map { row -> row[t.status] }.first() == null)
}

Throwing the exception:

image

@vincentlauvlwj vincentlauvlwj added the bug Something isn't working label Jun 23, 2022
@zuisong
Copy link
Contributor

zuisong commented Jun 25, 2022

Maybe you can change your code to this

enum class Status(val code: Int) {
    ONE(1), TWO(2), THREE(3);

    companion object {
        fun forCode(code: Int?): Status? {
            return values().firstOrNull { it.code == code }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants