001: /*
002: * $Id: BaseReport.java,v 1.1 2006/01/10 21:02:37 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2006 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040: package org.axiondb.tools;
041:
042: import java.io.PrintWriter;
043: import java.sql.ResultSet;
044: import java.sql.ResultSetMetaData;
045: import java.sql.SQLException;
046: import java.util.ArrayList;
047: import java.util.List;
048:
049: /**
050: * @author Girish Patil
051: * Utility class for Console and Batch command runner to format the output.
052: */
053: public class BaseReport {
054: public BaseReport(PrintWriter pw) {
055: this ._writer = pw;
056: }
057:
058: public void reportException(Exception e) {
059: _writer.println(e.toString());
060: }
061:
062: public void reportUpdateCount(int count) {
063: switch (count) {
064: case -1:
065: _writer.println("Executed.");
066: break;
067: case 0:
068: _writer.println("No rows changed.");
069: break;
070: case 1:
071: _writer.println("1 row changed.");
072: break;
073: default:
074: _writer.println(count + " rows changed.");
075: break;
076: }
077: }
078:
079: public void reportResultSet(ResultSet rset) throws SQLException {
080: if (rset != null) {
081: ResultSetMetaData meta = rset.getMetaData();
082: int count = meta.getColumnCount();
083: int[] colWidths = new int[count];
084: List[] colValues = new List[count];
085: for (int i = 0; i < count; i++) {
086: String label = meta.getColumnLabel(i + 1);
087: if (label != null) {
088: colWidths[i] = label.length();
089: }
090: colValues[i] = new ArrayList();
091: }
092: // find column values and widths
093: while (rset.next()) {
094: for (int i = 0; i < count; i++) {
095: String val = rset.getString(i + 1);
096: if (rset.wasNull()) {
097: val = "NULL";
098: }
099: if (val.length() > colWidths[i]) {
100: colWidths[i] = val.length();
101: }
102: colValues[i].add(val);
103: }
104: }
105: // table header
106: printBoundary('=', colWidths);
107: for (int i = 0; i < count; i++) {
108: String label = meta.getColumnLabel(i + 1);
109: _writer.print("|");
110: printCentered(label, colWidths[i]);
111: }
112: _writer.println("|");
113: printBoundary('=', colWidths);
114: // values
115: for (int i = 0, I = colValues[0].size(); i < I; i++) {
116: // for each row
117: for (int j = 0; j < count; j++) {
118: // for each column
119: String value = (String) colValues[j].get(i);
120: _writer.print("|");
121: printRight(value, colWidths[j]);
122: }
123: _writer.println("|");
124: printBoundary('-', colWidths);
125: }
126: }
127: }
128:
129: public void printBoundary(char boundaryChar, int[] colWidths) {
130: for (int i = 0; i < colWidths.length; i++) {
131: _writer.print("+");
132: for (int j = 0; j < colWidths[i] + 2; j++) {
133: _writer.print(boundaryChar);
134: }
135: }
136: _writer.println("+");
137: }
138:
139: public void printCentered(String value, int length) {
140: _writer.print(" ");
141: int diff = length - value.length();
142: int left = diff / 2;
143: int right = left;
144: if (diff % 2 == 1) {
145: left++;
146: }
147: for (int j = 0; j < left; j++) {
148: _writer.print(" ");
149: }
150: _writer.print(value);
151: for (int j = 0; j < right; j++) {
152: _writer.print(" ");
153: }
154: _writer.print(" ");
155: }
156:
157: public void printRight(String value, int length) {
158: _writer.print(" ");
159: int diff = length - value.length();
160: for (int j = 0; j < diff; j++) {
161: _writer.print(" ");
162: }
163: _writer.print(value);
164: _writer.print(" ");
165: }
166:
167: private PrintWriter _writer;
168: }
|