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

Delegate `#type_cast` to injected type objects on SQLite3

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