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

Add case sensitive regexp

Explicitly declare if this is case sensitive or not

currently postgres assumes case insensitive regexp
no other databases support regexps
This commit is contained in:
Keenan Brock 2015-12-05 19:13:05 -05:00
parent 508a6783c8
commit 193d2ad214
5 changed files with 33 additions and 4 deletions

View file

@ -30,6 +30,7 @@ require 'arel/nodes/table_alias'
require 'arel/nodes/infix_operation'
require 'arel/nodes/over'
require 'arel/nodes/matches'
require 'arel/nodes/regexp'
# nary
require 'arel/nodes/and'

View file

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

14
lib/arel/nodes/regexp.rb Normal file
View file

@ -0,0 +1,14 @@
module Arel
module Nodes
class Regexp < Binary
attr_accessor :case_sensitive
def initialize(left, right, case_sensitive = true)
super(left, right)
@case_sensitive = case_sensitive
end
end
class NotRegexp < Regexp; end
end
end

View file

@ -26,11 +26,13 @@ module Arel
end
def visit_Arel_Nodes_Regexp o, collector
infix_value o, collector, ' ~ '
op = o.case_sensitive ? ' ~ ' : ' ~* '
infix_value o, collector, op
end
def visit_Arel_Nodes_NotRegexp o, collector
infix_value o, collector, ' !~ '
op = o.case_sensitive ? ' !~ ' : ' !~* '
infix_value o, collector, op
end
def visit_Arel_Nodes_DistinctOn o, collector

View file

@ -120,6 +120,13 @@ module Arel
}
end
it "can handle case insensitive" do
node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%'), false)
compile(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
@ -137,6 +144,13 @@ module Arel
}
end
it "can handle case insensitive" do
node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%'), false)
compile(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