1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

adding having nodes

This commit is contained in:
Aaron Patterson 2010-09-08 15:29:22 -07:00
parent 7b122f9a33
commit cb6d13877c
7 changed files with 39 additions and 1 deletions

View file

@ -7,6 +7,7 @@ require 'arel/nodes/in'
require 'arel/nodes/count'
require 'arel/nodes/sum'
require 'arel/nodes/max'
require 'arel/nodes/having'
require 'arel/nodes/sql_literal'
require 'arel/nodes/select_core'
require 'arel/nodes/select_statement'

11
lib/arel/nodes/having.rb Normal file
View file

@ -0,0 +1,11 @@
module Arel
module Nodes
class Having
attr_accessor :expr
def initialize expr
@expr = expr
end
end
end
end

View file

@ -2,12 +2,14 @@ module Arel
module Nodes
class SelectCore
attr_reader :froms, :projections, :wheres, :groups
attr_accessor :having
def initialize
@froms = []
@projections = []
@wheres = []
@groups = []
@having = nil
end
def initialize_copy other
@ -16,6 +18,7 @@ module Arel
@projections = @projections.clone
@wheres = @wheres.clone
@group = @groups.clone
@having = @having.clone if @having
end
end
end

View file

@ -40,6 +40,11 @@ module Arel
end
end
def having expr
@ctx.having = Nodes::Having.new(expr)
self
end
def project *projections
# FIXME: converting these to SQLLiterals is probably not good, but
# rails tests require it.

View file

@ -61,6 +61,10 @@ module Arel
tm.take amount
end
def having expr
tm.having expr
end
def columns
@columns ||= @engine.connection.columns(@name, "#{@name} Columns").map do |column|
Attributes.for(column).new self, column.name, column

View file

@ -57,10 +57,15 @@ module Arel
"SELECT #{o.projections.map { |x| visit x }.join ', '}",
("FROM #{o.froms.map { |x| visit x }.join ', ' }" unless o.froms.empty?),
("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?),
("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?)
("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?),
(visit(o.having) if o.having),
].compact.join ' '
end
def visit_Arel_Nodes_Having o
"HAVING #{visit o.expr}"
end
def visit_Arel_Nodes_Group o
visit o.expr
end

View file

@ -6,6 +6,15 @@ module Arel
@relation = Table.new(:users)
end
describe 'having' do
it 'adds a having clause' do
mgr = @relation.having @relation[:id].eq(10)
mgr.to_sql.should be_like %{
SELECT FROM "users" HAVING "users"."id" = 10
}
end
end
describe 'backwards compat' do
describe 'joins' do
it 'returns nil' do