package util.sqlparse.visitor.oracle.visitor;

import bean.DataBase;
import com.chenyang.druid.sql.ast.SQLExpr;
import com.chenyang.druid.sql.ast.SQLObject;
import com.chenyang.druid.sql.ast.expr.SQLIdentifierExpr;
import com.chenyang.druid.sql.ast.expr.SQLPropertyExpr;
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.SQLMergeStatement;
import com.chenyang.druid.sql.ast.statement.SQLSelect;
import com.chenyang.druid.sql.ast.statement.SQLSelectQuery;
import com.chenyang.druid.sql.ast.statement.SQLSelectStatement;
import com.chenyang.druid.sql.ast.statement.SQLTableSource;
import com.chenyang.druid.sql.ast.statement.SQLUpdateStatement;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import util.JdbcUtil;
import util.sqlparse.OracleParse;
import util.sqlparse.visitor.common.bean.SQLResult;
import util.sqlparse.visitor.common.bean.TableInfo;
import util.sqlparse.visitor.common.memo.TableMemo;
import util.sqlparse.visitor.oracle.OracleNameWrapper;
import util.sqlparse.visitor.oracle.SQLParser;

public class Desensitization {
   public Map parseColumn(Map params) {
      return this.parseData(params);
   }

   public Map parseData(Map params) {
      String sql = (String)params.get("sql");
      SQLParser parser = new SQLParser();
      DataBase dataBase = null;
      params.put("newSql", sql);
      String url = (String)params.get("url");
      String ipPort = OracleParse.getIpPort(url);
      Iterator var7 = JdbcUtil.dataBaseList.iterator();

      while(var7.hasNext()) {
         DataBase base = (DataBase)var7.next();
         if (base.getIp_port().equalsIgnoreCase(ipPort)) {
            dataBase = base;
         }
      }

      String schema = (String)params.get("schema");
      SQLResult result = parser.parse(sql, dataBase, schema);
      switch (result.statementType) {
         case select:
            FieldReplaceController controller = new FieldReplaceController(result, params);
            controller.perform();
         default:
            return params;
      }
   }

   public Map parseTable(Map params) {
      String replaceTable = params.get("replaceTable").toString();
      replaceTable = OracleNameWrapper.normalize(replaceTable);
      String replaceNewTable = params.get("replaceNewTable").toString();
      SQLResult result = this.getSqlResult(params);
      List<TableInfo> tables = result.tables;
      params.put("isMatched", "false");
      Iterator var6 = tables.iterator();

      while(var6.hasNext()) {
         TableInfo table = (TableInfo)var6.next();
         Iterator var8 = table.getMemos().iterator();

         while(var8.hasNext()) {
            TableMemo tableMemo = (TableMemo)var8.next();
            String sourceName = OracleNameWrapper.normalize(tableMemo.name);
            boolean isMatched = JdbcUtil.followRulesCaseSens(sourceName, "\"?" + replaceTable + "\"?");
            if (isMatched) {
               tableMemo.name = replaceNewTable;
               tableMemo.nowName = replaceNewTable;
               SQLExprTableSource ref = (SQLExprTableSource)tableMemo.ref;
               SQLExpr ts = ref.getExpr();
               if (ts instanceof SQLIdentifierExpr) {
                  ((SQLIdentifierExpr)ts).setName(replaceNewTable);
               } else if (ts instanceof SQLPropertyExpr) {
                  SQLPropertyExpr propertyExpr = (SQLPropertyExpr)ts;
                  propertyExpr.setName(replaceNewTable);
               }

               int type = this.getTableSourceTopType(ref);
               if (ref.getAlias() == null && type == 1) {
                  ref.setAlias(replaceTable);
               }

               params.put("isMatched", "true");
            }
         }
      }

      params.put("newSql", result.statement.toString());
      return params;
   }

   private int getTableSourceTopType(SQLTableSource ts) {
      if (ts == null) {
         return 0;
      } else {
         for(SQLObject cursor = ts; cursor != null; cursor = ((SQLObject)cursor).getParent()) {
            if (cursor instanceof SQLUpdateStatement) {
               return 0;
            }

            if (cursor instanceof SQLInsertStatement) {
               return 0;
            }

            if (cursor instanceof SQLDeleteStatement) {
               return 0;
            }

            if (cursor instanceof SQLMergeStatement) {
               return 0;
            }

            if (cursor instanceof SQLSelect) {
               return 1;
            }

            if (cursor instanceof SQLSelectQuery) {
               return 1;
            }

            if (cursor instanceof SQLSelectStatement) {
               return 1;
            }
         }

         return 0;
      }
   }

   public Map parseRowVisit(Map params) {
      SQLResult result = this.getSqlResult(params);
      RowVisitController controller = new RowVisitController(result, params);
      controller.perform();
      return params;
   }

   private SQLResult getSqlResult(Map params) {
      String sql = (String)params.get("sql");
      SQLParser parser = new SQLParser();
      DataBase dataBase = null;
      params.put("newSql", sql);
      String url = (String)params.get("url");
      String ipPort = OracleParse.getIpPort(url);
      Iterator var7 = JdbcUtil.dataBaseList.iterator();

      while(var7.hasNext()) {
         DataBase base = (DataBase)var7.next();
         if (base.getIp_port().equalsIgnoreCase(ipPort)) {
            dataBase = base;
         }
      }

      String schema = (String)params.get("schema");
      SQLResult result = parser.parse(sql, dataBase, schema);
      return result;
   }
}
