Permalink
Browse files

Changed locking to use Arel. Arel updated

  • Loading branch information...
1 parent c0f66b7 commit 345e686d83c54fec6cb53cf77dea62d640a31954 @miloops miloops committed Apr 24, 2009
Showing with 20 additions and 15 deletions.
  1. +19 −14 activerecord/lib/active_record/locking/optimistic.rb
  2. +1 −1 arel
@@ -89,14 +89,19 @@ def update_with_lock(attribute_names = @attributes.keys) #:nodoc:
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 @@ def destroy_with_lock #:nodoc:
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"

0 comments on commit 345e686

Please sign in to comment.