mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixes to have all test passing on PostgreSQL.
Calculations now use construct_calculation_arel, making construct_finder_arel less hackish. Updated ARel to support PostgreSQL.
This commit is contained in:
parent
d3042ef80c
commit
d19d4d2f20
3 changed files with 29 additions and 18 deletions
|
@ -688,9 +688,9 @@ module ActiveRecord #:nodoc:
|
||||||
# Person.exists?(['name LIKE ?', "%#{query}%"])
|
# Person.exists?(['name LIKE ?', "%#{query}%"])
|
||||||
# Person.exists?
|
# Person.exists?
|
||||||
def exists?(id_or_conditions = {})
|
def exists?(id_or_conditions = {})
|
||||||
construct_finder_arel({
|
connection.select_all(construct_finder_arel({
|
||||||
:conditions =>expand_id_conditions(id_or_conditions)
|
:conditions => expand_id_conditions(id_or_conditions)
|
||||||
}).project(arel_table[primary_key]).take(1).count > 0
|
}).project(arel_table[primary_key]).take(1).to_sql).size > 0
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates an object (or multiple objects) and saves it to the database, if validations pass.
|
# Creates an object (or multiple objects) and saves it to the database, if validations pass.
|
||||||
|
@ -1688,9 +1688,9 @@ module ActiveRecord #:nodoc:
|
||||||
scope = scope(:find)
|
scope = scope(:find)
|
||||||
|
|
||||||
# TODO add lock to Arel
|
# TODO add lock to Arel
|
||||||
arel_table(options[:from] || table_name).
|
arel_table(table_name).
|
||||||
join(options[:merged_joins] || construct_join(options[:joins], scope)).
|
join(construct_join(options[:joins], scope)).
|
||||||
where(options[:merged_conditions] || construct_conditions(options[:conditions], scope)).
|
where(construct_conditions(options[:conditions], scope)).
|
||||||
project(options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))).
|
project(options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))).
|
||||||
group(construct_group(options[:group], options[:having], scope)).
|
group(construct_group(options[:group], options[:having], scope)).
|
||||||
order(construct_order(options[:order], scope)).
|
order(construct_order(options[:order], scope)).
|
||||||
|
@ -1704,7 +1704,6 @@ module ActiveRecord #:nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def construct_join(joins, scope = :auto)
|
def construct_join(joins, scope = :auto)
|
||||||
scope = scope(:find) if :auto == scope
|
|
||||||
merged_joins = scope && scope[:joins] && joins ? merge_joins(scope[:joins], joins) : (joins || scope && scope[:joins])
|
merged_joins = scope && scope[:joins] && joins ? merge_joins(scope[:joins], joins) : (joins || scope && scope[:joins])
|
||||||
case merged_joins
|
case merged_joins
|
||||||
when Symbol, Hash, Array
|
when Symbol, Hash, Array
|
||||||
|
@ -1738,7 +1737,6 @@ module ActiveRecord #:nodoc:
|
||||||
|
|
||||||
def construct_order(order, scope = :auto)
|
def construct_order(order, scope = :auto)
|
||||||
sql = ''
|
sql = ''
|
||||||
scope = scope(:find) if :auto == scope
|
|
||||||
scoped_order = scope[:order] if scope
|
scoped_order = scope[:order] if scope
|
||||||
if order
|
if order
|
||||||
sql << order.to_s
|
sql << order.to_s
|
||||||
|
@ -1752,19 +1750,16 @@ module ActiveRecord #:nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def construct_limit(options, scope = :auto)
|
def construct_limit(options, scope = :auto)
|
||||||
scope = scope(:find) if :auto == scope
|
|
||||||
options[:limit] ||= scope[:limit] if scope
|
options[:limit] ||= scope[:limit] if scope
|
||||||
options[:limit]
|
options[:limit]
|
||||||
end
|
end
|
||||||
|
|
||||||
def construct_offset(options, scope = :auto)
|
def construct_offset(options, scope = :auto)
|
||||||
scope = scope(:find) if :auto == scope
|
|
||||||
options[:offset] ||= scope[:offset] if scope
|
options[:offset] ||= scope[:offset] if scope
|
||||||
options[:offset]
|
options[:offset]
|
||||||
end
|
end
|
||||||
|
|
||||||
def construct_conditions(conditions, scope = :auto)
|
def construct_conditions(conditions, scope = :auto)
|
||||||
scope = scope(:find) if :auto == scope
|
|
||||||
conditions = [conditions]
|
conditions = [conditions]
|
||||||
conditions << scope[:conditions] if scope
|
conditions << scope[:conditions] if scope
|
||||||
conditions << type_condition if finder_needs_type_condition?
|
conditions << type_condition if finder_needs_type_condition?
|
||||||
|
|
|
@ -153,9 +153,9 @@ module ActiveRecord
|
||||||
conditions << construct_limited_ids_condition(conditions, options, join_dependency) if join_dependency && !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
|
conditions << construct_limited_ids_condition(conditions, options, join_dependency) if join_dependency && !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
|
||||||
|
|
||||||
if options[:group]
|
if options[:group]
|
||||||
return execute_grouped_calculation(operation, column_name, options.merge(:merged_conditions => conditions, :merged_joins => joins, :distinct => distinct))
|
return execute_grouped_calculation(operation, column_name, options.merge(:conditions => conditions, :joins => joins, :distinct => distinct))
|
||||||
else
|
else
|
||||||
return execute_simple_calculation(operation, column_name, options.merge(:merged_conditions => conditions, :merged_joins => joins, :distinct => distinct))
|
return execute_simple_calculation(operation, column_name, options.merge(:conditions => conditions, :joins => joins, :distinct => distinct))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
0
|
0
|
||||||
|
@ -163,15 +163,16 @@ module ActiveRecord
|
||||||
|
|
||||||
def execute_simple_calculation(operation, column_name, options) #:nodoc:
|
def execute_simple_calculation(operation, column_name, options) #:nodoc:
|
||||||
table = options[:from] || table_name
|
table = options[:from] || table_name
|
||||||
|
|
||||||
value = if operation == 'count'
|
value = if operation == 'count'
|
||||||
if column_name == :all && options[:select].blank?
|
if column_name == :all && options[:select].blank?
|
||||||
column_name = "*"
|
column_name = "*"
|
||||||
elsif !options[:select].blank?
|
elsif !options[:select].blank?
|
||||||
column_name = options[:select]
|
column_name = options[:select]
|
||||||
end
|
end
|
||||||
construct_finder_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).count(options[:distinct]))).select_value
|
construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).count(options[:distinct]))).select_value
|
||||||
else
|
else
|
||||||
construct_finder_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).send(operation))).select_value
|
construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).send(operation))).select_value
|
||||||
end
|
end
|
||||||
|
|
||||||
type_cast_calculated_value(value, column_for(column_name), operation)
|
type_cast_calculated_value(value, column_for(column_name), operation)
|
||||||
|
@ -196,7 +197,7 @@ module ActiveRecord
|
||||||
options[:select] = "#{arel_column.as(aggregate_alias).to_sql}, #{group_field} AS #{group_alias}"
|
options[:select] = "#{arel_column.as(aggregate_alias).to_sql}, #{group_field} AS #{group_alias}"
|
||||||
end
|
end
|
||||||
|
|
||||||
calculated_data = connection.select_all(construct_finder_sql(options))
|
calculated_data = connection.select_all(construct_calculation_arel(options).to_sql)
|
||||||
|
|
||||||
if association
|
if association
|
||||||
key_ids = calculated_data.collect { |row| row[group_alias] }
|
key_ids = calculated_data.collect { |row| row[group_alias] }
|
||||||
|
@ -213,7 +214,22 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
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)
|
def construct_count_options_from_args(*args)
|
||||||
options = {}
|
options = {}
|
||||||
column_name = :all
|
column_name = :all
|
||||||
|
|
2
arel
2
arel
|
@ -1 +1 @@
|
||||||
Subproject commit f44853a5aa7f4481d99a3af4585f3a51272bc7f7
|
Subproject commit de843c86518e4ac871d4bb5b0873bb6c184ac304
|
Loading…
Reference in a new issue