diff options
-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 | ||||
-rw-r--r-- | spec/arel/engines/sql/unit/relations/having_spec.rb | 36 | ||||
-rw-r--r-- | spec/schemas/mysql_schema.rb | 7 | ||||
-rw-r--r-- | spec/schemas/postgresql_schema.rb | 7 | ||||
-rw-r--r-- | spec/schemas/sqlite3_schema.rb | 7 |
10 files changed, 86 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 diff --git a/spec/arel/engines/sql/unit/relations/having_spec.rb b/spec/arel/engines/sql/unit/relations/having_spec.rb new file mode 100644 index 0000000000..dd170a256f --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/having_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +module Arel + describe Having do + before do + @relation = Table.new(:developers) + end + + describe '#to_sql' do + describe 'when given a predicate' do + it "manufactures sql with where clause conditions" do + sql = @relation.group(@relation[:department]).having("MIN(salary) > 1000").to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `developers`.`id`, `developers`.`name`, `developers`.`salary`, `developers`.`department` + FROM `developers` + GROUP BY `developers`.`department` + HAVING MIN(salary) > 1000 + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "developers"."id", "developers"."name", "developers"."salary", "developers"."department" + FROM "developers" + GROUP BY "developers"."department" + HAVING MIN(salary) > 1000 + }) + end + end + end + end + end +end + diff --git a/spec/schemas/mysql_schema.rb b/spec/schemas/mysql_schema.rb index dc2558fd6a..cb4c746776 100644 --- a/spec/schemas/mysql_schema.rb +++ b/spec/schemas/mysql_schema.rb @@ -11,6 +11,13 @@ sql = <<-SQL user_id INTEGER NOT NULL, camera_id INTEGER NOT NULL ); + DROP TABLE IF EXISTS developers; + CREATE TABLE developers ( + id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + salary INTEGER NOT NULL, + department VARCHAR(255) NOT NULL + ); SQL sql.split(/;/).select(&:present?).each do |sql_statement| diff --git a/spec/schemas/postgresql_schema.rb b/spec/schemas/postgresql_schema.rb index 30fa665902..8b7dac1c41 100644 --- a/spec/schemas/postgresql_schema.rb +++ b/spec/schemas/postgresql_schema.rb @@ -11,6 +11,13 @@ sql = <<-SQL user_id INTEGER NOT NULL, camera_id INTEGER NOT NULL ); + DROP TABLE IF EXISTS developers; + CREATE TABLE developers ( + id SERIAL PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + salary INTEGER NOT NULL, + department VARCHAR(255) NOT NULL + ); SQL sql.split(/;/).select(&:present?).each do |sql_statement| diff --git a/spec/schemas/sqlite3_schema.rb b/spec/schemas/sqlite3_schema.rb index 94d224520e..9dbb62428e 100644 --- a/spec/schemas/sqlite3_schema.rb +++ b/spec/schemas/sqlite3_schema.rb @@ -11,6 +11,13 @@ sql = <<-SQL user_id INTEGER NOT NULL, camera_id INTEGER NOT NULL ); + DROP TABLE IF EXISTS developers; + CREATE TABLE developers ( + id INTEGER NOT NULL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + salary INTEGER NOT NULL, + department VARCHAR(255) NOT NULL + ); SQL sql.split(/;/).select(&:present?).each do |sql_statement| |