示例#1
0
 private Connection connect(String driver, String database, String user, String password)
     throws ClassNotFoundException, SQLException {
   Connection conn = null;
   if (driver == null) {
     driver = JDBC.getDriverString(database);
   }
   JDBC.loadDriver(driver);
   conn = DriverManager.getConnection(database, user, password);
   // mysql complains if autocomit is true and you try to commit...
   // ddl operations are not transactional anyhow.
   conn.setAutoCommit(false);
   return conn;
 }
示例#2
0
  protected int getDbType() {
    int iResult = -1;

    try {
      iResult = JDBC.toType(this.getConn().getMetaData().getURL());
    } catch (SQLException e) {
    }

    return iResult;
  }
示例#3
0
  public boolean validate(String file, String database, String username, String password)
      throws ClassNotFoundException, IOException, SQLException {
    try {
      Document doc = this.readDocument(file);

      // //////////////////////////////////////////////////////
      // Make sure we can connect to the database
      m_conn = connect(database, username, password);
      DatabaseMetaData meta = m_conn.getMetaData();

      Iterator iterTab =
          Table.getTables(doc.getFirstChild(), JDBC.toType(database), this).iterator();

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

        try {
          ResultSet set = meta.getTables(null, null, tab.getName(), null);
          if (set.next() == true) {
            if (this.isQuiet() == false) System.out.println("Found Table : " + tab.getName());

            // Check the Columns
            Iterator iterCol = tab.getColumns().iterator();

            while (iterCol.hasNext() == true) {
              Column col = (Column) iterCol.next();

              set = meta.getColumns(null, null, tab.getName(), col.getName());
              if (set.next() == true) {
                if (this.isQuiet() == false) System.out.println("Found Column: " + col.getName());
              }
            }

            this.m_lCreatedTables++;
          }
        } catch (SQLException e) {
          if (this.isQuiet() == false) {
            System.out.println("Error: " + "Cannot find 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 validated.");
        System.out.println(fmt.format(this.m_lFailedTables) + " tables failed to validate.");
        System.out.println(fmt.format(this.m_lCreatedIndexes) + " indexes validated.");
        System.out.println(fmt.format(this.m_lFailedIndexes) + " indexes failed to validate.");
      }
    } catch (SAXException e) {
      throw new IOException(e.getMessage());
    } finally {
      try {
        if (m_conn != null) m_conn.close();
      } catch (SQLException e) {
      }
    }

    return true;
  }
示例#4
0
  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;
  }
示例#5
0
  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;
  }
示例#6
0
  protected boolean doSetup(
      Object source,
      String database,
      String username,
      String password,
      String table,
      boolean doDelete)
      throws ClassNotFoundException, IOException, SQLException {
    try {
      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

      JDBC.loadDriver(database);
      if (this.isQuiet() == false) System.out.println("Successfully loaded the database driver.");

      m_conn = connect(database, username, password);
      if (this.isQuiet() == false) System.out.println("Successfully connected to the database.");

      Collection collTypeMaps = null;
      if (m_typeMapFile != null) {
        collTypeMaps = TypeMap.readTypeMaps(readDocument(m_typeMapFile).getFirstChild());
      } else if (m_typeMapStream != null) {
        collTypeMaps = TypeMap.readTypeMaps(readDocument(m_typeMapStream).getFirstChild());
      } else {
        collTypeMaps = TypeMap.readTypeMaps(doc.getFirstChild());
      }

      /**
       * // check to see if we're dealing with a view or a table Node dbSetupNode =
       * doc.getFirstChild(); // actually, its the first child of the main node in the schema
       *
       * <p>NodeList nodeList = dbSetupNode.getChildNodes(); for(int i=0;i < nodeList.getLength();
       * i++) { Node rootNode = nodeList.item(i);
       * if(rootNode.getNodeName().equalsIgnoreCase("table")) {
       */
      Node rootNode = doc.getFirstChild();
      Iterator iterTab = Table.getTables(rootNode, JDBC.toType(database), this).iterator();

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

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

        if (table != null) {
          if (!table.equals(tab.getName())) {
            continue;
          }
          if (doDelete) tab.clear();
        }

        if (this.isVerbose() == true) System.out.println("Processing Table: " + tab.getName());

        if (m_bDMLonly == false) {
          // ////////////////////////////////////////////////////////////
          // Create the Table
          try {
            // Only attempt to create the table if the table tag has columns

            if (tab.getColumns().size() > 0) {
              tab.create(collTypeMaps);

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

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

            this.m_lFailedTables++;
          }

          // /////////////////////////////////////////////////////////////
          // Create the Indexes

          Iterator iterCol = tab.getIndexes().iterator();

          while (iterCol.hasNext() == true) {
            Index idx = (Index) iterCol.next();

            try {
              idx.create();

              if (this.isQuiet() == false)
                System.out.println(
                    "Created Index: "
                        + idx.getName()
                        + " for Table \'"
                        + idx.getTable().getName()
                        + '\'');

              this.m_lCreatedIndexes++;
            } catch (SQLException e) {
              if (this.isQuiet() == false) {
                System.out.println("Error: Cannot create index " + idx.getName());
                JDBC.printSQLException(e);
              }

              this.m_lFailedIndexes++;
            }
          }
        }

        // ////////////////////////////////////////////////////////////
        // Create the Data

        DataSet dataset = tab.getDataSet();

        try {
          int iRowCnt = dataset.create();

          if (this.isQuiet() == false) System.out.println("Created " + iRowCnt + " Row(s).");
        } catch (SQLException e) {
          if (this.isQuiet() == false) {
            System.out.println("Error: " + "Cannot create Row.");
            JDBC.printSQLException(e);

            if (this.isVerbose() == true) e.printStackTrace();
          }
        }
      }
      // }

      // if(rootNode.getNodeName().equalsIgnoreCase("view")) {
      // process views
      Iterator iterView = View.getViews(rootNode, JDBC.toType(database), this).iterator();
      // //////////////////////////////////////////////////
      // /////////////
      // Open the log file if we are supposed to log SQL statements.
      if (m_logFileName != null) {
        m_log = new FileWriter(m_logFileName, m_logAppend);
      }

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

        if (this.isVerbose() == true) System.out.println("Processing view: " + view.getName());

        if (m_bDMLonly == false) {
          // //////////////////////////////////////////
          // ////////////////
          // Create the View
          try {
            view.create(collTypeMaps);

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

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

              if (this.isVerbose() == true) e.printStackTrace();
            }
          }
        }
      }
      // }
      // }

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

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

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

      try {
        if (m_conn != null) m_conn.close();
      } catch (SQLException e) {
      }
    }

    return true;
  }