All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nocrala.tools.database.sentinel.plugin.ShowDatabaseOperation Maven / Gradle / Ivy

The newest version!
package org.nocrala.tools.database.sentinel.plugin;

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

import org.nocrala.tools.database.sentinel.BuildInformation;
import org.nocrala.tools.database.sentinel.Snapshot;
import org.nocrala.tools.database.sentinel.SnapshotRenderer;
import org.nocrala.tools.database.sentinel.util.XUtil;
import org.nocrala.tools.database.tartarus.exception.ReaderException;
import org.nocrala.tools.database.tartarus.utils.JdbcUtil;

public class ShowDatabaseOperation {

  private String alias = null;
  private String driverclass = null;
  private String url = null;
  private String username = null;
  private String password = null;
  private String catalog = null;
  private String schema = null;

  public ShowDatabaseOperation(final String alias, final String driverclass, final String url, final String username,
      final String password, final String catalog, final String schema) {
    this.alias = alias;
    this.driverclass = driverclass;
    this.url = url;
    this.username = username;
    this.password = password;
    this.catalog = catalog;
    this.schema = schema;
  }

  public void execute(final Feedback feedback) throws Exception {

    feedback.info("Sentinel " + BuildInformation.VERSION + " - build " + BuildInformation.BUILD_ID);

    this.catalog = this.catalog == null || this.catalog.isEmpty() ? null : this.catalog;
    this.schema = this.schema == null || this.schema.isEmpty() ? null : this.schema;

    feedback.info("Inspecting live database at: " + this.url);

    Connection conn = null;
    try {

      // 1. Connect to database

      try {
        conn = JdbcUtil.buildStandAloneConnection(this.driverclass, this.url, this.username, this.password, null);
      } catch (ClassNotFoundException | SQLException e) {
        throw new Exception("Could not connect to database: " + XUtil.renderThrowable(e));
      }

      // 2. Take the snapshot

      Snapshot s;
      try {
        s = Snapshot.takeSnapshot(conn, this.catalog, this.schema, this.alias);
      } catch (ReaderException | SQLException e) {
        throw new Exception("Could not take database snapshot: " + XUtil.renderThrowable(e));
      }

      // 3. Display snapshot

      SnapshotRenderer r = new SnapshotRenderer();
      feedback.info(" ");
      s.renderHeader().stream().forEach(l -> feedback.info(l));
      r.render(s).stream().forEach(l -> feedback.info(l));
      feedback.info(" ");

    } finally {

      // 5. Close the database connection

      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException e) {
          throw new Exception("Database error: " + XUtil.renderThrowable(e));
        }
      }

    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy