static public Method

MysqlCompat.define_all_hashes_method!

add all_hashes method to standard mysql-c bindings or pure ruby version

Source Code

# File active_record/connection_adapters/mysql_adapter.rb, line 6
def self.define_all_hashes_method!
  raise 'Mysql not loaded' unless defined?(::Mysql)

  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) && (Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700)
    target.class_eval "def all_hashes\nrows = []\neach_hash { |row| rows << row }\nrows\nend\n"

  # adapters before 2.7 don't have a version constant
  # and don't return null values in each_hash
  else
    target.class_eval "def all_hashes\nrows = []\nall_fields = fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields }\neach_hash { |row| rows << all_fields.dup.update(row) }\nrows\nend\n"
  end

  unless target.instance_methods.include?('all_hashes') ||
         target.instance_methods.include?(:all_hashes)
    raise "Failed to defined #{target.name}#all_hashes method. Mysql::VERSION = #{Mysql::VERSION.inspect}"
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.