oshai
oshai

Reputation: 15365

Why Mysql INT(10) unsigned column to row.getLong throws exception in jasync-sql

INT(10) unsigned in mysql has limit up to 4b+ and when use with getLong of row, it throws the following error:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
    at com.github.jasync.sql.db.RowData$DefaultImpls.getLong(RowData.kt:48) ~[jasync-common-0.9.23.jar:?]
    at com.github.jasync.sql.db.general.ArrayRowData.getLong(ArrayRowData.kt:5) ~[jasync-common-0.9.23.jar:?]
    at com.richdevt.hthookserver.repository.ProjectRepository.findProjectEnvironment(ProjectRepository.kt:29) ~[classes/:?]
    at com.richdevt.hthookserver.repository.ProjectRepository$findProjectEnvironment$3.invokeSuspend(ProjectRepository.kt) ~[classes/:?]
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32) [kotlin-stdlib-1.3.21.jar:1.3.21-release-158 (1.3.21)]
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233) [kotlinx-coroutines-core-1.1.1.jar:?]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:163) [netty-common-4.1.33.Final.jar:4.1.33.Final]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) [netty-common-4.1.33.Final.jar:4.1.33.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) [netty-common-4.1.33.Final.jar:4.1.33.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:495) [netty-transport-4.1.33.Final.jar:4.1.33.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:905) [netty-common-4.1.33.Final.jar:4.1.33.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-common-4.1.33.Final.jar:4.1.33.Final]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_152

Why is the conversion not possible?

Upvotes: 1

Views: 621

Answers (1)

oshai
oshai

Reputation: 15365

The type INT(10) UNSIGNED is in the size of int (4 bytes) and unsigned: What is the maximum size of int(10) in Mysql

However, Java has no such type (ie the maximum value is bigger than the maximum Java int). On the other hand, the type returned from MySQL is INT as defined here: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html without additional info about unsigned. In such case jasync-sql converts it to a regular Int type. More details are here: https://github.com/jasync-sql/jasync-sql/issues/102

As a workaround for such case it is possible to do a widening conversion like suggested here: Best way to convert a signed integer to an unsigned long?

Upvotes: 2

Related Questions