Thursday, 5 September 2013

How to add a WHERE clause in UPDATE query in Datamapper

How to add a WHERE clause in UPDATE query in Datamapper

I have a Datamapper model, say XYZ. Now, There are multiple threads which,
at times, read a row using this model and attempt to update the same row -
only one should should succeed, depending on a property of XYZ, say abc
class XYZ
include DataMapper::Resource
property :id # primary key
property :abc
end
Now:
obj = XYZ.get(some_id)
obj.update(abc: 10) # Assume abc column value was 5 earlier
This may happen in several threads, and may happen simultaneously. Also,
the new value for column abc is different in each thread. Once, when a
thread updates abc, others should not. Essentially what I am trying to do
is to run this query via datamapper:
UPDATE `xyz` SET `abc` = 20 WHERE `id` = <some id> AND `abc` = 5
the model.update function does not allows to update an attribute and put a
condition of that attribute simultaneously. I know I can run an SQL query
directly; but is there any other way?

No comments:

Post a Comment