https://kotlinlang.org logo
#exposed
Title
# exposed
m

MrPowerGamerBR

02/17/2022, 10:09 PM
I think this is a dumb question but I can't find the solution for this anywhere: I want to do this in Exposed:
update profiles set money = money * 0.95;
,
money
is a bigint field on the table. I tried doing this
Copy code
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 :(
b

Bogdan

02/18/2022, 11:12 AM
bigint is an integer data type
the code generates a request, but nothing changes. If you copy this request and execute it in console h2, then everything will work fine
Copy code
object 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)

        }

    }
}
work:
Copy code
transaction {
        exec("UPDATE PROFILES SET MONEY=(PROFILES.MONEY * 0.95)")
    }
2 Views