aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2016-07-02 00:06:04 -0300
committerGitHub <noreply@github.com>2016-07-02 00:06:04 -0300
commite12e7a8f87d6a0f7c0132ebea663cba94da57be0 (patch)
treef0661cad539bbba5e69e9df69d908b2b1369d7bb
parent193d907981fded25bc228fb4d6204dc616f769c2 (diff)
parent763de2ac1c85d3e9f82639783a55244d6c0517c2 (diff)
downloadrails-e12e7a8f87d6a0f7c0132ebea663cba94da57be0.tar.gz
rails-e12e7a8f87d6a0f7c0132ebea663cba94da57be0.tar.bz2
rails-e12e7a8f87d6a0f7c0132ebea663cba94da57be0.zip
Merge pull request #25507 from bquorning/optimize-for-first-result-and-remove-mysql-select_one
Remove #select_one from Mysql2Adapter
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb13
-rw-r--r--activerecord/lib/active_record/result.rb8
-rw-r--r--activerecord/test/cases/result_test.rb10
3 files changed, 17 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb
index 13c9b6cbd9..87f0ff7d85 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb
@@ -13,19 +13,6 @@ module ActiveRecord
result
end
- # Returns a record hash with the column names as keys and column values
- # as values.
- def select_one(arel, name = nil, binds = [])
- arel, binds = binds_from_relation(arel, binds)
- @connection.query_options.merge!(as: :hash)
- select_result(to_sql(arel, binds), name, binds) do |result|
- @connection.next_result while @connection.more_results?
- result.first
- end
- ensure
- @connection.query_options.merge!(as: :array)
- end
-
# Returns an array of arrays containing the field values.
# Order is the same as that returned by +columns+.
def select_rows(sql, name = nil, binds = [])
diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb
index 8e6cd6c82f..b9fd0f5326 100644
--- a/activerecord/lib/active_record/result.rb
+++ b/activerecord/lib/active_record/result.rb
@@ -75,8 +75,14 @@ module ActiveRecord
hash_rows[idx]
end
+ def first
+ return nil if @rows.empty?
+ Hash[@columns.zip(@rows.first)]
+ end
+
def last
- hash_rows.last
+ return nil if @rows.empty?
+ Hash[@columns.zip(@rows.last)]
end
def cast_values(type_overrides = {}) # :nodoc:
diff --git a/activerecord/test/cases/result_test.rb b/activerecord/test/cases/result_test.rb
index dec01dfa76..83dc5347e0 100644
--- a/activerecord/test/cases/result_test.rb
+++ b/activerecord/test/cases/result_test.rb
@@ -22,6 +22,16 @@ module ActiveRecord
], result.to_hash
end
+ test "first returns first row as a hash" do
+ assert_equal(
+ {'col_1' => 'row 1 col 1', 'col_2' => 'row 1 col 2'}, result.first)
+ end
+
+ test "last returns last row as a hash" do
+ assert_equal(
+ {'col_1' => 'row 3 col 1', 'col_2' => 'row 3 col 2'}, result.last)
+ end
+
test "each with block returns row hashes" do
result.each do |row|
assert_equal ['col_1', 'col_2'], row.keys