diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/algebra/relations.rb | 1 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/having.rb | 13 | ||||
-rw-r--r-- | lib/arel/algebra/relations/relation.rb | 3 | ||||
-rw-r--r-- | lib/arel/algebra/relations/utilities/compound.rb | 2 | ||||
-rw-r--r-- | lib/arel/engines/sql/formatters.rb | 6 | ||||
-rw-r--r-- | lib/arel/engines/sql/relations/relation.rb | 6 |
6 files changed, 29 insertions, 2 deletions
diff --git a/lib/arel/algebra/relations.rb b/lib/arel/algebra/relations.rb index 951b69d970..9481554667 100644 --- a/lib/arel/algebra/relations.rb +++ b/lib/arel/algebra/relations.rb @@ -7,6 +7,7 @@ require 'arel/algebra/relations/writes' require 'arel/algebra/relations/operations/alias' require 'arel/algebra/relations/operations/from' require 'arel/algebra/relations/operations/group' +require 'arel/algebra/relations/operations/having' require 'arel/algebra/relations/operations/join' require 'arel/algebra/relations/operations/order' require 'arel/algebra/relations/operations/project' diff --git a/lib/arel/algebra/relations/operations/having.rb b/lib/arel/algebra/relations/operations/having.rb new file mode 100644 index 0000000000..cd16535609 --- /dev/null +++ b/lib/arel/algebra/relations/operations/having.rb @@ -0,0 +1,13 @@ +module Arel + class Having < Compound + attributes :relation, :havings + deriving :== + + def initialize(relation, *havings, &block) + @relation = relation + @havings = (havings + arguments_from_block(relation, &block)) \ + .collect { |g| g.bind(relation) } + end + end +end + diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index b24e7c24d0..e848c8aa1c 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -43,7 +43,7 @@ module Arel join(other_relation, OuterJoin) end - [:where, :project, :order, :take, :skip, :group, :from].each do |operation_name| + [:where, :project, :order, :take, :skip, :group, :from, :having].each do |operation_name| class_eval <<-OPERATION, __FILE__, __LINE__ def #{operation_name}(*arguments, &block) arguments.all?(&:blank?) && !block_given?? self : #{operation_name.to_s.classify}.new(self, *arguments, &block) @@ -131,6 +131,7 @@ module Arel def orders; [] end def inserts; [] end def groupings; [] end + def havings; [] end def joins(formatter = nil); nil end # FIXME def taken; nil end def skipped; nil end diff --git a/lib/arel/algebra/relations/utilities/compound.rb b/lib/arel/algebra/relations/utilities/compound.rb index 9967472d88..1acf92fef8 100644 --- a/lib/arel/algebra/relations/utilities/compound.rb +++ b/lib/arel/algebra/relations/utilities/compound.rb @@ -5,7 +5,7 @@ module Arel :column_for, :engine, :sources, :locked, :to => :relation - [:attributes, :wheres, :groupings, :orders].each do |operation_name| + [:attributes, :wheres, :groupings, :orders, :havings].each do |operation_name| class_eval <<-OPERATION, __FILE__, __LINE__ def #{operation_name} @#{operation_name} ||= relation.#{operation_name}.collect { |o| o.bind(self) } 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 |