class Lhm::Table::Parser

Public Class Methods

new(table_name, connection) click to toggle source
# 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

Public Instance Methods

ddl() click to toggle source
# File lib/lhm/table.rb, line 41
def ddl
  sql = "show create table `#{ @table_name }`"
  specification = nil
  @connection.execute(sql).each { |row| specification = row.last }
  specification
end
parse() click to toggle source
# File lib/lhm/table.rb, line 48
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

Private Instance Methods

extract_indices(indices) click to toggle source
# File lib/lhm/table.rb, line 93
def extract_indices(indices)
  indices.
    map do |row|
      key_name = struct_key(row, 'Key_name')
      column_name = struct_key(row, 'COLUMN_NAME')
      [row[key_name], row[column_name]]
    end.
    inject(Hash.new { |h, k| h[k] = [] }) do |memo, (idx, column)|
      memo[idx] << column
      memo
    end
end
extract_primary_key(schema) click to toggle source
# File lib/lhm/table.rb, line 106
def extract_primary_key(schema)
  cols = schema.select do |defn|
    column_key = struct_key(defn, 'COLUMN_KEY')
    defn[column_key] == 'PRI'
  end

  keys = cols.map do |defn|
    column_name = struct_key(defn, 'COLUMN_NAME')
    defn[column_name]
  end

  keys.length == 1 ? keys.first : keys
end
read_indices() click to toggle source
# File lib/lhm/table.rb, line 86
def read_indices
  @connection.select_all %Q{
    show indexes from `#{ @schema_name }`.`#{ @table_name }`
   where key_name != 'PRIMARY'
  }
end
read_information_schema() click to toggle source
# File lib/lhm/table.rb, line 77
def read_information_schema
  @connection.select_all %Q{
    select *
      from information_schema.columns
     where table_name = '#{ @table_name }'
       and table_schema = '#{ @schema_name }'
  }
end