diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/relations/compound.rb | 2 | ||||
-rw-r--r-- | lib/arel/relations/join.rb | 53 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 21 | ||||
-rw-r--r-- | lib/arel/relations/table.rb | 3 |
4 files changed, 38 insertions, 41 deletions
diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index 1658efd636..f0a3392e09 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -4,7 +4,7 @@ module Arel hash_on :relation delegate :joins, :selects, :orders, :groupings, :inserts, :taken, :skipped, :name, :alias, :aggregation?, :column_for, - :engine, :name_for, :table, :relation_for, + :engine, :name_for, :table, :relation_for, :table_sql, :to => :relation def attributes diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index a30c179e4e..01e7e36f37 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -1,7 +1,7 @@ module Arel class Join < Relation attr_reader :join_sql, :relation1, :relation2, :predicates - delegate :engine, :to => :relation1 + delegate :engine, :name, :to => :relation1 hash_on :relation1 def initialize(join_sql, relation1, relation2 = Nil.new, *predicates) @@ -16,23 +16,19 @@ module Arel ) end - def attributes - (externalize(relation1).attributes + - externalize(relation2).attributes).collect { |a| a.bind(self) } - end - - def joins(formatter = Sql::TableReference.new(self)) + def joins(environment, formatter = Sql::TableReference.new(environment)) this_join = [ join_sql, - if relation2.aggregation? - relation2.to_sql(formatter) - else - relation2.table.table_sql(formatter) - end, + externalize(relation2).table_sql(formatter), ("ON" unless predicates.blank?), - predicates.collect { |p| p.bind(formatter.environment).to_sql }.join(' AND ') + predicates.collect { |p| p.bind(environment).to_sql }.join(' AND ') ].compact.join(" ") - [relation1.joins(formatter), this_join, relation2.joins(formatter)].compact.join(" ") + [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") + end + + def attributes + (externalize(relation1).attributes + + externalize(relation2).attributes).collect { |a| a.bind(self) } end def selects @@ -40,20 +36,17 @@ module Arel end def table - relation1.aggregation?? relation1 : relation1.table + externalize(relation1).table + end + + def table_sql(formatter = Sql::TableReference.new(self)) + externalize(table).table_sql(formatter) end - - delegate :name, :to => :relation1 def relation_for(attribute) - x = [relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2| + externalize([relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2| (attribute % a1).size <=> (attribute % a2).size - end.relation - if x.aggregation? - x - else - x.relation_for(attribute) - end + end.relation).relation_for(attribute) end private @@ -66,6 +59,18 @@ module Arel relation.aggregation?? [] : relation.selects end + def table + relation.aggregation?? relation : relation.table + end + + def relation_for(attribute) + relation.aggregation?? relation : relation.relation_for(attribute) + end + + def table_sql(formatter = Sql::TableReference.new(relation)) + relation.aggregation?? relation.to_sql(formatter) : relation.table.table_sql(formatter) + end + def attributes relation.aggregation?? relation.attributes.collect(&:to_attribute) : relation.attributes end diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 440e6c9db6..14d2143a8b 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -4,6 +4,7 @@ module Arel Session.new end + # INVESTIGATE def name_for(relation) relation.name end @@ -12,23 +13,15 @@ module Arel formatter.select [ "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ')}", "FROM #{table_sql(Sql::TableReference.new(self))}", - (joins(Sql::TableReference.new(self)) unless joins.blank? ), - ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(self)) }.join("\n\tAND ")}" unless selects.blank? ), - ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) }.join(', ')}" unless orders.blank? ), - ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), - ("LIMIT #{taken}" unless taken.blank? ), - ("OFFSET #{skipped}" unless skipped.blank? ) + (joins(self) unless joins(self).blank? ), + ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(self)) }.join("\n\tAND ")}" unless selects.blank? ), + ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) }.join(', ')}" unless orders.blank? ), + ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), + ("LIMIT #{taken}" unless taken.blank? ), + ("OFFSET #{skipped}" unless skipped.blank? ) ].compact.join("\n"), name end alias_method :to_s, :to_sql - - def table_sql(formatter = Sql::TableReference.new(self)) - if table.aggregation? - table.to_sql(Sql::TableReference.new(self)) - else - table.table_sql(Sql::TableReference.new(self)) - end - end def inclusion_predicate_sql "IN" diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb index 735159508e..5c3ba6c575 100644 --- a/lib/arel/relations/table.rb +++ b/lib/arel/relations/table.rb @@ -3,8 +3,7 @@ module Arel include Recursion::BaseCase cattr_accessor :engine - attr_reader :name, :engine - + attr_reader :name, :engine hash_on :name def initialize(name, engine = Table.engine) |