Class: Baza::Driver::Sqlite3::Index

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

Direct Known Subclasses

Baza::Driver::Sqlite3Java::Index

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.



4
5
6
7
8
9
# File 'lib/baza/driver/sqlite3/index.rb', line 4

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

Instance Method Details

#column_namesObject



46
47
48
# File 'lib/baza/driver/sqlite3/index.rb', line 46

def column_names
  @columns
end

#dataObject



38
39
40
41
42
43
44
# File 'lib/baza/driver/sqlite3/index.rb', line 38

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

#dropObject



23
24
25
# File 'lib/baza/driver/sqlite3/index.rb', line 23

def drop
  @db.query("DROP INDEX `#{name}`")
end

#nameObject



11
12
13
# File 'lib/baza/driver/sqlite3/index.rb', line 11

def name
  @data.fetch(:name)
end

#reloadObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/baza/driver/sqlite3/index.rb', line 54

def reload
  data = nil
  @db.query("PRAGMA index_list(`#{@db.escape_table(name)}`)") do |d_indexes|
    next unless d_indexes.fetch(:name) == name
    data = d_indexes
    break
  end

  raise Baza::Errors::IndexNotFound unless data
  @data = data
  self
end

#rename(newname) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/baza/driver/sqlite3/index.rb', line 27

def rename(newname)
  newname = newname.to_sym

  create_args = data
  create_args[:name] = newname

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

#tableObject



19
20
21
# File 'lib/baza/driver/sqlite3/index.rb', line 19

def table
  @db.tables[table_name]
end

#table_nameObject



15
16
17
# File 'lib/baza/driver/sqlite3/index.rb', line 15

def table_name
  @args.fetch(:table_name)
end

#unique?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/baza/driver/sqlite3/index.rb', line 50

def unique?
  @data.fetch(:unique).to_i == 1
end