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

import com.chenyang.druid.sql.ast.SQLExpr;
import com.chenyang.druid.sql.ast.SQLName;
import com.chenyang.druid.sql.ast.SQLObject;
import com.chenyang.druid.sql.ast.SQLReplaceable;
import com.chenyang.druid.sql.ast.SQLStatementImpl;
import com.chenyang.druid.sql.ast.expr.SQLIdentifierExpr;
import com.chenyang.druid.sql.ast.expr.SQLPropertyExpr;
import com.chenyang.druid.sql.visitor.SQLASTVisitor;
import java.util.ArrayList;
import java.util.List;

public class SQLShowColumnsStatement extends SQLStatementImpl implements SQLShowStatement, SQLReplaceable {
   private boolean full;
   private SQLName table;
   private SQLName database;
   private SQLExpr like;
   private SQLExpr where;

   public boolean isFull() {
      return this.full;
   }

   public void setFull(boolean full) {
      this.full = full;
   }

   public SQLName getTable() {
      return this.table;
   }

   public void setTable(SQLName table) {
      if (table instanceof SQLPropertyExpr) {
         SQLPropertyExpr propExpr = (SQLPropertyExpr)table;
         this.setDatabase((SQLName)propExpr.getOwner());
         this.table = new SQLIdentifierExpr(propExpr.getName());
      } else {
         this.table = table;
      }
   }

   public SQLName getDatabase() {
      return this.database;
   }

   public void setDatabase(SQLName database) {
      this.database = database;
   }

   public SQLExpr getLike() {
      return this.like;
   }

   public void setLike(SQLExpr like) {
      this.like = like;
   }

   public SQLExpr getWhere() {
      return this.where;
   }

   public void setWhere(SQLExpr where) {
      this.where = where;
   }

   public void accept0(SQLASTVisitor visitor) {
      if (visitor.visit(this)) {
         this.acceptChild(visitor, this.table);
         this.acceptChild(visitor, this.database);
         this.acceptChild(visitor, this.like);
         this.acceptChild(visitor, this.where);
      }

      visitor.endVisit(this);
   }

   public List<SQLObject> getChildren() {
      List<SQLObject> children = new ArrayList();
      if (this.table != null) {
         children.add(this.table);
      }

      if (this.database != null) {
         children.add(this.database);
      }

      if (this.like != null) {
         children.add(this.like);
      }

      if (this.where != null) {
         children.add(this.where);
      }

      return children;
   }

   public boolean replace(SQLExpr expr, SQLExpr target) {
      if (this.table == expr) {
         this.setTable((SQLName)target);
         return true;
      } else if (this.database == expr) {
         this.setDatabase((SQLName)target);
         return true;
      } else if (this.like == expr) {
         this.setLike(target);
         return true;
      } else if (this.where == expr) {
         this.setWhere(target);
         return true;
      } else {
         return false;
      }
   }
}
