Sha256: 632a4908bf6bae8bb63fe2553222cb57f34e9f66aae06404b5db404dd61c0bd4

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

class Baza::Driver::Mysql::Index < Baza::Index
  attr_reader :args, :columns
  attr_accessor :table_name

  def initialize(args)
    @db = args.fetch(:db)
    @data = args.fetch(:data)
    @table_name = args.fetch(:table_name)
    @columns = []
  end

  # Used to validate in Wref::Map.
  def __object_unique_id__
    name
  end

  def name
    @data.fetch(:Key_name)
  end

  def table
    @db.tables[@table_name]
  end

  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

  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

  def data
    {
      name: name,
      columns: @columns
    }
  end

  # Returns true if the index is a unique-index.
  def unique?
    if @data.fetch(:Index_type) == "UNIQUE" || @data.fetch(:Non_unique).to_i == 0
      return true
    else
      return false
    end
  end

  # Returns true if the index is a primary-index.
  def primary?
    return true if @data.fetch(:Key_name) == "PRIMARY"
    false
  end

  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
baza-0.0.38 lib/baza/driver/mysql/index.rb
baza-0.0.37 lib/baza/driver/mysql/index.rb
baza-0.0.36 lib/baza/driver/mysql/index.rb