class Baza::Driver::Mysql::Index

Attributes

args[R]
columns[R]
table_name[RW]

Public Class Methods

new(args) click to toggle source
# File lib/baza/driver/mysql/index.rb, line 5
def initialize(args)
  @db = args.fetch(:db)
  @data = args.fetch(:data)
  @table_name = args.fetch(:table_name)
  @columns = []
end

Public Instance Methods

__object_unique_id__() click to toggle source

Used to validate in Wref::Map.

# File lib/baza/driver/mysql/index.rb, line 13
def __object_unique_id__
  name
end
data() click to toggle source
# File lib/baza/driver/mysql/index.rb, line 50
def data
  {
    name: name,
    columns: @columns
  }
end
drop() click to toggle source
# File lib/baza/driver/mysql/index.rb, line 25
def drop
  sql = "DROP INDEX `#{name}` ON `#{@table_name}`"

  begin
    @db.query(sql)
  rescue => e
    # The index has already been dropped - ignore.
    raise e if e.message.index("check that column/key exists") == nil
  end

  self
end
name() click to toggle source
# File lib/baza/driver/mysql/index.rb, line 17
def name
  @data.fetch(:Key_name)
end
primary?() click to toggle source

Returns true if the index is a primary-index.

# File lib/baza/driver/mysql/index.rb, line 67
def primary?
  return true if @data.fetch(:Key_name) == "PRIMARY"
  false
end
reload() click to toggle source
# File lib/baza/driver/mysql/index.rb, line 72
def reload
  data = @db.query("SHOW INDEX FROM #{@db.quote_table(@table_name)} WHERE #{@db.quote_column("Key_name")} = #{@db.quote_value(name)}").fetch
  raise Baza::Errors::IndexNotFound unless data
  @data = data
  self
end
rename(newname) click to toggle source
# File lib/baza/driver/mysql/index.rb, line 38
def rename(newname)
  newname = newname.to_s
  create_args = data
  create_args[:name] = newname

  drop
  table.create_indexes([create_args])
  @data[:Key_name] = newname

  self
end
table() click to toggle source
# File lib/baza/driver/mysql/index.rb, line 21
def table
  @db.tables[@table_name]
end
unique?() click to toggle source

Returns true if the index is a unique-index.

# File lib/baza/driver/mysql/index.rb, line 58
def unique?
  if @data.fetch(:Index_type) == "UNIQUE" || @data.fetch(:Non_unique).to_i == 0
    return true
  else
    return false
  end
end