Class: DbAgent::TableOrderer::TSortComputation

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/db_agent/table_orderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, except = []) ⇒ TSortComputation

Returns a new instance of TSortComputation.



42
43
44
45
# File 'lib/db_agent/table_orderer.rb', line 42

def initialize(db, except = [])
  @db = db
  @except = except
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



46
47
48
# File 'lib/db_agent/table_orderer.rb', line 46

def db
  @db
end

#exceptObject (readonly)

Returns the value of attribute except.



46
47
48
# File 'lib/db_agent/table_orderer.rb', line 46

def except
  @except
end

Instance Method Details

#graphObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/db_agent/table_orderer.rb', line 48

def graph
  g = Hash.new{|h,k| h[k] = [] }
  tsort_each_node.each do |table|
    tsort_each_child(table) do |child|
      g[child] << table
    end
  end
  g
rescue TSort::Cyclic
  raise unless killed = to_kill
  TSortComputation.new(db, except + [killed]).graph
end

#to_aObject



61
62
63
64
65
66
# File 'lib/db_agent/table_orderer.rb', line 61

def to_a
  tsort.to_a
rescue TSort::Cyclic
  raise unless killed = to_kill
  TSortComputation.new(db, except + [killed]).to_a
end

#tsort_each_child(table, &bl) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/db_agent/table_orderer.rb', line 72

def tsort_each_child(table, &bl)
  db.foreign_key_list(table)
    .map{|fk| fk[:table] }
    .reject{|t|
      except.any?{|killed| killed.first == table && killed.last == t }
    }
    .each(&bl)
end

#tsort_each_node(&bl) ⇒ Object



68
69
70
# File 'lib/db_agent/table_orderer.rb', line 68

def tsort_each_node(&bl)
  db.tables.each(&bl)
end