Example #1
0
    public void load(ISession session, PreparedStatement stmt) {
      ResultSet rs = null;
      try {
        rs = stmt.executeQuery();
        StringBuffer buf = new StringBuffer(4096);
        while (rs.next()) {
          String line1 = rs.getString(1);
          String line2 = rs.getString(2);
          buf.append(line1.trim() + " ");
          buf.append(line2.trim() + " ");
        }
        String source = "";
        if (buf.length() == 0 && sourceType == TABLE_TYPE) {
          ISQLDatabaseMetaData md = session.getMetaData();
          // TODO: Need to define a better approach to getting dialects.
          // That is, we don't really want to ever prompt the user in this
          // case.  It's always Oracle.  Yet, we may have a new OracleDialect
          // at some point.
          HibernateDialect dialect = DialectFactory.getDialect("Oracle");

          // TODO: How to let the user customize this??
          CreateScriptPreferences prefs = new CreateScriptPreferences();

          ITableInfo[] tabs = new ITableInfo[] {(ITableInfo) getDatabaseObjectInfo()};
          List<ITableInfo> tables = Arrays.asList(tabs);
          // Handle table source
          List<String> sqls = dialect.getCreateTableSQL(tables, md, prefs, false);
          String sep = session.getQueryTokenizer().getSQLStatementSeparator();
          for (String sql : sqls) {
            buf.append(sql);
            buf.append(sep);
            buf.append("\n");
          }
          source = buf.toString();
        } else {
          if (s_log.isDebugEnabled()) {
            s_log.debug("View source before formatting: " + buf.toString());
          }
          source = formatter.reformat(buf.toString());
        }
        getTextArea().setText(source);
        getTextArea().setCaretPosition(0);
      } catch (SQLException ex) {
        s_log.error("Unexpected exception: " + ex.getMessage(), ex);
        session.showErrorMessage(ex);
      } finally {
        SQLUtilities.closeResultSet(rs);
      }
    }
 /**
  * Reads the specified ResultSet of all of it's rows and closes it.
  *
  * @param rs the ResultSet to read from.
  * @throws DataSetException
  */
 private void setResultSet(ResultSet rs) throws DataSetException {
   if (rs == null) {
     return;
   }
   try {
     while (rs.next()) {
       Object[] row = getNextRow(rs);
       _allData.add(row);
     }
   } catch (SQLException e) {
     throw new DataSetException(e);
   } finally {
     SQLUtilities.closeResultSet(rs);
   }
 }
  /**
   * Counts the number of affected rows, using this where clause.
   *
   * @param whereClauseParts where clause to use
   * @param conn connection to use
   * @return number of rows in the database, which will be selected by the given whereClauseParts
   * @throws SQLException if an SQLExcetpion occurs.
   */
  private int count(List<IWhereClausePart> whereClauseParts, final ISQLConnection conn)
      throws SQLException {
    int count;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      String whereClause = whereClausePartUtil.createWhereClause(whereClauseParts);
      String countSql = "select count(*) from " + ti.getQualifiedName() + whereClause;
      pstmt = conn.prepareStatement(countSql);
      whereClausePartUtil.setParameters(pstmt, whereClauseParts, 1);

      rs = pstmt.executeQuery();
      rs.next();
      count = rs.getInt(1);
    } finally {
      // We don't care if these throw an SQLException.  Just squelch them
      // and report to the user what the outcome of the previous statements
      // were.
      SQLUtilities.closeResultSet(rs);
      SQLUtilities.closeStatement(pstmt);
    }
    return count;
  }
  /**
   * Create the child nodes for the passed parent node and return them. Note that this method should
   * <B>not</B> actually add the child nodes to the parent node as this is taken care of in the
   * caller.
   *
   * @param session Current session.
   * @param node Node to be expanded.
   * @return A list of <TT>ObjectTreeNode</TT> objects representing the child nodes for the passed
   *     node.
   */
  public List<ObjectTreeNode> createChildren(ISession session, ObjectTreeNode parentNode)
      throws SQLException {
    final List<ObjectTreeNode> childNodes = new ArrayList<ObjectTreeNode>();
    final IDatabaseObjectInfo parentDbinfo = parentNode.getDatabaseObjectInfo();
    final ISQLConnection conn = session.getSQLConnection();
    final SQLDatabaseMetaData md = session.getSQLConnection().getSQLMetaData();
    final String catalogName = parentDbinfo.getCatalogName();
    final String schemaName = parentDbinfo.getSchemaName();
    final ObjFilterMatcher filterMatcher = new ObjFilterMatcher(session.getProperties());

    String sql = SQL;
    if (isOS400) {
      sql = OS_400_SQL;
    }

    final PreparedStatement pstmt = conn.prepareStatement(sql);
    ResultSet rs = null;
    try {
      pstmt.setString(1, schemaName);
      pstmt.setString(2, filterMatcher.getSqlLikeMatchString());
      rs = pstmt.executeQuery();
      while (rs.next()) {
        IDatabaseObjectInfo si =
            new DatabaseObjectInfo(
                catalogName, schemaName, rs.getString(1), DatabaseObjectType.SEQUENCE, md);

        if (filterMatcher.matches(si.getSimpleName())) {
          childNodes.add(new ObjectTreeNode(session, si));
        }
      }
    } finally {
      SQLUtilities.closeResultSet(rs);
      SQLUtilities.closeStatement(pstmt);
    }
    return childNodes;
  }