Skip to content

Commit c5bf2b4

Browse files
committed
Inline _type_cast
Since `type_cast` was only calling `_type_cast`, and that method was private API, we can avoid that indirection.
1 parent 627406f commit c5bf2b4

File tree

4 files changed

+66
-72
lines changed

4 files changed

+66
-72
lines changed

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ def quote(value)
3030
# SQLite does not understand dates, so this method will convert a Date
3131
# to a String.
3232
def type_cast(value)
33-
_type_cast(value)
33+
case value
34+
when Symbol, ActiveSupport::Multibyte::Chars, Type::Binary::Data
35+
value.to_s
36+
when true then unquoted_true
37+
when false then unquoted_false
38+
# BigDecimals need to be put in a non-normalized form and quoted.
39+
when BigDecimal then value.to_s("F")
40+
when nil, Numeric, String then value
41+
when Type::Time::Value then quoted_time(value)
42+
when Date, Time then quoted_date(value)
43+
else raise TypeError, "can't cast #{value.class.name}"
44+
end
3445
end
3546

3647
# Quote a value to be used as a bound parameter of unknown type. For example,
@@ -205,21 +216,6 @@ def type_casted_binds(binds)
205216
def lookup_cast_type(sql_type)
206217
type_map.lookup(sql_type)
207218
end
208-
209-
def _type_cast(value)
210-
case value
211-
when Symbol, ActiveSupport::Multibyte::Chars, Type::Binary::Data
212-
value.to_s
213-
when true then unquoted_true
214-
when false then unquoted_false
215-
# BigDecimals need to be put in a non-normalized form and quoted.
216-
when BigDecimal then value.to_s("F")
217-
when nil, Numeric, String then value
218-
when Type::Time::Value then quoted_time(value)
219-
when Date, Time then quoted_date(value)
220-
else raise TypeError, "can't cast #{value.class.name}"
221-
end
222-
end
223219
end
224220
end
225221
end

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ def quoted_binary(value)
4949
"x'#{value.hex}'"
5050
end
5151

52+
# Override +type_cast+ we pass to mysql2 Date and Time objects instead
53+
# of Strings since mysql2 is able to handle those classes more efficiently.
54+
def type_cast(value) # :nodoc:
55+
case value
56+
when ActiveSupport::TimeWithZone
57+
# We need to check explicitly for ActiveSupport::TimeWithZone because
58+
# we need to transform it to Time objects but we don't want to
59+
# transform Time objects to themselves.
60+
if ActiveRecord.default_timezone == :utc
61+
value.getutc
62+
else
63+
value.getlocal
64+
end
65+
when Date, Time
66+
value
67+
else
68+
super
69+
end
70+
end
71+
5272
def column_name_matcher
5373
COLUMN_NAME
5474
end
@@ -84,27 +104,6 @@ def column_name_with_order_matcher
84104
/ix
85105

86106
private_constant :COLUMN_NAME, :COLUMN_NAME_WITH_ORDER
87-
88-
private
89-
# Override +_type_cast+ we pass to mysql2 Date and Time objects instead
90-
# of Strings since mysql2 is able to handle those classes more efficiently.
91-
def _type_cast(value)
92-
case value
93-
when ActiveSupport::TimeWithZone
94-
# We need to check explicitly for ActiveSupport::TimeWithZone because
95-
# we need to transform it to Time objects but we don't want to
96-
# transform Time objects to themselves.
97-
if ActiveRecord.default_timezone == :utc
98-
value.getutc
99-
else
100-
value.getlocal
101-
end
102-
when Date, Time
103-
value
104-
else
105-
super
106-
end
107-
end
108107
end
109108
end
110109
end

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ def quote_default_expression(value, column) # :nodoc:
9999
end
100100
end
101101

102+
def type_cast(value) # :nodoc:
103+
case value
104+
when Type::Binary::Data
105+
# Return a bind param hash with format as binary.
106+
# See https://deveiate.org/code/pg/PG/Connection.html#method-i-exec_prepared-doc
107+
# for more information
108+
{ value: value.to_s, format: 1 }
109+
when OID::Xml::Data, OID::Bit::Data
110+
value.to_s
111+
when OID::Array::Data
112+
encode_array(value)
113+
when Range
114+
encode_range(value)
115+
else
116+
super
117+
end
118+
end
119+
102120
def lookup_cast_type_from_column(column) # :nodoc:
103121
type_map.lookup(column.oid, column.fmod, column.sql_type)
104122
end
@@ -145,24 +163,6 @@ def lookup_cast_type(sql_type)
145163
super(query_value("SELECT #{quote(sql_type)}::regtype::oid", "SCHEMA").to_i)
146164
end
147165

148-
def _type_cast(value)
149-
case value
150-
when Type::Binary::Data
151-
# Return a bind param hash with format as binary.
152-
# See https://deveiate.org/code/pg/PG/Connection.html#method-i-exec_prepared-doc
153-
# for more information
154-
{ value: value.to_s, format: 1 }
155-
when OID::Xml::Data, OID::Bit::Data
156-
value.to_s
157-
when OID::Array::Data
158-
encode_array(value)
159-
when Range
160-
encode_range(value)
161-
else
162-
super
163-
end
164-
end
165-
166166
def encode_array(array_data)
167167
encoder = array_data.encoder
168168
values = type_cast_array(array_data.values)
@@ -188,7 +188,7 @@ def determine_encoding_of_strings_in_array(value)
188188
def type_cast_array(values)
189189
case values
190190
when ::Array then values.map { |item| type_cast_array(item) }
191-
else _type_cast(values)
191+
else type_cast(values)
192192
end
193193
end
194194

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ def unquoted_false
4545
0
4646
end
4747

48+
def type_cast(value) # :nodoc:
49+
case value
50+
when BigDecimal
51+
value.to_f
52+
when String
53+
if value.encoding == Encoding::ASCII_8BIT
54+
super(value.encode(Encoding::UTF_8))
55+
else
56+
super
57+
end
58+
else
59+
super
60+
end
61+
end
62+
4863
def column_name_matcher
4964
COLUMN_NAME
5065
end
@@ -80,22 +95,6 @@ def column_name_with_order_matcher
8095
/ix
8196

8297
private_constant :COLUMN_NAME, :COLUMN_NAME_WITH_ORDER
83-
84-
private
85-
def _type_cast(value)
86-
case value
87-
when BigDecimal
88-
value.to_f
89-
when String
90-
if value.encoding == Encoding::ASCII_8BIT
91-
super(value.encode(Encoding::UTF_8))
92-
else
93-
super
94-
end
95-
else
96-
super
97-
end
98-
end
9998
end
10099
end
101100
end

0 commit comments

Comments
 (0)