From 2c95c39e9345ef50a7e9c3ad573857b1d358e04a Mon Sep 17 00:00:00 2001 From: Kazuya NUMATA Date: Thu, 25 Dec 2014 12:58:42 +0900 Subject: {Matches,DoesNotMatch} support the ESCAPE clause with PostgreSQL to_SQL already has supported the ESCAPE clause in #318. PostgreSQL can use the ESCAPE clause too. --- lib/arel/visitors/postgresql.rb | 16 ++++++++++++++-- test/visitors/test_postgres.rb | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/arel/visitors/postgresql.rb b/lib/arel/visitors/postgresql.rb index f55aaf30fe..bd23fc0a47 100644 --- a/lib/arel/visitors/postgresql.rb +++ b/lib/arel/visitors/postgresql.rb @@ -4,11 +4,23 @@ module Arel private def visit_Arel_Nodes_Matches o, collector - infix_value o, collector, ' ILIKE ' + collector = infix_value o, collector, ' ILIKE ' + if o.escape + collector << ' ESCAPE ' + visit o.escape, collector + else + collector + end end def visit_Arel_Nodes_DoesNotMatch o, collector - infix_value o, collector, ' NOT ILIKE ' + collector = infix_value o, collector, ' NOT ILIKE ' + if o.escape + collector << ' ESCAPE ' + visit o.escape, collector + else + collector + end end def visit_Arel_Nodes_Regexp o, collector diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb index 368feb5977..d6de216d91 100644 --- a/test/visitors/test_postgres.rb +++ b/test/visitors/test_postgres.rb @@ -58,6 +58,13 @@ module Arel } end + it "can handle ESCAPE" do + node = @table[:name].matches('foo!%', '!') + compile(node).must_be_like %{ + "users"."name" ILIKE 'foo!%' ESCAPE '!' + } + end + it 'can handle subqueries' do subquery = @table.project(:id).where(@table[:name].matches('foo%')) node = @attr.in subquery @@ -75,6 +82,13 @@ module Arel } end + it "can handle ESCAPE" do + node = @table[:name].does_not_match('foo!%', '!') + compile(node).must_be_like %{ + "users"."name" NOT ILIKE 'foo!%' ESCAPE '!' + } + end + it 'can handle subqueries' do subquery = @table.project(:id).where(@table[:name].does_not_match('foo%')) node = @attr.in subquery -- cgit v1.2.3