Class: Ezframe::ColumnSets

Inherits:
Object show all
Defined in:
lib/ezframe/column_set.rb

Class Method Summary collapse

Class Method Details

.[](colset_name) ⇒ Object



57
58
59
# File 'lib/ezframe/column_set.rb', line 57

def [](colset_name)
  return get(colset_name)
end

.add(colset_name, columns) ⇒ Object



32
33
34
35
36
# 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

.cloneObject



38
39
40
# File 'lib/ezframe/column_set.rb', line 38

def clone
  @colset_h.deep_dup
end

.create_one_table(table_name, column_set) ⇒ Object



82
83
84
85
86
# 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_tablesObject



72
73
74
75
76
77
78
79
80
# 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

.eachObject



61
62
63
# File 'lib/ezframe/column_set.rb', line 61

def each
  @colset_h.each {|k, v| yield(k, v) }
end

.full_join_structure(colset_id) ⇒ Object

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object



47
48
49
50
# 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) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# 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) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/ezframe/column_set.rb', line 6

def init(dir = nil)
  dir ||= "./column"
  unless @colset_h
    @colset_h = {}
    load_files(dir)
  end
end

.inspectObject



65
66
67
68
69
70
# 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_columnObject



124
125
126
# File 'lib/ezframe/column_set.rb', line 124

def join_complex_column

end

.load_files(dir) ⇒ Object



14
15
16
17
18
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



52
53
54
55
# 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