Class: TableSaw::Queries::ForeignKeyRelationships

Inherits:
Object
  • Object
show all
Defined in:
lib/table_saw/queries/foreign_key_relationships.rb

Constant Summary collapse

QUERY =
"select\n  tc.table_name as from_table,\n  kcu.column_name as from_column,\n  ccu.table_name as to_table,\n  ccu.column_name as to_column\nfrom information_schema.table_constraints tc\n  join information_schema.key_column_usage kcu on tc.constraint_name = kcu.constraint_name\n  join information_schema.constraint_column_usage ccu on tc.constraint_name = ccu.constraint_name\nwhere tc.constraint_type = 'FOREIGN KEY'\n"

Instance Method Summary collapse

Instance Method Details

#belongs_toObject



20
21
22
23
24
# File 'lib/table_saw/queries/foreign_key_relationships.rb', line 20

def belongs_to
  @belongs_to ||= result.each_with_object(Hash.new { |h, k| h[k] = {} }) do |row, memo|
    memo[row['from_table']][row['from_column']] = row['to_table']
  end
end

#has_manyObject

rubocop:disable Naming/PredicateName



27
28
29
30
31
# File 'lib/table_saw/queries/foreign_key_relationships.rb', line 27

def has_many
  @has_many ||= result.each_with_object(Hash.new { |h, k| h[k] = [] }) do |row, memo|
    memo[row['to_table']].push([row['from_table'], row['from_column']])
  end
end