Module: MysqlCompat

Defined in:
lib/active_record/connection_adapters/mysql_adapter.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.define_all_hashes_method!Object

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/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') ||
            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                     # def all_hashes\n      rows = []                        #   rows = []\n      each_hash { |row| rows << row }  #   each_hash { |row| rows << row }\n      rows                             #   rows\n    end                                # end\n    end_eval\n\n  # adapters before 2.7 don't have a version constant\n  # and don't return null values in each_hash\n  else\n    target.class_eval <<-'end_eval'\n    def all_hashes                                            # def all_hashes\n      rows = []                                               #   rows = []\n      all_fields = fetch_fields.inject({}) { |fields, f|      #   all_fields = fetch_fields.inject({}) { |fields, f|\n        fields[f.name] = nil; fields                          #     fields[f.name] = nil; fields\n      }                                                       #   }\n      each_hash { |row| rows << all_fields.dup.update(row) }  #   each_hash { |row| rows << all_fields.dup.update(row) }\n      rows                                                    #   rows\n    end                                                       # end\n    end_eval\n  end\n\n  unless target.instance_methods.include?('all_hashes') ||\n         target.instance_methods.include?(:all_hashes)\n    raise \"Failed to defined \#{target.name}#all_hashes method. Mysql::VERSION = \#{Mysql::VERSION.inspect}\"\n  end\nend\n"