include/drivers/mysql/mysql_indexes.rb in baza-0.0.13 vs include/drivers/mysql/mysql_indexes.rb in baza-0.0.14

- old
+ new

@@ -3,33 +3,33 @@ @args = args end end class Baza::Driver::Mysql::Indexes::Index - attr_reader :columns - + attr_reader :args, :columns + def initialize(args) @args = args @columns = [] end - + #Used to validate in Knj::Wrap_map. def __object_unique_id__ return @args[:data][:Key_name] end - + def name return @args[:data][:Key_name] end - + def table return @args[:db].tables[@args[:table_name]] end - + 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 @@ -37,33 +37,42 @@ else raise e end end end - + + 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 + def data return { - :name => name, - :columns => @columns + name: name, + columns: @columns } end - + #Returns true if the index is a unique-index. def unique? if @args[:data][:Index_type] == "UNIQUE" return true else return false end end - + #Returns true if the index is a primary-index. def primary? - return true if @args[:data][:Index_type] == "BTREE" - return false + return true if @args[:data][:Key_name] == "PRIMARY" + return false end - - #Inspect crashes if this is not present? - knj. + def to_s - return "#<#{self.class.name}>" + return "#<Baza::Driver::Mysql::Index name: \"#{name}\", columns: #{@columns}, primary: #{primary?}, unique: #{unique?}>" end end \ No newline at end of file