Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5533

Show
Ignore:
Timestamp:
11/16/06 01:41:22 (2 years ago)
Author:
bitsweat
Message:

Mysql::Result#all_hashes compatibility with Mysql C driver 2.6.x. Closes #6601.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r5520 r5533  
    3838* Document other options available to migration's add_column. #6419 [grg] 
    3939 
    40 * MySQL: all_hashes compatibility with old MysqlRes class.  #6429 [Jeremy Kemper] 
     40* MySQL: all_hashes compatibility with old MysqlRes class.  #6429, #6601 [Jeremy Kemper] 
    4141 
    4242* Fix has_many :through to add the appropriate conditions when going through an association using STI. Closes #5783. [Jonathan Viney] 
  • trunk/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    r5384 r5533  
    66    raise 'Mysql not loaded' unless defined?(::Mysql) 
    77 
    8     # for compatibility 
    9     Object.const_set(:MysqlRes,   Mysql::Result) unless defined?(::MysqlRes) 
    10     Object.const_set(:MysqlField, Mysql::Field)  unless defined?(::MysqlField) 
    11     Object.const_set(:MysqlError, Mysql::Error)  unless defined?(::MysqlError) 
    12  
    13     return if ::MysqlRes.instance_methods.include?('all_hashes') 
     8    target = defined?(Mysql::Result) ? Mysql::Result : MysqlRes 
     9    return if target.instance_methods.include?('all_hashes') 
    1410 
    1511    # Ruby driver has a version string and returns null values in each_hash 
    1612    # C driver >= 2.7 returns null values in each_hash 
    17     if Mysql.const_defined?(:VERSION) 
    18       if Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700 
    19         ::MysqlRes.class_eval <<-'end_eval' 
    20         def all_hashes 
    21           rows = [] 
    22           each_hash { |row| rows << row } 
    23           rows 
    24         end 
    25         end_eval 
    26       end 
     13    if Mysql.const_defined?(:VERSION) && (Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700) 
     14      target.class_eval <<-'end_eval' 
     15      def all_hashes 
     16        rows = [] 
     17        each_hash { |row| rows << row } 
     18        rows 
     19      end 
     20      end_eval 
    2721 
    2822    # adapters before 2.7 don't have a version constant 
    2923    # and don't return null values in each_hash 
    3024    else 
    31       ::MysqlRes.class_eval <<-'end_eval' 
     25      target.class_eval <<-'end_eval' 
    3226      def all_hashes 
    3327        rows = [] 
     
    3832      end_eval 
    3933    end 
     34 
     35    unless target.instance_methods.include?('all_hashes') 
     36      raise "Failed to defined #{target.name}#all_hashes method. Mysql::VERSION = #{Mysql::VERSION.inspect}" 
     37    end 
    4038  end 
    4139end 
     
    4341module ActiveRecord 
    4442  class Base 
    45     # Establishes a connection to the database that's used by all Active Record objects. 
    46     def self.mysql_connection(config) # :nodoc: 
    47       # Only include the MySQL driver if one hasn't already been loaded 
     43    def self.require_mysql 
     44      # Include the MySQL driver if one hasn't already been loaded 
    4845      unless defined? Mysql 
    4946        begin 
    5047          require_library_or_gem 'mysql' 
    5148        rescue LoadError => cannot_require_mysql 
    52           # Only use the supplied backup Ruby/MySQL driver if no driver is already in place 
     49          # Use the bundled Ruby/MySQL driver if no driver is already in place 
    5350          begin 
    5451            require 'active_record/vendor/mysql' 
     
    6158      # Define Mysql::Result.all_hashes 
    6259      MysqlCompat.define_all_hashes_method! 
    63  
     60    end 
     61 
     62    # Establishes a connection to the database that's used by all Active Record objects. 
     63    def self.mysql_connection(config) # :nodoc: 
    6464      config = config.symbolize_keys 
    6565      host     = config[:host] 
     
    7575      end 
    7676 
     77      require_mysql 
    7778      mysql = Mysql.init 
    7879      mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey] 
     80 
    7981      ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket], config) 
    8082    end