package util.sqlparse.visitor.dm.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import util.JdbcUtil;
import util.SqlUtil;

public class DmDesensitizationTest {
   public static void main(String[] args) throws Exception {
      new DmDesensitizationTest();
      Map<String, Object> stringObjectMap = dmParseTest("SELECT * FROM ( select ROWNUM RN , TT.name  nm, TT.* FROM  (SELECT * FROM \"test\".\"sys_user\" ) TT ) where RN> ? AND RN <= ?");
   }

   static void dmDesensitization() throws Exception {
      Map<String, Object> stringObjectMap = dmParseTest("select UE_MOBILEPHONENUMBER from ITAI.Bus");
      Map<String, Object> params = new HashMap();
      params.put("sql", (String)stringObjectMap.get("sql"));
      params.put("dbType", "dm");
      params.put("schema", "test");
      params.put("url", "jdbc:dm://192.168.2.109:5236/test");
      params.put("replaceSchema", ".*ITAI.*");
      params.put("replaceTable", ".*Bus.*");
      params.put("replaceColumn", "UE_MOBILEPHONENUMBER");
      params.put("expression", "substr(${value},0,3)||\\\\'********\\\\'");
      SqlUtil sqlUtil = new SqlUtil();
      String newsql = sqlUtil.desensitization(params);
      System.out.println(newsql);
   }

   static void dmFieldReplace() throws Exception {
      Map<String, Object> stringObjectMap = dmParseTest("SELECT * FROM sys_user LIMIT 0, 1000;");
      Map<String, Object> params = new HashMap();
      params.put("sql", (String)stringObjectMap.get("sql"));
      params.put("dbType", "dm");
      params.put("schema", "test");
      params.put("url", "jdbc:dm://192.168.2.232:5236/test");
      String filedfullname = "TEST.SYS_USER.user_name";
      String[] split = filedfullname.split("\\.");
      params.put("replaceSchema", split[0]);
      params.put("replaceTable", split[1]);
      params.put("replaceColumn", split[2]);
      params.put("expression", "concat(left(${value},1), '**') ");
      SqlUtil sqlUtil = new SqlUtil();
      String newsql = sqlUtil.column_desensitization(params);
      System.out.println(newsql);
   }

   static void dmTableReplace() throws Exception {
      String content = "{\n      \"ip_port\": \"192.168.2.232:5236\",\n      \"url\": \"jdbc:dm://192.168.2.232:5236/test\",\n      \"username\": \"SYSDBA\",\n      \"password\": \"SYSDBA\",\n      \"dbType\": \"dm\",\n      \"excludeSchema\": [\"information_schema\",\"dm\",\"performance_schema\",\"skywalking\",\"ghca\",\"ghca2\"]\n    }";
      JdbcUtil.initData(content);
      String[] sqls = new String[]{"insert into sys_user_1 select * from sys_user where del_flag=0"};

      for(int i = 0; i < sqls.length; ++i) {
         String sql = sqls[i];
         Map<String, String> params = new HashMap();
         params.put("sql", sql);
         params.put("dbType", "dm");
         params.put("schema", "test");
         params.put("url", "jdbc:dm://192.168.2.232:5236/test");
         params.put("replaceNewTable", "sys_user1111");
         params.put("replaceTable", "SYS_USER");
         SqlUtil sqlUtil = new SqlUtil();
         String newSql = sqlUtil.table_desensitization(params);
         System.out.println(newSql);
         System.out.println("---------------------------");
      }

   }

   static void dmParseRowVisitTest8() {
      String content = "{\n      \"ip_port\": \"192.168.2.5236:3306\",\n      \"url\": \"jdbc:dm://192.168.2.232:5236/test\",\n      \"username\": \"SYSDBA\",\n      \"password\": \"SYSDBA\",\n      \"dbType\": \"dm\",\n      \"excludeSchema\": [\"information_schema\",\"dm\",\"performance_schema\",\"skywalking\"]\n    }";
      JdbcUtil.initData(content);

      try {
         Thread.sleep(2000L);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }

      String tableName = "SYS_USER";
      String columnName = "user_name";
      String columnData = "若依1";
      Map<String, List<String>> modifyTable = new HashMap();
      List<String> valueList = new ArrayList();
      valueList.add(columnData);
      modifyTable.put(tableName, valueList);
      Map<String, String> tableKeyWord = new HashMap();
      tableKeyWord.put(tableName, columnName);
      String dbType = "dm";
      String[] sqls = new String[0];
      String[] operateTypes = new String[]{"select", "insert", "update", "delete", "select", "select", "select", "select", "select", "select", "replace", "replace"};

      for(int i = 0; i < sqls.length; ++i) {
         String sql = sqls[i];
         String operateType = operateTypes[i];
         Map<String, Object> param = new HashMap();
         param.put("sql", sql);
         param.put("url", "jdbc:dm://192.168.2.232:5236/test?");
         param.put("schema", "test");
         param.put("modifyTable", modifyTable);
         param.put("tableKeyWord", tableKeyWord);
         param.put("operateType", operateType);
         param.put("dbType", dbType);
         SqlUtil util = new SqlUtil();
         String newSQL = util.rowVisitControl(param);
         System.out.println(newSQL);
         System.out.println("---------------------------");
      }

   }

   static Map<String, Object> dmParseTest(String sql) throws Exception {
      String content = "{\n      \"ip_port\": \"192.168.2.228:5237\",\n      \"url\": \"jdbc:dm://192.168.2.228:5237/bBb\",\n      \"username\": \"SYSDBA\",\n      \"password\": \"123123123\",\n      \"dbType\": \"dm\",\n      \"excludeSchema\": [\"information_schema\",\"dm\",\"performance_schema\",\"skywalking\", \"ghca\",  \"ghca2\",  \"ghca31\",  \"ghca32\",  \"ghca-clear\",  \"ghca-datasecurity\",  \"ghca-staticmask\",  \"ghca_dynamic_zero_trust_jiami\",  \"ghca_dynamic_zero_trust_jiami1\",  \"ghca_old\",  \"ghca_static_release\",  \"jimitest1\",  \"mcms\",  \"performance_schema\",  \"ry\",  \"sanbao\",  \"shangqi\",  \"shuchang\",  \"static_target\",  \"sys\",  \"target\",  \"test0\",  \"test1\",  \"test2\",  \"test3\",  \"test4\",  \"test10\",  \"asrc-db\",  \"asrc-db1\"]\n    }";
      JdbcUtil.initData(content);

      try {
         Thread.sleep(2000L);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }

      Map<String, String> params = new HashMap();
      params.put("sql", sql);
      params.put("dbType", "dm");
      params.put("schema", "test");
      params.put("url", "jdbc:dm://192.168.2.228:5237/bBb");
      SqlUtil sqlUtil = new SqlUtil();
      Map<String, Object> stringObjectMap = sqlUtil.parseSqlStructureAnd(params);
      System.out.println(stringObjectMap);
      return stringObjectMap;
   }
}
