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

无法使用kotlin unsigned类型进行扩展 #253

Closed
yh0xa55a8 opened this issue Feb 23, 2021 · 4 comments
Closed

无法使用kotlin unsigned类型进行扩展 #253

yh0xa55a8 opened this issue Feb 23, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@yh0xa55a8
Copy link

Ktorm version: 3.2.0

扩展的代码

object ULongSqlType: SqlType<ULong>(Types.BIGINT, "bigint unsigned") {
    override fun doGetResult(rs: ResultSet, index: Int): ULong? =
        (rs.getObject(index) as? BigInteger)?.toLong()?.toULong()

    override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: ULong) =
        ps.setLong(index, parameter.toLong())
}

fun BaseTable<*>.ulong(name: String) = this.registerColumn(name, ULongSqlType)

对应的Table object

object Users: Table<User>("user") {
    val id = ulong("id")
        .primaryKey()
        .bindTo { it.id }
}

配合spring boot使用
在User object初始化时报错:

java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because the return value of "java.lang.reflect.InvocationHandler.invoke(Object, java.lang.reflect.Method, Object[])" is null
        at com.sun.proxy.$Proxy133.getId-s-VKNKU(Unknown Source) ~[na:na]
	at cn.yh0x13f.teatalk.data.sql.table.Users.<clinit>(Users.kt:10) ~[main/:na]

断点可见问题出现在bindTo { it.id }
推测问题抛出在获取实体列绑定的properties中

@yh0xa55a8
Copy link
Author

更新 问题定位到org.ktorm.schema.ColumnBindingHandler[58]的return 因为unsigned type不是基本类型 所以此处返回了null,而外层一个定位不到的地方试图以此为结果转换到java.lang.Long (为什么会有这样的行为啊)然后就抛出了

@vincentlauvlwj
Copy link
Member

ColumnBindingHandler 是一个用于动态代理的类,它的目的是记录我们在 bind { } 方法的 lambda 表达式里面返回的具体是哪个属性,所以它的 invoke 方法的返回值实际上是没有意义的,因此才会有基本类型返回默认值,其他类型直接返回 null 的逻辑

ULong 是一个 inline class,这个类在 Java 里面不是基本类型,所以返回 null 了,但是 inline class 的行为有点特殊,它在编译时会视为其包装的基本类型,所以才会出现这个问题

解决方法:暂时先改成 Long,不要用 ULong,Ktorm 在下个版本会做一下兼容

@vincentlauvlwj vincentlauvlwj added the bug Something isn't working label Feb 23, 2021
@vincentlauvlwj
Copy link
Member

3.4.0 版本已兼容无符号整数

@vincentlauvlwj
Copy link
Member

3.5.0 版本已完全兼容所有内联类型

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