aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/type
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-20 18:10:54 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-20 18:10:54 -0300
commit52b558695d5a1b33ff21516cbc71d1ae1dee4632 (patch)
tree3bfcd3aaca10539014e352c5f1582175f42375e6 /activerecord/lib/active_record/connection_adapters/type
parent90a82cbfb26a24f801864a7fd2664553876b51f3 (diff)
parent8f387995054d9ce79522da8ef65fba28c6ea0785 (diff)
downloadrails-52b558695d5a1b33ff21516cbc71d1ae1dee4632.tar.gz
rails-52b558695d5a1b33ff21516cbc71d1ae1dee4632.tar.bz2
rails-52b558695d5a1b33ff21516cbc71d1ae1dee4632.zip
Merge pull request #15207 from sgrif/sg-inline-column-helpers
Inline typecasting helpers from Column to the appropriate types
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/type')
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/boolean.rb6
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/date.rb25
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/date_time.rb18
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/decimal.rb6
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/integer.rb6
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/time.rb11
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/time_value.rb8
7 files changed, 74 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/type/boolean.rb b/activerecord/lib/active_record/connection_adapters/type/boolean.rb
index c60a980e80..2337bdd563 100644
--- a/activerecord/lib/active_record/connection_adapters/type/boolean.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/boolean.rb
@@ -9,7 +9,11 @@ module ActiveRecord
private
def cast_value(value)
- Column.value_to_boolean(value)
+ if value == ''
+ nil
+ else
+ Column::TRUE_VALUES.include?(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 9208da6efe..599dbd320c 100644
--- a/activerecord/lib/active_record/connection_adapters/type/date.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/date.rb
@@ -9,7 +9,30 @@ module ActiveRecord
private
def cast_value(value)
- Column.value_to_date(value)
+ if value.is_a?(::String)
+ return if value.empty?
+ fast_string_to_date(value) || fallback_string_to_date(value)
+ elsif value.respond_to?(:to_date)
+ value.to_date
+ else
+ value
+ end
+ end
+
+ def fast_string_to_date(string)
+ if string =~ Column::Format::ISO_DATE
+ new_date $1.to_i, $2.to_i, $3.to_i
+ end
+ end
+
+ def fallback_string_to_date(string)
+ new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
+ end
+
+ def new_date(year, mon, mday)
+ if year && year != 0
+ ::Date.new(year, mon, mday) rescue nil
+ 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 757553cd68..c34f4c5a53 100644
--- a/activerecord/lib/active_record/connection_adapters/type/date_time.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/date_time.rb
@@ -11,7 +11,23 @@ module ActiveRecord
private
def cast_value(string)
- Column.string_to_time(string)
+ return string unless string.is_a?(::String)
+ return if string.empty?
+
+ fast_string_to_time(string) || fallback_string_to_time(string)
+ end
+
+ # '0.123456' -> 123456
+ # '1.123456' -> 123456
+ def microseconds(time)
+ time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
+ end
+
+ def fallback_string_to_time(string)
+ time_hash = ::Date._parse(string)
+ time_hash[:sec_fraction] = microseconds(time_hash)
+
+ new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset))
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 91957fe84b..6fb1a6c3b0 100644
--- a/activerecord/lib/active_record/connection_adapters/type/decimal.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/decimal.rb
@@ -11,7 +11,11 @@ module ActiveRecord
private
def cast_value(value)
- Column.value_to_decimal(value)
+ if value.respond_to?(:to_d)
+ value.to_d
+ else
+ value.to_s.to_d
+ 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 fbc72ae4ef..b41a726ef4 100644
--- a/activerecord/lib/active_record/connection_adapters/type/integer.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/integer.rb
@@ -11,7 +11,11 @@ module ActiveRecord
private
def cast_value(value)
- Column.value_to_integer(value)
+ case value
+ when true then 1
+ when false then 0
+ else value.to_i rescue nil
+ 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 b621e8f39c..4dd201e3fe 100644
--- a/activerecord/lib/active_record/connection_adapters/type/time.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/time.rb
@@ -11,7 +11,16 @@ module ActiveRecord
private
def cast_value(value)
- Column.string_to_dummy_time(value)
+ return value unless value.is_a?(::String)
+ return if value.empty?
+
+ dummy_time_value = "2000-01-01 #{value}"
+
+ fast_string_to_time(dummy_time_value) || begin
+ time_hash = ::Date._parse(dummy_time_value)
+ return if time_hash[:hour].nil?
+ new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/type/time_value.rb b/activerecord/lib/active_record/connection_adapters/type/time_value.rb
index d9564d7f48..654e5c7943 100644
--- a/activerecord/lib/active_record/connection_adapters/type/time_value.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/time_value.rb
@@ -18,6 +18,14 @@ module ActiveRecord
::Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
end
end
+
+ # Doesn't handle time zones.
+ def fast_string_to_time(string)
+ if string =~ Column::Format::ISO_DATETIME
+ microsec = ($7.to_r * 1_000_000).to_i
+ new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
+ end
+ end
end
end
end