Class: Baza::Driver::Mysql::Index
- Defined in:
- lib/baza/driver/mysql/index.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#__object_unique_id__ ⇒ Object
Used to validate in Wref::Map.
- #data ⇒ Object
- #drop ⇒ Object
-
#initialize(args) ⇒ Index
constructor
A new instance of Index.
- #name ⇒ Object
-
#primary? ⇒ Boolean
Returns true if the index is a primary-index.
- #reload ⇒ Object
- #rename(newname) ⇒ Object
- #table ⇒ Object
-
#unique? ⇒ Boolean
Returns true if the index is a unique-index.
Methods inherited from Index
Methods included from Baza::DatabaseModelFunctionality
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
2 3 4 |
# File 'lib/baza/driver/mysql/index.rb', line 2 def args @args end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
2 3 4 |
# File 'lib/baza/driver/mysql/index.rb', line 2 def columns @columns end |
#table_name ⇒ Object
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 |
#data ⇒ Object
50 51 52 53 54 55 |
# File 'lib/baza/driver/mysql/index.rb', line 50 def data { name: name, columns: @columns } end |
#drop ⇒ Object
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..index("check that column/key exists") == nil end self end |
#name ⇒ Object
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.
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 |
#reload ⇒ Object
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 |
#table ⇒ Object
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.
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 |