diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-04-28 14:01:33 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-04-28 14:01:33 -0700 |
commit | 09b2406e829cee71e4543a75fc4fbe226157d35e (patch) | |
tree | ee8a230c3b9c1d4f31cef38defe269f6b8868859 /lib/arel/relations | |
parent | 3ae7a601e962468468eac1399ebdfd7113730310 (diff) | |
download | rails-09b2406e829cee71e4543a75fc4fbe226157d35e.tar.gz rails-09b2406e829cee71e4543a75fc4fbe226157d35e.tar.bz2 rails-09b2406e829cee71e4543a75fc4fbe226157d35e.zip |
automatically aliasing tables
Diffstat (limited to 'lib/arel/relations')
-rw-r--r-- | lib/arel/relations/grouping.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/join.rb | 48 | ||||
-rw-r--r-- | lib/arel/relations/nil.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 6 | ||||
-rw-r--r-- | lib/arel/relations/table.rb | 4 |
5 files changed, 39 insertions, 27 deletions
diff --git a/lib/arel/relations/grouping.rb b/lib/arel/relations/grouping.rb index ccca600360..41af4ba0f6 100644 --- a/lib/arel/relations/grouping.rb +++ b/lib/arel/relations/grouping.rb @@ -15,5 +15,9 @@ module Arel def aggregation? true end + + def name + relation.name + '_aggregation' + end end end
\ No newline at end of file diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index e5ca49c284..ce236d79d0 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -24,9 +24,13 @@ module Arel end def prefix_for(attribute) - externalize([relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2| + name_for(relation_for(attribute)) + 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).prefix_for(attribute) + end.relation.relation_for(attribute) end # TESTME: Not sure which scenario needs this method, was driven by failing tests in ActiveRecord @@ -34,10 +38,10 @@ module Arel (relation1[attribute] || relation2[attribute]).column end - def joins + def joins(formatter = Sql::TableReference.new(engine)) this_join = [ join_sql, - externalize(relation2).table_sql, + externalize(relation2).table_sql(formatter), ("ON" unless predicates.blank?), predicates.collect { |p| p.bind(self).to_sql }.join(' AND ') ].compact.join(" ") @@ -45,29 +49,35 @@ module Arel end def selects - externalize(relation1).selects + externalize(relation2).selects + (externalize(relation1).selects + externalize(relation2).selects).collect { |s| s.bind(self) } end - def table_sql - externalize(relation1).table_sql + def table_sql(formatter = Sql::TableReference.new(engine)) + externalize(relation1).table_sql(formatter) + end + + def name_for(relation) + @used_names ||= Hash.new(0) + @relation_names ||= Hash.new do |h, k| + @used_names[k.name] += 1 + h[k] = k.name + (@used_names[k.name] > 1 ? "_#{@used_names[k.name]}" : '') + end + @relation_names[relation] end private def externalize(relation) - Externalizer.new(relation) + Externalizer.new(self, relation) end - Externalizer = Struct.new(:relation) do + Externalizer = Struct.new(:christener, :relation) do delegate :engine, :to => :relation - def table_sql - case - when relation.aggregation? - relation.to_sql(Sql::TableReference.new(engine)) - when relation.alias? - relation.table_sql + ' AS ' + engine.quote_table_name(relation.alias.to_s) + def table_sql(formatter = Sql::TableReference.new(engine)) + if relation.aggregation? + relation.to_sql(formatter) + ' AS ' + engine.quote_table_name(christener.name_for(relation)) else - relation.table_sql + relation.table_sql(formatter) + (relation.name != christener.name_for(relation) ? " AS " + engine.quote_table_name(christener.name_for(relation)) : '') end end @@ -78,12 +88,6 @@ module Arel def attributes relation.aggregation?? relation.attributes.collect(&:to_attribute) : relation.attributes end - - def prefix_for(attribute) - if relation[attribute] - relation.alias?? relation.alias : relation.prefix_for(attribute) - end - end end end end
\ No newline at end of file diff --git a/lib/arel/relations/nil.rb b/lib/arel/relations/nil.rb index 3c1d413953..67eb2d3a62 100644 --- a/lib/arel/relations/nil.rb +++ b/lib/arel/relations/nil.rb @@ -1,7 +1,7 @@ module Arel class Nil < Relation - def table_sql; '' end - + def table_sql(formatter = nil); '' end + def name; '' end def to_s; '' end def ==(other) diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 3bd40e8f6b..0a5bf2ff24 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -103,6 +103,10 @@ module Arel def alias? false end + + def relation_for(attribute) + self[attribute] and self + end end include Externalizable @@ -116,7 +120,7 @@ module Arel ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), ("LIMIT #{taken}" unless taken.blank? ), ("OFFSET #{skipped}" unless skipped.blank? ) - ].compact.join("\n"), self.alias + ].compact.join("\n") end alias_method :to_s, :to_sql diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb index b627558caf..7637a471bf 100644 --- a/lib/arel/relations/table.rb +++ b/lib/arel/relations/table.rb @@ -36,8 +36,8 @@ module Arel @attributes = @columns = nil end - def table_sql - engine.quote_table_name(name) + def table_sql(formatter = Sql::TableReference.new(engine)) + formatter.table name end end end
\ No newline at end of file |