package util.sqlparse.visitor.mariadb.visitor;

import com.chenyang.druid.sql.ast.SQLDataType;
import com.chenyang.druid.sql.ast.SQLObject;
import com.chenyang.druid.sql.ast.SQLStatement;
import com.chenyang.druid.sql.ast.statement.SQLAlterIndexStatement;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableAddIndex;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableAnalyzePartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableCheckPartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableCoalescePartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableDiscardPartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableDropPartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableExchangePartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableImportPartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableOptimizePartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableReOrganizePartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableRebuildPartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableRenameIndex;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableRepairPartition;
import com.chenyang.druid.sql.ast.statement.SQLAlterTableTruncatePartition;
import com.chenyang.druid.sql.ast.statement.SQLCheck;
import com.chenyang.druid.sql.ast.statement.SQLColumnDefinition;
import com.chenyang.druid.sql.ast.statement.SQLCreateIndexStatement;
import com.chenyang.druid.sql.ast.statement.SQLDeleteStatement;
import com.chenyang.druid.sql.ast.statement.SQLExprTableSource;
import com.chenyang.druid.sql.ast.statement.SQLInsertStatement;
import com.chenyang.druid.sql.ast.statement.SQLJoinTableSource;
import com.chenyang.druid.sql.ast.statement.SQLLateralViewTableSource;
import com.chenyang.druid.sql.ast.statement.SQLReplaceStatement;
import com.chenyang.druid.sql.ast.statement.SQLSelectStatement;
import com.chenyang.druid.sql.ast.statement.SQLSubqueryTableSource;
import com.chenyang.druid.sql.ast.statement.SQLUnionQuery;
import com.chenyang.druid.sql.ast.statement.SQLUnionQueryTableSource;
import com.chenyang.druid.sql.ast.statement.SQLUpdateStatement;
import com.chenyang.druid.sql.ast.statement.SQLWithSubqueryClause;
import com.chenyang.druid.sql.dialect.mariadb.ast.clause.MariadbSelectIntoStatement;
import com.chenyang.druid.sql.dialect.mariadb.ast.statement.MariadbDeleteStatement;
import com.chenyang.druid.sql.dialect.mariadb.ast.statement.MariadbInsertStatement;
import com.chenyang.druid.sql.dialect.mariadb.ast.statement.MariadbSelectQueryBlock;
import com.chenyang.druid.sql.dialect.mariadb.ast.statement.MariadbUpdateStatement;
import com.chenyang.druid.sql.dialect.mariadb.visitor.DefaultMariadbASTVisitor;
import java.util.List;
import util.sqlparse.visitor.common.scope.Scope;

public abstract class ParseVisitor extends DefaultMariadbASTVisitor {
   private boolean debug = false;
   protected Scope scope;

   public ParseVisitor(Scope scope) {
      this.scope = scope;
   }

   public void perform(List<SQLStatement> statements) {
      if (statements != null && statements.size() != 0) {
         for(SQLStatement statement : statements) {
            statement.accept(this);
         }

         this.scope.reset();
      }
   }

   public boolean visit(SQLSelectStatement x) {
      if (this.debug) {
         System.out.println("enter SQLSelectStatement" + x.toString());
      }

      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLSelectStatement x) {
      if (this.debug) {
         System.out.println("exit SQLSelectStatement" + x.toString());
      }

      this.scope.exitScope();
   }

   public boolean visit(MariadbSelectIntoStatement x) {
      if (this.debug) {
         System.out.println("enter MySqlSelectIntoStatement" + x.toString());
      }

      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(MariadbSelectIntoStatement x) {
      if (this.debug) {
         System.out.println("exit MySqlSelectIntoStatement" + x.toString());
      }

      this.scope.exitScope();
   }

   public boolean visit(SQLInsertStatement x) {
      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLInsertStatement x) {
      this.scope.exitScope();
   }

   public boolean visit(MariadbInsertStatement x) {
      return this.visit((SQLInsertStatement)x);
   }

   public void endVisit(MariadbInsertStatement x) {
      this.scope.exitScope();
   }

   public boolean visit(SQLUpdateStatement x) {
      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLUpdateStatement x) {
      this.scope.exitScope();
   }

   public boolean visit(SQLDeleteStatement x) {
      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLDeleteStatement x) {
      this.scope.exitScope();
   }

   public boolean visit(MariadbDeleteStatement x) {
      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(MariadbDeleteStatement x) {
      this.scope.exitScope();
   }

   public boolean visit(MariadbUpdateStatement x) {
      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(MariadbUpdateStatement x) {
      this.scope.exitScope();
   }

   public boolean visit(SQLReplaceStatement x) {
      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLReplaceStatement x) {
      this.scope.exitScope();
   }

   public boolean visit(SQLWithSubqueryClause x) {
      if (this.debug) {
         System.out.println("enter SQLWithSubqueryClause" + x.toString());
      }

      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLWithSubqueryClause x) {
      if (this.debug) {
         System.out.println("exit SQLWithSubqueryClause" + x.toString());
      }

      this.scope.exitScope();
   }

   public boolean visit(SQLWithSubqueryClause.Entry x) {
      if (this.debug) {
         System.out.println("enter SQLWithSubqueryClause.Entry" + x.toString());
      }

      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLWithSubqueryClause.Entry x) {
      if (this.debug) {
         System.out.println("exit SQLWithSubqueryClause.Entry" + x.toString());
      }

      this.scope.exitScope();
   }

   public boolean visit(MariadbSelectQueryBlock x) {
      if (this.debug) {
         System.out.println("enter MySqlSelectQueryBlock " + x.toString());
      }

      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(MariadbSelectQueryBlock x) {
      if (this.debug) {
         System.out.println("exit MySqlSelectQueryBlock " + x.toString());
      }

      this.scope.exitScope();
   }

   public boolean visit(SQLUnionQuery x) {
      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLUnionQuery x) {
      this.scope.exitScope();
   }

   public boolean visit(SQLLateralViewTableSource x) {
      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLLateralViewTableSource x) {
      this.scope.exitScope();
   }

   public boolean visit(SQLSubqueryTableSource x) {
      if (this.debug) {
         System.out.println("enter SQLSubqueryTableSource" + x.toString());
      }

      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLSubqueryTableSource x) {
      if (this.debug) {
         System.out.println("exit SQLSubqueryTableSource" + x.toString());
      }

      for(Scope s : this.scope.getCurrent().getChildren()) {
         s.isolated = false;
      }

      this.scope.exitScope();
   }

   public boolean visit(SQLJoinTableSource x) {
      return true;
   }

   public boolean visit(SQLUnionQueryTableSource x) {
      if (this.debug) {
         System.out.println("enter SQLUnionQueryTableSource" + x.toString());
      }

      this.scope.enterScope((SQLObject)x);
      return true;
   }

   public void endVisit(SQLUnionQueryTableSource x) {
      if (this.debug) {
         System.out.println("exit SQLUnionQueryTableSource" + x.toString());
      }

      for(Scope s1 : this.scope.getChildren()) {
         s1.isolated = false;
      }

      this.scope.exitScope();
   }

   public void endVisit(SQLJoinTableSource x) {
   }

   public boolean visit(SQLExprTableSource x) {
      return true;
   }

   public boolean visit(SQLAlterTableRenameIndex x) {
      return false;
   }

   public boolean visit(SQLAlterTableExchangePartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableAnalyzePartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableDropPartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableImportPartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableCheckPartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableOptimizePartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableRebuildPartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableRepairPartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableTruncatePartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableDiscardPartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableReOrganizePartition x) {
      return false;
   }

   public boolean visit(SQLAlterTableCoalescePartition x) {
      return false;
   }

   public void endVisit(SQLExprTableSource x) {
   }

   protected void visitChild(SQLObject x) {
      if (x != null) {
         x.accept(this);
      }
   }

   protected void visitChild(List<? extends SQLObject> x) {
      if (x != null && x.size() != 0) {
         for(SQLObject sqlObject : x) {
            this.visitChild(sqlObject);
         }

      }
   }

   public boolean isParentTypeRecursive(SQLObject parent) {
      if (null != parent) {
         return !(parent instanceof SQLDataType) && !(parent instanceof SQLCheck) && !(parent instanceof SQLColumnDefinition) && !(parent instanceof SQLCreateIndexStatement) && !(parent instanceof SQLAlterIndexStatement) && !(parent instanceof SQLAlterTableAddIndex) ? this.isParentTypeRecursive(parent.getParent()) : true;
      } else {
         return false;
      }
   }

   protected boolean isEqual(String name1, String name2) {
      return this.scope.dialector.isEqual(name1, name2);
   }
}
