From 14210279b23788d47a18f0615f5e20234550c8ac Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sun, 18 May 2008 22:11:05 -0700 Subject: can't remember what i was working on --- README | 2 +- lib/arel/relations/aggregation.rb | 6 +++++- lib/arel/relations/compound.rb | 10 +++++++++- lib/arel/relations/grouping.rb | 4 ---- lib/arel/relations/relation.rb | 10 +++++++--- lib/arel/sql/formatters.rb | 4 ++-- .../arel/integration/joins/with_aggregations_spec.rb | 10 ++++++++-- spec/arel/unit/relations/alias_spec.rb | 20 ++++++++++++++++++++ 8 files changed, 52 insertions(+), 14 deletions(-) diff --git a/README b/README index bb8c6eb71a..efd379d784 100644 --- a/README +++ b/README @@ -1,6 +1,6 @@ == Abstract == -Arel is a Relational Algebra for Ruby. It 1) simplifies the generation of both the simplest and the most complex of SQL queries and it 2) transparently adapts to various RDBMS systems. It is intended to be a framework framework; that is, you can build your own ORM with it, focusing on innovative object and collection modeling as opposed to database compatibility and query generation. +Arel is a Relational Algebra for Ruby. It 1) simplifies the generation complex of SQL queries and it 2) transparently adapts to various RDBMS systems. It is intended to be a framework framework; that is, you can build your own ORM with it, focusing on innovative object and collection modeling as opposed to database compatibility and query generation. == A Gentle Introduction == 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) diff --git a/spec/arel/integration/joins/with_aggregations_spec.rb b/spec/arel/integration/joins/with_aggregations_spec.rb index db34bb1b46..ab904d103d 100644 --- a/spec/arel/integration/joins/with_aggregations_spec.rb +++ b/spec/arel/integration/joins/with_aggregations_spec.rb @@ -39,8 +39,14 @@ module Arel end describe 'with the aggregation on both sides' do - it '' do - @aggregation.join(@aggregation.alias).on(@predicate).to_sql.should == '' + it 'it properly aliases the aggregations' do + aggregation2 = @aggregation.alias + @aggregation.join(aggregation2).on(aggregation2[:user_id].eq(@aggregation[:user_id])).to_sql.should be_like(" + SELECT `photos_aggregation`.`user_id`, `photos_aggregation`.`cnt`, `photos_aggregation_2`.`user_id`, `photos_aggregation_2`.`cnt` + FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_aggregation` + INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_aggregation_2` + ON `photos_aggregation_2`.`user_id` = `photos_aggregation`.`user_id` + ") end end diff --git a/spec/arel/unit/relations/alias_spec.rb b/spec/arel/unit/relations/alias_spec.rb index 96e06f48bc..ce35debadf 100644 --- a/spec/arel/unit/relations/alias_spec.rb +++ b/spec/arel/unit/relations/alias_spec.rb @@ -12,5 +12,25 @@ module Arel (aliaz = Alias.new(@relation)).should == aliaz end end + + describe '#to_sql' do + describe 'when there is no ambiguity' do + it 'does not alias table names anywhere a table name can appear' do + @relation \ + .select(@relation[:id].eq(1)) \ + .order(@relation[:id]) \ + .project(@relation[:id]) \ + .group(@relation[:id]) \ + .alias \ + .to_sql.should be_like(" + SELECT `users`.`id` + FROM `users` + WHERE `users`.`id` = 1 + ORDER BY `users`.`id` + GROUP BY `users`.`id` + ") + end + end + end end end \ No newline at end of file -- cgit v1.2.3