1 package com.quadcap.app.bugdb; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.PrintWriter ; 47 48 import java.sql.Connection ; 49 import java.sql.DriverManager ; 50 import java.sql.ResultSet ; 51 import java.sql.ResultSetMetaData ; 52 import java.sql.SQLException ; 53 import java.sql.Statement ; 54 55 60 public class Loader { 61 Connection conn; 62 StringBuffer buffer = new StringBuffer (); 63 PrintWriter writer = null; 64 65 69 public Loader() { 70 } 71 72 77 public Loader(Connection conn) { 78 this.conn = conn; 79 } 80 81 86 public void setConnection(Connection conn) { 87 this.conn = conn; 88 } 89 90 95 public Connection getConnection() { 96 return this.conn; 97 } 98 99 104 public PrintWriter getWriter() { 105 return writer; 106 } 107 108 113 public void setWriter(PrintWriter writer) { 114 this.writer = writer; 115 } 116 117 122 final void print(String s) { 123 if (writer != null) writer.print(s); 124 } 125 126 132 final void println(String s) { 133 if (writer != null) writer.println(s); 134 } 135 136 141 final void print(Throwable t) { 142 if (writer != null) t.printStackTrace(writer); 143 if (t instanceof SQLException ) { 144 SQLException e = ((SQLException )t).getNextException(); 145 if (e != null) { 146 print(e); 147 } 148 } 149 } 150 151 158 final static String pad(int wid, String s) { 159 StringBuffer sb = new StringBuffer (s); 160 while (sb.length() < wid) sb.append(' '); 161 return sb.toString(); 162 } 163 164 170 final void showResultSet(ResultSet rs) throws SQLException { 171 ResultSetMetaData rmeta = rs.getMetaData(); 172 String delim = ""; 173 for (int i = 1; i <= rmeta.getColumnCount(); i++) { 174 int wid = rmeta.getColumnDisplaySize(i); 175 if (wid > 32) wid = 32; 176 print(delim); 177 delim = " "; 178 print(pad(wid, rmeta.getColumnName(i))); 179 } 180 println(""); 181 while (rs.next()) { 182 delim = ""; 183 for (int i = 1; i <= rmeta.getColumnCount(); i++) { 184 int wid = rmeta.getColumnDisplaySize(i); 185 if (wid > 32) wid = 32; 186 print(delim); 187 delim = " "; 188 Object obj = rs.getObject(i); 189 if (obj == null) obj = "<null>"; 190 if (obj instanceof byte[]) { 191 obj = hexBytes((byte[])obj); 192 } 193 print(pad(wid, obj.toString())); 194 } 195 println(""); 196 } 197 } 198 199 206 static String getLine(InputStream is) throws IOException { 207 StringBuffer sb = new StringBuffer (); 208 int ch; 209 int state = 0; 210 while ((ch = is.read()) > 0) { 211 char c = (char)ch; 212 switch (state) { 213 case 0: 214 if (c == '-') { 215 state = 1; 216 } else if (c == ';') { 217 if (sb.length() > 0) { 218 return sb.toString(); 219 } 220 } else if (Character.isWhitespace(c) && sb.length() == 0) { 221 } else { 222 sb.append(c); 223 } 224 break; 225 case 1: 226 if (c == '-') { 227 state = 2; 228 } else { 229 sb.append('-'); 230 sb.append(c); 231 state = 0; 232 } 233 break; 234 case 2: 235 if (c == '\r') state = 3; 236 if (c == '\n') state = 0; 237 break; 238 case 3: 239 if (c == '\n') state = 0; 240 break; 241 } 242 } 243 return null; 244 } 245 246 251 final void execute(String sql) { 252 try { 253 Statement stmt = conn.createStatement(); 254 try { 255 if (sql.equals("BEGINTRANSACTION")) { 256 conn.setAutoCommit(false); 257 } else if (sql.equals("ENDTRANSACTION") || 258 sql.equals("COMMIT")) { 259 conn.commit(); 260 conn.setAutoCommit(true); 261 } else if (sql.equals("ROLLBACK")) { 262 conn.rollback(); 263 conn.setAutoCommit(true); 264 } else { 265 if (stmt.execute(sql)) { 266 ResultSet rs = stmt.getResultSet(); 267 try { 268 showResultSet(rs); 269 } finally { 270 rs.close(); 271 } 272 } 273 } 274 } catch (Throwable t) { 275 print(t); 276 } finally { 277 stmt.close(); 278 } 279 } catch (Throwable t) { 280 print(t); 281 } 282 } 283 284 290 public void loadFile(String filename) throws IOException { 291 FileInputStream fis = new FileInputStream (filename); 292 loadStream(fis); 293 } 294 295 300 public void loadStream(InputStream is) { 301 try { 302 Statement stmt = conn.createStatement(); 303 BufferedInputStream bis = new BufferedInputStream (is, 1024); 304 String sql = null; 305 while ((sql = getLine(bis)) != null) { 306 try { 307 if (sql.equals("BEGINTRANSACTION")) { 308 conn.setAutoCommit(false); 309 } else if (sql.equals("ENDTRANSACTION") || 310 sql.equals("COMMIT")) { 311 conn.commit(); 312 conn.setAutoCommit(true); 313 } else if (sql.equals("ROLLBACK")) { 314 conn.rollback(); 315 conn.setAutoCommit(true); 316 } else { 317 if (stmt.execute(sql)) { 318 ResultSet rs = stmt.getResultSet(); 319 showResultSet(rs); 320 } 321 } 322 } catch (Throwable t) { 323 print(t); 324 } 325 } 326 } catch (Throwable t) { 327 print(t); 328 } finally { 329 try { 330 is.close(); 331 } catch (Exception e) {} 332 } 333 } 334 335 static char[] hexMap = {'0', '1', '2', '3', '4', '5', '6', '7', 336 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 337 338 345 public static String hexBytes(byte[] buf) { 346 if (buf == null) return "<null>"; 347 return hexBytes(buf, 0, buf.length); 348 } 349 350 359 public static String hexBytes(byte[] buf, int off, int len) { 360 if (buf == null) return "<null>"; 361 StringBuffer sb = new StringBuffer (); 362 for (int i = off; i < off + len; i++) { 363 byte b = buf[i]; 364 sb.append(hexMap[(b >> 4) & 0xf]); 365 sb.append(hexMap[b & 0xf]); 366 } 367 return sb.toString(); 368 } 369 } 370 | Popular Tags |