提交 96ac14a3 编写于 作者: S Sean Griffin

Always convert strings to UTF-8, regardless of column type in SQLite

All columns which would map to a string primitive need this behavior.
Binary has it's own marker type, so it won't go through this conversion.
String and text, which need this, will.

Fixes #18585.
上级 82173989
......@@ -50,16 +50,6 @@ def cast_value(value)
end
end
class SQLite3String < Type::String # :nodoc:
def type_cast_for_database(value)
if value.is_a?(::String) && value.encoding == Encoding::ASCII_8BIT
value.encode(Encoding::UTF_8, undef: :replace)
else
super
end
end
end
# The SQLite3 adapter works SQLite 3.6.16 or newer
# with the sqlite3-ruby drivers (available as gem from https://rubygems.org/gems/sqlite3).
#
......@@ -239,6 +229,12 @@ def _type_cast(value) # :nodoc:
case value
when BigDecimal
value.to_f
when String
if value.encoding == Encoding::ASCII_8BIT
super(value.encode(Encoding::UTF_8))
else
super
end
else
super
end
......@@ -496,7 +492,6 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
def initialize_type_map(m)
super
m.register_type(/binary/i, SQLite3Binary.new)
register_class_with_limit m, %r(char)i, SQLite3String
end
def table_structure(table_name)
......
......@@ -85,7 +85,7 @@ def quoted_id
def test_quoting_binary_strings
value = "hello".encode('ascii-8bit')
type = SQLite3String.new
type = Type::String.new
assert_equal "'hello'", @conn.quote(type.type_cast_for_database(value))
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册