diff options
Diffstat (limited to 'lib/arel')
-rw-r--r-- | lib/arel/relations/compound.rb | 2 | ||||
-rw-r--r-- | lib/arel/relations/join.rb | 34 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 5 | ||||
-rw-r--r-- | lib/arel/sql.rb | 1 |
4 files changed, 20 insertions, 22 deletions
diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index f4d6b199b5..735f586114 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -6,7 +6,7 @@ module Arel delegate :joins, :selects, :orders, :groupings, :inserts, :taken, :skipped, :name, :alias, :aggregation?, :prefix_for, :column_for, - :engine, + :engine, :name_for, :to => :relation def attributes diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index 1fb0055caa..cb24ab7717 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -1,6 +1,4 @@ module Arel - # TODO Explicitly model recursive structural decomposition / polymorphism - # TODO Explicitly model the namer/externalizer using interpreter jargon class Join < Relation attr_reader :join_sql, :relation1, :relation2, :predicates @@ -26,13 +24,7 @@ module Arel end def prefix_for(attribute) - externalize(relation_for(attribute)).table_sql # externalize or something? - end - - def relation_for(attribute) - [relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2| - (attribute % a1).size <=> (attribute % a2).size - end.relation.relation_for(attribute) + externalize(relation_for(attribute)).prefix_for(attribute) end # TESTME: Not sure which scenario needs this method, was driven by failing tests in ActiveRecord @@ -45,14 +37,9 @@ module Arel join_sql, externalize(relation2).table_sql(formatter), ("ON" unless predicates.blank?), - predicates.collect { |p| p.bind(self).to_sql }.join(' AND ') + predicates.collect { |p| p.bind(formatter.christener).to_sql }.join(' AND ') ].compact.join(" ") - [relation1.joins(formatter), relation2.joins(formatter), this_join].compact.join(" ") - end - - # FIXME - def name - 'user' + [relation1.joins(formatter), this_join, relation2.joins(formatter)].compact.join(" ") end def selects @@ -72,6 +59,13 @@ module Arel @relation_names[relation] end + protected + def relation_for(attribute) + [relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2| + (attribute % a1).size <=> (attribute % a2).size + end.relation.relation_for(attribute) + end + private def externalize(relation) Externalizer.new(self, relation) @@ -82,10 +76,8 @@ module Arel def table_sql(formatter = Sql::TableReference.new(self)) if relation.aggregation? - relation.to_sql(formatter) + ' AS ' + engine.quote_table_name(christener.name_for(relation) + '_aggregation') + relation.to_sql(formatter) + ' AS ' + engine.quote_table_name(formatter.name_for(relation) + (relation.aggregation?? '_aggregation' : '')) else - # not an aggregation - # all this can be is a join or a compound or a table relation.table_sql(formatter) end end @@ -97,6 +89,10 @@ module Arel def attributes relation.aggregation?? relation.attributes.collect(&:to_attribute) : relation.attributes end + + def prefix_for(attribute) + christener.name_for(relation) + (relation.aggregation?? '_aggregation' : '') + end end end end
\ No newline at end of file diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 02fc1c6284..7b414b9768 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -115,10 +115,11 @@ module Arel include Externalizable def to_sql(formatter = Sql::SelectStatement.new(engine)) + tr = Sql::TableReference.new(self) formatter.select [ "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(engine)) }.join(', ')}", - "FROM #{table_sql(Sql::TableReference.new(self))}", - (joins(Sql::TableReference.new(self)) unless joins.blank? ), + "FROM #{table_sql(tr)}", + (joins(tr) unless joins.blank? ), ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(engine)) }.join("\n\tAND ")}" unless selects.blank? ), ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(engine)) }.join(', ')}" unless orders.blank? ), ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), diff --git a/lib/arel/sql.rb b/lib/arel/sql.rb index 34a730c5f3..7a620009db 100644 --- a/lib/arel/sql.rb +++ b/lib/arel/sql.rb @@ -68,6 +68,7 @@ module Arel end class TableReference < Formatter + attr_reader :christener delegate :name_for, :to => :@christener def initialize(christener) |