diff options
author | Jamis Buck <jamis@37signals.com> | 2007-02-07 16:10:40 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2007-02-07 16:10:40 +0000 |
commit | 83752373b9c83bf585b5fe44836c65612b680cf9 (patch) | |
tree | 0096d86743e56f8266daa280d4e364db734e3dab /activerecord/lib | |
parent | f458b376c545d8eae5189d1ecc16245cafe7e796 (diff) | |
download | rails-83752373b9c83bf585b5fe44836c65612b680cf9.tar.gz rails-83752373b9c83bf585b5fe44836c65612b680cf9.tar.bz2 rails-83752373b9c83bf585b5fe44836c65612b680cf9.zip |
Made increment_counter/decrement_counter play nicely with optimistic locking, and added a more general update_counters method
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6139 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 23 | ||||
-rw-r--r-- | activerecord/lib/active_record/locking/optimistic.rb | 25 |
2 files changed, 44 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index af3d1997c5..2127f64a1b 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -519,18 +519,37 @@ module ActiveRecord #:nodoc: connection.select_value(sql, "#{name} Count").to_i end + # A generic "counter updater" implementation, intended primarily to be + # used by increment_counter and decrement_counter, but which may also + # be useful on its own. It simply does a direct SQL update for the record + # with the given ID, altering the given hash of counters by the amount + # given by the corresponding value: + # + # Post.update_counters 5, :comment_count => -1, :action_count => 1 + # # UPDATE posts + # # SET comment_count = comment_count - 1, + # # action_count = action_count + 1 + # # WHERE id = 5 + def update_counters(id, counters) + updates = counters.inject([]) { |list, (counter_name, increment)| + sign = increment < 0 ? "-" : "+" + list << "#{counter_name} = #{counter_name} #{sign} #{increment.abs}" + }.join(", ") + update_all(updates, "#{primary_key} = #{quote_value(id)}") + end + # Increments the specified counter by one. So <tt>DiscussionBoard.increment_counter("post_count", # discussion_board_id)</tt> would increment the "post_count" counter on the board responding to discussion_board_id. # This is used for caching aggregate values, so that they don't need to be computed every time. Especially important # for looping over a collection where each element require a number of aggregate values. Like the DiscussionBoard # that needs to list both the number of posts and comments. def increment_counter(counter_name, id) - update_all "#{counter_name} = #{counter_name} + 1", "#{primary_key} = #{quote_value(id)}" + update_counters(id, counter_name => 1) end # Works like increment_counter, but decrements instead. def decrement_counter(counter_name, id) - update_all "#{counter_name} = #{counter_name} - 1", "#{primary_key} = #{quote_value(id)}" + update_counters(id, counter_name => -1) end diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index 02cf565017..217bd3def7 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -31,14 +31,14 @@ module ActiveRecord base.alias_method_chain :update, :lock base.alias_method_chain :attributes_from_column_definition, :lock - + class << base alias_method :locking_column=, :set_locking_column end end def locking_enabled? #:nodoc: - lock_optimistically && respond_to?(self.class.locking_column) + self.class.locking_enabled? end def attributes_from_column_definition_with_lock @@ -80,6 +80,20 @@ module ActiveRecord module ClassMethods DEFAULT_LOCKING_COLUMN = 'lock_version' + def self.extended(base) + class <<base + alias_method_chain :update_counters, :lock + end + end + + # Is optimistic locking enabled for this table? Returns true if the + # #lock_optimistically flag is set to true (which it is, by default) + # and the table includes the #locking_column column (defaults to + # lock_version). + def locking_enabled? + lock_optimistically && columns_hash[locking_column] + end + # Set the column to use for optimistic locking. Defaults to lock_version. def set_locking_column(value = nil, &block) define_attr_method :locking_column, value, &block @@ -100,6 +114,13 @@ module ActiveRecord def reset_locking_column set_locking_column DEFAULT_LOCKING_COLUMN end + + # make sure the lock version column gets updated when counters are + # updated. + def update_counters_with_lock(id, counters) + counters = counters.merge(locking_column => 1) if locking_enabled? + update_counters_without_lock(id, counters) + end end end end |