Skip to content

Instantly share code, notes, and snippets.

@mgrzeszczak
Created December 2, 2020 08:28
Show Gist options
  • Save mgrzeszczak/65b9fda212fb982542bb395e1a841535 to your computer and use it in GitHub Desktop.
Save mgrzeszczak/65b9fda212fb982542bb395e1a841535 to your computer and use it in GitHub Desktop.
interface VersionedTable {
val versionColumn: Column<Int>
}
fun <T> T.updateWithVersion(where: (SqlExpressionBuilder.() -> Op<Boolean>)? = null,
version: Int,
limit: Int? = null,
body: T.(UpdateStatement) -> Unit
): Int where T : Table, T : VersionedTable {
val whereClauseWithVersion: (SqlExpressionBuilder.() -> Op<Boolean>) = {
val versionEquals = this@updateWithVersion.versionColumn eq version
when (where != null) {
true -> where(this).and(versionEquals)
else -> versionEquals
}
}
val updateWithVersion: T.(UpdateStatement) -> Unit = {
body(it)
it[this.versionColumn] = version + 1
}
return this.update(whereClauseWithVersion, limit, updateWithVersion)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment