diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-12-16 07:42:17 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-12-16 07:42:17 -0800 |
commit | 899e8428c50c586e6c8d4884b542ac9fdf01e95f (patch) | |
tree | cea78b06495af32c84f7fa34f4cc104b6cb3c1bf /lib/arel | |
parent | 3c429c5d86e9e2201c2a35d934ca6a8911c18e69 (diff) | |
parent | 725cf0502eb279c728bd93320cc5e22134228ff1 (diff) | |
download | rails-899e8428c50c586e6c8d4884b542ac9fdf01e95f.tar.gz rails-899e8428c50c586e6c8d4884b542ac9fdf01e95f.tar.bz2 rails-899e8428c50c586e6c8d4884b542ac9fdf01e95f.zip |
Merge pull request #403 from kbrock/regexp
Case insensitivite match/regular expressions
Diffstat (limited to 'lib/arel')
-rw-r--r-- | lib/arel/nodes.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/binary.rb | 2 | ||||
-rw-r--r-- | lib/arel/nodes/matches.rb | 4 | ||||
-rw-r--r-- | lib/arel/nodes/regexp.rb | 14 | ||||
-rw-r--r-- | lib/arel/predications.rb | 24 | ||||
-rw-r--r-- | lib/arel/visitors/postgresql.rb | 12 |
6 files changed, 42 insertions, 15 deletions
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 8d61bb320f..0e66d2dd0c 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -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' diff --git a/lib/arel/nodes/binary.rb b/lib/arel/nodes/binary.rb index dddbde1431..763091c267 100644 --- a/lib/arel/nodes/binary.rb +++ b/lib/arel/nodes/binary.rb @@ -38,9 +38,7 @@ module Arel LessThanOrEqual NotEqual NotIn - NotRegexp Or - Regexp Union UnionAll Intersect diff --git a/lib/arel/nodes/matches.rb b/lib/arel/nodes/matches.rb index 583fb97c9b..0d9c1925dc 100644 --- a/lib/arel/nodes/matches.rb +++ b/lib/arel/nodes/matches.rb @@ -2,10 +2,12 @@ module Arel module Nodes class Matches < Binary attr_reader :escape + attr_accessor :case_sensitive - def initialize(left, right, escape = nil) + def initialize(left, right, escape = nil, case_sensitive = false) super(left, right) @escape = escape && Nodes.build_quoted(escape) + @case_sensitive = case_sensitive end end diff --git a/lib/arel/nodes/regexp.rb b/lib/arel/nodes/regexp.rb new file mode 100644 index 0000000000..784368f5bf --- /dev/null +++ b/lib/arel/nodes/regexp.rb @@ -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 diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb index b05fc6f99a..1d2b0de235 100644 --- a/lib/arel/predications.rb +++ b/lib/arel/predications.rb @@ -118,20 +118,28 @@ Passing a range to `#not_in` is deprecated. Call `#not_between`, instead. grouping_all :not_in, others end - def matches other, escape = nil - Nodes::Matches.new self, quoted_node(other), escape + def matches other, escape = nil, case_sensitive = false + Nodes::Matches.new self, quoted_node(other), escape, case_sensitive end - def matches_any others, escape = nil - grouping_any :matches, others, escape + def matches_regexp other, case_sensitive = true + Nodes::Regexp.new self, quoted_node(other), case_sensitive end - def matches_all others, escape = nil - grouping_all :matches, others, escape + def matches_any others, escape = nil, case_sensitive = false + grouping_any :matches, others, escape, case_sensitive end - def does_not_match other, escape = nil - Nodes::DoesNotMatch.new self, quoted_node(other), escape + def matches_all others, escape = nil, case_sensitive = false + grouping_all :matches, others, escape, case_sensitive + end + + def does_not_match other, escape = nil, case_sensitive = false + Nodes::DoesNotMatch.new self, quoted_node(other), escape, case_sensitive + end + + def does_not_match_regexp other, case_sensitive = true + Nodes::NotRegexp.new self, quoted_node(other), case_sensitive end def does_not_match_any others, escape = nil diff --git a/lib/arel/visitors/postgresql.rb b/lib/arel/visitors/postgresql.rb index bd23fc0a47..1ef0261bdd 100644 --- a/lib/arel/visitors/postgresql.rb +++ b/lib/arel/visitors/postgresql.rb @@ -4,7 +4,8 @@ module Arel private def visit_Arel_Nodes_Matches o, collector - collector = infix_value o, collector, ' ILIKE ' + op = o.case_sensitive ? ' LIKE ' : ' ILIKE ' + collector = infix_value o, collector, op if o.escape collector << ' ESCAPE ' visit o.escape, collector @@ -14,7 +15,8 @@ module Arel end def visit_Arel_Nodes_DoesNotMatch o, collector - collector = infix_value o, collector, ' NOT ILIKE ' + op = o.case_sensitive ? ' NOT LIKE ' : ' NOT ILIKE ' + collector = infix_value o, collector, op if o.escape collector << ' ESCAPE ' visit o.escape, collector @@ -24,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 |