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

Merge remote branch 'sven/in-subquery' into subquery

* sven/in-subquery:
  implementation for passing a subquery to #in and #not_in
  tests for passing a subquery to #in and #not_in
This commit is contained in:
Aaron Patterson 2010-12-07 10:41:06 -08:00
commit 6cbbbb0326
3 changed files with 30 additions and 10 deletions

View file

@ -31,7 +31,7 @@ module Arel
def in other
case other
when Arel::SelectManager
Nodes::In.new self, other.to_a.map { |x| x.id }
Arel::Nodes::In.new(self, other)
when Range
if other.exclude_end?
left = Nodes::GreaterThanOrEqual.new(self, other.begin)
@ -56,7 +56,7 @@ module Arel
def not_in other
case other
when Arel::SelectManager
Nodes::NotIn.new self, other.to_a.map { |x| x.id }
Arel::Nodes::NotIn.new(self, other)
when Range
if other.exclude_end?
left = Nodes::LessThan.new(self, other.begin)

View file

@ -101,6 +101,10 @@ module Arel
}.join ', '})"
end
def visit_Arel_SelectManager o
o.to_sql
end
def visit_Arel_Nodes_SelectStatement o
[
o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join,
@ -233,17 +237,11 @@ module Arel
end
def visit_Arel_Nodes_In o
right = o.right
"#{visit o.left} IN (#{
right.empty? ? 'NULL' : right.map { |x| visit x }.join(', ')
})"
"#{visit o.left} IN (#{visit o.right})"
end
def visit_Arel_Nodes_NotIn o
right = o.right
"#{visit o.left} NOT IN (#{
right.empty? ? 'NULL' : right.map { |x| visit x }.join(', ')
})"
"#{visit o.left} NOT IN (#{visit o.right})"
end
def visit_Arel_Nodes_And o
@ -320,6 +318,10 @@ module Arel
alias :visit_ActiveSupport_StringInquirer :visit_String
alias :visit_Class :visit_String
def visit_Array o
o.empty? ? 'NULL' : o.map { |x| visit x }.join(', ')
end
def quote value, column = nil
@connection.quote value, column
end

View file

@ -143,6 +143,15 @@ module Arel
}
end
it 'can handle subqueries' do
table = Table.new(:users)
subquery = table.project(:id).where(table[:name].eq('Aaron'))
node = @attr.in subquery
@visitor.accept(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" = 'Aaron')
}
end
it 'uses the same column for escaping values' do
@attr = Table.new(:users)[:name]
visitor = Class.new(ToSql) do
@ -191,6 +200,15 @@ module Arel
}
end
it 'can handle subqueries' do
table = Table.new(:users)
subquery = table.project(:id).where(table[:name].eq('Aaron'))
node = @attr.not_in subquery
@visitor.accept(node).must_be_like %{
"users"."id" NOT IN (SELECT id FROM "users" WHERE "users"."name" = 'Aaron')
}
end
it 'uses the same column for escaping values' do
@attr = Table.new(:users)[:name]
visitor = Class.new(ToSql) do