kotlin 复合货币列可以为空,如何分配一个可以为空的货币金额?

q8l4jmvw  于 2023-03-13  发布在  Kotlin
关注(0)|答案(2)|浏览(188)

我有一个专栏是这样的:

val notNullableAmount = compositeMoney(DECIMAL_PRECISION,
    DECIMAL_SCALE,
    "principal_sum",
    "principal_sum_currency")

在本例中,我可以将MonetaryAmount类型的值赋给列,如下所示:

it[notNullableAmount] = ggnCollectionDocument.principalSum!!

但是当我使用nullable()时,比如:

val principalSum = compositeMoney(DECIMAL_PRECISION,
    DECIMAL_SCALE,
    "principal_sum",
    "principal_sum_currency").nullable()

然后我就不能再分配MonetaryAmount了,它会显示一些通用的错误消息:

None of the following functions can be called with the arguments supplied.
set(Column<TypeVariable(ID)>, TypeVariable(E))   where S = TypeVariable(S), ID = TypeVariable(ID), E = TypeVariable(E) for    fun <S, ID : EntityID<S>, E : Expression<S>> set(column: Column<ID>, value: E): Unit defined in org.jetbrains.exposed.sql.statements.InsertStatement
set(Column<TypeVariable(ID)>, TypeVariable(S))   where S = TypeVariable(S), E = TypeVariable(E), ID = TypeVariable(ID) for    fun <S : Comparable<S>, E : S, ID : EntityID<E>?> set(column: Column<ID>, value: S): Unit defined in org.jetbrains.exposed.sql.statements.InsertStatement
set(Column<TypeVariable(S)>, TypeVariable(S))   where S = TypeVariable(S) for    fun <S> set(column: Column<S>, value: S): Unit defined in org.jetbrains.exposed.sql.statements.InsertStatement
set(Column<TypeVariable(S)>, Query)   where S = TypeVariable(S) for    fun <S> set(column: Column<S>, value: Query): Unit defined in org.jetbrains.exposed.sql.statements.InsertStatement
set(Column<TypeVariable(T)>, TypeVariable(E))   where T = TypeVariable(T), S = TypeVariable(S), E = TypeVariable(E) for    fun <T, S : T, E : Expression<S>> set(column: Column<T>, value: E): Unit defined in org.jetbrains.exposed.sql.statements.InsertStatement
set(CompositeColumn<TypeVariable(S)>, TypeVariable(S))   where S = TypeVariable(S) for    fun <S : Any> set(column: CompositeColumn<S>, value: S): Unit defined in org.jetbrains.exposed.sql.statements.InsertStatement
pgccezyw

pgccezyw1#

在表的声明中:

val principalSum = (compositeMoney(DECIMAL_PRECISION,
    DECIMAL_SCALE,
    "principal_sum",
    "principal_sum_currency").nullable()) as CompositeMoneyColumn<*, *, MonetaryAmount?>

并且在用法上:

it[principalSum] = ggnCollectionDocument.principalSum

对于超载机操作员:

private operator fun InsertStatement<*>.set(column: CompositeMoneyColumn<*,*,MonetaryAmount?>, value: MonetaryAmount?) {
   val insertStatement = this
   insertStatement.set(column.amount as Column<BigDecimal?>, value?.number?.numberValue(BigDecimal::class.java))
   insertStatement.set(column.currency as Column<CurrencyUnit?>, value?.currency)
}
ddhy6vgd

ddhy6vgd2#

一个稍微简单一点的解决方法可能是按照建议定义可空列:

val principalSum = compositeMoney(DECIMAL_PRECISION,
DECIMAL_SCALE,
"principal_sum",
"principal_sum_currency").nullable()

然后使用下面的扩展函数:

operator fun <S : Any?> InsertStatement<*>.set(column: CompositeColumn<S>, value: S) {
    column.getRealColumnsWithValues(value)
        .forEach { (realColumn, itsValue) -> set(realColumn as Column<Any?>, itsValue) }
}

相关问题