Created
December 2, 2020 08:28
-
-
Save mgrzeszczak/65b9fda212fb982542bb395e1a841535 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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