KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > formatter > ResultSetFormatter


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Jeff Mesnil.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.text.formatter;
26
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.ResultSetMetaData JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
32 import org.objectweb.cjdbc.console.text.ColorPrinter;
33 import org.objectweb.cjdbc.console.text.Console;
34 import org.objectweb.cjdbc.console.text.ConsoleException;
35
36 /**
37  * Utility class to format a <code>ResultSet</code> to display it prettily in the text console
38  *
39  * @author <a HREF="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
40  */

41 public class ResultSetFormatter
42 {
43   /** Max column width when displaying a <code>ResultSet</code>. */
44   private static final int MAX_COLUMN_DISPLAY_WIDTH = 25;
45
46   /**
47    * Format and display the given <code>ResultSet</code> on the console.
48    *
49    * @param rs the <code>ResultSet</code> to display
50    * @param fetchsize fetchisze value
51    * @param console console where the ResultSet will be displayed
52    * and he size of the result set)
53    *
54    * @throws SQLException if an error occurs
55    */

56   public static void formatAndDisplay(ResultSet JavaDoc rs, int fetchsize, Console console) throws SQLException JavaDoc
57   {
58     // Get the metadata
59
ResultSetMetaData JavaDoc meta = rs.getMetaData();
60     int columnCount = meta.getColumnCount();
61   
62     console.println();
63     
64     appendSeparatorLine(columnCount, meta, console);
65   
66     // Print the column names
67
console.print("|", ColorPrinter.STATUS);
68     for (int i = 1; i <= columnCount; i++)
69     {
70       console.print(" ");
71       // Pad the column name and print it
72
int size = meta.getColumnDisplaySize(i);
73       String JavaDoc columnName = meta.getColumnName(i);
74       if (size <= 0)
75       {
76         if (columnName != null)
77           size = columnName.length();
78         else
79           size = 0;
80       }
81       appendPad(columnName, size, console);
82       console.print(" |", ColorPrinter.STATUS);
83     }
84     console.println();
85   
86     appendSeparatorLine(columnCount, meta, console);
87   
88     // Display the results
89
Object JavaDoc object;
90     int line = 0;
91     while (rs.next())
92     {
93       console.print("|", ColorPrinter.STATUS);
94       for (int i = 1; i <= columnCount; i++)
95       {
96         console.print(" ");
97         object = rs.getObject(i);
98         String JavaDoc value = (object != null) ? rs.getObject(i).toString() : "";
99         // Pad the value and print it
100
int size = meta.getColumnDisplaySize(i);
101         if (size <= 0)
102         {
103           if (value != null)
104             size = value.length();
105           else
106             size = 0;
107         }
108         appendPad(value, size, console);
109         console.print(" |", ColorPrinter.STATUS);
110       }
111       console.println();
112       line++;
113       if (fetchsize != 0)
114       {
115         if (line % fetchsize == 0)
116         {
117           try
118           {
119             console.readLine(ConsoleTranslate.get("sql.display.next.rows",
120                 new Integer JavaDoc[]{new Integer JavaDoc(fetchsize), new Integer JavaDoc(line)}));
121           }
122           catch (ConsoleException ignore)
123           {
124           }
125         }
126       }
127     }
128   
129     appendSeparatorLine(columnCount, meta, console);
130   }
131
132   private static void appendSeparatorLine(int columnCount,
133       ResultSetMetaData JavaDoc meta, Console console) throws SQLException JavaDoc
134   {
135   
136     console.print("+", ColorPrinter.STATUS);
137     for (int i = 1; i <= columnCount; i++)
138     {
139       int size = meta.getColumnDisplaySize(i);
140       if (size > MAX_COLUMN_DISPLAY_WIDTH)
141         size = MAX_COLUMN_DISPLAY_WIDTH;
142       console.print("-", ColorPrinter.STATUS);
143       for (int j = 0; j < size; j++)
144         console.print("-", ColorPrinter.STATUS);
145       console.print("-+", ColorPrinter.STATUS);
146     }
147     console.println();
148   }
149
150   private static void appendPad(String JavaDoc text, int size, Console console)
151   {
152     if (size > MAX_COLUMN_DISPLAY_WIDTH)
153       size = MAX_COLUMN_DISPLAY_WIDTH;
154     if (size < text.length())
155     {
156       console.print(text.substring(0, size - 1) + "~", ColorPrinter.STATUS);
157       return;
158     }
159     StringBuffer JavaDoc toPad = new StringBuffer JavaDoc(size);
160     toPad.insert(0, text);
161     while (toPad.length() < size)
162       toPad.append(' ');
163     console.print(toPad.toString(), ColorPrinter.STATUS);
164   }
165 }
166
Popular Tags