提交 3b27a485 编写于 作者: E Emilio Tagua

Refactoring: Calculations now use construct_finder_sql instead they own method.

上级 b9088dce
......@@ -1696,11 +1696,9 @@ def arel_table(table = table_name)
@arel_table = Arel::Table.new(table)
end
def construct_finder_arel(options)
scope = scope(:find)
def construct_finder_arel(options, scope = scope(:find))
# TODO add lock to Arel
arel_table(table_name).
arel_table(options[:from] || table_name).
join(construct_join(options[:joins], scope)).
where(construct_conditions(options[:conditions], scope)).
project(options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))).
......@@ -1711,8 +1709,8 @@ def construct_finder_arel(options)
)
end
def construct_finder_sql(options)
construct_finder_arel(options).to_sql
def construct_finder_sql(options, scope = scope(:find))
construct_finder_arel(options, scope).to_sql
end
def construct_join(joins, scope)
......
......@@ -169,11 +169,11 @@ def execute_simple_calculation(operation, column_name, options) #:nodoc:
(column_name == :all ? "*" : column_name.to_s))
end
value = construct_calculation_arel(options.merge(
value = construct_finder_sql(options.merge(
:select => operation == 'count' ? column.count(options[:distinct]) : column.send(operation)
))
), nil)
type_cast_calculated_value(connection.select_value(value.to_sql), column_for(column_name), operation)
type_cast_calculated_value(connection.select_value(value), column_for(column_name), operation)
end
def execute_grouped_calculation(operation, column_name, options) #:nodoc:
......@@ -194,7 +194,7 @@ def execute_grouped_calculation(operation, column_name, options) #:nodoc:
options[:select] << ", #{group_field} AS #{group_alias}"
calculated_data = connection.select_all(construct_calculation_arel(options).to_sql)
calculated_data = connection.select_all(construct_finder_sql(options, nil))
if association
key_ids = calculated_data.collect { |row| row[group_alias] }
......@@ -212,21 +212,6 @@ def execute_grouped_calculation(operation, column_name, options) #:nodoc:
end
protected
def construct_calculation_arel(options)
scope = scope(:find)
arel_table(options[:from] || table_name).
join(options[:joins]).
where(options[:conditions]).
project(options[:select]).
group(construct_group(options[:group], options[:having], scope)).
order(options[:order].to_s).
take(construct_limit(options, scope)).
skip(construct_offset(options, scope)
)
end
def construct_count_options_from_args(*args)
options = {}
column_name = :all
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册