package com.alibaba.druid.sql.ast.statement;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLIndex;
import com.alibaba.druid.sql.ast.SQLIndexDefinition;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.List;

public class SQLAlterTableAddIndex extends SQLObjectImpl implements SQLAlterTableItem, SQLIndex {
   private SQLIndexDefinition indexDefinition = new SQLIndexDefinition();

   public SQLAlterTableAddIndex() {
      this.indexDefinition.setParent(this);
   }

   public SQLIndexDefinition getIndexDefinition() {
      return this.indexDefinition;
   }

   protected void accept0(SQLASTVisitor visitor) {
      if (visitor.visit(this)) {
         if (this.indexDefinition.getName() != null) {
            this.indexDefinition.getName().accept(visitor);
         }

         for(int i = 0; i < this.getColumns().size(); ++i) {
            SQLSelectOrderByItem item = (SQLSelectOrderByItem)this.getColumns().get(i);
            if (item != null) {
               item.accept(visitor);
            }
         }

         for(int i = 0; i < this.getCovering().size(); ++i) {
            SQLName item = (SQLName)this.getCovering().get(i);
            if (item != null) {
               item.accept(visitor);
            }
         }
      }

      visitor.endVisit(this);
   }

   public boolean isUnique() {
      return this.indexDefinition.getType() != null && this.indexDefinition.getType().equalsIgnoreCase("UNIQUE");
   }

   public void setUnique(boolean unique) {
      this.indexDefinition.setType("UNIQUE");
   }

   public List<SQLSelectOrderByItem> getItems() {
      return this.indexDefinition.getColumns();
   }

   public void addItem(SQLSelectOrderByItem item) {
      if (item != null) {
         item.setParent(this);
      }

      this.indexDefinition.getColumns().add(item);
   }

   public SQLName getName() {
      return this.indexDefinition.getName();
   }

   public void setName(SQLName name) {
      this.indexDefinition.setName(name);
   }

   public String getType() {
      return this.indexDefinition.getType();
   }

   public void setType(String type) {
      this.indexDefinition.setType(type);
   }

   public String getUsing() {
      return this.indexDefinition.hasOptions() ? this.indexDefinition.getOptions().getIndexType() : null;
   }

   public void setUsing(String using) {
      this.indexDefinition.getOptions().setIndexType(using);
   }

   public boolean isKey() {
      return this.indexDefinition.isKey();
   }

   public void setKey(boolean key) {
      this.indexDefinition.setKey(key);
   }

   public void cloneTo(MySqlTableIndex x) {
      this.indexDefinition.cloneTo(x.getIndexDefinition());
   }

   public void cloneTo(MySqlKey x) {
      this.indexDefinition.cloneTo(x.getIndexDefinition());
   }

   public SQLExpr getComment() {
      return this.indexDefinition.hasOptions() ? this.indexDefinition.getOptions().getComment() : null;
   }

   public void setComment(SQLExpr comment) {
      if (comment != null) {
         comment.setParent(this);
      }

      this.indexDefinition.getOptions().setComment(comment);
   }

   public SQLExpr getKeyBlockSize() {
      return this.indexDefinition.hasOptions() ? this.indexDefinition.getOptions().getKeyBlockSize() : null;
   }

   public void setKeyBlockSize(SQLExpr keyBlockSize) {
      this.indexDefinition.getOptions().setKeyBlockSize(keyBlockSize);
   }

   public String getParserName() {
      return this.indexDefinition.hasOptions() ? this.indexDefinition.getOptions().getParserName() : null;
   }

   public void setParserName(String parserName) {
      this.indexDefinition.getOptions().setParserName(parserName);
   }

   public boolean isHashMapType() {
      return this.indexDefinition.isHashMapType();
   }

   public void setHashMapType(boolean hashMapType) {
      this.indexDefinition.setHashMapType(hashMapType);
   }

   protected SQLExpr getOption(long hash64) {
      return this.indexDefinition.getOption(hash64);
   }

   public String getDistanceMeasure() {
      return this.indexDefinition.getDistanceMeasure();
   }

   public String getAlgorithm() {
      return this.indexDefinition.getAlgorithm();
   }

   public void addOption(String name, SQLExpr value) {
      this.indexDefinition.addOption(name, value);
   }

   public List<SQLAssignItem> getOptions() {
      return this.indexDefinition.getCompatibleOptions();
   }

   public boolean isGlobal() {
      return this.indexDefinition.isGlobal();
   }

   public void setGlobal(boolean global) {
      this.indexDefinition.setGlobal(global);
   }

   public SQLExpr getDbPartitionBy() {
      return this.indexDefinition.getDbPartitionBy();
   }

   public void setDbPartitionBy(SQLExpr x) {
      this.indexDefinition.setDbPartitionBy(x);
   }

   public SQLExpr getTablePartitionBy() {
      return this.indexDefinition.getTbPartitionBy();
   }

   public void setTablePartitionBy(SQLExpr x) {
      this.indexDefinition.setTbPartitionBy(x);
   }

   public SQLExpr getTablePartitions() {
      return this.indexDefinition.getTbPartitions();
   }

   public void setTablePartitions(SQLExpr x) {
      this.indexDefinition.setTbPartitions(x);
   }

   public List<SQLName> getCovering() {
      return this.indexDefinition.getCovering();
   }

   public List<SQLSelectOrderByItem> getColumns() {
      return this.indexDefinition.getColumns();
   }
}
