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.



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

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

Instance Method Details

#ddlObject



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

def ddl
  query = "SHOW CREATE TABLE #{ @connection.quote_table_name(@table_name) }"

  @connection.select_one(query)["Create Table"]
end

#parseObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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')
      comment = struct_key(defn, 'COLUMN_COMMENT')
      collate = struct_key(defn, 'COLLATION_NAME')

      table.columns[defn[column_name]] = {
        :type => defn[column_type],
        :is_nullable => defn[is_nullable],
        :column_default => defn[column_default],
        :comment => defn[comment],
        :collate => defn[collate],
      }
    end

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