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

adding outer joins

This commit is contained in:
Aaron Patterson 2010-08-19 10:37:50 -07:00
parent 6b851c686f
commit 6451188abf
9 changed files with 62 additions and 14 deletions

View file

@ -7,6 +7,7 @@ module Arel
um = UpdateManager.new @engine
if String === values
values = SqlLiteral.new values
um.table @ctx.froms.last
else
um.table values.first.first.relation

View file

@ -11,5 +11,7 @@ require 'arel/nodes/update_statement'
require 'arel/nodes/delete_statement'
require 'arel/nodes/unqualified_column'
require 'arel/nodes/table_alias'
require 'arel/nodes/join'
require 'arel/nodes/inner_join'
require 'arel/nodes/outer_join'
require 'arel/nodes/on'

View file

@ -1,13 +1,6 @@
module Arel
module Nodes
class InnerJoin
attr_accessor :left, :right, :constraint
def initialize left, right, constraint
@left = left
@right = right
@constraint = constraint
end
class InnerJoin < Arel::Nodes::Join
end
end
end

13
lib/arel/nodes/join.rb Normal file
View file

@ -0,0 +1,13 @@
module Arel
module Nodes
class Join
attr_accessor :left, :right, :constraint
def initialize left, right, constraint
@left = left
@right = right
@constraint = constraint
end
end
end
end

View file

@ -0,0 +1,6 @@
module Arel
module Nodes
class OuterJoin < Arel::Nodes::Join
end
end
end

View file

@ -19,10 +19,6 @@ module Arel
end
def project projection
projection = ::String == projection.class ?
Nodes::SqlLiteral.new(projection) :
projection
@ctx.projections << projection
self
end
@ -40,5 +36,14 @@ module Arel
@head.limit = limit
self
end
def join_sql
viz = Visitors::ToSql.new @engine
@ctx.froms.grep(Nodes::Join).map { |x| viz.accept x }.join ', '
end
def joins manager
manager.join_sql
end
end
end

View file

@ -61,6 +61,10 @@ module Arel
"#{visit o.relation} #{quote_table_name o.name}"
end
def visit_Arel_Nodes_OuterJoin o
"#{visit o.left} OUTER JOIN #{visit o.right} #{visit o.constraint}"
end
def visit_Arel_Nodes_InnerJoin o
"#{visit o.left} INNER JOIN #{visit o.right} #{visit o.constraint}"
end

View file

@ -23,6 +23,30 @@ module Arel
end
describe 'select manager' do
describe 'joins' do
it 'returns join sql' do
table = Table.new :users
aliaz = table.alias
manager = Arel::SelectManager.new Table.engine
manager.from Nodes::InnerJoin.new(table, aliaz, table[:id].eq(aliaz[:id]))
manager.join_sql.should be_like %{
"users" INNER JOIN "users" "users_2" "users"."id" = "users_2"."id"
}
check manager.joins(manager).should == manager.join_sql
end
it 'returns outer join sql' do
table = Table.new :users
aliaz = table.alias
manager = Arel::SelectManager.new Table.engine
manager.from Nodes::OuterJoin.new(table, aliaz, table[:id].eq(aliaz[:id]))
manager.join_sql.should be_like %{
"users" OUTER JOIN "users" "users_2" "users"."id" = "users_2"."id"
}
check manager.joins(manager).should == manager.join_sql
end
end
describe 'delete' do
it "copies from" do
engine = EngineProxy.new Table.engine

View file

@ -33,14 +33,14 @@ module Arel
describe 'take' do
it "should add a limit" do
manager = @relation.take 1
manager.project '*'
manager.project SqlLiteral.new '*'
manager.to_sql.should be_like %{ SELECT * FROM "users" LIMIT 1 }
end
end
describe 'project' do
it 'can project' do
manager = @relation.project '*'
manager = @relation.project SqlLiteral.new '*'
manager.to_sql.should be_like %{
SELECT *
FROM "users"