I want to create a PostgreSQL query to update multiple columns (SMALLINT) with a lower boundary at zero. I'm thinking at the Java method Math.max(x,y). Something like Math.max(columnValue, 0)
. There is the method DSL.greatest(x,y)
, which seems to be perfect. But, this only accepts Field objects as parameters.
The example would be:
dslContext
.update(TABLE)
.set(TABLE.COLUMN1, DSL.greatest(TABLE.COLUMN1.minus(1), (short) 0))
.set(TABLE.COLUMN2, DSL.greatest(TABLE.COLUMN2.minus(1), (short) 0))
... some more
.where(TABLE.ID.eq(tableId))
.execute();
This leads to compilation errors at the zeros. Is it possible to create a Field object out of a number? Or how can I achieve this in any other another way?
Thank you for your support.
Either use DSL.inline()
to inline or DSL.val()
to bind the variable.
dslContext
.update(TABLE)
.set(TABLE.COLUMN1, DSL.greatest(TABLE.COLUMN1.minus(1), DSL.inline((short) 0)))
.set(TABLE.COLUMN2, DSL.greatest(TABLE.COLUMN2.minus(1), DSL.inline((short) 0)))
... some more
.where(TABLE.ID.eq(tableId))
.execute();