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

adding a join source node

This commit is contained in:
Aaron Patterson 2010-12-14 09:43:19 -08:00
parent 87c022bc98
commit 9653da3c79
18 changed files with 94 additions and 83 deletions

View file

@ -38,6 +38,7 @@ lib/arel/nodes/in.rb
lib/arel/nodes/inner_join.rb lib/arel/nodes/inner_join.rb
lib/arel/nodes/insert_statement.rb lib/arel/nodes/insert_statement.rb
lib/arel/nodes/join.rb lib/arel/nodes/join.rb
lib/arel/nodes/join_source.rb
lib/arel/nodes/less_than.rb lib/arel/nodes/less_than.rb
lib/arel/nodes/less_than_or_equal.rb lib/arel/nodes/less_than_or_equal.rb
lib/arel/nodes/lock.rb lib/arel/nodes/lock.rb
@ -103,6 +104,7 @@ test/test_activerecord_compat.rb
test/test_attributes.rb test/test_attributes.rb
test/test_crud.rb test/test_crud.rb
test/test_delete_manager.rb test/test_delete_manager.rb
test/test_factory_methods.rb
test/test_insert_manager.rb test/test_insert_manager.rb
test/test_select_manager.rb test/test_select_manager.rb
test/test_table.rb test/test_table.rb

View file

@ -2,12 +2,12 @@ module Arel
### ###
# Methods for creating various nodes # Methods for creating various nodes
module FactoryMethods module FactoryMethods
def create_join from, to, on = nil, klass = Nodes::InnerJoin def create_join to, constraint = nil, klass = Nodes::InnerJoin
klass.new(from, to, on) klass.new(to, constraint)
end end
def create_string_join from, to def create_string_join from, to
create_join from, to, nil, Nodes::StringJoin create_join from, to, Nodes::StringJoin
end end
def create_and clauses def create_and clauses

View file

@ -15,6 +15,7 @@ require 'arel/nodes/less_than'
require 'arel/nodes/less_than_or_equal' require 'arel/nodes/less_than_or_equal'
require 'arel/nodes/matches' require 'arel/nodes/matches'
require 'arel/nodes/does_not_match' require 'arel/nodes/does_not_match'
require 'arel/nodes/join_source'
require 'arel/nodes/in' require 'arel/nodes/in'
require 'arel/nodes/not_in' require 'arel/nodes/not_in'

View file

@ -1,12 +1,12 @@
module Arel module Arel
module Nodes module Nodes
class Join < Arel::Nodes::Node class Join < Arel::Nodes::Binary
attr_accessor :left, :right, :constraint
def initialize left, right, constraint alias :single_source :left
@left = left alias :constraint :right
@right = right
@constraint = constraint def initialize single_source, constraint
super
end end
end end
end end

View file

@ -0,0 +1,20 @@
module Arel
module Nodes
###
# Class that represents a join source
#
# http://www.sqlite.org/syntaxdiagrams.html#join-source
class JoinSource < Arel::Nodes::Binary
def initialize single_source, joinop = []
super
end
def initialize_copy other
super
@left = @left.clone if @left
@right = @right.clone if @right
end
end
end
end

View file

@ -1,23 +1,31 @@
module Arel module Arel
module Nodes module Nodes
class SelectCore < Arel::Nodes::Node class SelectCore < Arel::Nodes::Node
attr_accessor :from, :projections, :wheres, :groups attr_accessor :projections, :wheres, :groups
attr_accessor :having attr_accessor :having, :source
alias :froms= :from=
alias :froms :from
def initialize def initialize
@from = nil @source = JoinSource.new nil
@projections = [] @projections = []
@wheres = [] @wheres = []
@groups = [] @groups = []
@having = nil @having = nil
end end
def from
@source.left
end
def from= value
@source.left = value
end
alias :froms= :from=
alias :froms :from
def initialize_copy other def initialize_copy other
super super
@from = @from.clone if @from @source = @source.clone if @source
@projections = @projections.clone @projections = @projections.clone
@wheres = @wheres.clone @wheres = @wheres.clone
@group = @groups.clone @group = @groups.clone

View file

@ -1,11 +1,6 @@
module Arel module Arel
module Nodes module Nodes
class StringJoin < Arel::Nodes::Join class StringJoin < Arel::Nodes::Join
undef :constraint
def initialize left, right, on = nil
super
end
end end
end end
end end

View file

