diff options
-rw-r--r-- | lib/arel/predicates.rb | 2 | ||||
-rw-r--r-- | lib/arel/primitives/attribute.rb | 28 | ||||
-rw-r--r-- | lib/arel/primitives/expression.rb | 32 | ||||
-rw-r--r-- | lib/arel/relations/aggregation.rb | 6 | ||||
-rw-r--r-- | lib/arel/relations/compound.rb | 1 | ||||
-rw-r--r-- | lib/arel/relations/join.rb | 31 | ||||
-rw-r--r-- | lib/arel/relations/nil.rb | 1 | ||||
-rw-r--r-- | lib/arel/relations/projection.rb | 13 | ||||
-rw-r--r-- | lib/arel/relations/recursion.rb | 1 | ||||
-rw-r--r-- | lib/arel/relations/table.rb | 10 | ||||
-rw-r--r-- | spec/arel/integration/joins/with_adjacency_spec.rb | 12 |
11 files changed, 72 insertions, 65 deletions
diff --git a/lib/arel/predicates.rb b/lib/arel/predicates.rb index 7faaebfc17..cad4969952 100644 --- a/lib/arel/predicates.rb +++ b/lib/arel/predicates.rb @@ -55,7 +55,7 @@ module Arel end class Match < Binary - alias_method :regexp, :operand2 + def predicate_sql; 'LIKE' end end class In < Binary diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 22ee37524e..9d5b98474f 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -20,17 +20,9 @@ module Arel def column original_relation.column_for(self) end - - def original_relation - relation.relation_for(self) - end - - def original_attribute - original_relation[self] - end - + def format(object) - object.to_sql(formatter) + object.to_sql(Sql::Attribute.new(self)) end def to_sql(formatter = Sql::WhereCondition.new(relation)) @@ -45,6 +37,17 @@ module Arel ancestor == other.ancestor end + module Origin + def original_relation + relation.relation_for(self) + end + + def original_attribute + original_relation[self] + end + end + include Origin + module Transformations def self.included(klass) klass.hash_on :name @@ -134,10 +137,5 @@ module Arel end end include Expressions - - private - def formatter - Sql::Attribute.new(self) - end end end
\ No newline at end of file diff --git a/lib/arel/primitives/expression.rb b/lib/arel/primitives/expression.rb index 10e72f426e..3cde9d8724 100644 --- a/lib/arel/primitives/expression.rb +++ b/lib/arel/primitives/expression.rb @@ -7,22 +7,6 @@ module Arel def initialize(attribute, function_sql, aliaz = nil, ancestor = nil) @attribute, @function_sql, @alias, @ancestor = attribute, function_sql, aliaz, ancestor end - - module Transformations - def as(aliaz) - Expression.new(attribute, function_sql, aliaz, self) - end - - def bind(new_relation) - # new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self) - self - end - - def to_attribute - Attribute.new(relation, @alias, :ancestor => self) - end - end - include Transformations def to_sql(formatter = Sql::SelectClause.new(relation)) formatter.expression self @@ -39,5 +23,21 @@ module Arel ancestor == other.ancestor and @alias == other.alias end + + module Transformations + def as(aliaz) + Expression.new(attribute, function_sql, aliaz, self) + end + + def bind(new_relation) + # new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self) + self + end + + def to_attribute + Attribute.new(relation, @alias, :ancestor => self) + end + end + include Transformations end end
\ No newline at end of file diff --git a/lib/arel/relations/aggregation.rb b/lib/arel/relations/aggregation.rb index 55f24997cc..4c4ef0dfed 100644 --- a/lib/arel/relations/aggregation.rb +++ b/lib/arel/relations/aggregation.rb @@ -22,4 +22,10 @@ module Arel self.class == other.class and self.relation == other.relation end end + + class Relation + def externalize + aggregation?? Aggregation.new(self) : self + end + end end
\ No newline at end of file diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index 94616ff14f..1459c163cf 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -11,6 +11,7 @@ module Arel @attributes ||= relation.attributes.collect { |a| a.bind(self) } end + # XXX def relation_for(attribute) join? && relation.relation_for(attribute) || self[attribute] && self end diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index 05ed6efc23..92bbbd5626 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -16,39 +16,42 @@ module Arel ) end + def table_sql(formatter = Sql::TableReference.new(self)) + relation1.externalize.table_sql(formatter) + end + def joins(environment, formatter = Sql::TableReference.new(environment)) this_join = [ join_sql, - externalize(relation2).table_sql(formatter), + relation2.externalize.table_sql(formatter), ("ON" unless predicates.blank?), - (predicates + externalize(relation2).selects).collect { |p| p.bind(environment).to_sql }.join(' AND ') + (predicates + relation2.externalize.selects).collect { |p| p.bind(environment).to_sql }.join(' AND ') ].compact.join(" ") [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") end def attributes - @attributes ||= (externalize(relation1).attributes + - externalize(relation2).attributes).collect { |a| a.bind(self) } + @attributes ||= (relation1.externalize.attributes + + relation2.externalize.attributes).collect { |a| a.bind(self) } end + # XXX def selects - (externalize(relation1).selects).collect { |s| s.bind(self) } - end - - def table_sql(formatter = Sql::TableReference.new(self)) - externalize(relation1).table_sql(formatter) + (relation1.externalize.selects).collect { |s| s.bind(self) } end + # XXX def relation_for(attribute) [ - externalize(relation1).relation_for(attribute), - externalize(relation2).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 end + # TESTME def aggregation? relation1.aggregation? or relation2.aggregation? end @@ -56,11 +59,5 @@ module Arel def join? true end - - private - # FIXME - make instance method - def externalize(relation) - relation.aggregation?? Aggregation.new(relation) : relation - end end end
\ No newline at end of file diff --git a/lib/arel/relations/nil.rb b/lib/arel/relations/nil.rb index 34f9ea8722..e78ebe6088 100644 --- a/lib/arel/relations/nil.rb +++ b/lib/arel/relations/nil.rb @@ -1,5 +1,6 @@ module Arel class Nil < Relation + # XXX include Recursion::BaseCase def table_sql(formatter = nil); '' end diff --git a/lib/arel/relations/projection.rb b/lib/arel/relations/projection.rb index 66aa61f1d9..87b952d672 100644 --- a/lib/arel/relations/projection.rb +++ b/lib/arel/relations/projection.rb @@ -10,18 +10,19 @@ module Arel @attributes ||= projections.collect { |p| p.bind(self) } end - def ==(other) - self.class == other.class and - relation == other.relation and - projections == other.projections - end - def aggregation? attributes.any?(&:aggregation?) end + # XXX def relation_for(attribute) self[attribute] && self || relation.relation_for(attribute) end + + def ==(other) + self.class == other.class and + relation == other.relation and + projections == other.projections + end end end
\ No newline at end of file diff --git a/lib/arel/relations/recursion.rb b/lib/arel/relations/recursion.rb index c5ca171fd2..7507613a12 100644 --- a/lib/arel/relations/recursion.rb +++ b/lib/arel/relations/recursion.rb @@ -1,6 +1,7 @@ module Arel module Recursion module BaseCase + # XXX def table self end diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb index 5c3ba6c575..b95e51a307 100644 --- a/lib/arel/relations/table.rb +++ b/lib/arel/relations/table.rb @@ -20,11 +20,6 @@ module Arel self[attribute] and columns.detect { |c| c.name == attribute.name.to_s } end - def ==(other) - self.class == other.class and - name == other.name - end - def columns @columns ||= engine.columns(name, "#{name} Columns") end @@ -32,5 +27,10 @@ module Arel def reset @attributes = @columns = nil end + + def ==(other) + self.class == other.class and + name == other.name + end end end
\ No newline at end of file diff --git a/spec/arel/integration/joins/with_adjacency_spec.rb b/spec/arel/integration/joins/with_adjacency_spec.rb index eaa0b6cdf5..74d461319c 100644 --- a/spec/arel/integration/joins/with_adjacency_spec.rb +++ b/spec/arel/integration/joins/with_adjacency_spec.rb @@ -22,7 +22,7 @@ module Arel describe 'when joining with a selection on the same relation' do it 'manufactures sql aliasing the tables properly' do @relation1 \ - .join(@relation2.select(@relation2[:id].eq(1))) \ + .join(@relation2.select(@relation2[:id].eq(1))) \ .on(@predicate) \ .to_sql.should be_like(" SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name` @@ -35,8 +35,8 @@ module Arel describe 'when the selection occurs before the alias' do it 'manufactures sql aliasing the predicates properly' do relation2 = @relation1.select(@relation1[:id].eq(1)).alias - @relation1 \ - .join(relation2) \ + @relation1 \ + .join(relation2) \ .on(relation2[:id].eq(@relation1[:id])) \ .to_sql.should be_like(" SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name` @@ -55,8 +55,10 @@ module Arel describe 'when joining left-associatively' do it 'manufactures sql aliasing the tables properly' do - @relation1 \ - .join(@relation2.join(@relation3).on(@relation2[:id].eq(@relation3[:id]))) \ + @relation1 \ + .join(@relation2 \ + .join(@relation3) \ + .on(@relation2[:id].eq(@relation3[:id]))) \ .on(@relation1[:id].eq(@relation2[:id])) \ .to_sql.should be_like(" SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name` |