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, #sql, #table?, #update, #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
42
43
44
# File 'lib/lhm/table.rb', line 39

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

#parseObject



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

def parse
  schema = read_information_schema

  Table.new(@table_name, extract_primary_key(schema), ddl).tap do |table|
    schema.each do |defn|
      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