From 36fde2b704164ac02380518350f01a17d3e0208e Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Tue, 20 May 2014 06:16:47 -0700 Subject: Delegate `#type_cast` to injected type objects on SQLite3 --- .../connection_adapters/sqlite3_adapter.rb | 26 +++++++++++++++++----- .../connection_adapters/type/boolean.rb | 6 +++++ .../active_record/connection_adapters/type/date.rb | 6 +++++ .../connection_adapters/type/date_time.rb | 6 +++++ .../connection_adapters/type/decimal.rb | 6 +++++ .../connection_adapters/type/float.rb | 6 +++++ .../connection_adapters/type/integer.rb | 6 +++++ .../connection_adapters/type/string.rb | 10 +++++++++ .../active_record/connection_adapters/type/time.rb | 6 +++++ .../connection_adapters/type/value.rb | 10 +++++++++ activerecord/test/cases/column_test.rb | 2 +- 11 files changed, 83 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 03ff0d4ead..7f83891043 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 938d227632..c60a980e80 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 @@ module ActiveRecord 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 1632f3c8f4..9208da6efe 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 @@ module ActiveRecord 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 1485fd3d80..e1cc9cee4a 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 @@ module ActiveRecord 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 5b39ea9e2f..9581cd5964 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 @@ module ActiveRecord 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 089169e7c9..2d436d4aa3 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 @@ module ActiveRecord 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 5510a11bd4..b839e043ad 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 @@ module ActiveRecord 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 0feb4299f5..d6c1c64834 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 @@ module ActiveRecord 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 a3a687a8ad..c5c9c44676 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 @@ module ActiveRecord 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 f7d7b9351b..a83f0e652d 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 ActiveRecord 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 fffcb19e56..c5d455f59c 100644 --- a/activerecord/test/cases/column_test.rb +++ b/activerecord/test/cases/column_test.rb @@ -146,7 +146,7 @@ module ActiveRecord 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) -- cgit v1.2.3