Skip to content

Commit 0dc919b

Browse files
authored
Merge pull request rails#47684 from rails/revert-47493-quote_binary_strings
Revert "Quote binary strings in Arel"
2 parents 3b7e413 + bb7f3be commit 0dc919b

File tree

6 files changed

+11
-87
lines changed

6 files changed

+11
-87
lines changed

activemodel/lib/active_model/type/immutable_string.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,13 @@ def type
4646
def serialize(value)
4747
case value
4848
when ::Numeric, ::Symbol, ActiveSupport::Duration then value.to_s
49-
when ::String then serialize_cast_value(value)
5049
when true then @true
5150
when false then @false
5251
else super
5352
end
5453
end
5554

5655
def serialize_cast_value(value) # :nodoc:
57-
if value&.encoding == Encoding::BINARY
58-
# If we can treat the bytes as UTF-8 without changing them, then use UTF-8 as encoding
59-
new_value = value.dup.force_encoding(Encoding::UTF_8)
60-
return new_value if new_value.valid_encoding?
61-
end
62-
6356
value
6457
end
6558

activemodel/test/cases/type/immutable_string_test.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,6 @@ class ImmutableStringTest < ActiveModel::TestCase
1717
assert_same s, type.cast(s)
1818
assert_same s, type.deserialize(s)
1919
end
20-
21-
test "leaves validly encoded strings untouched" do
22-
s = "string with àccénts".encode(Encoding::ISO_8859_1)
23-
type = Type::ImmutableString.new
24-
assert_same s, type.serialize(s)
25-
end
26-
27-
test "serializes valid, binary-encoded strings to UTF-8" do
28-
s = "string with àccénts".b
29-
type = Type::ImmutableString.new
30-
serialized = type.serialize(s)
31-
assert_equal Encoding::UTF_8, serialized.encoding
32-
assert_equal s.bytes, serialized.bytes
33-
end
34-
35-
test "leaves true binary data untouched" do
36-
binary_data = "\xEE\x49\xC7".b
37-
type = Type::ImmutableString.new
38-
assert_same binary_data, type.serialize(binary_data)
39-
end
4020
end
4121
end
4222
end

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ 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-
# 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)
19+
when BigDecimal then value.to_s("F")
2320
when Numeric then value.to_s
2421
when Type::Binary::Data then quoted_binary(value)
2522
when Type::Time::Value then "'#{quoted_time(value)}'"

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

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,14 @@ module ActiveRecord
66
module ConnectionAdapters
77
module MySQL
88
module Quoting # :nodoc:
9-
def quote(value)
10-
case value
11-
when String
12-
if value.encoding == Encoding::BINARY
13-
quoted_binary(value)
14-
else
15-
"'#{quote_string(value.to_s)}'"
16-
end
17-
else
18-
super
19-
end
20-
end
21-
229
def cast_bound_value(value)
2310
case value
2411
when Rational
2512
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)
3113
when Numeric
3214
value.to_s
15+
when BigDecimal
16+
value.to_s("F")
3317
when true
3418
"1"
3519
when false
@@ -67,11 +51,7 @@ def quoted_date(value)
6751
end
6852

6953
def quoted_binary(value)
70-
if value.is_a? String
71-
"x'#{value.unpack1("H*")}'"
72-
else
73-
"x'#{value.hex}'"
74-
end
54+
"x'#{value.hex}'"
7555
end
7656

7757
def unquote_identifier(identifier)

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@ module ActiveRecord
44
module ConnectionAdapters
55
module SQLite3
66
module Quoting # :nodoc:
7-
def quote(value)
8-
case value
9-
when String
10-
if value.encoding == Encoding::BINARY
11-
quoted_binary(value)
12-
else
13-
"'#{quote_string(value.to_s)}'"
14-
end
15-
else
16-
super
17-
end
18-
end
19-
207
def quote_string(s)
218
::SQLite3::Database.quote(s)
229
end
@@ -39,11 +26,7 @@ def quoted_time(value)
3926
end
4027

4128
def quoted_binary(value)
42-
if value.is_a? String
43-
"x'#{value.unpack1("H*")}'"
44-
else
45-
"x'#{value.hex}'"
46-
end
29+
"x'#{value.hex}'"
4730
end
4831

4932
def quoted_true
@@ -79,6 +62,12 @@ def type_cast(value) # :nodoc:
7962
case value
8063
when BigDecimal
8164
value.to_f
65+
when String
66+
if value.encoding == Encoding::ASCII_8BIT
67+
super(value.encode(Encoding::UTF_8))
68+
else
69+
super
70+
end
8271
else
8372
super
8473
end

activerecord/test/cases/binary_test.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,4 @@ def test_load_save
3737
assert_equal data, bin.reload.data, "Reloaded data differs from original"
3838
end
3939
end
40-
41-
unless current_adapter?(:PostgreSQLAdapter)
42-
def test_does_not_cause_database_warnings
43-
original_db_warnings_action = ActiveRecord.db_warnings_action
44-
ActiveRecord.db_warnings_action = :raise
45-
46-
Binary.delete_all
47-
binary_data = "\xEE\x49\xC7".b
48-
Binary.connection.insert(Arel.sql("INSERT INTO binaries(data) VALUES(?)", binary_data))
49-
binary = Binary.first
50-
assert_equal binary_data, binary.data
51-
ensure
52-
ActiveRecord.db_warnings_action = original_db_warnings_action || :ignore
53-
end
54-
end
5540
end

0 commit comments

Comments
 (0)