Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add missing KDocs for exposed-dao EntityClass API #2018

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@ import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.statements.BatchUpdateStatement
import java.util.*

/**
* Class responsible for performing a batch update operation on multiple instances of an [Entity] class.
*
* @param klass The [EntityClass] associated with the entities to batch update.
*/
class EntityBatchUpdate(private val klass: EntityClass<*, Entity<*>>) {

private val data = ArrayList<Pair<EntityID<*>, SortedMap<Column<*>, Any?>>>()

/**
* Adds the specified [entity] to the list of entities to batch update.
*
* The column-value mapping for this entity will initially be empty.
* Columns to update should be assigned by using the `set()` operator on this [EntityBatchUpdate] instance.
*
* @throws IllegalStateException If the entity being added cannot be associated with the [EntityClass]
* provided on instantiation of this [EntityBatchUpdate].
*/
fun addBatch(entity: Entity<*>) {
if (entity.klass != klass) error("Entity class${entity.klass} differs from expected entity class $klass")
data.add(entity.id to TreeMap())
Expand All @@ -25,6 +39,10 @@ class EntityBatchUpdate(private val klass: EntityClass<*, Entity<*>>) {
values[column] = value
}

/**
* Executes the batch update SQL statement for each added entity in the provided [transaction]
* and returns the number of updated rows.
*/
fun execute(transaction: Transaction): Int {
val updateSets = data.filterNot { it.second.isEmpty() }.groupBy { it.second.keys }
return updateSets.values.fold(0) { acc, set ->
Expand Down
Loading
Loading