From 28ec8c4a57197a43e4369bfbdfa92625bd592fe0 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Thu, 14 Apr 2016 09:49:39 +0900 Subject: Add `quoted_time` for truncating the date part of a time column value Context #24522. TIME column on MariaDB doesn't ignore the date part of the string when it coerces to time. ``` root@localhost [test] > CREATE TABLE `foos` (`id` int AUTO_INCREMENT PRIMARY KEY, `start` time(0), `finish` time(4)) ENGINE=InnoDB; Query OK, 0 rows affected (0.02 sec) root@localhost [test] > INSERT INTO `foos` (`start`, `finish`) VALUES ('2000-01-01 12:30:00', '2000-01-01 12:30:00.999900'); Query OK, 1 row affected, 2 warnings (0.00 sec) Note (Code 1265): Data truncated for column 'start' at row 1 Note (Code 1265): Data truncated for column 'finish' at row 1 root@localhost [test] > SELECT `foos`.* FROM `foos`; +----+----------+---------------+ | id | start | finish | +----+----------+---------------+ | 1 | 12:30:00 | 12:30:00.9999 | +----+----------+---------------+ 1 row in set (0.00 sec) root@localhost [test] > SELECT `foos`.* FROM `foos` WHERE `foos`.`start` = '2000-01-01 12:30:00' LIMIT 1; Empty set (0.00 sec) root@localhost [test] > SELECT `foos`.* FROM `foos` WHERE `foos`.`start` = '12:30:00' LIMIT 1; +----+----------+---------------+ | id | start | finish | +----+----------+---------------+ | 1 | 12:30:00 | 12:30:00.9999 | +----+----------+---------------+ 1 row in set (0.00 sec) ``` --- .../lib/active_record/connection_adapters/abstract/quoting.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/lib/active_record/connection_adapters') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 2eeefb13d7..860ef17dca 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -146,6 +146,10 @@ module ActiveRecord end end + def quoted_time(value) # :nodoc: + quoted_date(value).sub(/\A2000-01-01 /, '') + end + def prepare_binds_for_database(binds) # :nodoc: binds.map(&:value_for_database) end @@ -166,6 +170,7 @@ module ActiveRecord # BigDecimals need to be put in a non-normalized form and quoted. when BigDecimal then value.to_s('F') when Numeric, ActiveSupport::Duration then value.to_s + when Type::Time::Value then "'#{quoted_time(value)}'" when Date, Time then "'#{quoted_date(value)}'" when Symbol then "'#{quote_string(value.to_s)}'" when Class then "'#{value}'" @@ -181,6 +186,7 @@ module ActiveRecord when false then unquoted_false # BigDecimals need to be put in a non-normalized form and quoted. when BigDecimal then value.to_s('F') + when Type::Time::Value then quoted_time(value) when Date, Time then quoted_date(value) when *types_which_need_no_typecasting value -- cgit v1.2.3