Skip to content

Commit 4a4286a

Browse files
committed
Extract quoted_binary and use it rather than override _quote
Each databases have different binary representation. Therefore all adapters overrides `_quote` for quoting binary. Extract `quoted_binary` for quoting binary and use it rather than override `_quote`.
1 parent 56b3849 commit 4a4286a

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ def quoted_time(value) # :nodoc:
150150
quoted_date(value).sub(/\A2000-01-01 /, "")
151151
end
152152

153+
def quoted_binary(value) # :nodoc:
154+
"'#{quote_string(value.to_s)}'"
155+
end
156+
153157
private
154158

155159
def type_casted_binds(binds)
@@ -162,14 +166,15 @@ def types_which_need_no_typecasting
162166

163167
def _quote(value)
164168
case value
165-
when String, ActiveSupport::Multibyte::Chars, Type::Binary::Data
169+
when String, ActiveSupport::Multibyte::Chars
166170
"'#{quote_string(value.to_s)}'"
167171
when true then quoted_true
168172
when false then quoted_false
169173
when nil then "NULL"
170174
# BigDecimals need to be put in a non-normalized form and quoted.
171175
when BigDecimal then value.to_s("F")
172176
when Numeric, ActiveSupport::Duration then value.to_s
177+
when Type::Binary::Data then quoted_binary(value)
173178
when Type::Time::Value then "'#{quoted_time(value)}'"
174179
when Date, Time then "'#{quoted_date(value)}'"
175180
when Symbol then "'#{quote_string(value.to_s)}'"

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ def quoted_date(value)
3636
end
3737
end
3838

39-
private
40-
41-
def _quote(value)
42-
if value.is_a?(Type::Binary::Data)
43-
"x'#{value.hex}'"
44-
else
45-
super
46-
end
47-
end
39+
def quoted_binary(value)
40+
"x'#{value.hex}'"
41+
end
4842
end
4943
end
5044
end

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def quoted_date(value) #:nodoc:
5555
end
5656
end
5757

58+
def quoted_binary(value) # :nodoc:
59+
"'#{escape_bytea(value.to_s)}'"
60+
end
61+
5862
def quote_default_expression(value, column) # :nodoc:
5963
if value.is_a?(Proc)
6064
value.call
@@ -76,8 +80,6 @@ def lookup_cast_type_from_column(column) # :nodoc:
7680

7781
def _quote(value)
7882
case value
79-
when Type::Binary::Data
80-
"'#{escape_bytea(value.to_s)}'"
8183
when OID::Xml::Data
8284
"xml '#{quote_string(value.to_s)}'"
8385
when OID::Bit::Data

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ def quoted_time(value)
1818
quoted_date(value)
1919
end
2020

21-
private
21+
def quoted_binary(value)
22+
"x'#{value.hex}'"
23+
end
2224

23-
def _quote(value)
24-
if value.is_a?(Type::Binary::Data)
25-
"x'#{value.hex}'"
26-
else
27-
super
28-
end
29-
end
25+
private
3026

3127
def _type_cast(value)
3228
case value

0 commit comments

Comments
 (0)