提交 36fde2b7 编写于 作者: S Sean Griffin

Delegate `#type_cast` to injected type objects on SQLite3

上级 528aff12
......@@ -42,13 +42,21 @@ def sqlite3_connection(config)
module ConnectionAdapters #:nodoc:
class SQLite3Column < Column #:nodoc:
class << self
def binary_to_string(value)
if value.encoding != Encoding::ASCII_8BIT
value = value.force_encoding(Encoding::ASCII_8BIT)
end
value
def type_cast(value)
if encoded?
super
else
cast_type.type_cast(value)
end
end
end
class SQLite3Binary < Type::Binary # :nodoc:
def cast_value(value)
if value.encoding != Encoding::ASCII_8BIT
value = value.force_encoding(Encoding::ASCII_8BIT)
end
value
end
end
......@@ -502,6 +510,12 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
end
protected
def initialize_type_map(m)
super
m.register_type(/binary/i, SQLite3Binary.new)
end
def select(sql, name = nil, binds = []) #:nodoc:
exec_query(sql, name, binds)
end
......
......@@ -5,6 +5,12 @@ class Boolean < Value # :nodoc:
def type
:boolean
end
private
def cast_value(value)
Column.value_to_boolean(value)
end
end
end
end
......
......@@ -5,6 +5,12 @@ class Date < Value # :nodoc:
def type
:date
end
private
def cast_value(value)
Column.value_to_date(value)
end
end
end
end
......
......@@ -5,6 +5,12 @@ class DateTime < Value # :nodoc:
def type
:datetime
end
private
def cast_value(string)
Column.string_to_time(string)
end
end
end
end
......
......@@ -5,6 +5,12 @@ class Decimal < Value # :nodoc:
def type
:decimal
end
private
def cast_value(value)
Column.value_to_decimal(value)
end
end
end
end
......
......@@ -5,6 +5,12 @@ class Float < Value # :nodoc:
def type
:float
end
private
def cast_value(value)
value.to_f
end
end
end
end
......
......@@ -5,6 +5,12 @@ class Integer < Value # :nodoc:
def type
:integer
end
private
def cast_value(value)
Column.value_to_integer(value)
end
end
end
end
......
......@@ -5,6 +5,16 @@ class String < Value # :nodoc:
def type
:string
end
private
def cast_value(value)
case value
when true then "1"
when false then "0"
else value.to_s
end
end
end
end
end
......
......@@ -5,6 +5,12 @@ class Time < Value # :nodoc:
def type
:time
end
private
def cast_value(value)
Column.string_to_dummy_time(value)
end
end
end
end
......
......@@ -3,6 +3,16 @@ module ConnectionAdapters
module Type
class Value # :nodoc:
def type; end
def type_cast(value)
cast_value(value) unless value.nil?
end
private
def cast_value(value)
value
end
end
end
end
......
......@@ -146,7 +146,7 @@ def test_string_to_time_with_timezone
if current_adapter?(:SQLite3Adapter)
def test_binary_encoding
column = SQLite3Column.new("field", nil, Type::Binary.new)
column = SQLite3Column.new("field", nil, SQLite3Binary.new)
utf8_string = "a string".encode(Encoding::UTF_8)
type_cast = column.type_cast(utf8_string)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册