Пример #1
0
  /**
   * this method adds a tuple
   *
   * @param t the tuple to add
   */
  public void addTuple(Tuple t) {
    // add the tuple to the list of tuples
    tuples.add(t);

    int i = 0;
    String s;
    // iterate over columns to determine maximum columnwidth
    for (Value v : t) {
      s = v.toString();
      if (s == null) {
        s = Constants.PRINT_NULL;
      } else {
        // update column width if value not null
        columnWidth[i] = Math.max(columnWidth[i], s.length());
      }

      // if stringbuffer creation is toggled on
      if (createSB) {
        stringBuffer.append(
            StringUtils.format(s, columnNames.get(i).length(), v.isNumeric()) + " ");
      }
      i++;
    }
    if (createSB) stringBuffer.append("\r\n");
  }
Пример #2
0
  /**
   * extracts the column headers for display
   *
   * @param rsmd the resultsetmetadata object of the resultset
   */
  public void extractColumnHeaders(ResultSetMetaData rsmd) {
    try {
      // number of columns
      int columnCount = rsmd.getColumnCount();
      // create collection which stores the column headers as Strings
      List<String> columnHeaders = new ArrayList<String>(columnCount);

      // number of printed characters
      int[] columnPrintWidth = new int[columnCount];
      // numeric column type?
      boolean[] columnIsNumeric = new boolean[columnCount];

      // parse in column headers from metadata
      for (int column = 0; column < columnCount; column++) {
        // retrieve information about the column
        String columnName = rsmd.getColumnName(column + 1);
        int columnDisplaySize = rsmd.getColumnDisplaySize(column + 1);
        if (columnDisplaySize <= 0) {
          columnDisplaySize = Constants.DEFAULT_PRINT_WIDTH;
        }
        if (columnDisplaySize > Constants.MAX_PRINT_WIDTH) {
          columnDisplaySize = Constants.MAX_PRINT_WIDTH;
        }

        columnIsNumeric[column] = rsmd.isSigned(column + 1); // hack for isNumeric
        columnPrintWidth[column] = Math.max(columnDisplaySize, columnName.length());

        // add column name to collection
        columnHeaders.add(StringUtils.format(columnName, columnPrintWidth[column] + 1, false));
      }

      // save columnheaders
      setColumnNames(columnHeaders);
    } catch (SQLException e) {
      System.out.println(
          "SQLException occured during extraction of column headers from metadata for ResultSetModel.");
      e.printStackTrace();
    }
  }
Пример #3
0
  /**
   * this method returns a StringBuffer containing the contents of this ResultSetModel
   *
   * @param newLine the LineDelimiter for a newline character
   * @return a StringBuffer of the contents
   */
  public StringBuffer getStringBufferFromRSM(String newLine) {
    int columnCount = columnNames.size(); // number of columns

    if (!createSB) {
      stringBuffer = new StringBuffer();
      // print data
      Iterator<Tuple> iter = tuples.iterator();
      Tuple tuple;
      String valueFreeOfSpecialChars;
      while (iter.hasNext()) { // for each row
        tuple = iter.next();
        for (int column = 0; column < columnCount; column++) { // for each column
          // print value
          Value value = tuple.getValue(column);
          if (!value.isNull()) {
            valueFreeOfSpecialChars = StringUtils.replaceDelChars(value.toString());
            stringBuffer.append(
                StringUtils.format(
                    valueFreeOfSpecialChars, columnWidth[column], value.isNumeric()));
          } else {
            stringBuffer.append(
                StringUtils.format(Constants.PRINT_NULL, columnWidth[column], value.isNumeric()));
          }
          if (column < columnCount) {
            stringBuffer.append(' ');
          }
        }
        stringBuffer.append(newLine);
      }
      stringBuffer.append(newLine);
      // toggle flag to get into other path, when next request for this model
      createSB = true;
    }

    StringBuffer sb = new StringBuffer(); // create new StringBuffer

    // print column headers
    for (int column = 0; column < columnCount; column++) {
      // retrieve information about the column
      String columnName = columnNames.get(column);
      // print column name
      sb.append(StringUtils.format(columnName, columnWidth[column], false)).append(" ");
    }
    sb.append(newLine);

    // print separator line
    for (int column = 0; column < columnCount; column++) {
      for (int i = 0; i < columnWidth[column]; i++) {
        sb.append('-');
      }
      sb.append(' ');
    }
    sb.append(newLine);

    // use already created stringbuffer to print out results
    sb.append(stringBuffer);

    // finally print tuple count
    sb.append(newLine + tuples.size() + " row(s) selected.");
    return sb;
  }