001: /**
002: * com.mckoi.util.ResultOutputUtil 22 Sep 2000
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.util;
024:
025: import java.sql.*;
026: import java.io.*;
027: import java.util.Vector;
028:
029: /**
030: * Utilities for parsing a ResultSet and outputing it in different forms. The
031: * forms included are straight text (mono-spaced), HTML, etc.
032: *
033: * @author Tobias Downer
034: */
035:
036: public class ResultOutputUtil {
037:
038: /**
039: * Writes a break.
040: * eg. "+--------+----------+---------------+"
041: */
042: private static void writeBreak(int[] widths, PrintWriter out) {
043: out.print('+');
044: for (int i = 0; i < widths.length; ++i) {
045: int wid = widths[i] + 2;
046: for (int n = 0; n < wid; ++n) {
047: out.print('-');
048: }
049: out.print('+');
050: }
051: out.println();
052: }
053:
054: /**
055: * Writes a row of data.
056: * eg. "|1 |Greetings |Part-54445 |"
057: */
058: private static void writeRow(int[] widths, String[] cols,
059: PrintWriter out) {
060: out.print('|');
061: for (int i = 0; i < widths.length; ++i) {
062: String str = cols[i];
063: out.print(' ');
064: out.print(str);
065: // Write padding
066: int wid = (widths[i] + 1) - str.length();
067: for (int n = 0; n < wid; ++n) {
068: out.print(' ');
069: }
070: out.print('|');
071: }
072: out.println();
073: }
074:
075: /**
076: * Formats the ResultSet as plain mono-spaced text and outputs the result to
077: * the PrintWriter.
078: */
079: public static void formatAsText(ResultSet result_set,
080: PrintWriter out) throws SQLException {
081: ResultSetMetaData meta_data = result_set.getMetaData();
082: // Maximum widths of each column.
083: int[] max_widths = new int[meta_data.getColumnCount()];
084: Vector[] data = new Vector[meta_data.getColumnCount()];
085: for (int i = 0; i < data.length; ++i) {
086: data[i] = new Vector();
087: }
088: int row_count = 0;
089:
090: for (int i = 0; i < data.length; ++i) {
091: String str = meta_data.getColumnLabel(i + 1);
092: max_widths[i] = Math.max(str.length(), max_widths[i]);
093: }
094:
095: // Read in the data for the result set,
096: while (result_set.next()) {
097: for (int i = 0; i < data.length; ++i) {
098: Object ob = result_set.getObject(i + 1);
099: String str = "NULL";
100: if (ob != null) {
101: str = ob.toString();
102: }
103: data[i].addElement(str);
104: max_widths[i] = Math.max(str.length(), max_widths[i]);
105: }
106: ++row_count;
107: }
108:
109: // Output the data we stored
110: String[] line = new String[data.length];
111:
112: writeBreak(max_widths, out);
113: for (int n = 0; n < line.length; ++n) {
114: line[n] = meta_data.getColumnLabel(n + 1);
115: }
116: writeRow(max_widths, line, out);
117: writeBreak(max_widths, out);
118: for (int i = 0; i < row_count; ++i) {
119: for (int n = 0; n < line.length; ++n) {
120: line[n] = (String) data[n].elementAt(i);
121: }
122: writeRow(max_widths, line, out);
123: }
124: writeBreak(max_widths, out);
125:
126: }
127:
128: }
|