package com.alibaba.druid.sql.dialect.spark.ast.expr;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.dialect.spark.ast.SparkSqlExpr;
import com.alibaba.druid.sql.dialect.spark.ast.SparkSqlExprImpl;
import com.alibaba.druid.sql.dialect.spark.visitor.SparkVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.ArrayList;
import java.util.List;
import util.sqlparse.visitor.common.Objects;

public class SparkTransformExpr extends SQLMethodInvokeExpr implements SparkSqlExpr {
   private HiveRowFormat rowFormat;
   private SQLExpr recordWriter;
   private CommandScriptDef commandScript;
   private HiveRowFormat usingRowFormat;
   private SQLExpr recordReader;

   public List<SQLObject> getChildren() {
      List<SQLObject> result = new ArrayList();
      result.addAll(this.arguments);
      if (this.rowFormat != null) {
         result.add(this.rowFormat);
      }

      if (this.recordWriter != null) {
         result.add(this.recordWriter);
      }

      if (this.commandScript != null) {
         result.add(this.commandScript);
      }

      if (this.usingRowFormat != null) {
         result.add(this.usingRowFormat);
      }

      if (this.recordReader != null) {
         result.add(this.recordReader);
      }

      return result;
   }

   protected void accept0(SQLASTVisitor v) {
      this.accept0((SparkVisitor)v);
   }

   public void accept0(SparkVisitor v) {
      if (v.visit(this)) {
         this.acceptChild(v, this.arguments);
         this.acceptChild(v, this.rowFormat);
         this.acceptChild(v, this.recordWriter);
         this.acceptChild(v, this.commandScript);
         this.acceptChild(v, this.usingRowFormat);
         this.acceptChild(v, this.recordReader);
      }

      v.endVisit(this);
   }

   public SparkTransformExpr clone() {
      SparkTransformExpr e = new SparkTransformExpr();

      for(SQLExpr expr : this.arguments) {
         e.arguments.add(expr.clone());
      }

      if (this.rowFormat != null) {
         e.rowFormat = this.rowFormat.clone();
      }

      if (this.recordWriter != null) {
         e.recordWriter = this.recordWriter.clone();
      }

      if (this.commandScript != null) {
         e.commandScript = this.commandScript.clone();
      }

      if (this.usingRowFormat != null) {
         e.usingRowFormat = this.usingRowFormat.clone();
      }

      if (this.recordReader != null) {
         e.recordReader = this.recordReader.clone();
      }

      return e;
   }

   public HiveRowFormat getRowFormat() {
      return this.rowFormat;
   }

   public void setRowFormat(HiveRowFormat rowFormat) {
      this.rowFormat = rowFormat;
   }

   public SQLExpr getRecordWriter() {
      return this.recordWriter;
   }

   public void setRecordWriter(SQLExpr recordWriter) {
      this.recordWriter = recordWriter;
   }

   public CommandScriptDef getCommandScript() {
      return this.commandScript;
   }

   public void setCommandScript(CommandScriptDef commandScript) {
      this.commandScript = commandScript;
   }

   public HiveRowFormat getUsingRowFormat() {
      return this.usingRowFormat;
   }

   public void setUsingRowFormat(HiveRowFormat usingRowFormat) {
      this.usingRowFormat = usingRowFormat;
   }

   public SQLExpr getRecordReader() {
      return this.recordReader;
   }

   public void setRecordReader(SQLExpr recordReader) {
      this.recordReader = recordReader;
   }

   public boolean equals(Object o) {
      if (this == o) {
         return true;
      } else if (o != null && this.getClass() == o.getClass()) {
         SparkTransformExpr that = (SparkTransformExpr)o;
         return Objects.equals(this.arguments, that.arguments) && Objects.equals(this.rowFormat, that.rowFormat) && Objects.equals(this.recordWriter, that.recordWriter) && Objects.equals(this.commandScript, that.commandScript) && Objects.equals(this.usingRowFormat, that.usingRowFormat);
      } else {
         return false;
      }
   }

   public int hashCode() {
      return Objects.hash(this.arguments, this.rowFormat, this.recordWriter, this.commandScript, this.usingRowFormat);
   }

   public static class CommandScriptDef extends SparkSqlExprImpl {
      private SQLExpr commandOrScript;
      private List<SparkColumnDef> colDefs = new ArrayList();

      public SQLExpr getCommandOrScript() {
         return this.commandOrScript;
      }

      public void setCommandOrScript(SQLExpr commandOrScript) {
         this.commandOrScript = commandOrScript;
      }

      public List<SparkColumnDef> getColDefs() {
         return this.colDefs;
      }

      public void setColDefs(List<SparkColumnDef> colDefs) {
         this.colDefs = colDefs;
      }

      public List<SQLObject> getChildren() {
         List<SQLObject> result = new ArrayList();
         result.add(this.commandOrScript);
         result.addAll(this.colDefs);
         return result;
      }

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

         v.endVisit(this);
      }

      public CommandScriptDef clone() {
         CommandScriptDef e = new CommandScriptDef();
         e.commandOrScript = this.commandOrScript.clone();

         for(SparkColumnDef colDef : this.colDefs) {
            e.colDefs.add(colDef.clone());
         }

         return e;
      }

      public boolean equals(Object o) {
         if (this == o) {
            return true;
         } else if (o != null && this.getClass() == o.getClass()) {
            CommandScriptDef that = (CommandScriptDef)o;
            return Objects.equals(this.commandOrScript, that.commandOrScript) && Objects.equals(this.colDefs, that.colDefs);
         } else {
            return false;
         }
      }

      public int hashCode() {
         return Objects.hash(this.commandOrScript, this.colDefs);
      }
   }
}
