class MySQLExpectations::Key

An table key has a name and a sequence of index fields.

Constants

NON_UNIQUE
UNIQUE

Attributes

fields[R]
name[R]
unique[R]

Public Class Methods

collation_from_s(collation) click to toggle source
# File lib/mysql_expectations/key.rb, line 77
def self.collation_from_s(collation)
  return KeyField::ORDER_DESC if collation == 'D'
  KeyField::ORDER_ASC
end
find_key_element(table_element, index_name, field_index) click to toggle source
# File lib/mysql_expectations/key.rb, line 71
def self.find_key_element(table_element, index_name, field_index)
  query = "key[@Key_name='#{index_name}' and " \
    "@Seq_in_index='#{field_index + 1}']"
  table_element.elements[query]
end
key?(table_element, key_name) click to toggle source
# File lib/mysql_expectations/key.rb, line 46
def self.key?(table_element, key_name)
  result = table_element.elements["key[@Key_name='#{key_name}']"]
  !result.nil?
end
load_all_keys(table_element) click to toggle source
# File lib/mysql_expectations/key.rb, line 60
def self.load_all_keys(table_element)
  keys = {}
  table_element.elements.each('key') do |e|
    key_name = e.attributes['Key_name']
    unless keys.key? key_name
      keys[key_name] = load_key(table_element, key_name)
    end
  end
  keys.values
end
load_key(table_element, key_name) click to toggle source
# File lib/mysql_expectations/key.rb, line 51
def self.load_key(table_element, key_name)
  if key?(table_element, key_name)
    key_fields = load_key_fields(table_element, key_name)
    unique = load_uniqueness(table_element, key_name)
    return Key.new(key_name, unique, key_fields)
  end
  nil
end
load_key_field(key_element) click to toggle source
# File lib/mysql_expectations/key.rb, line 82
def self.load_key_field(key_element)
  name = key_element.attributes['Column_name']
  collation = key_element.attributes['Collation']
  collation = collation_from_s(collation)
  length = key_element.attributes['Length']
  KeyField.new(name, collation, length)
end
load_key_fields(table_element, key_name) click to toggle source
# File lib/mysql_expectations/key.rb, line 96
def self.load_key_fields(table_element, key_name)
  key_fields = []
  (0..1000).each do |field_index|
    key_element = find_key_element(table_element, key_name, field_index)
    break if key_element.nil?
    key_fields << load_key_field(key_element)
  end
  key_fields
end
load_uniqueness(table_element, key_name) click to toggle source
# File lib/mysql_expectations/key.rb, line 90
def self.load_uniqueness(table_element, key_name)
  key_element = find_key_element(table_element, key_name, 0)
  return nil if key_element.nil?
  (key_element.attributes['Non_unique'] == '0')
end
new(name, unique = nil, fields) click to toggle source
# File lib/mysql_expectations/key.rb, line 16
def initialize(name, unique = nil, fields)
  @name = name
  @unique = unique
  @fields = []
  fields.each do |field|
    field = KeyField.new(field) unless field.instance_of?(KeyField)
    @fields << field
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/mysql_expectations/key.rb, line 26
def ==(other)
  name == other.name && unique == other.unique && fields == other.fields
end
to_s() click to toggle source
# File lib/mysql_expectations/key.rb, line 30
def to_s
  result = "{ '#{name}', #{uniqueness_to_s}, ["
  fields.each_with_index do |field, index|
    result << ', ' if index > 0
    result << field.to_s
  end
  result << '] }'
  result
end
uniqueness_to_s() click to toggle source
# File lib/mysql_expectations/key.rb, line 40
def uniqueness_to_s
  return 'nil' if unique.nil?
  return 'UNIQUE' if unique == true
  return 'NON_UNIQUE' if unique == false
end