@ -46,7 +46,7 @@ module Arel
end end
def on *exprs def on *exprs
@ctx.froms.constraint = Nodes::On.new(collapse(exprs)) @ctx.source.right.last.right = Nodes::On.new(collapse(exprs))
self self
end end
@ -66,16 +66,14 @@ module Arel
# FIXME: this is a hack to support # FIXME: this is a hack to support
# test_with_two_tables_in_from_without_getting_double_quoted # test_with_two_tables_in_from_without_getting_double_quoted
# from the AR tests. # from the AR tests.
if @ctx.from
source = @ctx.from
if Nodes::SqlLiteral === table && Nodes::Join === source case table
source.left = table when Nodes::SqlLiteral, Arel::Table
table = source @ctx.source.left = table
end when Nodes::Join
@ctx.source.right << table
end end
@ctx.from = table
self self
end end
@ -92,7 +90,8 @@ module Arel
klass = Nodes::StringJoin klass = Nodes::StringJoin
end end
from create_join(@ctx.from, relation, nil, klass) @ctx.source.right << create_join(relation, nil, klass)
self
end end
def having expr def having expr
@ -140,7 +139,7 @@ module Arel
end end
def join_sql def join_sql
return nil unless @ctx.froms return nil if @ctx.source.right.empty?
sql = @visitor.dup.extend(Visitors::JoinSql).accept @ctx sql = @visitor.dup.extend(Visitors::JoinSql).accept @ctx
Nodes::SqlLiteral.new sql Nodes::SqlLiteral.new sql

View file

@ -66,7 +66,7 @@ primary_key (#{caller.first}) is deprecated and will be removed in ARel 3.0.0
klass = Nodes::StringJoin klass = Nodes::StringJoin
end end
from create_join(self, relation, nil, klass) from(self).join(relation, klass)
end end
def group *columns def group *columns

View file

@ -39,14 +39,6 @@ module Arel
visit o.distinct visit o.distinct
end end
def join o
visit o.left
visit o.right
visit o.constraint
end
alias :visit_Arel_Nodes_InnerJoin :join
alias :visit_Arel_Nodes_OuterJoin :join
def nary o def nary o
o.children.each { |child| visit child } o.children.each { |child| visit child }
end end
@ -65,6 +57,7 @@ module Arel
alias :visit_Arel_Nodes_GreaterThan :binary alias :visit_Arel_Nodes_GreaterThan :binary
alias :visit_Arel_Nodes_GreaterThanOrEqual :binary alias :visit_Arel_Nodes_GreaterThanOrEqual :binary
alias :visit_Arel_Nodes_In :binary alias :visit_Arel_Nodes_In :binary
alias :visit_Arel_Nodes_InnerJoin :binary
alias :visit_Arel_Nodes_LessThan :binary alias :visit_Arel_Nodes_LessThan :binary
alias :visit_Arel_Nodes_LessThanOrEqual :binary alias :visit_Arel_Nodes_LessThanOrEqual :binary
alias :visit_Arel_Nodes_Matches :binary alias :visit_Arel_Nodes_Matches :binary
@ -72,6 +65,7 @@ module Arel
alias :visit_Arel_Nodes_NotIn :binary alias :visit_Arel_Nodes_NotIn :binary
alias :visit_Arel_Nodes_Or :binary alias :visit_Arel_Nodes_Or :binary
alias :visit_Arel_Nodes_Ordering :binary alias :visit_Arel_Nodes_Ordering :binary
alias :visit_Arel_Nodes_OuterJoin :binary
alias :visit_Arel_Nodes_StringJoin :binary alias :visit_Arel_Nodes_StringJoin :binary
alias :visit_Arel_Nodes_TableAlias :binary alias :visit_Arel_Nodes_TableAlias :binary
alias :visit_Arel_Nodes_Values :binary alias :visit_Arel_Nodes_Values :binary

View file

@ -36,7 +36,6 @@ module Arel
def visit_Arel_Nodes_TableAlias o def visit_Arel_Nodes_TableAlias o
visit_edge o, "name" visit_edge o, "name"
visit_edge o, "relation" visit_edge o, "relation"
visit_edge o, "columns"
end end
def visit_Arel_Nodes_Sum o def visit_Arel_Nodes_Sum o
@ -90,7 +89,7 @@ module Arel
end end
def visit_Arel_Nodes_SelectCore o def visit_Arel_Nodes_SelectCore o
visit_edge o, "froms" visit_edge o, "source"
visit_edge o, "projections" visit_edge o, "projections"
visit_edge o, "wheres" visit_edge o, "wheres"
end end
@ -146,6 +145,7 @@ module Arel
alias :visit_Arel_Nodes_NotIn :visit_Arel_Nodes_Equality alias :visit_Arel_Nodes_NotIn :visit_Arel_Nodes_Equality
alias :visit_Arel_Nodes_DoesNotMatch :visit_Arel_Nodes_Equality alias :visit_Arel_Nodes_DoesNotMatch :visit_Arel_Nodes_Equality
alias :visit_Arel_Nodes_Matches :visit_Arel_Nodes_Equality alias :visit_Arel_Nodes_Matches :visit_Arel_Nodes_Equality
alias :visit_Arel_Nodes_JoinSource :visit_Arel_Nodes_Equality
def visit_String o def visit_String o
@node_stack.last.fields << o @node_stack.last.fields << o

View file

@ -12,8 +12,7 @@ module Arel
private private
def visit_Arel_Nodes_SelectCore o def visit_Arel_Nodes_SelectCore o
return '' unless Nodes::Join === o.from o.source.right.map { |j| visit j }.join ' '
visit o.from
end end
def visit_Arel_Nodes_StringJoin o def visit_Arel_Nodes_StringJoin o
@ -22,20 +21,6 @@ module Arel
visit(o.right) visit(o.right)
].compact.join ' ' ].compact.join ' '
end end
def visit_Arel_Nodes_OuterJoin o
[
(visit o.left if Nodes::Join === o.left),
"LEFT OUTER JOIN #{visit o.right} #{visit o.constraint if o.constraint}"
].compact.join ' '
end
def visit_Arel_Nodes_InnerJoin o
[
(visit o.left if Nodes::Join === o.left),
"INNER JOIN #{visit o.right} #{visit o.constraint if o.constraint}"
].compact.join ' '
end
end end
end end
end end

