1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow all calculations to take the :include option, not just COUNT (closes #4840) [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4264 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson 2006-04-25 05:25:04 +00:00
parent abc2dba1ed
commit 4251662c1a
4 changed files with 55 additions and 68 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Allow all calculations to take the :include option, not just COUNT (closes #4840) [Rick]
* Update inconsistent migrations documentation. #4683 [machomagna@gmail.com] * Update inconsistent migrations documentation. #4683 [machomagna@gmail.com]
* Add ActiveRecord::Errors#to_xml [Jamis Buck] * Add ActiveRecord::Errors#to_xml [Jamis Buck]

View file

@ -966,14 +966,6 @@ module ActiveRecord
end end
end end
def count_with_associations(options = {})
catch :invalid_query do
join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins])
return count_by_sql(construct_counter_sql_with_included_associations(options, join_dependency))
end
0
end
def find_with_associations(options = {}) def find_with_associations(options = {})
catch :invalid_query do catch :invalid_query do
join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins]) join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins])
@ -1117,31 +1109,6 @@ module ActiveRecord
"#{name} Load Including Associations" "#{name} Load Including Associations"
) )
end end
def construct_counter_sql_with_included_associations(options, join_dependency)
scope = scope(:find)
sql = "SELECT COUNT(DISTINCT #{table_name}.#{primary_key})"
# A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT.
if !Base.connection.supports_count_distinct?
sql = "SELECT COUNT(*) FROM (SELECT DISTINCT #{table_name}.#{primary_key}"
end
sql << " FROM #{table_name} "
sql << join_dependency.join_associations.collect{|join| join.association_join }.join
add_joins!(sql, options, scope)
add_conditions!(sql, options[:conditions], scope)
add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections)
if !Base.connection.supports_count_distinct?
sql << ")"
end
return sanitize_sql(sql)
end
def construct_finder_sql_with_included_associations(options, join_dependency) def construct_finder_sql_with_included_associations(options, join_dependency)
scope = scope(:find) scope = scope(:find)

View file

