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 a PostgreSQL Database.
 *  @author Jay Hallam
 *  @since  NeXtMidas 2.1.0
 */
public class PostgreSQLDB extends NxmDB {
  /** The default database port */
  public static final int defaultPort = 5432;

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

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

  /** PostgreSQLDB 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 PostgreSQLDB(String host, String db, String user, String passwd) {
    super(host, defaultPort, db, user, passwd);
  }

  /** PostgreSQLDB 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 PostgreSQLDB(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("org.postgresql.Driver").getConstructor().newInstance(); // updated in 3.9.0 to use constructor
      conStr = "jdbc:postgresql://" + myHost + ":" + myPort + "/" + myName;
      if (verbose) {
        System.out.println("Connection: " + conStr);
        System.out.println("For User: " + myUserName);
      }
      myUserPassword = "";
      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();
      String qstr = "select relname from pg_class where ";
      qstr += "relkind like 'r' and relname !~ '^pg_'";
      rs = stmt.executeQuery(qstr);
    } catch (SQLException e) {
      if (verbose) { System.out.println("getTables(): " + e);}
      rs = null;
    }
    return(rs);
  }
}