package util.sqlparse.visitor.kingbase.test;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import util.JdbcUtil;
import util.SqlUtil;

public class DesensitizationTest {
   public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
      kingbaseParseTest("SELECT \"c1\", \"c2\", \"cc3\", \"c3\", \"Cc3\" FROM \"Test\".\"Table_name\"");
   }

   static void kingbaseDesensitization() {
      Map<String, Object> stringObjectMap = kingbaseParseTest("SELECT \"c1\", \"c2\", \"cc3\", \"c3\", \"Cc3\" FROM \"Test\".\"Table_name\"");
      Map<String, Object> params = new HashMap();
      params.put("sql", (String)stringObjectMap.get("sql"));
      params.put("dbType", "kingbase");
      params.put("schema", "Test");
      params.put("url", "jdbc:kingbase8://192.168.2.166:54321/aaa");
      params.put("replaceSchema", ".*");
      params.put("replaceTable", ".*");
      params.put("replaceColumn", ".*c1.*");
      params.put("expression", "concat(left(${value},1), '**') ");
      SqlUtil sqlUtil = new SqlUtil();
      String newsql = null;

      try {
         newsql = sqlUtil.desensitization(params);
      } catch (Exception e) {
         e.printStackTrace();
      }

      System.out.println(newsql);
   }

   static void kingbaseFieldReplace() {
      Map<String, Object> stringObjectMap = kingbaseParseTest("SELECT \"c1\", \"c2\", \"cc3\", \"c3\", \"Cc3\" FROM \"Test\".\"Table_name\"");
      Map<String, Object> params = new HashMap();
      params.put("sql", (String)stringObjectMap.get("sql"));
      params.put("dbType", "kingbase");
      params.put("schema", "Test");
      params.put("url", "jdbc:kingbase8://192.168.2.166:54321/aaa");
      String filedfullname = "Test.Table_name.c1";
      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 = null;

      try {
         newsql = sqlUtil.column_desensitization(params);
      } catch (Exception e) {
         e.printStackTrace();
      }

      System.out.println(newsql);
   }

   static void kingbaseTableReplace() {
      Map<String, Object> stringObjectMap = kingbaseParseTest("SELECT \"c1\", \"c2\", \"cc3\", \"c3\", \"Cc3\" FROM \"Test\".\"table_name\"");
      String sql = (String)stringObjectMap.get("sql");
      Map<String, String> params = new HashMap();
      params.put("sql", sql);
      params.put("dbType", "kingbase");
      params.put("schema", "Test");
      params.put("url", "jdbc:kingbase8://192.168.2.166:54321/aaa");
      params.put("replaceNewTable", "\"Table_name\"");
      params.put("replaceTable", "\"table_name\"");
      SqlUtil sqlUtil = new SqlUtil();
      String newSql = null;

      try {
         newSql = sqlUtil.table_desensitization(params);
      } catch (Exception e) {
         e.printStackTrace();
      }

      System.out.println(newSql);
      System.out.println("---------------------------");
   }

   static void kingbaseParseRowVisitTest8() {
      Map<String, Object> stringObjectMap = kingbaseParseTest("select \"aaA\" from aaa.\"Test\".\"Table_name\"\n");
      String tableName = "\"Table_name\"";
      String columnName = "aaA";
      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 = "kingbase";
      String sql = (String)stringObjectMap.get("sql");
      String operateType = "select";
      Map<String, Object> param = new HashMap();
      param.put("sql", sql);
      param.put("url", "jdbc:kingbase8://192.168.2.166:54321/aaa");
      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 void kingbaseParseRowVisitTest9() {
      Map<String, Object> stringObjectMap = kingbaseParseTest("SELECT \"test\".aaA AS aaA, \"test\".aaa AS aaa, \"test\".name AS name\nFROM \"test\".\"test\"");
      String tableName = "test";
      String columnName = "aaA";
      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 = "kingbase";
      String sql = (String)stringObjectMap.get("sql");
      String operateType = "select";
      Map<String, Object> param = new HashMap();
      param.put("sql", sql);
      param.put("url", "jdbc:kingbase8://192.168.2.166:54321/aaa");
      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> kingbaseParseTest(String sql) {
      String content = "{\n      \"ip_port\": \"192.168.2.109:54321\",\n      \"url\": \"jdbc:kingbase8://192.168.2.109:54321/TEST\",\n      \"username\": \"system\",\n      \"password\": \"123456\",\n      \"dbType\": \"kingbase\",\n      \"isIgnorecase\": \"0\",\n      \"excludeSchema\": []\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", "kingbase");
      params.put("schema", "TEST");
      params.put("url", "jdbc:kingbase8://192.168.2.109:54321/TEST");
      SqlUtil sqlUtil = new SqlUtil();
      Map<String, Object> stringObjectMap = null;

      try {
         stringObjectMap = sqlUtil.parseSqlStructureAnd(params);
      } catch (Exception e) {
         e.printStackTrace();
      }

      System.out.println(stringObjectMap);
      return stringObjectMap;
   }
}
