module JunglePath::Gen::SchemaTree

Public Class Methods

gen_node_tree(tables_hash, node_filter=nil) click to toggle source
# File lib/jungle_path/gen/schema_tree.rb, line 30
def self.gen_node_tree(tables_hash, node_filter=nil)
  # test node filter:
  ###node_filter = Gen.test_node_filter
  # create tree structure based on pk/fk releationships of tables.
  tables = tables_hash.values.sort {|a, b| a.table_name <=> b.table_name}
  root = JunglePath::Gen::SchemaTree::Node.new "root"
  root.tables_hash = {}

  tables.each do |v|
    #puts v.table_name.class
    #puts v.table_name
    next unless node_filter == nil || node_filter.allow?(v.table_name)

    # build tables hash:
    root.tables_hash[v.table_name] = v

    #puts v.table_name
    #puts "v.table_name: #{v.table_name}."
    table_node = JunglePath::Gen::SchemaTree::Node.new(v.table_name)
    root.nodes[v.table_name] = table_node

    # make sub nodes for each column
    columns = v.columns_sequence_order
    columns.each do |column|
      next unless node_filter == nil || node_filter.allow?(v.table_name, column.name)
      if column.foreign_key?
        name = column.name.to_s[0..-4].to_sym # cut off "..._id" from column name and use as node name.
        symbol = "->"
        child_table_name = column.foreign_key_table_name
        #puts "tables_hash: #{tables_hash}"
        #puts "column.foreign_key_table_name: #{column.foreign_key_table_name}"
        child_table_join_column_name = tables_hash[column.foreign_key_table_name].primary_key_columns.first[0]
        parent_table_join_column_name = column.name
      else
        name = column.name
        symbol = nil
        child_table_name = nil
        child_table_join_column_name = nil
        parent_table_join_column_name = nil
      end
      #puts "  #{name}, symbol: #{symbol}."
      table_node.nodes[name] = JunglePath::Gen::SchemaTree::Node.new(name, symbol, child_table_name, child_table_join_column_name, parent_table_join_column_name)
    end

    # make sub nodes for each table having a fk reference to this (the parent) table -- exclude self.
    tables.each do |child_table|
      next unless node_filter == nil || node_filter.allow?(child_table.table_name)
      if v.table_name != child_table.table_name #nor own table
        fk_columns = child_table.foreign_key_columns_by_table_name[v.table_name]
        if fk_columns
          #puts "  #{child_table.table_name}. count: #{fk_columns.count}."
          name = child_table.table_name
          table_node.nodes[name] = []
          fk_columns.each do |key, fk_column|
            #puts "    fk_column: #{fk_column.name}."
            #puts "      name: #{name}, #{name.class}."
            #puts "      class: #{table_node.nodes[name].class}."
            #puts "      count: #{table_node.nodes[name].count}."
            if fk_column.primary_key? and child_table.primary_key_columns.count == 1
              symbol = "<-"
            else
              symbol = "<="
            end
            child_table_name = child_table.table_name
            child_table_join_column_name = fk_column.name
            parent_table_join_column_name = v.primary_key_columns.first[0]
            table_node.nodes[name] << JunglePath::Gen::SchemaTree::Node.new(name, symbol, child_table_name, child_table_join_column_name, parent_table_join_column_name)
            #puts "    nodes[name].count: #{table_node.nodes[name].count}."
          end
        end
      end
    end
  end
  root
end
test_node_filter() click to toggle source

configatron.schema.filters = {

allow: [
  {table: "zztop"},
  {table: /./, columns: nil},
  {table: :contact, columns: []},
  {table: :user, columns: /./},
  {table: :opportunity, columns: :name}
]
deny: [
  {table: :xyz, columns: :zzz}
]

}

# File lib/jungle_path/gen/schema_tree.rb, line 22
def self.test_node_filter()
  JunglePath::Gen::SchemaTree::Filter.new(#{
    #allow: [{table: /./}],
    #deny: [{table: /^siebel_/}]
  #}
  )
end