Hi - is there a way to create the SQL for { it[na...
# exposed
p
Hi - is there a way to create the SQL for { it[name] = "foo" } and { it[director] = "bar") }, to then later combine them and call Movies.update() { it[name] = "foo") ; it[director] = "bar" }
e
What I’ve done in the past is I wrote a
setIfNotNull()
extension function on
UpdateStatement
then made an “update” function with optional arguments. Here’s the implementation of `setIfNotNull()`:
Copy code
/**
 * Updates the value of a column if and only if the passed value is non-null
 */
fun <T> UpdateStatement.setIfNotNull(column: Column<T>, value: T?) {
    if (value != null) {
        this[column] = value
    }
}
For updates on nullable fields, I also have this:
Copy code
fun <T> UpdateStatement.setIfIntentional(column: Column<T?>, update: NullableUpdate<T>) {
    if (update.shouldUpdate) {
        this[column] = update.newValue
    }
}
And the NullableUpdate object:
Copy code
data class NullableUpdate<T>(val newValue: T?, val shouldUpdate: Boolean = true) {
    companion object {
        fun <T> doNotUpdate() = NullableUpdate<T>(null, false)
    }
}