package net.northking.cctp.maven.analyse.util;

import net.northking.cctp.maven.analyse.JDBCConfig;
import net.northking.cctp.maven.analyse.Messages;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库访问的简易工具类
 *
 * @author 谢秀松
 * @since 1.0
 */
public final class DBUtil
{
  /**
   * 单例对象
   */
  private static DBUtil instance = new DBUtil();

  /**
   * 隐藏构造函数
   */
  private DBUtil()
  {
  }

  /**
   * 获取数据库访问实例，单例模式
   *
   * @return 数据库访问实例
   */
  public static DBUtil getInstance()
  {
    return instance;
  }

  /**
   * JDBC数据库连接
   */
  private Connection connection;

  /**
   * 获取JDBC数据库连接
   *
   * @return JDBC数据库连接
   * @throws ClassNotFoundException 数据库驱动类异常
   * @throws SQLException           数据库异常
   * @throws InstantiationException InstantiationException
   * @throws IllegalAccessException IllegalAccessException
   */
  public Connection getConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException
  {
    return this.createConn(JDBCConfig.getInstance());
  }

  /**
   * 创建JDBC数据库连接
   *
   * @param jdbcConfig JDBC配置
   * @return JDBC数据库连接
   * @throws ClassNotFoundException 数据库驱动类异常
   * @throws SQLException           数据库异常
   * @throws InstantiationException InstantiationException
   * @throws IllegalAccessException IllegalAccessException
   */
  private synchronized Connection createConn(final JDBCConfig jdbcConfig)
      throws SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException
  {
    if (connection != null)
    {
      return connection;
    }
    Driver driver = getDriver(jdbcConfig.getDriverClass());

    Properties props = new Properties();

    if (StringUtil.hasValue(jdbcConfig.getUserId()))
    {
      props.setProperty("user", jdbcConfig.getUserId());
    }

    if (StringUtil.hasValue(jdbcConfig.getPassword()))
    {
      props.setProperty("password", jdbcConfig.getPassword());
    }

    props.put("remarksReporting", "true");

    connection = driver.connect(jdbcConfig.getUrl(), props);

    if (connection == null)
    {
      throw new SQLException(Messages.getMessage("Error.CreateConn", jdbcConfig.toString())); //$NON-NLS-1$
    }
    Messages.info("Print.OpenConn", jdbcConfig.getUrl());
    return connection;
  }

  /**
   * 获取JDBC驱动实例
   *
   * @param driverClass JDBC驱动
   * @return JDBC驱动实例
   * @throws ClassNotFoundException 数据库驱动类异常
   * @throws InstantiationException InstantiationException
   * @throws IllegalAccessException IllegalAccessException
   */
  private Driver getDriver(final String driverClass) throws ClassNotFoundException, IllegalAccessException, InstantiationException
  {
    Class<?> clazz = Class.forName(driverClass);
    return (Driver) clazz.newInstance();
  }

  /**
   * 关闭JDBC连接
   */
  public void close()
  {
    try
    {
      if (connection != null && !connection.isClosed())
      {
        connection.close();
        Messages.info("Print.CloseConn", JDBCConfig.getInstance().getUrl());
      }
    } catch (SQLException e)
    {
      Messages.error("Error.CloseConn");
    }
  }

}
