class Ezframe::ColumnSets

Public Class Methods

[](colset_name) click to toggle source
# File lib/ezframe/column_set.rb, line 57
def [](colset_name)
  return get(colset_name)
end
add(colset_name, columns) click to toggle source
# File lib/ezframe/column_set.rb, line 32
def add(colset_name, columns)
  @colset_h[colset_name.to_sym] = cs = ColumnSet.new(parent: self, name: colset_name, columns: columns)
  cs.set(columns)
  return cs
end
clone() click to toggle source
# File lib/ezframe/column_set.rb, line 38
def clone
  @colset_h.deep_dup
end
create_one_table(table_name, column_set) click to toggle source
# File lib/ezframe/column_set.rb, line 82
def create_one_table(table_name, column_set)
  col_h = column_set.get_hash(:db_type)
  EzLog.info "create_one_table: col_h=#{col_h.inspect}"
  DB.create_table(table_name, col_h)
end
create_tables() click to toggle source
# File lib/ezframe/column_set.rb, line 72
def create_tables
  self.each do |table_name, column_set|
    begin
      create_one_table(table_name, column_set)
    rescue => e
      EzLog.error("create_tables: #{e.inspect}\n#{$@.inspect}")
    end
  end
end
each() { |k, v| ... } click to toggle source
# File lib/ezframe/column_set.rb, line 61
def each
  @colset_h.each {|k, v| yield(k, v) }
end
full_join_structure(colset_id) click to toggle source

foreignから生成したテーブル連結情報を返す

# File lib/ezframe/column_set.rb, line 89
def full_join_structure(colset_id)
  struct = { tables: [colset_id] }
  colset = @colset_h[colset_id.to_sym]
  colset_keys = colset.keys
  struct[:column_list] = colset_keys.map { |k| "#{colset_id}.#{k}" }
  join_cond_h = {}
  colset_keys.each do |key|
    column = colset[key]
    if column.type.to_s == "foreign"
      # 連結するテーブル名をtable: で指定する。
      foreign_table = column.attribute[:table]
      # 指定されてなければ、キーの名称をテーブル名とする
      # そのテーブルが定義されてなければ、エラーとしてログに残す。
      unless foreign_table
        if @colset_h[key]
          foreign_table = key
        else
          EzLog.error "There is no related table: #{key}"
          next
        end
      end
      raise "no table: key=#{key}" unless foreign_table
      foreign_column = column.attribute[:column]&.to_sym || :id
      foreign_table = foreign_table.to_sym
      next if struct[:tables].include?(foreign_table)
      # join_cond_h["#{colset_id}.#{key}"] = "#{colset_id}.#{key} = #{foreign_table}.#{foreign_column}"
      join_cond_h[foreign_table] = "#{colset_id}.#{key} = #{foreign_table}.#{foreign_column}"
      struct[:tables].push(foreign_table)
      struct[:column_list] += ColumnSets.refer(foreign_table).keys.map {|k| "#{foreign_table}.#{k}" }
    end
  end
  struct[:join_condition] = join_cond_h
  return struct
end
get(colset_name) click to toggle source
# File lib/ezframe/column_set.rb, line 47
def get(colset_name)
  return nil unless colset_name
  return @colset_h[colset_name.to_sym].deep_dup
end
has_key?(key) click to toggle source
# File lib/ezframe/column_set.rb, line 42
def has_key?(key)
  return nil unless key
  return @colset_h[key.to_sym]
end
init(dir = nil) click to toggle source
# File lib/ezframe/column_set.rb, line 6
def init(dir = nil)
  dir ||= "./column"
  unless @colset_h
    @colset_h = {}
    load_files(dir)
  end
end
inspect() click to toggle source
# File lib/ezframe/column_set.rb, line 65
def inspect
  return @colset_h.map do |name, colset|
    # "[#{name}]:#{colset.inspect}"
    "[#{name}]:\n"
  end.join
end
join_complex_column() click to toggle source
# File lib/ezframe/column_set.rb, line 124
def join_complex_column

end
load_files(dir) click to toggle source
# File lib/ezframe/column_set.rb, line 14
def load_files(dir)
  Dir["#{dir}/*.yml"].each do |filename|
    load_one_file(filename)
  end
end
load_one_file(filename) click to toggle source
# File lib/ezframe/column_set.rb, line 20
def load_one_file(filename)
  colset_name = $1 if filename =~ /(\w+).ya?ml$/
  yaml = YAML.load(File.open(filename), symbolize_names: true)
  if yaml.length == 0
    EzLog.error("[ERROR] columns file is empty: #{filename}")
    return
  end
  column_info = yaml # .recursively_symbolize_keys
  # puts "load_one_file: filename=#{filename} column_info=#{column_info.inspect}"
  add(colset_name, column_info)
end
refer(colset_name) click to toggle source
# File lib/ezframe/column_set.rb, line 52
def refer(colset_name)
  return nil unless colset_name
  return @colset_h[colset_name.to_sym]
end