Skip to content

Commit

Permalink
Merge pull request #36800 from jamespearson/matches_regex_mysql
Browse files Browse the repository at this point in the history
Enabled matches_regex for MySql
  • Loading branch information
kamipo committed Jul 29, 2019
2 parents 79740de + 92c265b commit 2e6d9af
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Allow matches_regex on MySQL

*James Pearson*

* Allow specifying fixtures to be ignored by setting `ignore` in YAML file's '_fixture' section.

*Tongfei Gao*
Expand Down
8 changes: 8 additions & 0 deletions activerecord/lib/arel/visitors/mysql.rb
Expand Up @@ -48,6 +48,14 @@ def visit_Arel_Nodes_IsDistinctFrom(o, collector)
visit_Arel_Nodes_IsNotDistinctFrom o, collector
end

def visit_Arel_Nodes_Regexp(o, collector)
infix_value o, collector, " REGEXP "
end

def visit_Arel_Nodes_NotRegexp(o, collector)
infix_value o, collector, " NOT REGEXP "
end

# In the simple case, MySQL allows us to place JOINs directly into the UPDATE
# query. However, this does not allow for LIMIT, OFFSET and ORDER. To support
# these, we must use a subquery.
Expand Down
46 changes: 46 additions & 0 deletions activerecord/test/cases/arel/visitors/mysql_test.rb
Expand Up @@ -104,6 +104,52 @@ def compile(node)
sql.must_be_like %{ NOT "users"."name" <=> NULL }
end
end

describe "Nodes::Regexp" do
before do
@table = Table.new(:users)
@attr = @table[:id]
end

it "should know how to visit" do
node = @table[:name].matches_regexp("foo.*")
node.must_be_kind_of Nodes::Regexp
compile(node).must_be_like %{
"users"."name" REGEXP 'foo.*'
}
end

it "can handle subqueries" do
subquery = @table.project(:id).where(@table[:name].matches_regexp("foo.*"))
node = @attr.in subquery
compile(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" REGEXP 'foo.*')
}
end
end

describe "Nodes::NotRegexp" do
before do
@table = Table.new(:users)
@attr = @table[:id]
end

it "should know how to visit" do
node = @table[:name].does_not_match_regexp("foo.*")
node.must_be_kind_of Nodes::NotRegexp
compile(node).must_be_like %{
"users"."name" NOT REGEXP 'foo.*'
}
end

it "can handle subqueries" do
subquery = @table.project(:id).where(@table[:name].does_not_match_regexp("foo.*"))
node = @attr.in subquery
compile(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" NOT REGEXP 'foo.*')
}
end
end
end
end
end

0 comments on commit 2e6d9af

Please sign in to comment.