package com.alibaba.druid.sql.ast;

import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import java.util.ArrayList;
import java.util.List;

public abstract class SQLPartitionBy extends SQLObjectImpl {
   protected SQLSubPartitionBy subPartitionBy;
   protected SQLExpr partitionsCount;
   protected boolean linear;
   protected List<SQLPartition> partitions = new ArrayList();
   protected List<SQLName> storeIn = new ArrayList();
   protected List<SQLExpr> columns = new ArrayList();
   protected SQLIntegerExpr lifecycle;

   public List<SQLPartition> getPartitions() {
      return this.partitions;
   }

   public void addPartition(SQLPartition partition) {
      if (partition != null) {
         partition.setParent(this);
      }

      this.partitions.add(partition);
   }

   public SQLSubPartitionBy getSubPartitionBy() {
      return this.subPartitionBy;
   }

   public void setSubPartitionBy(SQLSubPartitionBy subPartitionBy) {
      if (subPartitionBy != null) {
         subPartitionBy.setParent(this);
      }

      this.subPartitionBy = subPartitionBy;
   }

   public SQLExpr getPartitionsCount() {
      return this.partitionsCount;
   }

   public void setPartitionsCount(SQLExpr x) {
      if (x != null) {
         x.setParent(this);
      }

      this.partitionsCount = x;
   }

   public void setPartitionsCount(int partitionsCount) {
      this.partitionsCount = new SQLIntegerExpr(partitionsCount);
   }

   public boolean isLinear() {
      return this.linear;
   }

   public void setLinear(boolean linear) {
      this.linear = linear;
   }

   public List<SQLName> getStoreIn() {
      return this.storeIn;
   }

   public List<SQLExpr> getColumns() {
      return this.columns;
   }

   public void addColumn(SQLExpr column) {
      if (column != null) {
         column.setParent(this);
      }

      this.columns.add(column);
   }

   public void cloneTo(SQLPartitionBy x) {
      if (this.subPartitionBy != null) {
         x.setSubPartitionBy(this.subPartitionBy.clone());
      }

      if (this.partitionsCount != null) {
         x.setPartitionsCount(this.partitionsCount.clone());
      }

      x.linear = this.linear;

      for(SQLPartition p : this.partitions) {
         SQLPartition p2 = p.clone();
         p2.setParent(x);
         x.partitions.add(p2);
      }

      for(SQLName name : this.storeIn) {
         SQLName name2 = name.clone();
         name2.setParent(x);
         x.storeIn.add(name2);
      }

      x.lifecycle = this.lifecycle;
   }

   public boolean isPartitionByColumn(long columnNameHashCode64) {
      for(SQLExpr column : this.columns) {
         if (column instanceof SQLIdentifierExpr && ((SQLIdentifierExpr)column).nameHashCode64() == columnNameHashCode64) {
            return true;
         }
      }

      if (this.subPartitionBy != null) {
         return this.subPartitionBy.isPartitionByColumn(columnNameHashCode64);
      } else {
         return false;
      }
   }

   public SQLIntegerExpr getLifecycle() {
      return this.lifecycle;
   }

   public void setLifecycle(SQLIntegerExpr lifecycle) {
      this.lifecycle = lifecycle;
   }

   public abstract SQLPartitionBy clone();
}
