Parent

Included Modules

Class/Module Index [+]

Quicksearch

RR::TableSorter

This class sorts a given list of tables so that tables referencing other tables via foreign keys are placed behind those referenced tables.

Rationale: If tables are sorted in that sequence, the risk of foreign key violations is smaller.

Attributes

session[RW]

The active Session

tables[RW]

The list of table names to be ordered

Public Class Methods

new(session, tables) click to toggle source

Initializes the TableSorter

  • session: The active Session instance

  • tables: an array of table names

# File lib/rubyrep/table_sorter.rb, line 65
def initialize(session, tables)
  self.session = session
  self.tables = tables
end

Public Instance Methods

referenced_tables() click to toggle source

The table dependencies. Format as described e. g. here: PostgreSQLExtender#referenced_tables

# File lib/rubyrep/table_sorter.rb, line 24
def referenced_tables
  unless @referenced_tables
    @referenced_tables = session.left.referenced_tables(tables)

    # Strip away all unrelated tables
    @referenced_tables.each_pair do |table, references|
      references.delete_if do |reference|
        not tables.include? reference
      end
    end
  end
  @referenced_tables
end
sort() click to toggle source
# File lib/rubyrep/table_sorter.rb, line 53
def sort
  # Note:
  # We should not use TSort#tsort as this one throws an exception if
  # there are cyclic redundancies.
  # (Our goal is to just get the best ordering that is possible and then
  # take our chances.)
  strongly_connected_components.flatten
end
tsort_each_child(table) click to toggle source

Yields all tables that are references by table.

# File lib/rubyrep/table_sorter.rb, line 47
def tsort_each_child(table)
  referenced_tables[table].each do |reference|
    yield reference
  end
end
tsort_each_node() click to toggle source

Yields each table. For details see standard library: TSort#sort_each_node.

# File lib/rubyrep/table_sorter.rb, line 40
def tsort_each_node
  referenced_tables.each_key do |table|
    yield table
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.