ktorm icon indicating copy to clipboard operation
ktorm copied to clipboard

Is it possible to auto increment a column value on Update?

Open lawkai opened this issue 4 years ago • 3 comments

I have a column in a table called 'version' which keep tracks of the number of updates for the row. Is it possible to use Ktorm to auto increment this column value whenever Ktorm updates the entity? Thanks.

lawkai avatar Jul 06 '20 03:07 lawkai

Self increment:

database.update(Foo) {
    it.version to it.version + 1
    where { 
        (it.id eq 1) and (it.version eq 1)
    }
}

Generated SQL:

update foo set version = version + ? where id = ? and version = ?

vincentlauvlwj avatar Jul 06 '20 06:07 vincentlauvlwj

Thanks Vincent, but is there a way that this can be managed by the entity class itself without using the DSL? (i.e. just do flushChanges() on the entity object without the DSL?) What I really want to achieve is to have Ktorm to do something like Hibernate/JPA Optimistic Locking. Using the version as part of the WHERE clause whenever flushChanges()

lawkai avatar Jul 06 '20 07:07 lawkai

Got it, but it is not supported yet. We can directly use SQL DSL as a workaround.

vincentlauvlwj avatar Jul 06 '20 14:07 vincentlauvlwj