コード例 #1
0
ファイル: DBSetup.java プロジェクト: vmware/ep-ops-management
  protected boolean doUninstall(Object source, String database, String username, String password)
      throws ClassNotFoundException, IOException, SQLException {
    boolean bProceed = false;
    Statement stmt = null;

    try {
      // /////////////////////////////////////////////////////////////////
      // Open the log file if we are supposed to log SQL statements.
      if (m_logFileName != null) {
        m_log = new FileWriter(m_logFileName, m_logAppend);
      }

      Document doc = null;
      if (source instanceof String) {
        doc = this.readDocument((String) source);

      } else if (source instanceof InputStream) {
        doc = this.readDocument((InputStream) source);
      }

      // //////////////////////////////////////////////////////
      // Make sure we can connect to the database
      m_conn = connect(database, username, password);
      stmt = m_conn.createStatement();

      // //////////////////////////////////////////////////////
      // Verify that the user wants to do this.
      if (this.isQuiet() == false && (!this.isNoninteractive())) {
        System.out.print(
            "DBSetup will permanently drop tables and data. DO YOU WANT TO PROCEED? (Y/N): ");

        if (Character.toUpperCase((char) System.in.read()) == 'Y') {
          System.out.println();
          bProceed = true;
        } else System.out.println("Aborted.");
      } else bProceed = true; // If they go quiet, delete without asking.

      if (bProceed == true) {
        // drop the views first
        StrongCollection views =
            (StrongCollection) View.getViews(doc.getFirstChild(), JDBC.toType(database), this);

        Iterator iterTab = views.iterator();

        while (iterTab.hasNext() == true) {
          View view = (View) iterTab.next();

          try {
            view.drop();

            if (this.isQuiet() == false) System.out.println("Dropped View: " + view.getName());

            this.m_lCreatedViews++;
          } catch (SQLException e) {
            if (this.isQuiet() == false) {
              System.out.println("Error: Cannot drop view " + view.getName());
              JDBC.printSQLException(e);
            }

            this.m_lFailedViews++;
          }
        }

        // Post Processing
        View.uninstallCleanup(this);

        // remove tables in reverse order of their creation
        // to bypass dependency constraints
        StrongCollection tables =
            (StrongCollection) Table.getTables(doc.getFirstChild(), JDBC.toType(database), this);
        tables.reverse();

        iterTab = tables.iterator();

        while (iterTab.hasNext() == true) {
          Table tab = (Table) iterTab.next();

          try {
            tab.drop();

            if (this.isQuiet() == false) System.out.println("Dropped Table: " + tab.getName());

            this.m_lCreatedTables++;
          } catch (SQLException e) {
            if (this.isQuiet() == false) {
              System.out.println("Error: " + "Cannot drop table " + tab.getName());
              JDBC.printSQLException(e);
            }

            this.m_lFailedTables++;
          }
        }

        // Post Processing
        Table.uninstallCleanup(this);
      }

      // ////////////////////////////////////////////////////////////
      // Print Results

      if (this.isQuiet() == false) {
        DecimalFormat fmt = new DecimalFormat("#,###");

        System.out.println();
        System.out.println(fmt.format(this.m_lCreatedViews) + " views dropped succesfully.");
        System.out.println(fmt.format(this.m_lFailedViews) + " views failed to drop.");
        System.out.println(fmt.format(this.m_lCreatedTables) + " tables dropped succesfully.");
        System.out.println(fmt.format(this.m_lFailedTables) + " tables failed to drop.");
        // System.out.println(fmt.format(this.m_lCreatedIndexes) + " indexes dropped
        // successfully.");
        // System.out.println(fmt.format(this.m_lFailedIndexes) + " indexes failed to drop.");
      }
    } catch (SAXException e) {
      throw new IOException(e.getMessage());
    } finally {
      if (m_log != null) {
        try {
          m_log.close();
        } catch (Exception e) {
        }
      }
      try {
        if (stmt != null) stmt.close();
      } catch (SQLException e) {
      }
      try {
        if (m_conn != null) m_conn.close();
      } catch (SQLException e) {
      }
    }

    return true;
  }
コード例 #2
0
ファイル: DBSetup.java プロジェクト: vmware/ep-ops-management
  public boolean clear(String file, String database, String username, String password, String table)
      throws ClassNotFoundException, IOException, SQLException {
    boolean bProceed = false;
    Statement stmt = null;

    try {
      Document doc = this.readDocument(file);

      // //////////////////////////////////////////////////////
      // Make sure we can connect to the database
      m_conn = connect(database, username, password);
      stmt = m_conn.createStatement();

      // //////////////////////////////////////////////////////
      // Verify that the user wants to do this.
      if (this.isQuiet() == false && (!this.isNoninteractive())) {
        System.out.print(
            "DBSetup will permanently delete data. " + "DO YOU WANT TO PROCEED? (Y/N): ");

        if (Character.toUpperCase((char) System.in.read()) == 'Y') {
          System.out.println();
          bProceed = true;
        } else System.out.println("Aborted.");
      } else bProceed = true; // If they go quiet, delete without asking.

      if (bProceed == true) {
        // remove data from tables in reverse order of their creation
        // to bypass dependency constraints
        StrongCollection tables =
            (StrongCollection) Table.getTables(doc.getFirstChild(), JDBC.toType(database), this);
        tables.reverse();

        Iterator iterTab = tables.iterator();

        while (iterTab.hasNext() == true) {
          Table tab = (Table) iterTab.next();

          if (table != null && tab.getName().compareToIgnoreCase(table) != 0) continue;

          try {
            tab.clear();

            if (this.isQuiet() == false) System.out.println("Clear Table: " + tab.getName());

            this.m_lCreatedTables++;
          } catch (SQLException e) {
            if (this.isQuiet() == false) {
              System.out.println("Error: Cannot clear table " + tab.getName());
              JDBC.printSQLException(e);
            }

            this.m_lFailedTables++;
          }
        }
      }

      // ////////////////////////////////////////////////////////////
      // Print Results

      if (this.isQuiet() == false) {
        DecimalFormat fmt = new DecimalFormat("#,###");

        System.out.println();
        System.out.println(fmt.format(this.m_lCreatedTables) + " tables cleared succesfully.");
        System.out.println(fmt.format(this.m_lFailedTables) + " tables failed to clear.");
      }
    } catch (SAXException e) {
      throw new IOException(e.getMessage());
    } finally {
      try {
        if (stmt != null) stmt.close();
      } catch (SQLException e) {
      }
      try {
        if (m_conn != null) m_conn.close();
      } catch (SQLException e) {
      }
    }

    return true;
  }