https://kotlinlang.org logo
#exposed
Title
# exposed
t

Timur Atakishiev

01/10/2020, 7:55 AM
hey guys, can not find how to implement batch update in exposed, please help! I am using DSL
t

tapac

01/10/2020, 8:42 AM
Copy code
val batchUpdate = BatchUpdateStatement(FooTable)
dataToUpdate.forEach { entry ->
   batchUpdate.addBatch(entry.id)
   batchUpdate[FooTable.bar] = entry.bar
}
batchUpdate.execute(TransactionManager.current())
t

Timur Atakishiev

01/10/2020, 8:52 AM
Thank you very much
m

Mourad

02/26/2020, 3:32 PM
@tapac How would one do a batch update with more than one condition in the where clause? I see that
BatchUpdateStatement
extends from
UpdateStatement
but does not allow to override the where section... Perhaps this is done because the id of that element is been put there (obviously) to know which element to update. Is there a way to extend this where-clause with more conditions?
t

tapac

02/26/2020, 3:33 PM
Could you explain what the
batch update with where condition
means?
m

Mourad

02/26/2020, 3:41 PM
Perhaps an example would explain this a bit more. So lets say that with the code you mentioned above the following sql will be generated:
UPDATE Items SET columnName='value' WHERE id = '2e9a4830-33b3-44f9-99af-98b7b5472c22'
The
BatchUpdateStatement
does not allow to override the where-clause but generates the where-clause by using:
${transaction.identity(table.id)} = ?
Is there a way to extend the where-clause to something like:
WHERE id = '2e9a4830-33b3-44f9-99af-98b7b5472c22' AND organizationId = 1
@tapac I could also solve this by checking whether the items within that organization exists before triggering this batch update. Just wondering whether I could solve this in a single operation.
t

tapac

02/26/2020, 4:39 PM
Do you want to set the same
value
for every id ?
m

Mourad

02/26/2020, 4:42 PM
@tapac Yes, I do (if you are talking about the organization field)
t

tapac

02/26/2020, 4:44 PM
Why not to make a single update then?
Copy code
Items.update({
   (id eq '123' and orgId eq '1212') or 
   (id eq '1234' and orgId eq '12152')
}) {
 it[value] = '123'
}
I'm not sure what will be faster
m

Mourad

02/26/2020, 4:51 PM
Ah that is what you meant by asking whether I wanted to set the same values for each item. Unfortunately, no… this is not the case, so per item different fields could’ve changed
21 Views