Changeset 5219
- Timestamp:
- 10/02/06 20:43:13 (2 years ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r5215 r5219 1 1 *SVN* 2 3 * MySQL: introduce Mysql::Result#all_hashes to support further optimization. #5581 [Stefan Kaes] 2 4 3 5 * save! shouldn't validate twice. #6324 [maiha, Bob Silva] trunk/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
r4596 r5219 1 1 require 'active_record/connection_adapters/abstract_adapter' 2 3 module 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 35 end 2 36 3 37 module ActiveRecord … … 18 52 end 19 53 end 54 55 # Define Mysql::Result.all_hashes 56 MysqlCompat.define_all_hashes_method! 20 57 21 58 config = config.symbolize_keys … … 84 121 super(connection, logger) 85 122 @connection_options, @config = connection_options, config 86 @null_values_in_each_hash = Mysql.const_defined?(:VERSION) 123 87 124 connect 88 125 end … … 340 377 @connection.query_with_result = true 341 378 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 349 380 result.free 350 381 rows 351 382 end 352 383 353 384 def supports_views? 354 385 version[0] >= 5 355 386 end 356 387 357 388 def version 358 389 @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }