package com.alibaba.druid.sql.dialect.greenplum.ast.expr.tablesource;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.dialect.greenplum.ast.GPSQLObject;
import com.alibaba.druid.sql.dialect.greenplum.ast.expr.GPColumnSimpleDefinitionExpr;
import com.alibaba.druid.sql.dialect.greenplum.visitor.GPASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.ArrayList;
import java.util.List;
import util.sqlparse.visitor.common.Objects;

public class GPRowsFromTableSource extends SQLExprTableSource implements GPSQLObject, GPTableSource {
   protected boolean only;
   protected boolean withOrdinality;
   protected boolean isRowsFrom;
   protected List<GPMethodInvokeTableSource> invokers = new ArrayList();
   protected List<SQLExpr> parameters = new ArrayList();
   protected List<GPColumnSimpleDefinitionExpr> defines = new ArrayList();

   public List<GPColumnSimpleDefinitionExpr> getDefines() {
      return this.defines;
   }

   public void setDefines(List<GPColumnSimpleDefinitionExpr> defines) {
      this.defines = defines;
   }

   public boolean isRowsFrom() {
      return this.isRowsFrom;
   }

   public void setRowsFrom(boolean rowsFrom) {
      this.isRowsFrom = rowsFrom;
   }

   public List<GPMethodInvokeTableSource> getInvokers() {
      return this.invokers;
   }

   public void setInvokers(List<GPMethodInvokeTableSource> invokers) {
      this.invokers = invokers;
   }

   public List<SQLExpr> getParameters() {
      return this.parameters;
   }

   public void setParameters(List<SQLExpr> parameters) {
      this.parameters = parameters;
   }

   public boolean isOnly() {
      return this.only;
   }

   public void setOnly(boolean only) {
      this.only = only;
   }

   public boolean isWithOrdinality() {
      return this.withOrdinality;
   }

   public void setWithOrdinality(boolean withOrdinality) {
      this.withOrdinality = withOrdinality;
   }

   public void addTableSource(GPMethodInvokeTableSource ts) {
      ts.setParent(this);
      this.invokers.add(ts);
   }

   public void addColumnAlias(SQLName name) {
      if (this.columns == null) {
         this.columns = new ArrayList();
      }

      this.columns.add(name);
   }

   public void addColumnAlias(GPColumnSimpleDefinitionExpr define) {
      define.setParent(this);
      this.defines.add(define);
   }

   public boolean equals(Object o) {
      if (this == o) {
         return true;
      } else if (o != null && this.getClass() == o.getClass()) {
         if (!super.equals(o)) {
            return false;
         } else {
            GPRowsFromTableSource that = (GPRowsFromTableSource)o;
            return this.only == that.only && this.withOrdinality == that.withOrdinality && this.isRowsFrom == that.isRowsFrom && Objects.equals(this.invokers, that.invokers);
         }
      } else {
         return false;
      }
   }

   public int hashCode() {
      return Objects.hash(super.hashCode(), this.only, this.withOrdinality, this.isRowsFrom, this.invokers);
   }

   public void accept0(SQLASTVisitor visitor) {
      this.accept0((GPASTVisitor)visitor);
   }

   public void accept0(GPASTVisitor visitor) {
      if (visitor.visit(this)) {
         if (this.expr != null) {
            this.expr.accept(visitor);
         }

         if (this.parameters != null && this.parameters.size() > 0) {
            for(SQLExpr parameter : this.parameters) {
               parameter.accept(visitor);
            }
         }

         if (this.invokers != null && this.invokers.size() > 0) {
            for(GPMethodInvokeTableSource parameter : this.invokers) {
               parameter.accept(visitor);
            }
         }

         if (this.sampling != null) {
            this.sampling.accept(visitor);
         }
      }

      visitor.endVisit(this);
   }
}
