Skip to content

Commit

Permalink
[#5441 state:resolved] refactoring code to determine aggregate column
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 30, 2010
1 parent de3c0d7 commit fc1bd2b
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -183,12 +183,16 @@ def perform_calculation(operation, column_name, options = {})
end
end

def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
column = if @klass.column_names.include?(column_name.to_s)
def aggregate_column(column_name)
if @klass.column_names.include?(column_name.to_s)
Arel::Attribute.new(@klass.unscoped.table, column_name)
else
Arel::SqlLiteral.new(column_name == :all ? "*" : column_name.to_s)
Arel.sql(column_name == :all ? "*" : column_name.to_s)
end
end

def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
column = aggregate_column(column_name)

# Postgresql doesn't like ORDER BY when there are no GROUP BY
relation = except(:order)
Expand All @@ -209,18 +213,17 @@ def execute_grouped_calculation(operation, column_name) #:nodoc:

group = @klass.connection.adapter_name == 'FrontBase' ? group_alias : group_field

aggregate_alias = column_alias_for(operation, column_name)

select_statement = if operation == 'count' && column_name == :all
["COUNT(*) AS count_all"]
if operation == 'count' && column_name == :all
aggregate_alias = 'count_all'
else
[Arel::Attribute.new(@klass.unscoped.table, column_name).send(operation).as(aggregate_alias)]
aggregate_alias = column_alias_for(operation, column_name)
end

select_statement << "#{group_field} AS #{group_alias}"

relation = except(:group).group(group)
relation.select_values = select_statement
relation.select_values = [
aggregate_column(column_name).send(operation).as(aggregate_alias),
"#{group_field} AS #{group_alias}"
]

calculated_data = @klass.connection.select_all(relation.to_sql)

Expand Down

0 comments on commit fc1bd2b

Please sign in to comment.