Class: Baza::Driver::Mysql::Index

Inherits:
Index
  • Object
show all
Defined in:
lib/baza/drivers/mysql/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Index

#inspect, #to_s

Constructor Details

#initialize(args) ⇒ Index

Returns a new instance of Index.



4
5
6
7
# File 'lib/baza/drivers/mysql/index.rb', line 4

def initialize(args)
  @args = args
  @columns = []
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



2
3
4
# File 'lib/baza/drivers/mysql/index.rb', line 2

def args
  @args
end

#columnsObject (readonly)

Returns the value of attribute columns.



2
3
4
# File 'lib/baza/drivers/mysql/index.rb', line 2

def columns
  @columns
end

Instance Method Details

#__object_unique_id__Object

Used to validate in Wref::Map.



10
11
12
# File 'lib/baza/drivers/mysql/index.rb', line 10

def __object_unique_id__
  return @args[:data][:Key_name]
end

#dataObject



47
48
49
50
51
52
# File 'lib/baza/drivers/mysql/index.rb', line 47

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

#dropObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/baza/drivers/mysql/index.rb', line 22

def drop
  sql = "DROP INDEX `#{self.name}` ON `#{self.table.name}`"

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

#nameObject



14
15
16
# File 'lib/baza/drivers/mysql/index.rb', line 14

def name
  return @args[:data][:Key_name]
end

#primary?Boolean

Returns true if the index is a primary-index.

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/baza/drivers/mysql/index.rb', line 64

def primary?
  return true if @args[:data][:Key_name] == "PRIMARY"
  return false
end

#rename(newname) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/baza/drivers/mysql/index.rb', line 37

def rename newname
  newname = newname.to_sym
  create_args = data
  create_args[:name] = newname

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

#tableObject



18
19
20
# File 'lib/baza/drivers/mysql/index.rb', line 18

def table
  return @args[:db].tables[@args[:table_name]]
end

#unique?Boolean

Returns true if the index is a unique-index.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/baza/drivers/mysql/index.rb', line 55

def unique?
  if @args[:data][:Index_type] == "UNIQUE" || @args[:data][:Non_unique].to_i == 0
    return true
  else
    return false
  end
end