Class: Lhm::Table::Parser

Inherits:
Object
  • Object
show all
Includes:
SqlHelper
Defined in:
lib/lhm/table.rb

Instance Method Summary collapse

Methods included from SqlHelper

#annotation, #idx_name, #idx_spec, #version_string

Constructor Details

#initialize(table_name, connection) ⇒ Parser

Returns a new instance of Parser.



33
34
35
36
37
# File 'lib/lhm/table.rb', line 33

def initialize(table_name, connection)
  @table_name = table_name.to_s
  @schema_name = connection.current_database
  @connection = connection
end

Instance Method Details

#ddlObject



39
40
41
# File 'lib/lhm/table.rb', line 39

def ddl
  @connection.show_create(@table_name)
end

#parseObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lhm/table.rb', line 43

def parse
  schema = read_information_schema

  Table.new(@table_name, extract_primary_key(schema), ddl).tap do |table|
    schema.each do |defn|
      column_name    = struct_key(defn, 'COLUMN_NAME')
      column_type    = struct_key(defn, 'COLUMN_TYPE')
      is_nullable    = struct_key(defn, 'IS_NULLABLE')
      column_default = struct_key(defn, 'COLUMN_DEFAULT')
      table.columns[defn[column_name]] = {
        :type => defn[column_type],
        :is_nullable => defn[is_nullable],
        :column_default => defn[column_default]
      }
    end

    extract_indices(read_indices).each do |idx, columns|
      table.indices[idx] = columns
    end
  end
end