aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-04-24 17:27:05 -0300
committerEmilio Tagua <miloops@gmail.com>2009-04-24 17:27:05 -0300
commit345e686d83c54fec6cb53cf77dea62d640a31954 (patch)
tree57f7e03f2fabac547a86845e2c883ed1020966b2 /activerecord/lib
parentc0f66b764e4a45764d64c6f047f13ec92b074c03 (diff)
downloadrails-345e686d83c54fec6cb53cf77dea62d640a31954.tar.gz
rails-345e686d83c54fec6cb53cf77dea62d640a31954.tar.bz2
rails-345e686d83c54fec6cb53cf77dea62d640a31954.zip
Changed locking to use Arel. Arel updated
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/locking/optimistic.rb33
1 files changed, 19 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index 7fa7e267d8..ab0f5fc58d 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -89,14 +89,19 @@ module ActiveRecord
attribute_names.uniq!
begin
- affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking")
- UPDATE #{self.class.quoted_table_name}
- SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, false, attribute_names))}
- WHERE #{self.class.primary_key} = #{quote_value(id)}
- AND #{self.class.quoted_locking_column} = #{quote_value(previous_value)}
- end_sql
-
- unless affected_rows == 1
+ table = Arel(self.class.table_name)
+ affected_rows = table.where(
+ table[self.class.primary_key].eq(quoted_id).and(
+ table[self.class.locking_column].eq(quote_value(previous_value))
+ )
+ )
+
+ attributes = {}
+ attributes_with_quotes(false, false, attribute_names).map { |k,v|
+ attributes.merge!(table[k] => v)
+ }
+
+ unless affected_rows.update(attributes) == 1
raise ActiveRecord::StaleObjectError, "Attempted to update a stale object"
end
@@ -116,12 +121,12 @@ module ActiveRecord
lock_col = self.class.locking_column
previous_value = send(lock_col).to_i
- affected_rows = connection.delete(
- "DELETE FROM #{self.class.quoted_table_name} " +
- "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quoted_id} " +
- "AND #{self.class.quoted_locking_column} = #{quote_value(previous_value)}",
- "#{self.class.name} Destroy"
- )
+ table = Arel(self.class.table_name, connection)
+ affected_rows = table.where(
+ table[self.class.primary_key].eq(quoted_id).and(
+ table[self.class.locking_column].eq(quote_value(previous_value))
+ )
+ ).delete
unless affected_rows == 1
raise ActiveRecord::StaleObjectError, "Attempted to delete a stale object"