diff options
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb | 38 | ||||
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 29 |
2 files changed, 33 insertions, 34 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 507ddd2499..0325bdbbd8 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -6,6 +6,11 @@ module ActiveRecord module ConnectionAdapters #:nodoc: # An abstract definition of a column in a table. class Column + module Format + ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/ + ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/ + end + attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale attr_accessor :primary @@ -109,18 +114,16 @@ module ActiveRecord def string_to_date(string) return string unless string.is_a?(String) + return nil if string.empty? - new_date *ParseDate.parsedate(string)[0..2] + fast_string_to_date(string) || fallback_string_to_date(string) end def string_to_time(string) return string unless string.is_a?(String) return nil if string.empty? - 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) + fast_string_to_time(string) || fallback_string_to_time(string) end def string_to_dummy_time(string) @@ -174,6 +177,31 @@ module ActiveRecord # Append zero calendar reform start to account for dates skipped by calendar reform DateTime.new(year, mon, mday, hour, min, sec, zone_offset, 0) rescue nil end + + def fast_string_to_date(string) + if string =~ Format::ISO_DATE + new_date $1.to_i, $2.to_i, $3.to_i + end + end + + # Doesn't handle time zones. + def fast_string_to_time(string) + if string =~ Format::ISO_DATETIME + microsec = ($7.to_f * 10e6).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 + + def fallback_string_to_date(string) + new_date *ParseDate.parsedate(string)[0..2] + 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) + end end private diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index b385af7321..f0d6e443a7 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -91,11 +91,6 @@ module ActiveRecord module ConnectionAdapters class MysqlColumn < Column #:nodoc: - module Format - DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/ - DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(?:\.(\d{6}))?\z/ - end - def extract_default(default) if type == :binary || type == :text if default.blank? @@ -110,30 +105,6 @@ module ActiveRecord end end - class << self - def string_to_date(string) - return string unless string.is_a?(String) - return nil if string.empty? - - if string =~ Format::DATE - new_date $1.to_i, $2.to_i, $3.to_i - else - super - end - end - - def string_to_time(string) - return string unless string.is_a?(String) - return nil if string.empty? - - if string =~ Format::DATETIME - new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, $7.to_i - else - super - end - end - end - private def simplified_type(field_type) return :boolean if MysqlAdapter.emulate_booleans && field_type.downcase.index("tinyint(1)") |