001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.datasetplugin.jdbc;
015:
016: import org.jetbrains.annotations.NotNull;
017:
018: import java.sql.ResultSet;
019: import java.sql.ResultSetMetaData;
020: import java.sql.SQLException;
021: import java.util.ArrayList;
022:
023: /**
024: * User: Martin
025: * Date: 04.03.2006
026: * Time: 16:11:30
027: */
028: @SuppressWarnings({"UseOfSystemOutOrSystemErr"})
029: public class ResultSetPrinter {
030: private ResultSetPrinter() {
031: }
032:
033: public static void printResultSet(@NotNull
034: ResultSet resultSet) throws SQLException {
035: ResultSetMetaData metaData = resultSet.getMetaData();
036: ArrayList<String> columnNames = new ArrayList<String>();
037: ArrayList<ColumnInfo> columnInfos = new ArrayList<ColumnInfo>();
038:
039: int cc = metaData.getColumnCount();
040: for (int i = 1; i <= cc; i++) {
041: String columnName = metaData.getColumnName(i);
042: columnNames.add(columnName);
043: columnInfos.add(new ColumnInfo());
044: if (columnName != null) {
045: columnInfos.get(i - 1).width = columnName.length();
046: }
047: }
048:
049: ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
050: while (resultSet.next()) {
051: ArrayList<String> row = new ArrayList<String>();
052: for (int i = 1; i <= cc; i++) {
053: String value = resultSet.getString(i);
054: row.add(value);
055: if (value != null) {
056: columnInfos.get(i - 1).width = Math.max(columnInfos
057: .get(i - 1).width, value.length());
058: }
059: }
060: data.add(row);
061: }
062:
063: //output
064: StringBuilder fillerBuilder = new StringBuilder(1000);
065: for (int i = 0; i < 100; i++) {
066: fillerBuilder.append(" ");//10
067: }
068: String filler = fillerBuilder.toString();
069:
070: printRow(columnInfos, columnNames, filler);
071: for (ColumnInfo columnInfo : columnInfos) {
072: for (int n = 0; n < columnInfo.width; n++) {
073: System.out.print("-");
074: }
075: System.out.print("+");
076: }
077: System.out.println();
078:
079: for (ArrayList<String> row : data) {
080: printRow(columnInfos, row, filler);
081: }
082:
083: System.out.println();
084: }
085:
086: private static void printRow(@NotNull
087: ArrayList<ColumnInfo> columnInfos, @NotNull
088: ArrayList<String> row, @NotNull
089: String filler) {
090: for (int i = 0; i < columnInfos.size(); i++) {
091: ColumnInfo columnInfo = columnInfos.get(i);
092: String v = (row.get(i) + filler).substring(0,
093: columnInfo.width);
094: System.out.print(v);
095: System.out.print("|");
096: }
097: System.out.println();
098: }
099:
100: private static class ColumnInfo {
101: public int width;
102: }
103: }
|