@ -1,6 +1,6 @@
module ActiveRecord module ActiveRecord
module Calculations #:nodoc: module Calculations #:nodoc:
CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset] CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include]
def self.included(base) def self.included(base)
base.extend(ClassMethods) base.extend(ClassMethods)
end end
@ -42,13 +42,7 @@ module ActiveRecord
# #
# Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead. # Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead.
def count(*args) def count(*args)
column_name, options = construct_count_options_from_legacy_args(*args) calculate(:count, *construct_count_options_from_legacy_args(*args))
if options[:include] || scope(:find, :include)
count_with_associations(options)
else
calculate(:count, column_name, options)
end
end end
# Calculates average value on a given column. The value is returned as a float. See #calculate for examples with options. # Calculates average value on a given column. The value is returned as a float. See #calculate for examples with options.
@ -120,13 +114,14 @@ module ActiveRecord
column_name = options[:select] if options[:select] column_name = options[:select] if options[:select]
column_name = '*' if column_name == :all column_name = '*' if column_name == :all
column = column_for column_name column = column_for column_name
aggregate = select_aggregate(operation, column_name, options) catch :invalid_query do
aggregate_alias = column_alias_for(operation, column_name) if options[:group]
if options[:group] return execute_grouped_calculation(operation, column_name, column, options)
execute_grouped_calculation(operation, column_name, column, aggregate, aggregate_alias, options) else
else return execute_simple_calculation(operation, column_name, column, options)
execute_simple_calculation(operation, column_name, column, aggregate, aggregate_alias, options) end
end end
0
end end
protected protected
@ -143,7 +138,7 @@ module ActiveRecord
else else
# Handle legacy paramter options: def count(conditions=nil, joins=nil) # Handle legacy paramter options: def count(conditions=nil, joins=nil)
options.merge!(:conditions => args[0]) if args.length > 0 options.merge!(:conditions => args[0]) if args.length > 0
options.merge!(:joins => args[1]) if args.length > 1 options.merge!(:joins => args[1]) if args.length > 1
end end
else else
raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})") raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})")
@ -151,34 +146,56 @@ module ActiveRecord
[column_name, options] [column_name, options]
end end
def construct_calculation_sql(aggregate, aggregate_alias, options) #:nodoc: def construct_calculation_sql(operation, column_name, options) #:nodoc:
scope = scope(:find) scope = scope(:find)
sql = "SELECT #{aggregate} AS #{aggregate_alias}" merged_includes = merge_includes(scope ? scope[:include] : [], options[:include])
aggregate_alias = column_alias_for(operation, column_name)
use_workaround = !Base.connection.supports_count_distinct? && options[:distinct] && operation.to_s.downcase == 'count'
join_dependency = nil
if merged_includes.any? && operation.to_s.downcase == 'count'
options[:distinct] = true
column_name = [table_name, primary_key] * '.'
end
sql = "SELECT #{operation}(#{'DISTINCT ' if options[:distinct]}#{column_name}) AS #{aggregate_alias}"
# A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT.
sql = "SELECT COUNT(*) AS #{aggregate_alias}" if use_workaround
sql << ", #{options[:group_field]} AS #{options[:group_alias]}" if options[:group] sql << ", #{options[:group_field]} AS #{options[:group_alias]}" if options[:group]
sql << " FROM (SELECT DISTINCT #{column_name}" if use_workaround
sql << " FROM #{table_name} " sql << " FROM #{table_name} "
if merged_includes.any?
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins])
sql << join_dependency.join_associations.collect{|join| join.association_join }.join
end
add_joins!(sql, options, scope) add_joins!(sql, options, scope)
add_conditions!(sql, options[:conditions], scope) add_conditions!(sql, options[:conditions], scope)
sql << " GROUP BY #{options[:group_field]}" if options[:group] sql << " GROUP BY #{options[:group_field]}" if options[:group]
sql << " HAVING #{options[:having]}" if options[:group] && options[:having] sql << " HAVING #{options[:having]}" if options[:group] && options[:having]
sql << " ORDER BY #{options[:order]}" if options[:order] sql << " ORDER BY #{options[:order]}" if options[:order]
add_limit!(sql, options) add_limited_ids_condition!(sql, options, join_dependency) if join_dependency && !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
add_limit!(sql, options, scope)
sql << ')' if use_workaround
sql sql
end end
def execute_simple_calculation(operation, column_name, column, aggregate, aggregate_alias, options) #:nodoc: def execute_simple_calculation(operation, column_name, column, options) #:nodoc:
value = connection.select_value(construct_calculation_sql(aggregate, aggregate_alias, options)) value = connection.select_value(construct_calculation_sql(operation, column_name, options))
type_cast_calculated_value(value, column, operation) type_cast_calculated_value(value, column, operation)
end end
def execute_grouped_calculation(operation, column_name, column, aggregate, aggregate_alias, options) #:nodoc: def execute_grouped_calculation(operation, column_name, column, options) #:nodoc:
group_attr = options[:group].to_s group_attr = options[:group].to_s
association = reflect_on_association(group_attr.to_sym) association = reflect_on_association(group_attr.to_sym)
associated = association && association.macro == :belongs_to # only count belongs_to associations associated = association && association.macro == :belongs_to # only count belongs_to associations
group_field = (associated ? "#{options[:group]}_id" : options[:group]).to_s group_field = (associated ? "#{options[:group]}_id" : options[:group]).to_s
group_alias = column_alias_for(group_field) group_alias = column_alias_for(group_field)
group_column = column_for group_field group_column = column_for group_field
sql = construct_calculation_sql(aggregate, aggregate_alias, options.merge(:group_field => group_field, :group_alias => group_alias)) sql = construct_calculation_sql(operation, column_name, options.merge(:group_field => group_field, :group_alias => group_alias))
calculated_data = connection.select_all(sql) calculated_data = connection.select_all(sql)
aggregate_alias = column_alias_for(operation, column_name)
if association if association
key_ids = calculated_data.collect { |row| row[group_alias] } key_ids = calculated_data.collect { |row| row[group_alias] }
@ -195,15 +212,7 @@ module ActiveRecord
private private
def validate_calculation_options(operation, options = {}) def validate_calculation_options(operation, options = {})
if operation.to_s == 'count' options.assert_valid_keys(CALCULATIONS_OPTIONS)
options.assert_valid_keys(CALCULATIONS_OPTIONS + [:include])
else
options.assert_valid_keys(CALCULATIONS_OPTIONS)
end
end
def select_aggregate(operation, column_name, options)
"#{operation}(#{'DISTINCT ' if options[:distinct]}#{column_name})"
end end
# converts a given key to the value that the database adapter returns as # converts a given key to the value that the database adapter returns as

View file

@ -21,6 +21,16 @@ class CalculationsTest < Test::Unit::TestCase
assert_equal 60, Account.maximum(:credit_limit) assert_equal 60, Account.maximum(:credit_limit)
end end
def test_should_get_maximum_of_field_with_include
assert_equal 50, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
end
def test_should_get_maximum_of_field_with_scoped_include
Account.with_scope :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
assert_equal 50, Account.maximum(:credit_limit)
end
end
def test_should_get_minimum_of_field def test_should_get_minimum_of_field
assert_equal 50, Account.minimum(:credit_limit) assert_equal 50, Account.minimum(:credit_limit)
end end
@ -174,7 +184,6 @@ class CalculationsTest < Test::Unit::TestCase
Company.send(:validate_calculation_options, :count, :include => true) Company.send(:validate_calculation_options, :count, :include => true)
end end
assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :include => :posts) }
assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) } assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) } assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
end end