Handle quoting of Rational numbers for MySQL

This commit is contained in:
Kevin McPhillips 2022-02-11 13:04:02 -05:00
parent f9a1671c18
commit f572e0ea1c
7 changed files with 31 additions and 0 deletions

View File

@ -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*

View File

@ -8,6 +8,8 @@ module ActiveRecord
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

View File

@ -72,6 +72,11 @@ module ActiveRecord
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

View File

@ -16,6 +16,10 @@ class Mysql2QuotingTest < ActiveRecord::Mysql2TestCase
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

View File

@ -37,6 +37,12 @@ module ActiveRecord
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

View File

@ -33,6 +33,11 @@ module ActiveRecord
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

View File

@ -336,6 +336,11 @@ module ActiveRecord
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