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

Yoavya

11/02/2020, 4:43 PM
Hi question about auto-fill created and updated columns: It seems like if I don’t use the Entity to update the table (using the DSL API) then the updated field is not treated As I understand it Entity hooks only apply to entity related changes and not to changes directly to the table using the DSL API. is there a way to do the same when using the DSL API to also auto-fill the updated column?
t

tapac

11/11/2020, 7:20 AM
You can define default value on column.
y

Yoavya

11/11/2020, 8:57 AM
but that will only work when I insert for the first time, it wouldn’t work when I update a row in the table
t

tapac

11/11/2020, 8:58 AM
So you want overwrite existing value with new one?
y

Yoavya

11/11/2020, 8:59 AM
yes, I want to update the
updated
column on every change to that row
the same way it is happening with entity hooks but with table DSL
t

tapac

11/11/2020, 9:06 AM
I think it's only possible with a very tricky way with StatementInterceptors or creating your own inheritor of
UpdateStatement
which will add required columns into every batch.
y

Yoavya

11/11/2020, 9:07 AM
I’ll look into it
wrote this:
Copy code
fun <T: BaseLongIdTable> T.update(where: (SqlExpressionBuilder.()->Op<Boolean>)? = null, limit: Int? = null, body: T.(UpdateStatement)->Unit): Int {
    val query = UpdateStatement(this, limit, where?.let { <http://SqlExpressionBuilder.it|SqlExpressionBuilder.it>() })
    body(query)
    query[this.updatedAt] = LocalDateTime.now()
    return query.execute(TransactionManager.current())!!
}
but then I have to make sure that I call the right update method, meaning the one I wrote and not the one in the Exposed library. seems like it can cause confusion any tips?
f

Filip Lastic

11/29/2021, 1:51 PM
@tapac Can it be really done also with
StatementInterceptors
? Because if I understand correctly, I am not able to modify arguments of current statement and getting exception
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 3.
Hmm Maybe I got it. 1. I added new value to UpdateStatement by
statement[column] = value
2. I get index of
Pair(column, value)
from UpdateStatement 3. add value to
(context.args as ArrayList)
to specified index. But it doesn't look to be good usage of exposed at all (:
5 Views