1 22 23 package org.continuent.sequoia.console.text.formatter; 24 25 import java.sql.ResultSet ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.SQLException ; 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 39 public class ResultSetFormatter 40 { 41 42 private static final int MAX_COLUMN_DISPLAY_WIDTH = 25; 43 44 53 public static void formatAndDisplay(ResultSet rs, int fetchsize, 54 Console console) throws SQLException 55 { 56 ResultSetMetaData meta = rs.getMetaData(); 58 int columnCount = meta.getColumnCount(); 59 60 console.println(); 61 62 appendSeparatorLine(columnCount, meta, console); 63 64 console.print("|"); 66 for (int i = 1; i <= columnCount; i++) 67 { 68 console.print(" "); 69 int size = meta.getColumnDisplaySize(i); 71 String 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 String 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 value = null; 96 try 97 { 98 object = rs.getString(i); 99 value = (object != null) ? object : ""; 100 } 101 catch (SQLException e) 102 { 103 value = e.getMessage(); 104 } 105 106 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 []{new Integer (fetchsize), new Integer (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 meta, Console console) throws SQLException 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 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 toPad = new StringBuffer (size); 167 toPad.insert(0, text); 168 while (toPad.length() < size) 169 toPad.append(' '); 170 console.print(toPad.toString()); 171 } 172 } 173 | Popular Tags |