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

Changeset 5219

Show
Ignore:
Timestamp:
10/02/06 20:43:13 (2 years ago)
Author:
bitsweat
Message:

MySQL: introduce Mysql::Result#all_hashes to support further optimization. Closes #5581.

Files:

Legend:

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

    r5215 r5219  
    11*SVN* 
     2 
     3* MySQL: introduce Mysql::Result#all_hashes to support further optimization.  #5581 [Stefan Kaes] 
    24 
    35* save! shouldn't validate twice.  #6324 [maiha, Bob Silva] 
  • trunk/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    r4596 r5219  
    11require 'active_record/connection_adapters/abstract_adapter' 
     2 
     3module MysqlCompat 
     4  # add all_hashes method to standard mysql-c bindings or pure ruby version 
     5  def self.define_all_hashes_method! 
     6    raise 'Mysql not loaded' unless defined?(::Mysql::Result) 
     7    return if ::Mysql::Result.instance_methods.include?('all_hashes') 
     8 
     9    # Ruby driver has a version string and returns null values in each_hash 
     10    # C driver >= 2.7 returns null values in each_hash 
     11    if Mysql.const_defined?(:VERSION) 
     12      if Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700 
     13        ::Mysql::Result.class_eval <<-'end_eval' 
     14        def all_hashes 
     15          rows = [] 
     16          each_hash { |row| rows << row } 
     17          rows 
     18        end 
     19        end_eval 
     20      end 
     21 
     22    # adapters before 2.7 don't have a version constant 
     23    # and don't return null values in each_hash 
     24    else 
     25      ::Mysql::Result.class_eval <<-'end_eval' 
     26      def all_hashes 
     27        rows = [] 
     28        all_fields = fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields } 
     29        each_hash { |row| rows << all_fields.dup.update(row) } 
     30        rows 
     31      end 
     32      end_eval 
     33    end 
     34  end 
     35end 
    236 
    337module ActiveRecord 
     
    1852        end 
    1953      end 
     54 
     55      # Define Mysql::Result.all_hashes 
     56      MysqlCompat.define_all_hashes_method! 
    2057 
    2158      config = config.symbolize_keys 
     
    84121        super(connection, logger) 
    85122        @connection_options, @config = connection_options, config 
    86         @null_values_in_each_hash = Mysql.const_defined?(:VERSION) 
     123 
    87124        connect 
    88125      end 
     
    340377          @connection.query_with_result = true 
    341378          result = execute(sql, name) 
    342           rows = [] 
    343           if @null_values_in_each_hash 
    344             result.each_hash { |row| rows << row } 
    345           else 
    346             all_fields = result.fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields } 
    347             result.each_hash { |row| rows << all_fields.dup.update(row) } 
    348           end 
     379          rows = result.all_hashes 
    349380          result.free 
    350381          rows 
    351382        end 
    352          
     383 
    353384        def supports_views? 
    354385          version[0] >= 5 
    355386        end 
    356          
     387 
    357388        def version 
    358389          @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }