package com.chenyang.nse.bussiness.tools.jdbc;

import com.chenyang.nse.bussiness.tools.command.CommandTool;
import com.chenyang.nse.bussiness.tools.io.FileTool;
import java.io.File;
import java.util.Map;

public class RapidsdbLoadTableTool {
   public static void executeLoadTable(Map<String, String> map) {
      FileTool.clearFileContent(File.separator + "data" + File.separator + "infa_mysqlload" + File.separator + (String)map.get("taskname") + File.separator + (String)map.get("tablename") + ".sh");
      FileTool.createFile(File.separator + "data" + File.separator + "infa_mysqlload" + File.separator + (String)map.get("taskname") + File.separator + (String)map.get("tablename") + ".sh");
      String command = "";
      if ("".equals(map.get("password"))) {
         command = "mysql -u" + (String)map.get("username") + " -h" + (String)map.get("hostname") + " --local_infile=1 -e \"LOAD DATA LOCAL INFILE '" + (String)map.get("filepath") + "' INTO TABLE " + (String)map.get("database") + "." + (String)map.get("tablename") + " FIELDS TERMINATED BY '" + (String)map.get("delimiter") + "' LINES TERMINATED BY  '\\n'\";";
      } else {
         command = "mysql -u" + (String)map.get("username") + " -p" + (String)map.get("password") + " -h" + (String)map.get("hostname") + " --local_infile=1 -e \"LOAD DATA LOCAL INFILE '" + (String)map.get("filepath") + "' INTO TABLE " + (String)map.get("database") + "." + (String)map.get("tablename") + " FIELDS TERMINATED BY '" + (String)map.get("delimiter") + "' LINES TERMINATED BY  '\\n'\";";
      }

      FileTool.writeContentToFile(File.separator + "data" + File.separator + "infa_mysqlload" + File.separator + (String)map.get("taskname") + File.separator + (String)map.get("tablename") + ".sh", command);
      CommandTool.command("chmod 777 " + File.separator + "data" + File.separator + "infa_mysqlload" + File.separator + (String)map.get("taskname") + File.separator + (String)map.get("tablename") + ".sh");
   }

   public static void executeLoadTable_new(Map<String, String> map) {
      FileTool.clearFileContent((String)map.get("path") + (String)map.get("tablename") + ".sh");
      FileTool.createFile((String)map.get("path") + (String)map.get("tablename") + ".sh");
      String command = "";
      if ("".equals(map.get("password"))) {
         command = "mysql -u" + (String)map.get("username") + " -h" + (String)map.get("hostname") + " --local_infile=1 -e \"LOAD DATA LOCAL INFILE '" + (String)map.get("filepath") + "' INTO TABLE " + (String)map.get("database") + "." + (String)map.get("tablename") + " FIELDS TERMINATED BY '" + (String)map.get("delimiter") + "' LINES TERMINATED BY  '\\n'\n" + (String)map.get("formatSet") + "\"";
      } else {
         command = "mysql -u" + (String)map.get("username") + " -p" + (String)map.get("password") + " -h" + (String)map.get("hostname") + " --local_infile=1 -e \"LOAD DATA LOCAL INFILE '" + (String)map.get("filepath") + "' INTO TABLE " + (String)map.get("database") + "." + (String)map.get("tablename") + " FIELDS TERMINATED BY '" + (String)map.get("delimiter") + "' LINES TERMINATED BY  '\\n'\n" + (String)map.get("formatSet") + "\"";
      }

      FileTool.writeContentToFile((String)map.get("path") + (String)map.get("tablename") + ".sh", command);
      CommandTool.command("chmod 777 " + (String)map.get("path") + (String)map.get("tablename") + ".sh");
   }

   public static void executeExportFileTable(Map<String, String> map) {
      FileTool.clearFileContent((String)map.get("path") + (String)map.get("tablename") + ".sh");
      FileTool.createFile((String)map.get("path") + (String)map.get("tablename") + ".sh");
      String command = "";
      if ("".equals(map.get("password"))) {
         command = "mysql -u" + (String)map.get("username") + " -h" + (String)map.get("hostname") + " -e 'SELECT " + (String)map.get("columnNameList") + " FROM " + (String)map.get("database") + "." + (String)map.get("tablename") + "' > " + (String)map.get("filepath") + ";";
      } else {
         command = "mysql -u" + (String)map.get("username") + " -p" + (String)map.get("password") + " -h" + (String)map.get("hostname") + " -e 'SELECT " + (String)map.get("columnNameList") + " FROM " + (String)map.get("database") + "." + (String)map.get("tablename") + "' > " + (String)map.get("filepath") + ";";
      }

      FileTool.writeContentToFile((String)map.get("path") + (String)map.get("tablename") + ".sh", command);
      CommandTool.command("chmod 777 " + (String)map.get("path") + (String)map.get("tablename") + ".sh");
   }
}
