MrPowerGamerBR
02/17/2022, 10:09 PMupdate profiles set money = money * 0.95;
, money
is a bigint field on the table.
I tried doing this
Profiles.update {
with(SqlExpressionBuilder) {
it.update(Profiles.money, Profiles.money * 0.95)
}
}
However this doesn't work, because Profiles.money
is a Long, so PostgreSQL doesn't allow long + double multiplication :(Bogdan
02/18/2022, 11:12 AMobject Profiles : Table() {
val money = long("money")
}
// copy TimesOp
class UnsafeTimesOp<T, S>(
expr1: Expression<T>,
expr2: Expression<S>,
columnType: IColumnType
) : CustomOperator<T>("*", columnType, expr1, expr2)
fun <T, S> unsafeTimes(expr1: ExpressionWithColumnType<T>, expr2: Expression<S>): UnsafeTimesOp<T, S> =
UnsafeTimesOp(expr1, expr2, expr1.columnType)
operator fun <T : Number, S : Number> ExpressionWithColumnType<T>.times(value: S): Expression<T> =
unsafeTimes(this, wrap(value))
private fun test() {
transaction {
addLogger(StdOutSqlLogger)
Profiles.update {
it[money] = money * 0.95
// or
// it.update(money, money * 0.95)
}
}
}
transaction {
exec("UPDATE PROFILES SET MONEY=(PROFILES.MONEY * 0.95)")
}