Skip to content

Commit 627406f

Browse files
committed
Inline _quote method
`quote` now was only calling `_quote` and since _quote was private API, there is no need to keep that indirection.
1 parent 2d821ef commit 627406f

File tree

3 files changed

+44
-48
lines changed

3 files changed

+44
-48
lines changed

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,21 @@ module Quoting
99
# Quotes the column value to help prevent
1010
# {SQL injection attacks}[https://en.wikipedia.org/wiki/SQL_injection].
1111
def quote(value)
12-
_quote(value)
12+
case value
13+
when String, Symbol, ActiveSupport::Multibyte::Chars
14+
"'#{quote_string(value.to_s)}'"
15+
when true then quoted_true
16+
when false then quoted_false
17+
when nil then "NULL"
18+
# BigDecimals need to be put in a non-normalized form and quoted.
19+
when BigDecimal then value.to_s("F")
20+
when Numeric, ActiveSupport::Duration then value.to_s
21+
when Type::Binary::Data then quoted_binary(value)
22+
when Type::Time::Value then "'#{quoted_time(value)}'"
23+
when Date, Time then "'#{quoted_date(value)}'"
24+
when Class then "'#{value}'"
25+
else raise TypeError, "can't quote #{value.class.name}"
26+
end
1327
end
1428

1529
# Cast a +value+ to a type that the database understands. For example,
@@ -23,7 +37,7 @@ def type_cast(value)
2337
# MySQL might perform dangerous castings when comparing a string to a number,
2438
# so this method will cast numbers to string.
2539
def quote_bound_value(value)
26-
_quote(value)
40+
quote(value)
2741
end
2842

2943
# If you are having to call this function, you are likely doing something
@@ -192,24 +206,6 @@ def lookup_cast_type(sql_type)
192206
type_map.lookup(sql_type)
193207
end
194208

195-
def _quote(value)
196-
case value
197-
when String, Symbol, ActiveSupport::Multibyte::Chars
198-
"'#{quote_string(value.to_s)}'"
199-
when true then quoted_true
200-
when false then quoted_false
201-
when nil then "NULL"
202-
# BigDecimals need to be put in a non-normalized form and quoted.
203-
when BigDecimal then value.to_s("F")
204-
when Numeric, ActiveSupport::Duration then value.to_s
205-
when Type::Binary::Data then quoted_binary(value)
206-
when Type::Time::Value then "'#{quoted_time(value)}'"
207-
when Date, Time then "'#{quoted_date(value)}'"
208-
when Class then "'#{value}'"
209-
else raise TypeError, "can't quote #{value.class.name}"
210-
end
211-
end
212-
213209
def _type_cast(value)
214210
case value
215211
when Symbol, ActiveSupport::Multibyte::Chars, Type::Binary::Data

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ module Quoting # :nodoc:
99
def quote_bound_value(value)
1010
case value
1111
when Numeric
12-
_quote(value.to_s)
12+
quote(value.to_s)
1313
when BigDecimal
14-
_quote(value.to_s("F"))
14+
quote(value.to_s("F"))
1515
when true
1616
"'1'"
1717
when false
1818
"'0'"
1919
else
20-
_quote(value)
20+
quote(value)
2121
end
2222
end
2323

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ def unescape_bytea(value)
1616
@connection.unescape_bytea(value) if value
1717
end
1818

19+
def quote(value) # :nodoc:
20+
case value
21+
when OID::Xml::Data
22+
"xml '#{quote_string(value.to_s)}'"
23+
when OID::Bit::Data
24+
if value.binary?
25+
"B'#{value}'"
26+
elsif value.hex?
27+
"X'#{value}'"
28+
end
29+
when Numeric
30+
if value.finite?
31+
super
32+
else
33+
"'#{value}'"
34+
end
35+
when OID::Array::Data
36+
quote(encode_array(value))
37+
when Range
38+
quote(encode_range(value))
39+
else
40+
super
41+
end
42+
end
43+
1944
# Quotes strings for use in SQL input.
2045
def quote_string(s) # :nodoc:
2146
PG::Connection.escape(s)
@@ -120,31 +145,6 @@ def lookup_cast_type(sql_type)
120145
super(query_value("SELECT #{quote(sql_type)}::regtype::oid", "SCHEMA").to_i)
121146
end
122147

123-
def _quote(value)
124-
case value
125-
when OID::Xml::Data
126-
"xml '#{quote_string(value.to_s)}'"
127-
when OID::Bit::Data
128-
if value.binary?
129-
"B'#{value}'"
130-
elsif value.hex?
131-
"X'#{value}'"
132-
end
133-
when Numeric
134-
if value.finite?
135-
super
136-
else
137-
"'#{value}'"
138-
end
139-
when OID::Array::Data
140-
_quote(encode_array(value))
141-
when Range
142-
_quote(encode_range(value))
143-
else
144-
super
145-
end
146-
end
147-
148148
def _type_cast(value)
149149
case value
150150
when Type::Binary::Data

0 commit comments

Comments
 (0)