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, #tagged, #version_string

Constructor Details

#initialize(table_name, connection) ⇒ Parser

Returns a new instance of Parser


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

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

Instance Method Details

#ddlObject


40
41
42
43
44
45
# File 'lib/lhm/table.rb', line 40

def ddl
  sql = "show create table `#{ @table_name }`"
  specification = nil
  @connection.execute(sql).each { |row| specification = row.last }
  specification
end

#parseObject


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lhm/table.rb', line 47

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