File tree Expand file tree Collapse file tree 6 files changed +11
-87
lines changed
lib/active_record/connection_adapters Expand file tree Collapse file tree 6 files changed +11
-87
lines changed Original file line number Diff line number Diff line change @@ -46,20 +46,13 @@ def type
46
46
def serialize ( value )
47
47
case value
48
48
when ::Numeric , ::Symbol , ActiveSupport ::Duration then value . to_s
49
- when ::String then serialize_cast_value ( value )
50
49
when true then @true
51
50
when false then @false
52
51
else super
53
52
end
54
53
end
55
54
56
55
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
-
63
56
value
64
57
end
65
58
Original file line number Diff line number Diff line change @@ -17,26 +17,6 @@ class ImmutableStringTest < ActiveModel::TestCase
17
17
assert_same s , type . cast ( s )
18
18
assert_same s , type . deserialize ( s )
19
19
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
40
20
end
41
21
end
42
22
end
Original file line number Diff line number Diff line change @@ -16,10 +16,7 @@ def quote(value)
16
16
when false then quoted_false
17
17
when nil then "NULL"
18
18
# 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" )
23
20
when Numeric then value . to_s
24
21
when Type ::Binary ::Data then quoted_binary ( value )
25
22
when Type ::Time ::Value then "'#{ quoted_time ( value ) } '"
Original file line number Diff line number Diff line change @@ -6,30 +6,14 @@ module ActiveRecord
6
6
module ConnectionAdapters
7
7
module MySQL
8
8
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
-
22
9
def cast_bound_value ( value )
23
10
case value
24
11
when Rational
25
12
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 )
31
13
when Numeric
32
14
value . to_s
15
+ when BigDecimal
16
+ value . to_s ( "F" )
33
17
when true
34
18
"1"
35
19
when false
@@ -67,11 +51,7 @@ def quoted_date(value)
67
51
end
68
52
69
53
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 } '"
75
55
end
76
56
77
57
def unquote_identifier ( identifier )
Original file line number Diff line number Diff line change @@ -4,19 +4,6 @@ module ActiveRecord
4
4
module ConnectionAdapters
5
5
module SQLite3
6
6
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
-
20
7
def quote_string ( s )
21
8
::SQLite3 ::Database . quote ( s )
22
9
end
@@ -39,11 +26,7 @@ def quoted_time(value)
39
26
end
40
27
41
28
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 } '"
47
30
end
48
31
49
32
def quoted_true
@@ -79,6 +62,12 @@ def type_cast(value) # :nodoc:
79
62
case value
80
63
when BigDecimal
81
64
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
82
71
else
83
72
super
84
73
end
Original file line number Diff line number Diff line change @@ -37,19 +37,4 @@ def test_load_save
37
37
assert_equal data , bin . reload . data , "Reloaded data differs from original"
38
38
end
39
39
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
55
40
end
You can’t perform that action at this time.
0 commit comments