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

import com.chenyang.druid.sql.ast.SQLName;
import com.chenyang.druid.sql.visitor.SQLASTVisitor;
import java.util.ArrayList;
import java.util.List;

public class SQLForeignKeyImpl extends SQLConstraintImpl implements SQLForeignKeyConstraint {
   private SQLExprTableSource referencedTable;
   private List<SQLName> referencingColumns = new ArrayList();
   private List<SQLName> referencedColumns = new ArrayList();
   private boolean onDeleteCascade = false;
   private boolean onDeleteSetNull = false;
   private boolean disableNovalidate = false;

   public List<SQLName> getReferencingColumns() {
      return this.referencingColumns;
   }

   public SQLExprTableSource getReferencedTable() {
      return this.referencedTable;
   }

   public SQLName getReferencedTableName() {
      return this.referencedTable == null ? null : this.referencedTable.getName();
   }

   public void setReferencedTableName(SQLName value) {
      if (value == null) {
         this.referencedTable = null;
      } else {
         this.setReferencedTable(new SQLExprTableSource(value));
      }
   }

   public void setReferencedTable(SQLExprTableSource x) {
      if (x != null) {
         x.setParent(this);
      }

      this.referencedTable = x;
   }

   public List<SQLName> getReferencedColumns() {
      return this.referencedColumns;
   }

   public boolean isOnDeleteCascade() {
      return this.onDeleteCascade;
   }

   public void setOnDeleteCascade(boolean onDeleteCascade) {
      this.onDeleteCascade = onDeleteCascade;
   }

   public boolean isOnDeleteSetNull() {
      return this.onDeleteSetNull;
   }

   public void setOnDeleteSetNull(boolean onDeleteSetNull) {
      this.onDeleteSetNull = onDeleteSetNull;
   }

   public boolean isDisableNovalidate() {
      return this.disableNovalidate;
   }

   public void setDisableNovalidate(boolean disableNovalidate) {
      this.disableNovalidate = disableNovalidate;
   }

   protected void accept0(SQLASTVisitor visitor) {
      if (visitor.visit(this)) {
         this.acceptChild(visitor, this.getName());
         this.acceptChild(visitor, this.getReferencedTableName());
         this.acceptChild(visitor, this.getReferencingColumns());
         this.acceptChild(visitor, this.getReferencedColumns());
      }

      visitor.endVisit(this);
   }

   public void cloneTo(SQLForeignKeyImpl x) {
      super.cloneTo((SQLConstraintImpl)x);
      if (this.referencedTable != null) {
         x.setReferencedTable(this.referencedTable.clone());
      }

      for(SQLName column : this.referencingColumns) {
         SQLName columnClone = column.clone();
         columnClone.setParent(x);
         x.getReferencingColumns().add(columnClone);
      }

      for(SQLName column : this.referencedColumns) {
         SQLName columnClone = column.clone();
         columnClone.setParent(x);
         x.getReferencedColumns().add(columnClone);
      }

   }

   public SQLForeignKeyImpl clone() {
      SQLForeignKeyImpl x = new SQLForeignKeyImpl();
      this.cloneTo(x);
      return x;
   }

   public static enum Match {
      FULL("FULL"),
      PARTIAL("PARTIAL"),
      SIMPLE("SIMPLE");

      public final String name;
      public final String name_lcase;

      private Match(String name) {
         this.name = name;
         this.name_lcase = name.toLowerCase();
      }
   }

   public static enum On {
      DELETE("DELETE"),
      UPDATE("UPDATE");

      public final String name;
      public final String name_lcase;

      private On(String name) {
         this.name = name;
         this.name_lcase = name.toLowerCase();
      }
   }

   public static enum Option {
      RESTRICT("RESTRICT"),
      CASCADE("CASCADE"),
      SET_NULL("SET NULL"),
      NO_ACTION("NO ACTION"),
      SET_DEFAULT("SET DEFAULT");

      public final String name;
      public final String name_lcase;

      private Option(String name) {
         this.name = name;
         this.name_lcase = name.toLowerCase();
      }

      public String getText() {
         return this.name;
      }
   }
}
