Skip to content

Commit f8e3bde

Browse files
authored
Merge pull request rails#47247 from matthewd/quote-bound-value
Bring back quote_bound_value & deprecate instead
2 parents a662e67 + 7991113 commit f8e3bde

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,25 @@ def type_cast(value)
4747
end
4848
end
4949

50+
# Quote a value to be used as a bound parameter of unknown type. For example,
51+
# MySQL might perform dangerous castings when comparing a string to a number,
52+
# so this method will cast numbers to string.
53+
#
54+
# Deprecated: Consider `Arel.sql("... ? ...", value)` or
55+
# +sanitize_sql+ instead.
56+
def quote_bound_value(value)
57+
ActiveRecord.deprecator.warn(<<~MSG.squish)
58+
#quote_bound_value is deprecated and will be removed in Rails 7.2.
59+
Consider Arel.sql(".. ? ..", value) or #sanitize_sql instead.
60+
MSG
61+
62+
quote(cast_bound_value(value))
63+
end
64+
5065
# Cast a value to be used as a bound parameter of unknown type. For example,
5166
# MySQL might perform dangerous castings when comparing a string to a number,
5267
# so this method will cast numbers to string.
53-
def cast_bound_value(value)
68+
def cast_bound_value(value) # :nodoc:
5469
value
5570
end
5671

activerecord/test/cases/adapters/mysql2/quoting_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,34 @@ def test_cast_bound_true
3232
def test_cast_bound_false
3333
assert_equal "0", @conn.cast_bound_value(false)
3434
end
35+
36+
def test_quote_bound_integer
37+
expected = assert_deprecated(ActiveRecord.deprecator) { @conn.quote_bound_value(42) }
38+
assert_equal "'42'", expected
39+
end
40+
41+
def test_quote_bound_big_decimal
42+
expected = assert_deprecated(ActiveRecord.deprecator) { @conn.quote_bound_value(BigDecimal("4.2")) }
43+
assert_equal "'4.2'", expected
44+
end
45+
46+
def test_quote_bound_rational
47+
expected = assert_deprecated(ActiveRecord.deprecator) { @conn.quote_bound_value(Rational(3, 4)) }
48+
assert_equal "'0.75'", expected
49+
end
50+
51+
def test_quote_bound_duration
52+
expected = assert_deprecated(ActiveRecord.deprecator) { @conn.quote_bound_value(42.seconds) }
53+
assert_equal "'42'", expected
54+
end
55+
56+
def test_quote_bound_true
57+
expected = assert_deprecated(ActiveRecord.deprecator) { @conn.quote_bound_value(true) }
58+
assert_equal "'1'", expected
59+
end
60+
61+
def test_quote_bound_false
62+
expected = assert_deprecated(ActiveRecord.deprecator) { @conn.quote_bound_value(false) }
63+
assert_equal "'0'", expected
64+
end
3565
end

0 commit comments

Comments
 (0)