Skip to content

Commit

Permalink
Add UpdateBuilder#setNull(T) to simplify usages like "it[column] = nu…
Browse files Browse the repository at this point in the history
…ll as Int?"
  • Loading branch information
vlsi committed Jun 19, 2021
1 parent 24c731a commit 97fc481
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ abstract class UpdateBuilder<out T>(type: StatementType, targets: List<Table>) :
values[column] = value
}

/**
* Sets column value to null.
* This method is helpful for "optional references" since compiler can't decide between
* "null as T?" and "null as EntityID<T>?".
*/
fun <T> setNull(column: Column<T?>) = set(column, null as T?)

open operator fun <S> set(column: Column<S>, value: Expression<out S>) = update(column, value)

open operator fun <S> set(column: CompositeColumn<S>, value: S) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ class InsertTests : DatabaseTestsBase() {
tbl.verifyInsert(null) {
it[nullableInt] = LiteralOp(nullableInt.columnType, null)
}

tbl.verifyInsert(null) {
it.setNull(nullableInt)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ class EntityTests : DatabaseTestsBase() {
// it[EntityTestsData.XTable.y1] = null
it[y1] = null as EntityID<String>?
}
EntityTestsData.XTable.insert {
it.setNull(y1)
}
}
}

Expand Down

0 comments on commit 97fc481

Please sign in to comment.