Skip to content

Commit

Permalink
regex: stop butchering them
Browse files Browse the repository at this point in the history
MariaDB, MySQL and PostgreSQL support PRCE.
  • Loading branch information
akimd committed May 11, 2023
1 parent 607d682 commit d77fa11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## Current Release

### Changes

- `regexp` and `not_regexp` no longer try to change the regexes.
MariaDB, MySQL and PostgreSQL perfectly support PRCE, hence we
no longer try to rewrite the regexes.
Note that `regexp_replace` did not perform any of these rewritings.

## Release v2.1.7/v1.3.7

### New Features

- `o.format_date` as an alternative to `o.format`
Expand Down
31 changes: 2 additions & 29 deletions lib/arel_extensions/comparators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,13 @@ def <=(other)
# REGEXP function
# Pattern matching using regular expressions
def =~(other)
# arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
# if arg == :string || arg == :text
Arel::Nodes::Regexp.new self, convert_regexp(other)
# end
Arel::Nodes::Regexp.new self, Arel.quoted(other, self)
end

# NOT_REGEXP function
# Negation of Regexp
def !~(other)
# arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
# if arg == :string || arg == :text
Arel::Nodes::NotRegexp.new self, convert_regexp(other)
# end
end

private

# Function used for not_regexp.
def convert_regexp(other)
case other
when String
# Do nothing.
when Regexp
other = other.source.gsub('\A', '^')
other.gsub!('\z', '$')
other.gsub!('\Z', '$')
other.gsub!('\d', '[0-9]')
other.gsub!('\D', '[^0-9]')
other.gsub!('\w', '[0-9A-Za-z]')
other.gsub!('\W', '[^A-Za-z0-9_]')
else
raise(ArgumentError)
end
Arel.quoted(other, self)
Arel::Nodes::NotRegexp.new self, Arel.quoted(other, self)
end
end
end

0 comments on commit d77fa11

Please sign in to comment.