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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Index

#inspect, #to_param, #to_s

Methods included from Baza::DatabaseModelFunctionality

#model_name, #to_model

Constructor Details

#initialize(args) ⇒ Index

Returns a new instance of Index.



5
6
7
8
9
10
# 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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#table_nameObject

Returns the value of attribute table_name.



3
4
5
# File 'lib/baza/driver/mysql/index.rb', line 3

def table_name
  @table_name
end

Instance Method Details

#__object_unique_id__Object

Used to validate in Wref::Map.



13
14
15
# File 'lib/baza/driver/mysql/index.rb', line 13

def __object_unique_id__
  name
end

#dataObject



50
51
52
53
54
55
# File 'lib/baza/driver/mysql/index.rb', line 50

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

#dropObject



25
26
27
28
29
30
31
32
33
34
35
36
# 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

#nameObject



17
18
19
# File 'lib/baza/driver/mysql/index.rb', line 17

def name
  @data.fetch(:Key_name)
end

#primary?Boolean

Returns true if the index is a primary-index.

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/baza/driver/mysql/index.rb', line 67

def primary?
  return true if @data.fetch(:Key_name) == "PRIMARY"
  false
end

#reloadObject



72
73
74
75
76
77
# File 'lib/baza/driver/mysql/index.rb', line 72

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

#rename(newname) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# 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

#tableObject



21
22
23
# File 'lib/baza/driver/mysql/index.rb', line 21

def table
  @db.tables[@table_name]
end

#unique?Boolean

Returns true if the index is a unique-index.

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# 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