From 6e1450a2a646e416aaea003eff19b7703c563bed Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sat, 17 May 2008 23:03:56 -0700 Subject: performance enhancements --- lib/arel/predicates.rb | 2 +- lib/arel/primitives/attribute.rb | 12 +++++------- lib/arel/relations/aggregation.rb | 2 +- lib/arel/relations/compound.rb | 6 +++--- lib/arel/relations/join.rb | 28 +++++++++++----------------- lib/arel/relations/nil.rb | 1 - lib/arel/relations/recursion.rb | 4 ---- 7 files changed, 21 insertions(+), 34 deletions(-) (limited to 'lib') diff --git a/lib/arel/predicates.rb b/lib/arel/predicates.rb index 93c7ed0099..3d01d5872f 100644 --- a/lib/arel/predicates.rb +++ b/lib/arel/predicates.rb @@ -17,7 +17,7 @@ module Arel end def bind(relation) - self.class.new(operand1.bind(relation), operand2.bind(relation)) + self.class.new(relation[operand1] || operand1, relation[operand2] || operand2) end def to_sql(formatter = nil) diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 5e769ac0eb..cb564c1587 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -38,16 +38,14 @@ module Arel end def original_relation - @original_relation ||= relation.relation_for(self) + original_attribute.relation end def original_attribute - @original_attribute ||= original_relation[self] + @original_attribute ||= history.detect { |a| !a.join? } end - module Transformations - delegate :size, :to => :history - + module Transformations def self.included(klass) klass.send :alias_method, :eql?, :== end @@ -75,8 +73,8 @@ module Arel @history ||= [self] + (ancestor ? ancestor.history : []) end - def match?(other) - history.last == other.history.last + def join? + relation.join? end def root diff --git a/lib/arel/relations/aggregation.rb b/lib/arel/relations/aggregation.rb index d409c34284..a6f40be786 100644 --- a/lib/arel/relations/aggregation.rb +++ b/lib/arel/relations/aggregation.rb @@ -15,7 +15,7 @@ module Arel end def attributes - @attributes ||= relation.attributes.collect(&:to_attribute) + @attributes ||= relation.attributes.collect(&:to_attribute).collect { |a| a.bind(self) } end def ==(other) diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index abc5838805..9921568157 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -10,9 +10,9 @@ module Arel def attributes @attributes ||= relation.attributes.collect { |a| a.bind(self) } end - - def relation_for(attribute) - join? && relation.relation_for(attribute) || has_attribute?(attribute) && self + + def selects + @selects || relation.selects.collect { |s| s.bind(self) } end end end \ No newline at end of file diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index abd4eae4f6..9cc9f95c81 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -7,14 +7,6 @@ module Arel def initialize(join_sql, relation1, relation2 = Nil.new, *predicates) @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates end - - def ==(other) - Join == other.class and - predicates == other.predicates and ( - (relation1 == other.relation1 and relation2 == other.relation2) or - (relation2 == other.relation1 and relation1 == other.relation2) - ) - end def table_sql(formatter = Sql::TableReference.new(self)) relation1.externalize.table_sql(formatter) @@ -25,7 +17,7 @@ module Arel join_sql, relation2.externalize.table_sql(formatter), ("ON" unless predicates.blank?), - (predicates + relation2.externalize.selects).collect { |p| p.bind(environment).to_sql(Sql::WhereClause.new(environment)) }.join(' AND ') + (ons + relation2.externalize.selects).collect { |p| p.bind(environment).to_sql(Sql::WhereClause.new(environment)) }.join(' AND ') ].compact.join(" ") [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") end @@ -39,14 +31,8 @@ module Arel relation1.externalize.selects end - def relation_for(attribute) - [ - relation1.externalize.relation_for(attribute), - relation2.externalize.relation_for(attribute) - ].max do |r1, r2| - a1, a2 = r1 && r1[attribute], r2 && r2[attribute] - attribute / a1 <=> attribute / a2 - end + def ons + @ons ||= @predicates.collect { |p| p.bind(self) } end # TESTME @@ -57,6 +43,14 @@ module Arel def join? true end + + def ==(other) + Join == other.class and + predicates == other.predicates and ( + (relation1 == other.relation1 and relation2 == other.relation2) or + (relation2 == other.relation1 and relation1 == other.relation2) + ) + end end class Relation diff --git a/lib/arel/relations/nil.rb b/lib/arel/relations/nil.rb index e8d4cbc481..c34fe71473 100644 --- a/lib/arel/relations/nil.rb +++ b/lib/arel/relations/nil.rb @@ -1,7 +1,6 @@ module Arel class Nil < Relation def table_sql(formatter = nil); '' end - def relation_for(attribute); nil end def name; '' end def ==(other) diff --git a/lib/arel/relations/recursion.rb b/lib/arel/relations/recursion.rb index c2fcf1bd21..848b059507 100644 --- a/lib/arel/relations/recursion.rb +++ b/lib/arel/relations/recursion.rb @@ -8,10 +8,6 @@ module Arel def table_sql(formatter = Sql::TableReference.new(self)) formatter.table self end - - def relation_for(attribute) - has_attribute?(attribute) && self - end end end end \ No newline at end of file -- cgit v1.2.3