diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-11-16 01:41:22 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-11-16 01:41:22 +0000 |
commit | be3a4c3daae637bf7285025e3bb10fa118e94728 (patch) | |
tree | f56e7bc7f2446a8bfd20465f6f0185cc8287bdc3 | |
parent | 65ca37b7b7097afa49f6837668d4108262334f4e (diff) | |
download | rails-be3a4c3daae637bf7285025e3bb10fa118e94728.tar.gz rails-be3a4c3daae637bf7285025e3bb10fa118e94728.tar.bz2 rails-be3a4c3daae637bf7285025e3bb10fa118e94728.zip |
Mysql::Result#all_hashes compatibility with Mysql C driver 2.6.x. Closes #6601.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5533 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 42 |
2 files changed, 23 insertions, 21 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 99e2003eea..cacb46ad38 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -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] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index d5e2348856..6c75896233 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -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 @@ module MysqlCompat 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 @@ module ActiveRecord # 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 @@ module ActiveRecord 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 |