提交 e8aa045d 编写于 作者: R Rafael Mendonça França

Merge pull request #243 from vanderhoorn/patch-1

PostgreSQL bugfix for invalid SQL in subqueries
......@@ -4,10 +4,12 @@ class PostgreSQL < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_Matches o, a
a = o.left if Arel::Attributes::Attribute === o.left
"#{visit o.left, a} ILIKE #{visit o.right, a}"
end
def visit_Arel_Nodes_DoesNotMatch o, a
a = o.left if Arel::Attributes::Attribute === o.left
"#{visit o.left, a} NOT ILIKE #{visit o.right, a}"
end
......
......@@ -5,6 +5,8 @@ module Visitors
describe 'the postgres visitor' do
before do
@visitor = PostgreSQL.new Table.engine.connection
@table = Table.new(:users)
@attr = @table[:id]
end
describe 'locking' do
......@@ -43,6 +45,40 @@ module Visitors
core.set_quantifier = Arel::Nodes::Distinct.new
assert_equal 'SELECT DISTINCT', @visitor.accept(core)
end
describe "Nodes::Matches" do
it "should know how to visit" do
node = @table[:name].matches('foo%')
@visitor.accept(node).must_be_like %{
"users"."name" ILIKE 'foo%'
}
end
it 'can handle subqueries' do
subquery = @table.project(:id).where(@table[:name].matches('foo%'))
node = @attr.in subquery
@visitor.accept(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" ILIKE 'foo%')
}
end
end
describe "Nodes::DoesNotMatch" do
it "should know how to visit" do
node = @table[:name].does_not_match('foo%')
@visitor.accept(node).must_be_like %{
"users"."name" NOT ILIKE 'foo%'
}
end
it 'can handle subqueries' do
subquery = @table.project(:id).where(@table[:name].does_not_match('foo%'))
node = @attr.in subquery
@visitor.accept(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" NOT ILIKE 'foo%')
}
end
end
end
end
end
......@@ -194,6 +194,40 @@ def dispatch
@visitor.accept(test).must_be_like %{ "users"."bool" = 't' }
end
describe "Nodes::Matches" do
it "should know how to visit" do
node = @table[:name].matches('foo%')
@visitor.accept(node).must_be_like %{
"users"."name" LIKE 'foo%'
}
end
it 'can handle subqueries' do
subquery = @table.project(:id).where(@table[:name].matches('foo%'))
node = @attr.in subquery
@visitor.accept(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" LIKE 'foo%')
}
end
end
describe "Nodes::DoesNotMatch" do
it "should know how to visit" do
node = @table[:name].does_not_match('foo%')
@visitor.accept(node).must_be_like %{
"users"."name" NOT LIKE 'foo%'
}
end
it 'can handle subqueries' do
subquery = @table.project(:id).where(@table[:name].does_not_match('foo%'))
node = @attr.in subquery
@visitor.accept(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" NOT LIKE 'foo%')
}
end
end
describe "Nodes::Ordering" do
it "should know how to visit" do
node = @attr.desc
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册