diff options
Diffstat (limited to 'lib/arel')
-rw-r--r-- | lib/arel/relations/aggregation.rb | 6 | ||||
-rw-r--r-- | lib/arel/relations/compound.rb | 10 | ||||
-rw-r--r-- | lib/arel/relations/grouping.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 10 | ||||
-rw-r--r-- | lib/arel/sql/formatters.rb | 4 |
5 files changed, 23 insertions, 11 deletions
diff --git a/lib/arel/relations/aggregation.rb b/lib/arel/relations/aggregation.rb index 9a34ea5d89..7e9cdfe612 100644 --- a/lib/arel/relations/aggregation.rb +++ b/lib/arel/relations/aggregation.rb @@ -11,13 +11,17 @@ module Arel end def table_sql(formatter = Sql::TableReference.new(relation)) - relation.to_sql(formatter) + formatter.select relation.select_sql, self end def attributes @attributes ||= relation.attributes.collect(&:to_attribute).collect { |a| a.bind(self) } end + def name + relation.name + '_aggregation' + end + def ==(other) Aggregation === other and self.relation == other.relation diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index 9921568157..f8af190644 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -12,7 +12,15 @@ module Arel end def selects - @selects || relation.selects.collect { |s| s.bind(self) } + @selects ||= relation.selects.collect { |s| s.bind(self) } + end + + def groupings + @groupings ||= relation.groupings.collect { |g| g.bind(self) } + end + + def orders + @orders ||= relation.orders.collect { |o| o.bind(self) } end end end
\ No newline at end of file diff --git a/lib/arel/relations/grouping.rb b/lib/arel/relations/grouping.rb index 6d11299051..de8643278b 100644 --- a/lib/arel/relations/grouping.rb +++ b/lib/arel/relations/grouping.rb @@ -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/relation.rb b/lib/arel/relations/relation.rb index 3704eb9318..9bf7e740aa 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -5,7 +5,12 @@ module Arel end def to_sql(formatter = Sql::SelectStatement.new(self)) - formatter.select [ + formatter.select select_sql, self + end + alias_method :to_s, :to_sql + + def select_sql + [ "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ')}", "FROM #{table_sql(Sql::TableReference.new(self))}", (joins(self) unless joins(self).blank? ), @@ -14,9 +19,8 @@ module Arel ("GROUP BY #{groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) }.join(', ')}" unless groupings.blank? ), ("LIMIT #{taken}" unless taken.blank? ), ("OFFSET #{skipped}" unless skipped.blank? ) - ].compact.join("\n"), name + ].compact.join("\n") end - alias_method :to_s, :to_sql def inclusion_predicate_sql "IN" diff --git a/lib/arel/sql/formatters.rb b/lib/arel/sql/formatters.rb index cc7a1fa7d5..068fb8d22d 100644 --- a/lib/arel/sql/formatters.rb +++ b/lib/arel/sql/formatters.rb @@ -21,7 +21,7 @@ module Arel end def select(select_sql, table) - "(#{select_sql}) AS #{quote_table_name(table)}" + "(#{select_sql}) AS #{quote_table_name(name_for(table))}" end def value(value) @@ -80,7 +80,7 @@ module Arel class TableReference < Formatter def select(select_sql, table) - "(#{select_sql}) AS #{quote_table_name(table)}" + "(#{select_sql}) AS #{quote_table_name(name_for(table))}" end def table(table) |