Skip to content

Commit d624a8e

Browse files
committed
Ensure BigDecimals are not binary-encoded
In Ruby 2.7, `BigDecimal#to_s` generates ASCII-8BIT-encoded strings. We don't want those to get hex-encoded in the generated SQL.
1 parent 20c3eae commit d624a8e

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

activerecord/lib/active_record/connection_adapters/abstract/quoting.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ def quote(value)
1616
when false then quoted_false
1717
when nil then "NULL"
1818
# BigDecimals need to be put in a non-normalized form and quoted.
19-
when BigDecimal then value.to_s("F")
19+
# Additionally, for Ruby 2.7, the string returned by `to_s` is ASCII-8BIT.
20+
# We want to avoid that, as that will cause the string to be quoted as
21+
# binary. It is safe to force the encoding to US-ASCII.
22+
when BigDecimal then value.to_s("F").force_encoding(Encoding::US_ASCII)
2023
when Numeric then value.to_s
2124
when Type::Binary::Data then quoted_binary(value)
2225
when Type::Time::Value then "'#{quoted_time(value)}'"

activerecord/lib/active_record/connection_adapters/mysql/quoting.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ def cast_bound_value(value)
2323
case value
2424
when Rational
2525
value.to_f.to_s
26+
when BigDecimal
27+
# For Ruby 2.7, the string returned by `to_s` is ASCII-8BIT.
28+
# We want to avoid that, as that will cause the string to be quoted as
29+
# binary. It is safe to force the encoding to US-ASCII.
30+
value.to_s("F").force_encoding(Encoding::US_ASCII)
2631
when Numeric
2732
value.to_s
28-
when BigDecimal
29-
value.to_s("F")
3033
when true
3134
"1"
3235
when false

0 commit comments

Comments
 (0)