Permalink
Browse files

Delay building arel relation as long as possible for improved introsp…

…ection
  • Loading branch information...
lifo committed Jan 12, 2010
1 parent b078f7f commit a04486dc997979a2d87fc013d30b6e71a3df4a64
@@ -2148,22 +2148,23 @@ def association_join
end
def relation
+ aliased = Arel::Table.new(table_name, :as => @aliased_table_name)
+
if reflection.macro == :has_and_belongs_to_many
- [Arel::Table.new(table_alias_for(options[:join_table], aliased_join_table_name)), Arel::Table.new(table_name_and_alias)]
+ [Arel::Table.new(options[:join_table], :as => aliased_join_table_name), aliased]
elsif reflection.options[:through]
- [Arel::Table.new(table_alias_for(through_reflection.klass.table_name, aliased_join_table_name)), Arel::Table.new(table_name_and_alias)]
+ [Arel::Table.new(through_reflection.klass.table_name, :as => aliased_join_table_name), aliased]
else
- Arel::Table.new(table_name_and_alias)
+ aliased
end
end
def join_relation(joining_relation, join = nil)
if (relations = relation).is_a?(Array)
- joining_relation.
- joins(relations.first, Arel::OuterJoin).on(association_join.first).
- joins(relations.last, Arel::OuterJoin).on(association_join.last)
+ joining_relation.joins(Relation::JoinOperation.new(relations.first, Arel::OuterJoin, association_join.first)).
+ joins(Relation::JoinOperation.new(relations.last, Arel::OuterJoin, association_join.last))
else
- joining_relation.joins(relations, Arel::OuterJoin).on(association_join)
+ joining_relation.joins(Relation::JoinOperation.new(relations, Arel::OuterJoin, association_join))
end
end
@@ -1663,7 +1663,7 @@ def merge_joins(*joins)
def build_association_joins(joins)
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, joins, nil)
- relation = active_relation.relation
+ relation = active_relation.table
join_dependency.join_associations.map { |association|
if (association_relation = association.relation).is_a?(Array)
[Arel::InnerJoin.new(relation, association_relation.first, association.association_join.first).joins(relation),
@@ -2039,7 +2039,7 @@ def expand_hash_conditions_for_aggregates(attrs)
def sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name)
attrs = expand_hash_conditions_for_aggregates(attrs)
- table = Arel::Table.new(default_table_name, active_relation_engine)
+ table = Arel::Table.new(self.table_name, :engine => active_relation_engine, :as => default_table_name)
builder = PredicateBuilder.new(active_relation_engine)
builder.build_from_hash(attrs, table).map(&:to_sql).join(' AND ')
end
@@ -1,19 +1,19 @@
module ActiveRecord
class Relation
- include QueryMethods, FinderMethods, CalculationMethods, SpawnMethods
+ JoinOperation = Struct.new(:relation, :join_class, :on)
+ ASSOCIATION_METHODS = [:includes, :eager_load, :preload]
+ MULTI_VALUE_METHODS = [:select, :group, :order, :joins, :where, :having]
+ SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :create_with, :from]
+
+ include FinderMethods, CalculationMethods, SpawnMethods, QueryMethods
delegate :length, :collect, :map, :each, :all?, :to => :to_a
- attr_reader :relation, :klass
- attr_writer :readonly, :table
- attr_accessor :preload_associations, :eager_load_associations, :includes_associations, :create_with_attributes
+ attr_reader :table, :klass
- def initialize(klass, relation)
- @klass, @relation = klass, relation
- @preload_associations = []
- @eager_load_associations = []
- @includes_associations = []
- @loaded, @readonly = false
+ def initialize(klass, table)
+ @klass, @table = klass, table
+ (ASSOCIATION_METHODS + MULTI_VALUE_METHODS).each {|v| instance_variable_set(:"@#{v}_values", [])}
end
def new(*args, &block)
@@ -29,7 +29,7 @@ def create!(*args, &block)
end
def respond_to?(method, include_private = false)
- return true if @relation.respond_to?(method, include_private) || Array.method_defined?(method)
+ return true if arel.respond_to?(method, include_private) || Array.method_defined?(method)
if match = DynamicFinderMatch.match(method)
return true if @klass.send(:all_attributes_exists?, match.attribute_names)
@@ -43,33 +43,35 @@ def respond_to?(method, include_private = false)
def to_a
return @records if loaded?
- find_with_associations = @eager_load_associations.any? || references_eager_loaded_tables?
+ find_with_associations = @eager_load_values.any? || (@includes_values.any? && references_eager_loaded_tables?)
@records = if find_with_associations
begin
@klass.send(:find_with_associations, {
- :select => @relation.send(:select_clauses).join(', '),
- :joins => @relation.joins(relation),
- :group => @relation.send(:group_clauses).join(', '),
+ :select => arel.send(:select_clauses).join(', '),
+ :joins => arel.joins(arel),
+ :group => arel.send(:group_clauses).join(', '),
:order => order_clause,
:conditions => where_clause,
- :limit => @relation.taken,
- :offset => @relation.skipped,
- :from => (@relation.send(:from_clauses) if @relation.send(:sources).present?)
+ :limit => arel.taken,
+ :offset => arel.skipped,
+ :from => (arel.send(:from_clauses) if arel.send(:sources).present?)
},
- ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations + @includes_associations, nil))
+ ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_values + @includes_values, nil))
rescue ThrowResult
[]
end
else
- @klass.find_by_sql(@relation.to_sql)
+ @klass.find_by_sql(arel.to_sql)
end
- preload = @preload_associations
- preload += @includes_associations unless find_with_associations
+ preload = @preload_values
+ preload += @includes_values unless find_with_associations
preload.each {|associations| @klass.send(:preload_associations, @records, associations) }
- @records.each { |record| record.readonly! } if @readonly
+ # @readonly_value is true only if set explicity. @implicit_readonly is true if there are JOINS and no explicit SELECT.
+ readonly = @readonly_value.nil? ? @implicit_readonly : @readonly_value
+ @records.each { |record| record.readonly! } if readonly
@loaded = true
@records
@@ -97,7 +99,7 @@ def many?
if block_given?
to_a.many? { |*block_args| yield(*block_args) }
else
- @relation.send(:taken).present? ? to_a.many? : size > 1
+ arel.send(:taken).present? ? to_a.many? : size > 1
end
end
@@ -107,7 +109,7 @@ def destroy_all
end
def delete_all
- @relation.delete.tap { reset }
+ arel.delete.tap { reset }
end
def delete(id_or_array)
@@ -124,28 +126,24 @@ def reload
end
def reset
- @first = @last = @to_sql = @order_clause = @scope_for_create = nil
+ @first = @last = @to_sql = @order_clause = @scope_for_create = @arel = nil
@records = []
self
end
- def table
- @table ||= Arel::Table.new(@klass.table_name, :engine => @klass.active_relation_engine)
- end
-
def primary_key
@primary_key ||= table[@klass.primary_key]
end
def to_sql
- @to_sql ||= @relation.to_sql
+ @to_sql ||= arel.to_sql
end
protected
def method_missing(method, *args, &block)
- if @relation.respond_to?(method)
- @relation.send(method, *args, &block)
+ if arel.respond_to?(method)
+ arel.send(method, *args, &block)
elsif Array.method_defined?(method)
to_a.send(method, *args, &block)
elsif match = DynamicFinderMatch.match(method)
@@ -168,23 +166,23 @@ def with_create_scope
def scope_for_create
@scope_for_create ||= begin
- @create_with_attributes || wheres.inject({}) do |hash, where|
+ @create_with_value || wheres.inject({}) do |hash, where|
hash[where.operand1.name] = where.operand2.value if where.is_a?(Arel::Predicates::Equality)
hash
end
end
end
def where_clause(join_string = " AND ")
- @relation.send(:where_clauses).join(join_string)
+ arel.send(:where_clauses).join(join_string)
end
def order_clause
- @order_clause ||= @relation.send(:order_clauses).join(', ')
+ @order_clause ||= arel.send(:order_clauses).join(', ')
end
def references_eager_loaded_tables?
- joined_tables = (tables_in_string(@relation.joins(relation)) + [table.name, table.table_alias]).compact.uniq
+ joined_tables = (tables_in_string(arel.joins(arel)) + [table.name, table.table_alias]).compact.uniq
(tables_in_string(to_sql) - joined_tables).any?
end
@@ -25,7 +25,7 @@ def calculate(operation, column_name, options = {})
operation = operation.to_s.downcase
if operation == "count"
- joins = @relation.joins(relation)
+ joins = arel.joins(arel)
if joins.present? && joins =~ /LEFT OUTER/i
distinct = true
column_name = @klass.primary_key if column_name == :all
@@ -40,7 +40,7 @@ def calculate(operation, column_name, options = {})
distinct = options[:distinct] || distinct
column_name = :all if column_name.blank? && operation == "count"
- if @relation.send(:groupings).any?
+ if arel.send(:groupings).any?
return execute_grouped_calculation(operation, column_name)
else
return execute_simple_calculation(operation, column_name, distinct)
@@ -63,7 +63,7 @@ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
end
def execute_grouped_calculation(operation, column_name) #:nodoc:
- group_attr = @relation.send(:groupings).first.value
+ group_attr = arel.send(:groupings).first.value
association = @klass.reflect_on_association(group_attr.to_sym)
associated = association && association.macro == :belongs_to # only count belongs_to associations
group_field = associated ? association.primary_key_name : group_attr
@@ -165,12 +165,8 @@ def type_cast_using_column(value, column)
column ? column.type_cast(value) : value
end
- def get_projection_name_from_chained_relations(relation = @relation)
- if relation.respond_to?(:projections) && relation.projections.present?
- relation.send(:select_clauses).join(', ')
- elsif relation.respond_to?(:relation)
- get_projection_name_from_chained_relations(relation.relation)
- end
+ def get_projection_name_from_chained_relations
+ @select_values.join(", ") if @select_values.present?
end
end
@@ -93,15 +93,15 @@ def find_some(ids)
result = where(primary_key.in(ids)).all
expected_size =
- if @relation.taken && ids.size > @relation.taken
- @relation.taken
+ if arel.taken && ids.size > arel.taken
+ arel.taken
else
ids.size
end
# 11 ids with limit 3, offset 9 should give 2 results.
- if @relation.skipped && (ids.size - @relation.skipped < expected_size)
- expected_size = ids.size - @relation.skipped
+ if arel.skipped && (ids.size - arel.skipped < expected_size)
+ expected_size = ids.size - arel.skipped
end
if result.size == expected_size
Oops, something went wrong.

0 comments on commit a04486d

Please sign in to comment.