diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 03ff0d4eadd9d27d467f6f432a91137cd339cfa4..7f83891043092e7eb79e4f3a9ca59618085eaee0 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/type/boolean.rb b/activerecord/lib/active_record/connection_adapters/type/boolean.rb index 938d227632e5116cb7dea78e163f149a3e043f52..c60a980e80aefea20b88b7f410c5df2f6001feb8 100644 --- a/activerecord/lib/active_record/connection_adapters/type/boolean.rb +++ b/activerecord/lib/active_record/connection_adapters/type/boolean.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/type/date.rb b/activerecord/lib/active_record/connection_adapters/type/date.rb index 1632f3c8f4e3f46b8bfa849abd15715e6c209738..9208da6efe176f5831f40cffc8633af0a714c5ae 100644 --- a/activerecord/lib/active_record/connection_adapters/type/date.rb +++ b/activerecord/lib/active_record/connection_adapters/type/date.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/type/date_time.rb b/activerecord/lib/active_record/connection_adapters/type/date_time.rb index 1485fd3d803daad2758d592777c37f24987a12be..e1cc9cee4ab8195ad33333c9bf27b39327998c73 100644 --- a/activerecord/lib/active_record/connection_adapters/type/date_time.rb +++ b/activerecord/lib/active_record/connection_adapters/type/date_time.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/type/decimal.rb b/activerecord/lib/active_record/connection_adapters/type/decimal.rb index 5b39ea9e2f9d1b4bcd3c66e6b552bcf4d0b3206b..9581cd5964d9f66d86a3c663230cdd98f1c10b01 100644 --- a/activerecord/lib/active_record/connection_adapters/type/decimal.rb +++ b/activerecord/lib/active_record/connection_adapters/type/decimal.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/type/float.rb b/activerecord/lib/active_record/connection_adapters/type/float.rb index 089169e7c9b6b41cea6a225f267a1eabb575ecd4..2d436d4aa360e3a6b25266a50f41f6d8e0bdcff1 100644 --- a/activerecord/lib/active_record/connection_adapters/type/float.rb +++ b/activerecord/lib/active_record/connection_adapters/type/float.rb @@ -5,6 +5,12 @@ class Float < Value # :nodoc: def type :float end + + private + + def cast_value(value) + value.to_f + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/type/integer.rb b/activerecord/lib/active_record/connection_adapters/type/integer.rb index 5510a11bd41828c3c1487b5ad67fa3cd1c4a62d6..b839e043ad19c748b654abd99e4b1c6e4b3f7dbc 100644 --- a/activerecord/lib/active_record/connection_adapters/type/integer.rb +++ b/activerecord/lib/active_record/connection_adapters/type/integer.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/type/string.rb b/activerecord/lib/active_record/connection_adapters/type/string.rb index 0feb4299f5be98a54fc55b49d2a851f6b281c1c1..d6c1c64834aa0cc057023381b7e058d1b9f38649 100644 --- a/activerecord/lib/active_record/connection_adapters/type/string.rb +++ b/activerecord/lib/active_record/connection_adapters/type/string.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/type/time.rb b/activerecord/lib/active_record/connection_adapters/type/time.rb index a3a687a8ad7ad5a63f2d4237ca86778a39beb987..c5c9c44676dcc5ac7e4406244c4d234aaddbfc41 100644 --- a/activerecord/lib/active_record/connection_adapters/type/time.rb +++ b/activerecord/lib/active_record/connection_adapters/type/time.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/type/value.rb b/activerecord/lib/active_record/connection_adapters/type/value.rb index f7d7b9351be370f6b43b6e5e8bc3e56ff12e3d2d..a83f0e652d4485ff5926dc3791be16c5e24eb9b1 100644 --- a/activerecord/lib/active_record/connection_adapters/type/value.rb +++ b/activerecord/lib/active_record/connection_adapters/type/value.rb @@ -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 diff --git a/activerecord/test/cases/column_test.rb b/activerecord/test/cases/column_test.rb index fffcb19e567f833aff284f33858a75f28806a2d4..c5d455f59cc2cfaa8b57cc73f73e1ecd8f60c9fe 100644 --- a/activerecord/test/cases/column_test.rb +++ b/activerecord/test/cases/column_test.rb @@ -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)