diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-01-02 11:17:55 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-01-02 11:17:55 -0300 |
commit | acb199191080d25404f2b5b24e57517bf1b57336 (patch) | |
tree | 7bf7021472f7e734a18a849c8667cc947438e796 /activerecord/lib | |
parent | bed2bdb17c50f72e61769c98cd839548da760ca9 (diff) | |
parent | 4157f5d172830df07b44a66d4096d152e068dc4a (diff) | |
download | rails-acb199191080d25404f2b5b24e57517bf1b57336.tar.gz rails-acb199191080d25404f2b5b24e57517bf1b57336.tar.bz2 rails-acb199191080d25404f2b5b24e57517bf1b57336.zip |
Merge pull request #18067 from kamipo/format_datetime_string_according_to_precision
Format the datetime string according to the precision of the datetime field.
Conflicts:
activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index d083e413a5..03d30b5dd0 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -591,6 +591,13 @@ module ActiveRecord when 0x1000000..0xffffffff; 'longtext' else raise(ActiveRecordError, "No text type has character length #{limit}") end + when 'datetime' + return super unless precision + + case precision + when 0..6; "datetime(#{precision})" + else raise(ActiveRecordError, "No datetime type has precision of #{precision}. The allowed range of precision is from 0 to 6.") + end else super end @@ -679,6 +686,11 @@ module ActiveRecord m.alias_type %r(year)i, 'integer' m.alias_type %r(bit)i, 'binary' + m.register_type(%r(datetime)i) do |sql_type| + precision = extract_precision(sql_type) + MysqlDateTime.new(precision: precision) + end + m.register_type(%r(enum)i) do |sql_type| limit = sql_type[/^enum\((.+)\)/i, 1] .split(',').map{|enum| enum.strip.length - 2}.max @@ -872,6 +884,22 @@ module ActiveRecord TableDefinition.new(native_database_types, name, temporary, options, as) end + class MysqlDateTime < Type::DateTime # :nodoc: + def type_cast_for_database(value) + if value.acts_like?(:time) && value.respond_to?(:usec) + result = super.to_s(:db) + case precision + when 1..6 + "#{result}.#{sprintf("%0#{precision}d", value.usec / 10 ** (6 - precision))}" + else + result + end + else + super + end + end + end + class MysqlString < Type::String # :nodoc: def type_cast_for_database(value) case value |