package com.capgemini.framework.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Define the migration database connection.
 **/
class DbConnection {
  private static final Logger log = LoggerFactory.getLogger(DbConnection.class);
  private Connection connection;

  /**
   * Connect directly to the database by specifying user and password.
   *
   * @param anUrl     Database URL.
   * @param anUser    User required for authentication.
   * @param aPassword Password required for authentication.
   **/
  DbConnection(final String anUrl, final String anUser, final String aPassword) {
    try {
      /* Load driver */
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

      /* Connect to database */
      connection = DriverManager.getConnection(anUrl, anUser, aPassword);

      /* By default enabled auto commit */
      connection.setAutoCommit(true);
    }
    catch (SQLException e) {
      log.error("Unable to prepare connection", e);
    }
  }

  /**
   * Getter for connection attribute.
   *
   * @return Active connection.
   */
  Connection getConnection() {
    return connection;
  }
}
