aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-02-05 17:45:03 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-02-05 17:45:03 -0200
commite8aa045d76c5513f161cdbec817efed51d278f3f (patch)
treede49c92ab3edec20c126da16f961fb0012e107d3
parentf0ba9e4e56cf4dfa266147fad7e8f58ca577d614 (diff)
parent557a8769ae5b1e82af0efb1cf07cbc7bc24e3452 (diff)
downloadrails-e8aa045d76c5513f161cdbec817efed51d278f3f.tar.gz
rails-e8aa045d76c5513f161cdbec817efed51d278f3f.tar.bz2
rails-e8aa045d76c5513f161cdbec817efed51d278f3f.zip
Merge pull request #243 from vanderhoorn/patch-1
PostgreSQL bugfix for invalid SQL in subqueries
-rw-r--r--lib/arel/visitors/postgresql.rb2
-rw-r--r--test/visitors/test_postgres.rb36
-rw-r--r--test/visitors/test_to_sql.rb34
3 files changed, 72 insertions, 0 deletions
diff --git a/lib/arel/visitors/postgresql.rb b/lib/arel/visitors/postgresql.rb
index 080e402e3b..7520a1ccc7 100644
--- a/lib/arel/visitors/postgresql.rb
+++ b/lib/arel/visitors/postgresql.rb
@@ -4,10 +4,12 @@ module Arel
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
diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb
index 921bd96c1a..dc68279b8d 100644
--- a/test/visitors/test_postgres.rb
+++ b/test/visitors/test_postgres.rb
@@ -5,6 +5,8 @@ module Arel
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 Arel
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
diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb
index eb35d1703a..34d3459a27 100644
--- a/test/visitors/test_to_sql.rb
+++ b/test/visitors/test_to_sql.rb
@@ -194,6 +194,40 @@ module Arel
@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