package com.palacesun.masking.tools;

import bean.Column;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class JdbcUtil {
   public static boolean init = false;

   public static Connection getConnection(String url, String username, String password) {
      Connection connection = null;

      try {
         connection = DriverManager.getConnection(url, username, password);
      } catch (Exception throwables) {
         throwables.printStackTrace();
      }

      return connection;
   }

   public static Connection getDmConnection(String url, String username, String password) {
      Connection connection = null;

      try {
         String driver = "dm.jdbc.driver.DmDriver";
         Class.forName(driver);
         connection = DriverManager.getConnection(url, username, password);
      } catch (Exception throwables) {
         throwables.printStackTrace();
      }

      return connection;
   }

   public static void closeConnection(Connection connection) {
      if (connection != null) {
         try {
            connection.close();
         } catch (SQLException throwables) {
            throwables.printStackTrace();
         }
      }

   }

   public static List<Column> getColumnOfMysql(Statement statement, String schema, String table) throws SQLException {
      List<Column> columns = new ArrayList();
      String sql = "select COLUMN_NAME,DATA_TYPE from information_schema.columns where TABLE_SCHEMA = '" + schema + "' and TABLE_NAME = '" + table + "'";
      ResultSet resultSet = statement.executeQuery(sql);

      while(resultSet.next()) {
         columns.add(new Column(resultSet.getString("COLUMN_NAME"), schema, table, resultSet.getString("DATA_TYPE")));
      }

      resultSet.close();
      return columns;
   }

   public static List<Column> getColumnOfOracle(Statement statement, String schema, String table) throws SQLException {
      List<Column> columnList = new ArrayList();
      String sql = "SELECT COLUMN_NAME,DATA_TYPE FROM  dba_tab_columns WHERE TABLE_NAME  = '" + table + "' and OWNER  = '" + schema + "'";
      ResultSet resultSet = statement.executeQuery(sql);

      while(resultSet.next()) {
         columnList.add(new Column(resultSet.getString("COLUMN_NAME"), schema, table, resultSet.getString("DATA_TYPE")));
      }

      resultSet.close();
      return columnList;
   }

   public static List<Column> getColumnOfPostgresql(Statement statement, String schema, String table) throws SQLException {
      List<Column> columns = new ArrayList();
      String sql = "select COLUMN_NAME,DATA_TYPE from information_schema.columns where TABLE_SCHEMA = '" + schema + "' and TABLE_NAME = '" + table + "'";
      ResultSet resultSet = statement.executeQuery(sql);

      while(resultSet.next()) {
         columns.add(new Column(resultSet.getString("COLUMN_NAME"), schema, table, resultSet.getString("DATA_TYPE")));
      }

      resultSet.close();
      return columns;
   }

   public static List<Column> getColumnOfHive(Statement statement, String schema, String table) throws SQLException {
      List<Column> columns = new ArrayList();
      String sql = "desc " + schema + "." + table;
      ResultSet resultSet = statement.executeQuery(sql);

      while(resultSet.next()) {
         columns.add(new Column(resultSet.getString("COL_NAME"), schema, table, resultSet.getString("DATA_TYPE")));
      }

      resultSet.close();
      return columns;
   }

   public static List<Column> getColumnOfDm(Statement statement, String schema, String table) throws SQLException {
      List<Column> columnList = new ArrayList();
      String sql = "select column_name,data_type from all_tab_columns where owner='" + schema + "' and Table_Name='" + table + "'";
      ResultSet resultSet = statement.executeQuery(sql);

      while(resultSet.next()) {
         columnList.add(new Column(resultSet.getString("COLUMN_NAME"), schema, table, resultSet.getString("DATA_TYPE")));
      }

      resultSet.close();
      return columnList;
   }

   public static List<Column> getColumnOfDb2(Statement statement, String schema, String table) throws SQLException {
      List<Column> columns = new ArrayList();
      String sql = "SELECT COLNAME,TYPENAME from syscat.columns where tabname='" + table + "' and tabschema='" + schema + "'";
      ResultSet resultSet = statement.executeQuery(sql);

      while(resultSet.next()) {
         columns.add(new Column(resultSet.getString("COLNAME"), schema, table, resultSet.getString("TYPENAME")));
      }

      resultSet.close();
      return columns;
   }

   public static List<Column> getColumnOfSqlserver(Statement statement, String schema, String table) throws SQLException {
      List<Column> columns = new ArrayList();
      String sql = "select * from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = '" + schema + "' and TABLE_NAME='" + table + "'";
      ResultSet resultSet = statement.executeQuery(sql);

      while(resultSet.next()) {
         columns.add(new Column(resultSet.getString("COLUMN_NAME"), schema, table, resultSet.getString("DATA_TYPE")));
      }

      resultSet.close();
      return columns;
   }

   public static List<Column> getColumnOfKingbase(Statement statement, String schema, String table) throws SQLException {
      List<Column> columns = new ArrayList();
      String sql = "select COLUMN_NAME,DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = '" + schema + "' and TABLE_NAME='" + table + "'";
      ResultSet resultSet = statement.executeQuery(sql);

      while(resultSet.next()) {
         columns.add(new Column(resultSet.getString("COLUMN_NAME"), schema, table, resultSet.getString("DATA_TYPE")));
      }

      resultSet.close();
      return columns;
   }
}
