Sha256: 2ea38f40e2f134bb0f99af9b9275da3a249d984b35ea6577e9041e34f8024c94

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'table_saw/connection'

module TableSaw
  module Queries
    class ForeignKeyRelationships
      QUERY = <<~SQL
        select
          tc.table_name as from_table,
          kcu.column_name as from_column,
          ccu.table_name as to_table,
          ccu.column_name as to_column
        from information_schema.table_constraints tc
          join information_schema.key_column_usage kcu on tc.constraint_name = kcu.constraint_name
          join information_schema.constraint_column_usage ccu on tc.constraint_name = ccu.constraint_name
        where tc.constraint_type = 'FOREIGN KEY'
      SQL

      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

      # rubocop:disable Naming/PredicateName
      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
      # rubocop:enable Naming/PredicateName

      private

      def result
        @result ||= TableSaw::Connection.with do |conn|
          conn.exec(QUERY)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
table_saw-0.3.0 lib/table_saw/queries/foreign_key_relationships.rb
table_saw-0.2.1 lib/table_saw/queries/foreign_key_relationships.rb
table_saw-0.2.0 lib/table_saw/queries/foreign_key_relationships.rb