Skip to content

Commit

Permalink
Merge pull request #44404 from kmcphillips/mysql-quote-rational
Browse files Browse the repository at this point in the history
Handle quoting of Rational numbers for MySQL
  • Loading branch information
rafaelfranca committed Feb 14, 2022
2 parents 25fe901 + f572e0e commit 7ac90d9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Fix quoting of `ActiveSupport::Duration` and `Rational` numbers in the MySQL adapter.

*Kevin McPhillips*

* Allow column name with COLLATE (e.g., title COLLATE "C") as safe SQL string

*Shugo Maeda*
Expand Down
Expand Up @@ -8,6 +8,8 @@ module MySQL
module Quoting # :nodoc:
def quote_bound_value(value)
case value
when Rational
quote(value.to_f.to_s)
when Numeric, ActiveSupport::Duration
quote(value.to_s)
when BigDecimal
Expand Down
Expand Up @@ -73,6 +73,11 @@ def test_where_with_decimal_for_string_column_using_bind_parameters
assert_equal 0, count
end

def test_where_with_rational_for_string_column_using_bind_parameters
count = Post.where("title = ?", Rational(0)).count
assert_equal 0, count
end

def test_where_with_duration_for_string_column_using_bind_parameters
count = Post.where("title = ?", 0.seconds).count
assert_equal 0, count
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/adapters/mysql2/quoting_test.rb
Expand Up @@ -16,6 +16,10 @@ def test_quote_bound_big_decimal
assert_equal "'4.2'", @conn.quote_bound_value(BigDecimal("4.2"))
end

def test_quote_bound_rational
assert_equal "'0.75'", @conn.quote_bound_value(Rational(3, 4))
end

def test_quote_bound_duration
assert_equal "'42'", @conn.quote_bound_value(42.seconds)
end
Expand Down
Expand Up @@ -38,6 +38,12 @@ def test_where_with_decimal_for_string_column_using_bind_parameters
end
end

def test_where_with_rational_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
Post.where("title = ?", Rational(0)).count
end
end

def test_where_with_duration_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
Post.where("title = ?", 0.seconds).count
Expand Down
Expand Up @@ -34,6 +34,11 @@ def test_where_with_decimal_for_string_column_using_bind_parameters
assert_equal 0, count
end

def test_where_with_rational_for_string_column_using_bind_parameters
count = Post.where("title = ?", Rational(0)).count
assert_equal 0, count
end

def test_where_with_duration_for_string_column_using_bind_parameters
count = Post.where("title = ?", 0.seconds).count
assert_equal 0, count
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/relation/where_test.rb
Expand Up @@ -336,6 +336,11 @@ def test_where_with_decimal_for_string_column
assert_equal 0, count
end

def test_where_with_rational_for_string_column
count = Post.where(title: Rational(0)).count
assert_equal 0, count
end

def test_where_with_duration_for_string_column
count = Post.where(title: 0.seconds).count
assert_equal 0, count
Expand Down

0 comments on commit 7ac90d9

Please sign in to comment.