diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/algebra/relations/operations/having.rb | 18 | ||||
-rw-r--r-- | lib/arel/engines/memory/relations/operations.rb | 2 | ||||
-rw-r--r-- | lib/arel/engines/sql/relations/compiler.rb | 4 |
3 files changed, 14 insertions, 10 deletions
diff --git a/lib/arel/algebra/relations/operations/having.rb b/lib/arel/algebra/relations/operations/having.rb index cd16535609..0cd86c284e 100644 --- a/lib/arel/algebra/relations/operations/having.rb +++ b/lib/arel/algebra/relations/operations/having.rb @@ -1,13 +1,17 @@ module Arel class Having < Compound - attributes :relation, :havings - deriving :== + attributes :relation, :predicates + deriving :== + requires :restricting - def initialize(relation, *havings, &block) - @relation = relation - @havings = (havings + arguments_from_block(relation, &block)) \ - .collect { |g| g.bind(relation) } + def initialize(relation, *predicates, &block) + predicates = [yield(relation)] + predicates if block_given? + @predicates = predicates.map { |p| p.bind(relation) } + @relation = relation + end + + def havings + @havings ||= relation.havings + predicates end end end - diff --git a/lib/arel/engines/memory/relations/operations.rb b/lib/arel/engines/memory/relations/operations.rb index 55b4dcb677..75777a0c7f 100644 --- a/lib/arel/engines/memory/relations/operations.rb +++ b/lib/arel/engines/memory/relations/operations.rb @@ -8,7 +8,7 @@ module Arel class Order < Compound def eval unoperated_rows.sort do |row1, row2| - ordering = orderings.detect { |o| o.eval(row1, row2) != 0 } || orderings.last + ordering = orders.detect { |o| o.eval(row1, row2) != 0 } || orders.last ordering.eval(row1, row2) end end diff --git a/lib/arel/engines/sql/relations/compiler.rb b/lib/arel/engines/sql/relations/compiler.rb index cc0deb7c88..374967b7be 100644 --- a/lib/arel/engines/sql/relations/compiler.rb +++ b/lib/arel/engines/sql/relations/compiler.rb @@ -12,9 +12,9 @@ module Arel "SELECT #{select_clauses.join(', ')}", "FROM #{from_clauses}", (joins(self) unless joins(self).blank? ), - ("WHERE #{where_clauses.join(" AND ")}" unless wheres.blank? ), + ("WHERE #{where_clauses.join(' AND ')}" unless wheres.blank? ), ("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ), - ("HAVING #{having_clauses.join(', ')}" unless havings.blank? ), + ("HAVING #{having_clauses.join(' AND ')}" unless havings.blank? ), ("ORDER BY #{order_clauses.join(', ')}" unless orders.blank? ) engine.add_limit_offset!(query,{ :limit => taken, :offset => skipped }) if taken || skipped query << " #{locked}" unless locked.blank? |