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

import com.chenyang.druid.DbType;
import com.chenyang.druid.sql.ast.SQLExpr;
import com.chenyang.druid.sql.ast.SQLName;
import com.chenyang.druid.sql.ast.SQLReplaceable;
import com.chenyang.druid.sql.ast.SQLStatementImpl;
import com.chenyang.druid.sql.ast.statement.SQLShowStatement;
import com.chenyang.druid.sql.dialect.spark.ast.expr.SparkTablePartition;
import com.chenyang.druid.sql.dialect.spark.visitor.SparkVisitor;
import com.chenyang.druid.sql.visitor.SQLASTVisitor;

public class SparkShowTableStatement extends SQLStatementImpl implements SQLShowStatement, SQLReplaceable, SparkStatement {
   private boolean extended = false;
   protected SQLName database;
   protected SQLExpr like;
   protected SparkTablePartition partition;

   public SparkShowTableStatement() {
      super(DbType.spark);
   }

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

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

   public SparkTablePartition getPartition() {
      return this.partition;
   }

   public void setPartition(SparkTablePartition partition) {
      this.partition = partition;
   }

   public void setDatabase(SQLName database) {
      if (database != null) {
         database.setParent(this);
      }

      this.database = database;
   }

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

   public void setLike(SQLExpr like) {
      if (like != null) {
         like.setParent(this);
      }

      this.like = like;
   }

   protected void accept0(SQLASTVisitor visitor) {
      if (visitor instanceof SparkVisitor) {
         this.accept0((SparkVisitor)visitor);
      } else {
         super.accept0(visitor);
      }

   }

   public void accept0(SparkVisitor v) {
      if (v.visit(this)) {
         this.acceptChild(v, this.database);
         this.acceptChild(v, this.like);
      }

      v.endVisit(this);
   }

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

   public boolean isExtended() {
      return this.extended;
   }

   public void setExtended(boolean extended) {
      this.extended = extended;
   }
}
