diff options
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/mysql_adapter.rb')
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index c73028aca4..94ec5cf595 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -94,6 +94,11 @@ module ActiveRecord TYPES_DISALLOWING_DEFAULT = Set.new([:binary, :text]) TYPES_ALLOWING_EMPTY_STRING_DEFAULT = Set.new([:string]) + 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 initialize(name, default, sql_type = nil, null = true) @original_default = default super @@ -101,6 +106,31 @@ module ActiveRecord @default = '' if @original_default == '' && no_default_allowed? 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 + new_date *ParseDate.parsedate(string)[0..2] + 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 + time_hash = Date._parse(string) + new_time *(time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec) << microseconds(time_hash)) + end + end + end + private def simplified_type(field_type) return :boolean if MysqlAdapter.emulate_booleans && field_type.downcase.index("tinyint(1)") |