KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > formatter > ResultSetFormatter


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Jeff Mesnil.
20  * Contributor(s): ______________________.
21  */

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

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

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