aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/engines
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-12-28 12:58:30 -0300
committerEmilio Tagua <miloops@gmail.com>2009-12-28 12:58:30 -0300
commitbd00a461e042a77aef48822040897467c544cd1a (patch)
tree8272c2d78da319bde1ab6d736ce6f66b352a25e6 /lib/arel/engines
parent4fe6bdf195336d54b082ca26b96c5294d8aae3d1 (diff)
downloadrails-bd00a461e042a77aef48822040897467c544cd1a.tar.gz
rails-bd00a461e042a77aef48822040897467c544cd1a.tar.bz2
rails-bd00a461e042a77aef48822040897467c544cd1a.zip
Added having to use in combination with group to filter records.
Diffstat (limited to 'lib/arel/engines')
-rw-r--r--lib/arel/engines/sql/formatters.rb6
-rw-r--r--lib/arel/engines/sql/relations/relation.rb6
2 files changed, 12 insertions, 0 deletions
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