1 16 package com.ibatis.common.jdbc; 17 18 import com.ibatis.common.resources.Resources; 19 20 import java.io.IOException ; 21 import java.io.LineNumberReader ; 22 import java.io.PrintWriter ; 23 import java.io.Reader ; 24 import java.sql.*; 25 import java.util.Map ; 26 27 32 public class ScriptRunner { 33 34 36 37 private String driver; 38 private String url; 39 private String username; 40 private String password; 41 private boolean stopOnError; 42 private boolean autoCommit; 43 private PrintWriter logWriter = new PrintWriter (System.out); 44 private PrintWriter errorLogWriter = new PrintWriter (System.err); 45 46 49 public ScriptRunner() { 50 stopOnError = false; 51 autoCommit = false; 52 } 53 54 59 public ScriptRunner(Map props) { 60 setDriver((String ) props.get("driver")); 61 setUrl((String ) props.get("url")); 62 setUsername((String ) props.get("username")); 63 setPassword((String ) props.get("password")); 64 setStopOnError("true".equals(props.get("stopOnError"))); 65 setAutoCommit("true".equals(props.get("autoCommit"))); 66 } 67 68 73 public boolean isStopOnError() { 74 return stopOnError; 75 } 76 77 82 public void setStopOnError(boolean stopOnError) { 83 this.stopOnError = stopOnError; 84 } 85 86 91 public boolean isAutoCommit() { 92 return autoCommit; 93 } 94 95 100 public void setAutoCommit(boolean autoCommit) { 101 this.autoCommit = autoCommit; 102 } 103 104 109 public PrintWriter getLogWriter() { 110 return logWriter; 111 } 112 113 118 public void setLogWriter(PrintWriter logWriter) { 119 this.logWriter = logWriter; 120 } 121 122 127 public PrintWriter getErrorLogWriter() { 128 return errorLogWriter; 129 } 130 131 136 public void setErrorLogWriter(PrintWriter errorLogWriter) { 137 this.errorLogWriter = errorLogWriter; 138 } 139 140 145 public String getDriver() { 146 return driver; 147 } 148 149 154 public void setDriver(String driver) { 155 this.driver = driver; 156 } 157 158 163 public String getUrl() { 164 return url; 165 } 166 167 172 public void setUrl(String url) { 173 this.url = url; 174 } 175 176 181 public String getUsername() { 182 return username; 183 } 184 185 190 public void setUsername(String username) { 191 this.username = username; 192 } 193 194 199 public String getPassword() { 200 return password; 201 } 202 203 208 public void setPassword(String password) { 209 this.password = password; 210 } 211 212 222 public void runScript(Reader reader) 223 throws ClassNotFoundException , SQLException, IOException , 224 IllegalAccessException , InstantiationException { 225 DriverManager.registerDriver((Driver) Resources.classForName(driver).newInstance()); 226 Connection conn = DriverManager.getConnection(url, username, password); 227 if (conn.getAutoCommit() != autoCommit) { 228 conn.setAutoCommit(autoCommit); 229 } 230 runScript(conn, reader); 231 conn.close(); 232 } 233 234 242 public void runScript(Connection conn, Reader reader) 243 throws IOException , SQLException { 244 StringBuffer command = null; 245 try { 246 LineNumberReader lineReader = new LineNumberReader (reader); 247 String line = null; 248 while ((line = lineReader.readLine()) != null) { 249 if (command == null) { 250 command = new StringBuffer (); 251 } 252 String trimmedLine = line.trim(); 253 if (trimmedLine.startsWith("--")) { 254 println(trimmedLine); 255 } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//")) { 259 } else if (trimmedLine.endsWith(";")) { 261 command.append(line.substring(0, line.lastIndexOf(";"))); 262 command.append(" "); 263 Statement statement = conn.createStatement(); 264 265 println(command); 266 270 boolean hasResults = false; 271 if (stopOnError) { 272 hasResults = statement.execute(command.toString()); 273 } else { 274 try { 275 statement.execute(command.toString()); 276 } catch (SQLException e) { 277 e.fillInStackTrace(); 278 printlnError("Error executing: " + command); 279 printlnError(e); 280 } 281 } 282 283 if (autoCommit && !conn.getAutoCommit()) { 284 conn.commit(); 285 } 286 287 ResultSet rs = statement.getResultSet(); 288 if (hasResults && rs != null) { 289 ResultSetMetaData md = rs.getMetaData(); 290 int cols = md.getColumnCount(); 291 for (int i = 0; i < cols; i++) { 292 String name = md.getColumnName(i); 293 print(name + "\t"); 294 } 295 println(""); 296 while (rs.next()) { 297 for (int i = 0; i < cols; i++) { 298 String value = rs.getString(i); 299 print(value + "\t"); 300 } 301 println(""); 302 } 303 } 304 305 command = null; 306 try { 307 statement.close(); 308 } catch (Exception e) { 309 } 311 Thread.yield(); 312 } else { 313 command.append(line); 314 command.append(" "); 315 } 316 } 317 if (!autoCommit) { 318 conn.commit(); 319 } 320 } catch (SQLException e) { 321 e.fillInStackTrace(); 322 printlnError("Error executing: " + command); 323 printlnError(e); 324 throw e; 326 } catch (IOException e) { 327 e.fillInStackTrace(); 328 printlnError("Error executing: " + command); 329 printlnError(e); 330 throw e; 332 } finally { 333 conn.rollback(); 334 flush(); 335 } 336 } 337 338 private void print(Object o) { 339 if (logWriter != null) { 340 System.out.print(o); 341 } 342 } 343 344 private void println(Object o) { 345 if (logWriter != null) { 346 logWriter.println(o); 347 } 348 } 349 350 private void printlnError(Object o) { 351 if (errorLogWriter != null) { 352 errorLogWriter.println(o); 353 } 354 } 355 356 private void flush() { 357 if (logWriter != null) { 358 logWriter.flush(); 359 } 360 if (errorLogWriter != null) { 361 errorLogWriter.flush(); 362 } 363 } 364 365 366 } 367 | Popular Tags |