aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-05-20 17:59:53 +0200
committerYves Senn <yves.senn@gmail.com>2014-05-20 17:59:53 +0200
commit89ca6806a72006ef493e5e6ceb50f8ed02dc2da3 (patch)
tree8213d3911d9d98406ff42c9657b24421944beb25 /activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
parentd17b05657153f4305e4112532c485239d49b77b8 (diff)
parentac371655974aff8c7d09ab374fa2d3e9f1fa6f15 (diff)
downloadrails-89ca6806a72006ef493e5e6ceb50f8ed02dc2da3.tar.gz
rails-89ca6806a72006ef493e5e6ceb50f8ed02dc2da3.tar.bz2
rails-89ca6806a72006ef493e5e6ceb50f8ed02dc2da3.zip
Merge pull request #15198 from sgrif/sg-delegate-type-cast-mysql
Delegate type_cast to injected type object in mysql
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/mysql_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb78
1 files changed, 40 insertions, 38 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index bf09bfe217..fc27e1dd66 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -68,26 +68,12 @@ module ActiveRecord
class MysqlAdapter < AbstractMysqlAdapter
class Column < AbstractMysqlAdapter::Column #:nodoc:
- def self.string_to_time(value)
- return super unless Mysql::Time === value
- new_time(
- value.year,
- value.month,
- value.day,
- value.hour,
- value.minute,
- value.second,
- value.second_part)
- end
-
- def self.string_to_dummy_time(v)
- return super unless Mysql::Time === v
- new_time(2000, 01, 01, v.hour, v.minute, v.second, v.second_part)
- end
-
- def self.string_to_date(v)
- return super unless Mysql::Time === v
- new_date(v.year, v.month, v.day)
+ def type_cast(value)
+ if encoded?
+ super
+ else
+ cast_type.type_cast(value)
+ end
end
def adapter
@@ -329,27 +315,37 @@ module ActiveRecord
end
end
- class DateTime < Type
- def type; :datetime; end
-
- def type_cast(value)
- return if value.nil?
-
- # FIXME: probably we can improve this since we know it is mysql
- # specific
- ConnectionAdapters::Column.string_to_time value
+ class DateTime < ConnectionAdapters::Type::DateTime
+ def cast_value(value)
+ if Mysql::Time === value
+ new_time(
+ value.year,
+ value.month,
+ value.day,
+ value.hour,
+ value.minute,
+ value.second,
+ value.second_part)
+ else
+ super
+ end
end
end
- class Time < Type
- def type; :time; end
-
- def type_cast(value)
- return if value.nil?
-
- # FIXME: probably we can improve this since we know it is mysql
- # specific
- ConnectionAdapters::Column.string_to_dummy_time value
+ class Time < ConnectionAdapters::Type::Time
+ def cast_value(value)
+ if Mysql::Time === value
+ new_time(
+ 2000,
+ 01,
+ 01,
+ value.hour,
+ value.minute,
+ value.second,
+ value.second_part)
+ else
+ super
+ end
end
end
@@ -418,6 +414,12 @@ module ActiveRecord
end
end
+ def initialize_type_map(m) # :nodoc:
+ super
+ m.register_type %r(datetime)i, Fields::DateTime.new
+ m.register_type %r(time)i, Fields::Time.new
+ end
+
def exec_without_stmt(sql, name = 'SQL') # :nodoc:
# Some queries, like SHOW CREATE TABLE don't work through the prepared
# statement API. For those queries, we need to use this method. :'(