aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2015-12-30 00:15:39 -0200
committerRafael França <rafaelmfranca@gmail.com>2015-12-30 00:15:39 -0200
commit6381d080789dd2ea613bc8e57d2b6fee7210f1c3 (patch)
tree37443948f4e5141a17dee277fb961c076e1e2020 /activerecord/lib
parentd3ec85402a50823ddacc2699699bc4b5f17e602e (diff)
parent7f43485508a277c43c046c5a701f6bdb6aed1ede (diff)
downloadrails-6381d080789dd2ea613bc8e57d2b6fee7210f1c3.tar.gz
rails-6381d080789dd2ea613bc8e57d2b6fee7210f1c3.tar.bz2
rails-6381d080789dd2ea613bc8e57d2b6fee7210f1c3.zip
Merge pull request #22803 from kamipo/improve_select_one_in_mysql2_adapter
Improve `select_one` in `Mysql2Adapter`
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb36
1 files changed, 9 insertions, 27 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
index f4686b680c..96a3a44b30 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
@@ -99,33 +99,15 @@ module ActiveRecord
# DATABASE STATEMENTS ======================================
#++
- # FIXME: re-enable the following once a "better" query_cache solution is in core
- #
- # The overrides below perform much better than the originals in AbstractAdapter
- # because we're able to take advantage of mysql2's lazy-loading capabilities
- #
- # # Returns a record hash with the column names as keys and column values
- # # as values.
- # def select_one(sql, name = nil)
- # result = execute(sql, name)
- # result.each(as: :hash) do |r|
- # return r
- # end
- # end
- #
- # # Returns a single value from a record
- # def select_value(sql, name = nil)
- # result = execute(sql, name)
- # if first = result.first
- # first.first
- # end
- # end
- #
- # # Returns an array of the values of the first column in a select:
- # # select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]
- # def select_values(sql, name = nil)
- # execute(sql, name).map { |row| row.first }
- # 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)
+ execute(to_sql(arel, binds), name).each(as: :hash) do |row|
+ @connection.next_result while @connection.more_results?
+ return row
+ end
+ end
# Returns an array of arrays containing the field values.
# Order is the same as that returned by +columns+.