Sha256: d2db066450f1879f69d28b575ceeaa4dcf96e1da47d0931570d77ca146c76faf

Contents?: true

Size: 1.48 KB

Versions: 16

Compression:

Stored size: 1.48 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.escape_table(@table_name)}` WHERE `Key_name` = '#{@db.esc(name)}'").fetch
    raise Baza::Errors::IndexNotFound unless data
    @data = data
    self
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
baza-0.0.35 lib/baza/driver/mysql/index.rb
baza-0.0.34 lib/baza/driver/mysql/index.rb
baza-0.0.33 lib/baza/driver/mysql/index.rb
baza-0.0.32 lib/baza/driver/mysql/index.rb
baza-0.0.31 lib/baza/driver/mysql/index.rb
baza-0.0.30 lib/baza/driver/mysql/index.rb
baza-0.0.29 lib/baza/driver/mysql/index.rb
baza-0.0.28 lib/baza/driver/mysql/index.rb
baza-0.0.27 lib/baza/driver/mysql/index.rb
baza-0.0.26 lib/baza/driver/mysql/index.rb
baza-0.0.25 lib/baza/driver/mysql/index.rb
baza-0.0.24 lib/baza/driver/mysql/index.rb
baza-0.0.23 lib/baza/driver/mysql/index.rb
baza-0.0.22 lib/baza/driver/mysql/index.rb
baza-0.0.21 lib/baza/driver/mysql/index.rb
baza-0.0.20 lib/baza/drivers/mysql/index.rb