View file

@ -127,7 +127,7 @@ module Arel
def visit_Arel_Nodes_SelectCore o def visit_Arel_Nodes_SelectCore o
[ [
"SELECT #{o.projections.map { |x| visit x }.join ', '}", "SELECT #{o.projections.map { |x| visit x }.join ', '}",
("FROM #{visit o.from}" if o.from), visit(o.source),
("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.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), (visit(o.having) if o.having),
@ -217,16 +217,26 @@ module Arel
"#{visit o.left} NOT LIKE #{visit o.right}" "#{visit o.left} NOT LIKE #{visit o.right}"
end end
def visit_Arel_Nodes_JoinSource o
return unless o.left || !o.right.empty?
[
"FROM",
(visit(o.left) if o.left),
o.right.map { |j| visit j }.join(' ')
].compact.join ' '
end
def visit_Arel_Nodes_StringJoin o def visit_Arel_Nodes_StringJoin o
"#{visit o.left} #{visit o.right}" "#{visit o.left} #{visit o.right}"
end end
def visit_Arel_Nodes_OuterJoin o def visit_Arel_Nodes_OuterJoin o
"#{visit o.left} LEFT OUTER JOIN #{visit o.right} #{visit o.constraint}" "LEFT OUTER JOIN #{visit o.left} #{visit o.right}"
end end
def visit_Arel_Nodes_InnerJoin o def visit_Arel_Nodes_InnerJoin o
"#{visit o.left} INNER JOIN #{visit o.right} #{visit o.constraint if o.constraint}" "INNER JOIN #{visit o.left} #{visit o.right if o.right}"
end end
def visit_Arel_Nodes_On o def visit_Arel_Nodes_On o

View file

@ -12,9 +12,9 @@ module Arel
end end
def test_create_join def test_create_join
join = @factory.create_join :one, :two, :three join = @factory.create_join :one, :two
assert_kind_of Nodes::Join, join assert_kind_of Nodes::Join, join
assert_equal :three, join.constraint assert_equal :two, join.constraint
end end
def test_create_on def test_create_on

View file

@ -292,20 +292,18 @@ module Arel
it 'should create join nodes' do it 'should create join nodes' do
relation = Arel::SelectManager.new Table.engine relation = Arel::SelectManager.new Table.engine
join = relation.create_join 'foo', 'bar', 'baz' join = relation.create_join 'foo', 'bar'
assert_kind_of Arel::Nodes::InnerJoin, join assert_kind_of Arel::Nodes::InnerJoin, join
assert_equal 'foo', join.left assert_equal 'foo', join.left
assert_equal 'bar', join.right assert_equal 'bar', join.right
assert_equal 'baz', join.constraint
end end
it 'should create join nodes with a klass' do it 'should create join nodes with a klass' do
relation = Arel::SelectManager.new Table.engine relation = Arel::SelectManager.new Table.engine
join = relation.create_join 'foo', 'bar', 'baz', Arel::Nodes::OuterJoin join = relation.create_join 'foo', 'bar', Arel::Nodes::OuterJoin
assert_kind_of Arel::Nodes::OuterJoin, join assert_kind_of Arel::Nodes::OuterJoin, join
assert_equal 'foo', join.left assert_equal 'foo', join.left
assert_equal 'bar', join.right assert_equal 'bar', join.right
assert_equal 'baz', join.constraint
end end
describe 'join' do describe 'join' do
@ -350,7 +348,7 @@ module Arel
table = Table.new :users table = Table.new :users
aliaz = table.alias aliaz = table.alias
manager = Arel::SelectManager.new Table.engine manager = Arel::SelectManager.new Table.engine
manager.from Nodes::InnerJoin.new(table, aliaz, table[:id].eq(aliaz[:id])) manager.from Nodes::InnerJoin.new(aliaz, table[:id].eq(aliaz[:id]))
manager.join_sql.must_be_like %{ manager.join_sql.must_be_like %{
INNER JOIN "users" "users_2" "users"."id" = "users_2"."id" INNER JOIN "users" "users_2" "users"."id" = "users_2"."id"
} }
@ -360,7 +358,7 @@ module Arel
table = Table.new :users table = Table.new :users
aliaz = table.alias aliaz = table.alias
manager = Arel::SelectManager.new Table.engine manager = Arel::SelectManager.new Table.engine
manager.from Nodes::OuterJoin.new(table, aliaz, table[:id].eq(aliaz[:id])) manager.from Nodes::OuterJoin.new(aliaz, table[:id].eq(aliaz[:id]))
manager.join_sql.must_be_like %{ manager.join_sql.must_be_like %{
LEFT OUTER JOIN "users" "users_2" "users"."id" = "users_2"."id" LEFT OUTER JOIN "users" "users_2" "users"."id" = "users_2"."id"
} }

View file

@ -14,19 +14,17 @@ module Arel
end end
it 'should create join nodes' do it 'should create join nodes' do
join = @relation.create_join 'foo', 'bar', 'baz' join = @relation.create_join 'foo', 'bar'
assert_kind_of Arel::Nodes::InnerJoin, join assert_kind_of Arel::Nodes::InnerJoin, join
assert_equal 'foo', join.left assert_equal 'foo', join.left
assert_equal 'bar', join.right assert_equal 'bar', join.right
assert_equal 'baz', join.constraint
end end
it 'should create join nodes with a klass' do it 'should create join nodes with a klass' do
join = @relation.create_join 'foo', 'bar', 'baz', Arel::Nodes::OuterJoin join = @relation.create_join 'foo', 'bar', Arel::Nodes::OuterJoin
assert_kind_of Arel::Nodes::OuterJoin, join assert_kind_of Arel::Nodes::OuterJoin, join
assert_equal 'foo', join.left assert_equal 'foo', join.left
assert_equal 'bar', join.right assert_equal 'bar', join.right
assert_equal 'baz', join.constraint
end end
it 'should return an insert manager' do it 'should return an insert manager' do

View file

@ -66,15 +66,15 @@ module Arel
end end
def test_inner_join def test_inner_join
join = Nodes::InnerJoin.new :a, :b, :c join = Nodes::InnerJoin.new :a, :b
@visitor.accept join @visitor.accept join
assert_equal [:a, :b, :c, join], @collector.calls assert_equal [:a, :b, join], @collector.calls
end end
def test_outer_join def test_outer_join
join = Nodes::OuterJoin.new :a, :b, :c join = Nodes::OuterJoin.new :a, :b
@visitor.accept join @visitor.accept join
assert_equal [:a, :b, :c, join], @collector.calls assert_equal [:a, :b, join], @collector.calls
end end
[ [

View file

@ -11,9 +11,9 @@ module Arel
describe 'inner join' do describe 'inner join' do
it 'should visit left if left is a join' do it 'should visit left if left is a join' do
t = Table.new :users t = Table.new :users
join = Nodes::InnerJoin.new t, t, Nodes::On.new(t[:id]) sm = t.select_manager
j2 = Nodes::InnerJoin.new join, t, Nodes::On.new(t[:id]) sm.join(t).on(t[:id]).join(t).on(t[:id])
@visitor.accept(j2).must_be_like %{ sm.join_sql.must_be_like %{
INNER JOIN "users" ON "users"."id" INNER JOIN "users" ON "users"."id"
INNER JOIN "users" ON "users"."id" INNER JOIN "users" ON "users"."id"
} }
@ -23,9 +23,10 @@ module Arel
describe 'outer join' do describe 'outer join' do
it 'should visit left if left is a join' do it 'should visit left if left is a join' do
t = Table.new :users t = Table.new :users
join = Nodes::OuterJoin.new t, t, Nodes::On.new(t[:id]) sm = t.select_manager
j2 = Nodes::OuterJoin.new join, t, Nodes::On.new(t[:id]) sm.join(t, Nodes::OuterJoin).on(t[:id]).join(
@visitor.accept(j2).must_be_like %{ t, Nodes::OuterJoin).on(t[:id])
sm.join_sql.must_be_like %{
LEFT OUTER JOIN "users" ON "users"."id" LEFT OUTER JOIN "users" ON "users"."id"
LEFT OUTER JOIN "users" ON "users"."id" LEFT OUTER JOIN "users" ON "users"."id"
} }