1 24 25 package org.objectweb.cjdbc.console.text.formatter; 26 27 import java.sql.ResultSet ; 28 import java.sql.ResultSetMetaData ; 29 import java.sql.SQLException ; 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 41 public class ResultSetFormatter 42 { 43 44 private static final int MAX_COLUMN_DISPLAY_WIDTH = 25; 45 46 56 public static void formatAndDisplay(ResultSet rs, int fetchsize, Console console) throws SQLException 57 { 58 ResultSetMetaData meta = rs.getMetaData(); 60 int columnCount = meta.getColumnCount(); 61 62 console.println(); 63 64 appendSeparatorLine(columnCount, meta, console); 65 66 console.print("|", ColorPrinter.STATUS); 68 for (int i = 1; i <= columnCount; i++) 69 { 70 console.print(" "); 71 int size = meta.getColumnDisplaySize(i); 73 String 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 Object 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 value = (object != null) ? rs.getObject(i).toString() : ""; 99 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 []{new Integer (fetchsize), new Integer (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 meta, Console console) throws SQLException 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 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 toPad = new StringBuffer (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 |