Class: ActiveRecord::ConnectionAdapters::MysqlAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/connection_adapters/mysql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#foreign_key_constraints(table, name = nil) ⇒ Object



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
# File 'lib/connection_adapters/mysql_adapter.rb', line 10

def foreign_key_constraints(table, name = nil)
  constraints = [] 
  execute("SHOW CREATE TABLE #{table}", name).each do |row|
    row[1].each do |create_line|
      if create_line.strip =~ /CONSTRAINT `([^`]+)` FOREIGN KEY \(`([^`]+)`\) REFERENCES `([^`]+)` \(`([^`]+)`\)([^,]*)/          
        constraint = ForeignKeyConstraint.new(Regexp.last_match(1), table, Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(4), nil, nil)
      
        constraint_params = {}
        
        unless Regexp.last_match(5).nil?
          Regexp.last_match(5).strip.split('ON ').each do |param|
            constraint_params[Regexp.last_match(1).upcase] = Regexp.last_match(2).strip.upcase if param.strip =~ /([^ ]+) (.+)/
          end
        end
      
        constraint.on_update = symbolize_foreign_key_constraint_action(constraint_params['UPDATE']) if constraint_params.include? 'UPDATE'
        constraint.on_delete = symbolize_foreign_key_constraint_action(constraint_params['DELETE']) if constraint_params.include? 'DELETE'

        constraints << constraint
      end
    end
  end
    
  constraints
end

#remove_foreign_key_constraint(table_name, constraint_name) ⇒ Object



36
37
38
# File 'lib/connection_adapters/mysql_adapter.rb', line 36

def remove_foreign_key_constraint(table_name, constraint_name)
  execute "ALTER TABLE #{table_name} DROP FOREIGN KEY #{constraint_name}"
end

#supports_fetch_foreign_keys?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/connection_adapters/mysql_adapter.rb', line 6

def supports_fetch_foreign_keys?
  true
end