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

Add Regexp and NotRegexp nodes for PostgreSQL

This commit is contained in:
James Le Cuirot 2014-03-07 13:15:33 +00:00 committed by James Le Cuirot
parent 7515445966
commit 6296617c15
6 changed files with 74 additions and 0 deletions

View file

@ -40,7 +40,9 @@ module Arel
Matches Matches
NotEqual NotEqual
NotIn NotIn
NotRegexp
Or Or
Regexp
Union Union
UnionAll UnionAll
Intersect Intersect

View file

@ -79,8 +79,10 @@ module Arel
alias :visit_Arel_Nodes_Matches :binary alias :visit_Arel_Nodes_Matches :binary
alias :visit_Arel_Nodes_NotEqual :binary alias :visit_Arel_Nodes_NotEqual :binary
alias :visit_Arel_Nodes_NotIn :binary alias :visit_Arel_Nodes_NotIn :binary
alias :visit_Arel_Nodes_NotRegexp :binary
alias :visit_Arel_Nodes_Or :binary alias :visit_Arel_Nodes_Or :binary
alias :visit_Arel_Nodes_OuterJoin :binary alias :visit_Arel_Nodes_OuterJoin :binary
alias :visit_Arel_Nodes_Regexp :binary
alias :visit_Arel_Nodes_RightOuterJoin :binary alias :visit_Arel_Nodes_RightOuterJoin :binary
alias :visit_Arel_Nodes_TableAlias :binary alias :visit_Arel_Nodes_TableAlias :binary
alias :visit_Arel_Nodes_Values :binary alias :visit_Arel_Nodes_Values :binary

View file

@ -11,6 +11,14 @@ module Arel
"#{visit o.left} NOT ILIKE #{visit o.right}" "#{visit o.left} NOT ILIKE #{visit o.right}"
end end
def visit_Arel_Nodes_Regexp o
"#{visit o.left} ~ #{visit o.right}"
end
def visit_Arel_Nodes_NotRegexp o
"#{visit o.left} !~ #{visit o.right}"
end
def visit_Arel_Nodes_DistinctOn o def visit_Arel_Nodes_DistinctOn o
"DISTINCT ON ( #{visit o.expr} )" "DISTINCT ON ( #{visit o.expr} )"
end end

View file

@ -435,6 +435,14 @@ module Arel
"#{visit o.left} NOT LIKE #{visit o.right}" "#{visit o.left} NOT LIKE #{visit o.right}"
end end
def visit_Arel_Nodes_Regexp o
raise NotImplementedError, '~ not implemented for this db'
end
def visit_Arel_Nodes_NotRegexp o
raise NotImplementedError, '!~ not implemented for this db'
end
def visit_Arel_Nodes_JoinSource o def visit_Arel_Nodes_JoinSource o
[ [
(visit(o.left) if o.left), (visit(o.left) if o.left),

View file

@ -79,6 +79,40 @@ module Arel
} }
end end
end end
describe "Nodes::Regexp" do
it "should know how to visit" do
node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%'))
@visitor.accept(node).must_be_like %{
"users"."name" ~ 'foo%'
}
end
it 'can handle subqueries' do
subquery = @table.project(:id).where(Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%')))
node = @attr.in subquery
@visitor.accept(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" ~ 'foo%')
}
end
end
describe "Nodes::NotRegexp" do
it "should know how to visit" do
node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%'))
@visitor.accept(node).must_be_like %{
"users"."name" !~ 'foo%'
}
end
it 'can handle subqueries' do
subquery = @table.project(:id).where(Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%')))
node = @attr.in subquery
@visitor.accept(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" !~ 'foo%')
}
end
end
end end
end end
end end

View file

@ -526,6 +526,26 @@ module Arel
end end
end end
end end
describe 'Nodes::Regexp' do
it 'raises not implemented error' do
node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%'))
assert_raises(NotImplementedError) do
@visitor.accept(node)
end
end
end
describe 'Nodes::NotRegexp' do
it 'raises not implemented error' do
node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%'))
assert_raises(NotImplementedError) do
@visitor.accept(node)
end
end
end
end end
end end
end end