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:
parent
7b122f9a33
commit
cb6d13877c
7 changed files with 39 additions and 1 deletions
|
@ -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
11
lib/arel/nodes/having.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
class Having
|
||||
attr_accessor :expr
|
||||
|
||||
def initialize expr
|
||||
@expr = expr
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue