From bd00a461e042a77aef48822040897467c544cd1a Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Mon, 28 Dec 2009 12:58:30 -0300 Subject: Added having to use in combination with group to filter records. --- lib/arel/engines/sql/formatters.rb | 6 ++++++ lib/arel/engines/sql/relations/relation.rb | 6 ++++++ 2 files changed, 12 insertions(+) (limited to 'lib/arel/engines') diff --git a/lib/arel/engines/sql/formatters.rb b/lib/arel/engines/sql/formatters.rb index 626803a887..e05dc3eb93 100644 --- a/lib/arel/engines/sql/formatters.rb +++ b/lib/arel/engines/sql/formatters.rb @@ -57,6 +57,12 @@ module Arel end end + class HavingClause < PassThrough + def attribute(attribute) + attribute + end + end + class WhereCondition < Formatter def attribute(attribute) "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" diff --git a/lib/arel/engines/sql/relations/relation.rb b/lib/arel/engines/sql/relations/relation.rb index 12b328151f..940f985358 100644 --- a/lib/arel/engines/sql/relations/relation.rb +++ b/lib/arel/engines/sql/relations/relation.rb @@ -17,6 +17,7 @@ module Arel (joins(self) unless joins(self).blank? ), ("WHERE #{where_clauses.join("\n\tAND ")}" unless wheres.blank? ), ("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ), + ("HAVING #{having_clauses.join(', ')}" unless havings.blank? ), ("#{locked}" unless locked.blank? ) build_query \ @@ -32,6 +33,7 @@ module Arel (joins(self) unless joins(self).blank? ), ("WHERE #{where_clauses.join("\n\tAND ")}" unless wheres.blank? ), ("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ), + ("HAVING #{having_clauses.join(', ')}" unless havings.blank? ), ("ORDER BY #{order_clauses.join(', ')}" unless orders.blank? ), ("LIMIT #{taken}" unless taken.blank? ), ("OFFSET #{skipped}" unless skipped.blank? ), @@ -69,6 +71,10 @@ module Arel groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) } end + def having_clauses + havings.collect { |g| g.to_sql(Sql::HavingClause.new(self)) } + end + def order_clauses orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) } end -- cgit v1.2.3