aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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