package nxm.database.lib;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

/** This class provides the ability to connect to an Oracle Database.
 *  @author Jay Hallam
 *  @since  NeXtMidas 2.1.0
 */
public class OracleDB extends NxmDB {
  /** The default database port */
  public static final int defaultPort = 1521;

  /** OracleDB Default Constructor */
  public OracleDB() {
    super();
    if (myPort == 0) myPort = defaultPort;
  }

  /** OracleDB Constructor
   *  @param db     name of database
   *  @param user   username to login to database
   *  @param passwd password to database
   */
  public OracleDB(String db, String user, String passwd) {
    super("localhost", defaultPort, db, user, passwd);
  }

  /** OracleDB Constructor
   *  @param host   hostname of the database server
   *  @param db     name of database
   *  @param user   username to login to database
   *  @param passwd password to database
   */
  public OracleDB(String host, String db, String user, String passwd) {
    super(host, defaultPort, db, user, passwd);
  }

  /** OracleDB Constructor
   *  @param host   hostname of the database server
   *  @param port   port the database is listening on
   *  @param db     name of database
   *  @param user   username to login to database
   *  @param passwd password to database
   */
  public OracleDB(String host, int port, String db, String user, String passwd) {
    super(host, port, db, user, passwd);
  }

  public void connect() throws DatabaseException {
    String conStr;
    try {
      myDriver = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").getConstructor().newInstance(); // updated in 3.9.0 to use constructor
      conStr = "jdbc:oracle:thin:@" + myHost + ":" + myPort + ":" + myName;
      myConnection = DriverManager.getConnection(conStr, myUserName, myUserPassword);

      if (verbose) {
        SQLWarning warn = myConnection.getWarnings();
        while (warn != null) {
          System.out.println("SQLState = " + warn.getSQLState());
          System.out.println("Message  = " + warn.getMessage());
          System.out.println("Vendor   = " + warn.getErrorCode());
          System.out.println("");
          warn = warn.getNextWarning();
        }
      }
    } catch (Exception e) {
      throw(new DatabaseException(e.getMessage()));
    }

  }

  public ResultSet getTables() {
    ResultSet rs;
    try {
      Statement stmt = myConnection.createStatement();
      rs = stmt.executeQuery("SELECT DISTINCT TABLE_NAME from USER_TAB_COLUMNS");
    } catch (SQLException e) {
      if (verbose) { System.out.println("getTables(): " + e);}
      rs = null;
    }
    return(rs);
  }

  public ResultSet describeTable(String t) {
    ResultSet rs;
    try {
      rs = myConnection.getMetaData().getColumns(null, null, t, null);
    } catch (SQLException e) {
      if (verbose) { System.out.println("getTables(): " + e);}
      rs = null;
    }
    return (rs);
  }

}