Permalink
Browse files

Mysql::Result#all_hashes compatibility with Mysql C driver 2.6.x. Clo…

…ses #6601.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5533 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 65ca37b commit be3a4c3daae637bf7285025e3bb10fa118e94728 @jeremy jeremy committed Nov 16, 2006
Showing with 23 additions and 21 deletions.
  1. +1 −1 activerecord/CHANGELOG
  2. +22 −20 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -37,7 +37,7 @@
* Document other options available to migration's add_column. #6419 [grg]
-* MySQL: all_hashes compatibility with old MysqlRes class. #6429 [Jeremy Kemper]
+* MySQL: all_hashes compatibility with old MysqlRes class. #6429, #6601 [Jeremy Kemper]
* Fix has_many :through to add the appropriate conditions when going through an association using STI. Closes #5783. [Jonathan Viney]
@@ -5,30 +5,24 @@ module MysqlCompat
def self.define_all_hashes_method!
raise 'Mysql not loaded' unless defined?(::Mysql)
- # for compatibility
- Object.const_set(:MysqlRes, Mysql::Result) unless defined?(::MysqlRes)
- Object.const_set(:MysqlField, Mysql::Field) unless defined?(::MysqlField)
- Object.const_set(:MysqlError, Mysql::Error) unless defined?(::MysqlError)
-
- return if ::MysqlRes.instance_methods.include?('all_hashes')
+ target = defined?(Mysql::Result) ? Mysql::Result : MysqlRes
+ return if target.instance_methods.include?('all_hashes')
# Ruby driver has a version string and returns null values in each_hash
# C driver >= 2.7 returns null values in each_hash
- if Mysql.const_defined?(:VERSION)
- if Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700
- ::MysqlRes.class_eval <<-'end_eval'
- def all_hashes
- rows = []
- each_hash { |row| rows << row }
- rows
- end
- end_eval
+ if Mysql.const_defined?(:VERSION) && (Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700)
+ target.class_eval <<-'end_eval'
+ def all_hashes
+ rows = []
+ each_hash { |row| rows << row }
+ rows
end
+ end_eval
# adapters before 2.7 don't have a version constant
# and don't return null values in each_hash
else
- ::MysqlRes.class_eval <<-'end_eval'
+ target.class_eval <<-'end_eval'
def all_hashes
rows = []
all_fields = fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields }
@@ -37,19 +31,22 @@ def all_hashes
end
end_eval
end
+
+ unless target.instance_methods.include?('all_hashes')
+ raise "Failed to defined #{target.name}#all_hashes method. Mysql::VERSION = #{Mysql::VERSION.inspect}"
+ end
end
end
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
+ def self.require_mysql
+ # Include the MySQL driver if one hasn't already been loaded
unless defined? Mysql
begin
require_library_or_gem 'mysql'
rescue LoadError => cannot_require_mysql
- # Only use the supplied backup Ruby/MySQL driver if no driver is already in place
+ # Use the bundled Ruby/MySQL driver if no driver is already in place
begin
require 'active_record/vendor/mysql'
rescue LoadError
@@ -60,7 +57,10 @@ def self.mysql_connection(config) # :nodoc:
# Define Mysql::Result.all_hashes
MysqlCompat.define_all_hashes_method!
+ end
+ # Establishes a connection to the database that's used by all Active Record objects.
+ def self.mysql_connection(config) # :nodoc:
config = config.symbolize_keys
host = config[:host]
port = config[:port]
@@ -74,8 +74,10 @@ def self.mysql_connection(config) # :nodoc:
raise ArgumentError, "No database specified. Missing argument: database."
end
+ require_mysql
mysql = Mysql.init
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey]
+
ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket], config)
end
end

0 comments on commit be3a4c3

Please sign in to comment.