aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2005-10-11 03:10:07 +0000
committerMarcel Molina <marcel@vernix.org>2005-10-11 03:10:07 +0000
commit31ae8121e47e69f667df1ef86bff8f61e9f860cc (patch)
tree2a6dc535b47869a990ca89022192fa3ad0e0d46d /activerecord/lib/active_record
parent0efeeba7802b3e5137cd62cbb6fcf303a8a238a2 (diff)
downloadrails-31ae8121e47e69f667df1ef86bff8f61e9f860cc.tar.gz
rails-31ae8121e47e69f667df1ef86bff8f61e9f860cc.tar.bz2
rails-31ae8121e47e69f667df1ef86bff8f61e9f860cc.zip
Optimization for Mysql selects using mysql-ruby extension greater than 2.6.3. Closes #2426.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2529 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/mysql_adapter.rb18
1 files changed, 15 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 886363bcbe..4d034adec5 100755
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -4,20 +4,25 @@ module ActiveRecord
class Base
# Establishes a connection to the database that's used by all Active Record objects.
def self.mysql_connection(config) # :nodoc:
+ # Only include the MySQL driver if one hasn't already been loaded
unless self.class.const_defined?(:Mysql)
begin
- # Only include the MySQL driver if one hasn't already been loaded
require_library_or_gem 'mysql'
+ # The C version of mysql returns null fields in each_hash if Mysql::VERSION is defined
+ ConnectionAdapters::MysqlAdapter.null_values_in_each_hash = Mysql.const_defined?(:VERSION)
rescue LoadError => cannot_require_mysql
# Only use the supplied backup Ruby/MySQL driver if no driver is already in place
begin
require 'active_record/vendor/mysql'
require 'active_record/vendor/mysql411'
+ # The ruby version of mysql returns null fields in each_hash
+ ConnectionAdapters::MysqlAdapter.null_values_in_each_hash = true
rescue LoadError
raise cannot_require_mysql
end
end
end
+
config = config.symbolize_keys
host = config[:host]
@@ -73,6 +78,9 @@ module ActiveRecord
@@emulate_booleans = true
cattr_accessor :emulate_booleans
+ cattr_accessor :null_values_in_each_hash
+ @@null_values_in_each_hash = false
+
LOST_CONNECTION_ERROR_MESSAGES = [
"Server shutdown in progress",
"Broken pipe",
@@ -288,8 +296,12 @@ module ActiveRecord
@connection.query_with_result = true
result = execute(sql, name)
rows = []
- all_fields_initialized = result.fetch_fields.inject({}) { |all_fields, f| all_fields[f.name] = nil; all_fields }
- result.each_hash { |row| rows << all_fields_initialized.dup.update(row) }
+ if @@null_values_in_each_hash
+ result.each_hash { |row| rows << row }
+ else
+ all_fields = result.fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields }
+ result.each_hash { |row| rows << all_fields.dup.update(row) }
+ end
result.free
rows
end