diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-01 21:23:55 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-01 21:23:55 -0700 |
commit | 6c61ebb4d677f9bc5e1a669e7351c1226cbc9582 (patch) | |
tree | e55074078699abbf4cb3d9ecb6259702749ed1d5 | |
parent | 3c52d91b5dc8b582e03d9ad84e3b97a85273110e (diff) | |
download | rails-6c61ebb4d677f9bc5e1a669e7351c1226cbc9582.tar.gz rails-6c61ebb4d677f9bc5e1a669e7351c1226cbc9582.tar.bz2 rails-6c61ebb4d677f9bc5e1a669e7351c1226cbc9582.zip |
naming of aliased relations seemed to be solved; now aggregate relations are still broken
-rw-r--r-- | lib/arel/relations/compound.rb | 2 | ||||
-rw-r--r-- | lib/arel/relations/grouping.rb | 6 | ||||
-rw-r--r-- | lib/arel/relations/join.rb | 18 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 14 | ||||
-rw-r--r-- | lib/arel/relations/table.rb | 4 | ||||
-rw-r--r-- | lib/arel/sql.rb | 10 | ||||
-rw-r--r-- | spec/arel/unit/relations/join_spec.rb | 10 |
7 files changed, 38 insertions, 26 deletions
diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index ae848433be..f4d6b199b5 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, :table_sql, :inserts, :taken, + delegate :joins, :selects, :orders, :groupings, :inserts, :taken, :skipped, :name, :alias, :aggregation?, :prefix_for, :column_for, :engine, :to => :relation diff --git a/lib/arel/relations/grouping.rb b/lib/arel/relations/grouping.rb index 41af4ba0f6..2815f62b79 100644 --- a/lib/arel/relations/grouping.rb +++ b/lib/arel/relations/grouping.rb @@ -1,6 +1,6 @@ module Arel class Grouping < Compound - attr_reader :expressions, :groupings + attr_reader :groupings def initialize(relation, *groupings) @relation, @groupings = relation, groupings.collect { |g| g.bind(relation) } @@ -15,9 +15,5 @@ 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 6c12a0ea26..1fb0055caa 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -1,4 +1,6 @@ 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 @@ -24,7 +26,7 @@ module Arel end def prefix_for(attribute) - name_for(relation_for(attribute)) + externalize(relation_for(attribute)).table_sql # externalize or something? end def relation_for(attribute) @@ -38,14 +40,14 @@ module Arel (relation1[attribute] || relation2[attribute]).column end - def joins(formatter = Sql::TableReference.new(engine)) + def joins(formatter = Sql::TableReference.new(self)) this_join = [ join_sql, externalize(relation2).table_sql(formatter), ("ON" unless predicates.blank?), predicates.collect { |p| p.bind(self).to_sql }.join(' AND ') ].compact.join(" ") - [relation1.joins, relation2.joins, this_join].compact.join(" ") + [relation1.joins(formatter), relation2.joins(formatter), this_join].compact.join(" ") end # FIXME @@ -57,7 +59,7 @@ module Arel (externalize(relation1).selects + externalize(relation2).selects).collect { |s| s.bind(self) } end - def table_sql(formatter = Sql::TableReference.new(engine)) + def table_sql(formatter = Sql::TableReference.new(self)) externalize(relation1).table_sql(formatter) end @@ -78,11 +80,13 @@ module Arel Externalizer = Struct.new(:christener, :relation) do delegate :engine, :to => :relation - def table_sql(formatter = Sql::TableReference.new(engine)) + def table_sql(formatter = Sql::TableReference.new(self)) if relation.aggregation? - relation.to_sql(formatter) + ' AS ' + engine.quote_table_name(christener.name_for(relation)) + relation.to_sql(formatter) + ' AS ' + engine.quote_table_name(christener.name_for(relation) + '_aggregation') else - relation.table_sql(formatter) + (relation.name != christener.name_for(relation) ? " AS " + engine.quote_table_name(christener.name_for(relation)) : '') + # not an aggregation + # all this can be is a join or a compound or a table + relation.table_sql(formatter) end end diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 3453b9adc6..02fc1c6284 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -103,14 +103,22 @@ module Arel def relation_for(attribute) self[attribute] and self end + + def name_for(relation) + relation.name + end + + def table_sql(formatter = Sql::TableReference.new(self)) + formatter.table name, formatter.name_for(self) + end end include Externalizable def to_sql(formatter = Sql::SelectStatement.new(engine)) formatter.select [ "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(engine)) }.join(', ')}", - "FROM #{table_sql}", - (joins unless joins.blank? ), + "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(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? ), @@ -159,7 +167,7 @@ module Arel def orders; [] end def inserts; [] end def groupings; [] end - def joins; nil end + def joins(formatter = nil); nil end def taken; nil end def skipped; nil end end diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb index 7637a471bf..9c7d90d4fc 100644 --- a/lib/arel/relations/table.rb +++ b/lib/arel/relations/table.rb @@ -35,9 +35,5 @@ module Arel def reset @attributes = @columns = nil end - - def table_sql(formatter = Sql::TableReference.new(engine)) - formatter.table name - end end end
\ No newline at end of file diff --git a/lib/arel/sql.rb b/lib/arel/sql.rb index 3cb8d13680..34a730c5f3 100644 --- a/lib/arel/sql.rb +++ b/lib/arel/sql.rb @@ -68,12 +68,18 @@ module Arel end class TableReference < Formatter + delegate :name_for, :to => :@christener + + def initialize(christener) + @christener, @engine = christener, christener.engine + end + def select(select_sql) "(#{select_sql})" end - def table(name) - quote_table_name(name) + def table(name, aliaz) + quote_table_name(name) + (name != aliaz ? " AS " + engine.quote_table_name(aliaz) : '') end end diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb index f74bd32ef8..c08ff68f36 100644 --- a/spec/arel/unit/relations/join_spec.rb +++ b/spec/arel/unit/relations/join_spec.rb @@ -146,16 +146,18 @@ module Arel describe 'when joining the same relation to itself' do describe '#to_sql' do it '' do + relation2 = @relation1.alias + relation3 = @relation1.alias @relation1 \ - .join(@relation1.alias.join(@relation1.alias).on(@relation1[:id].eq(1))) \ - .on(@relation1[:id].eq(1)) \ + .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` FROM `users` INNER JOIN `users` AS `users_2` - ON `users`.`id` = 1 + ON `users_2`.`id` = `users_3`.`id` INNER JOIN `users` AS `users_3` - ON `users`.`id` = 1 + ON `users_2`.`id` = `users_3`.`id` ") end |