How to increase columns using DBIx :: Class?

I am trying to convert some raw DBI calls to DBIx :: Class. I sometimes come across something like:

UPDATE 'foo' SET bar = bar + 1 WHERE ... 

Is there a way for DBIx :: Class to execute just such a query? I do not want to do something like:

 $row->update({ bar => $row->bar() + 1 }); 

because there is a race condition if several processes try to do the same.

I could get around this with some database-level locking, but it seems worse to me than just using the original query. Basically, I just want to know if there is a clean way to use DBIC for this, or if I just keep using raw DBI calls here.

+6
source share
2 answers

Use the solution from @ThisSuitIsBlackNot comment, but replace update_all with update :

 $rs->search(...)->update({ bar => \'bar + 1', }); 

This will create one UPDATE statement. (Explanation: update_all works by calling update on each Row in a ResultSet, including things like DBIC triggers, so it must retrieve rows first. update in the ResultSet does a bubbling SQL UPDATE.)

+3
source

Will DBIx :: Class :: Storage :: TxnScopeGuard help be used? You can wrap your code block in a transaction like this

 my $guard = $schema->txn_scope_guard; # your increment $guard->commit; 
0
source

Source: https://habr.com/ru/post/977891/


All Articles