<commit_before>require 'active_record/connection_adapters/abstract_adapter'
require 'active_support/core_ext/kernel/requires'
require 'set'

module MysqlCompat #:nodoc:
  # add all_hashes method to standard mysql-c bindings or pure ruby version
  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') ||
              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 <<-'end_eval'
      def all_hashes                     # def all_hashes
        rows = []                        #   rows = []
        each_hash { |row| rows << row }  #   each_hash { |row| rows << row }
        rows                             #   rows
      end                                # end
      end_eval

    # adapters before 2.7 don't have a version constant
    # and don't return null values in each_hash
    else
      target.class_eval <<-'end_eval'
      def all_hashes                                            # def all_hashes
        rows = []                                               #   rows = []
        all_fields = fetch_fields.inject({}) { |fields, f|      #   all_fields = fetch_fields.inject({}) { |fields, f|
          fields[f.name] = nil; fields                          #     fields[f.name] = nil; fields
        }                                                       #   }
        each_hash { |row| rows << all_fields.dup.update(row) }  #   each_hash { |row| rows << all_fields.dup.update(row) }
        rows                                                    #   rows
      end                                                       # end
      end_eval
    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
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:
      config = config.symbolize_keys
      host     = config[:host]
      port     = config[:port]
      socket   = config[:socket]
      username = config[:username] ? config[:username].to_s : 'root'
      password = config[:password].to_s
      database = config[:database]

      # Require the MySQL driver and define Mysql::Result.all_hashes
      unless defined? Mysql
        begin
          require_library_or_gem('mysql')
        rescue LoadError
          $stderr.puts '!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.'
          raise
        end
      end<commit_msg>Better MySQL Error message. [#3775 state:resolved]<commit_after>require 'active_record/connection_adapters/abstract_adapter'
require 'active_support/core_ext/kernel/requires'
require 'set'

module MysqlCompat #:nodoc:
  # add all_hashes method to standard mysql-c bindings or pure ruby version
  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') ||
              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 <<-'end_eval'
      def all_hashes                     # def all_hashes
        rows = []                        #   rows = []
        each_hash { |row| rows << row }  #   each_hash { |row| rows << row }
        rows                             #   rows
      end                                # end
      end_eval

    # adapters before 2.7 don't have a version constant
    # and don't return null values in each_hash
    else
      target.class_eval <<-'end_eval'
      def all_hashes                                            # def all_hashes
        rows = []                                               #   rows = []
        all_fields = fetch_fields.inject({}) { |fields, f|      #   all_fields = fetch_fields.inject({}) { |fields, f|
          fields[f.name] = nil; fields                          #     fields[f.name] = nil; fields
        }                                                       #   }
        each_hash { |row| rows << all_fields.dup.update(row) }  #   each_hash { |row| rows << all_fields.dup.update(row) }
        rows                                                    #   rows
      end                                                       # end
      end_eval
    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
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:
      config = config.symbolize_keys
      host     = config[:host]
      port     = config[:port]
      socket   = config[:socket]
      username = config[:username] ? config[:username].to_s : 'root'
      password = config[:password].to_s
      database = config[:database]

      # Require the MySQL driver and define Mysql::Result.all_hashes
      unless defined? Mysql
        begin
          require_library_or_gem('mysql')
        rescue LoadError
          $stderr.puts '!!! Please install the mysql gem and try again: gem install mysql.'
          raise
        end
      end
{'fraction_non_alphanumeric': 0.09290873276428102, 'fraction_numerical': 0.003939592908732764, 'mean_word_length': 1.8130193905817173, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4824079', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1611, 'n_words': 440}