Example #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;
 }
Example #2
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;
  }