aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2017-07-18 13:04:47 -0400
committerSean Griffin <sean@seantheprogrammer.com>2017-07-18 13:06:43 -0400
commit8ebe1f2feed30809abb3f114242dda7379e66e4b (patch)
treee74ea295436ad469fd2140ad73b057b929655ae7 /activerecord
parent58c567adaeba1208522640e7890db9e07cbb768a (diff)
downloadrails-8ebe1f2feed30809abb3f114242dda7379e66e4b.tar.gz
rails-8ebe1f2feed30809abb3f114242dda7379e66e4b.tar.bz2
rails-8ebe1f2feed30809abb3f114242dda7379e66e4b.zip
Don't convert dates to strings when using prepared statements in mysql
Dates are able to be natively handled by the mysql2 gem. libmysql (and the wire protocol) represent each portion of the date as an integer, which is significantly faster to encode and decode. By passing the Ruby date objects through directly, we can save a good bit of time and memory.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql/quoting.rb8
-rw-r--r--activerecord/test/cases/quoting_test.rb14
2 files changed, 19 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb b/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb
index d4f5906b33..0cc0ac74fe 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb
@@ -39,6 +39,14 @@ module ActiveRecord
def quoted_binary(value)
"x'#{value.hex}'"
end
+
+ def _type_cast(value)
+ case value
+ when Type::Time::Value then value.__getobj__
+ when Date, Time then value
+ else super
+ end
+ end
end
end
end
diff --git a/activerecord/test/cases/quoting_test.rb b/activerecord/test/cases/quoting_test.rb
index b21adccc4b..98b20915c6 100644
--- a/activerecord/test/cases/quoting_test.rb
+++ b/activerecord/test/cases/quoting_test.rb
@@ -174,13 +174,21 @@ module ActiveRecord
def test_type_cast_date
date = Date.today
- expected = @conn.quoted_date(date)
+ if current_adapter?(:Mysql2Adapter)
+ expected = date
+ else
+ expected = @conn.quoted_date(date)
+ end
assert_equal expected, @conn.type_cast(date)
end
def test_type_cast_time
time = Time.now
- expected = @conn.quoted_date(time)
+ if current_adapter?(:Mysql2Adapter)
+ expected = time
+ else
+ expected = @conn.quoted_date(time)
+ end
assert_equal expected, @conn.type_cast(time)
end
@@ -257,7 +265,7 @@ module ActiveRecord
def test_type_cast_ar_object
value = DatetimePrimaryKey.new(id: @time)
- assert_equal "2017-02-14 12:34:56.789000", @connection.type_cast(value)
+ assert_equal @connection.type_cast(value.id), @connection.type_cast(value)
end
end
end