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

refactoring join_sql to return nil if there are no sources

This commit is contained in:
Aaron Patterson 2010-09-28 16:57:33 -07:00
parent f4c12fcc4a
commit f0bf1cf762
4 changed files with 24 additions and 11 deletions

View file

@ -128,6 +128,8 @@ module Arel
end end
def join_sql def join_sql
return nil unless @ctx.froms
viz = Visitors::JoinSql.new @engine viz = Visitors::JoinSql.new @engine
Nodes::SqlLiteral.new viz.accept @ctx Nodes::SqlLiteral.new viz.accept @ctx
end end

View file

@ -49,40 +49,39 @@ module Arel
end end
def join relation, klass = Nodes::InnerJoin def join relation, klass = Nodes::InnerJoin
return select_manager unless relation return from(self) unless relation
sm = SelectManager.new(@engine)
case relation case relation
when String, Nodes::SqlLiteral when String, Nodes::SqlLiteral
raise if relation.blank? raise if relation.blank?
sm.from Nodes::StringJoin.new(self, relation) from Nodes::StringJoin.new(self, relation)
else else
sm.from klass.new(self, relation, nil) from klass.new(self, relation, nil)
end end
end end
def group *columns def group *columns
select_manager.group(*columns) from(self).group(*columns)
end end
def order *expr def order *expr
select_manager.order(*expr) from(self).order(*expr)
end end
def where condition def where condition
select_manager.where condition from(self).where condition
end end
def project *things def project *things
select_manager.project(*things) from(self).project(*things)
end end
def take amount def take amount
select_manager.take amount from(self).take amount
end end
def having expr def having expr
select_manager.having expr from(self).having expr
end end
def columns def columns
@ -98,7 +97,7 @@ module Arel
end end
def select_manager def select_manager
SelectManager.new(@engine, self) SelectManager.new(@engine)
end end
private private

View file

@ -299,6 +299,11 @@ module Arel
manager.join_sql.should be_like %{ 'hello' } manager.join_sql.should be_like %{ 'hello' }
check manager.joins(manager).should == manager.join_sql check manager.joins(manager).should == manager.join_sql
end end
it 'returns nil join sql' do
manager = Arel::SelectManager.new Table.engine
manager.join_sql.should be_nil
end
end end
describe 'order_clauses' do describe 'order_clauses' do

View file

@ -12,6 +12,13 @@ module Arel
end end
end end
describe 'select_manager' do
it 'should return an empty select manager' do
sm = @relation.select_manager
sm.to_sql.should be_like 'SELECT'
end
end
describe 'having' do describe 'having' do
it 'adds a having clause' do it 'adds a having clause' do
mgr = @relation.having @relation[:id].eq(10) mgr = @relation.having @relation[:id].eq(10)