Author: cbegin Date: Mon Jun 15 05:12:23 2009 New Revision: 784653 URL: http://svn.apache.org/viewvc?rev=784653&view=rev Log: Cleaned up ScriptRunner code
Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/RuntimeSqlException.java Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java ibatis/trunk/java/ibatis-3/version.properties Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/RuntimeSqlException.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/RuntimeSqlException.java?rev=784653&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/RuntimeSqlException.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/RuntimeSqlException.java Mon Jun 15 05:12:23 2009 @@ -0,0 +1,21 @@ +package org.apache.ibatis.jdbc; + +public class RuntimeSqlException extends RuntimeException { + + public RuntimeSqlException() { + super(); + } + + public RuntimeSqlException(String message) { + super(message); + } + + public RuntimeSqlException(String message, Throwable cause) { + super(message, cause); + } + + public RuntimeSqlException(Throwable cause) { + super(cause); + } + +} Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java?rev=784653&r1=784652&r2=784653&view=diff ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java (original) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java Mon Jun 15 05:12:23 2009 @@ -1,6 +1,9 @@ package org.apache.ibatis.jdbc; -import java.io.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Reader; import java.sql.*; public class ScriptRunner { @@ -11,6 +14,7 @@ private boolean stopOnError; private boolean autoCommit; + private boolean sendFullScript; private PrintWriter logWriter = new PrintWriter(System.out); private PrintWriter errorLogWriter = new PrintWriter(System.err); @@ -30,6 +34,10 @@ this.autoCommit = autoCommit; } + public void setSendFullScript(boolean sendFullScript) { + this.sendFullScript = sendFullScript; + } + public void setLogWriter(PrintWriter logWriter) { this.logWriter = logWriter; } @@ -46,10 +54,24 @@ this.fullLineDelimiter = fullLineDelimiter; } - public void runScript(Reader reader) throws IOException, SQLException { + public void runScript(Reader reader) { + setAutoCommit(); + + StringBuffer command = new StringBuffer(); try { - runScriptWithConnection(connection, reader); + BufferedReader lineReader = new BufferedReader(reader); + String line; + while ((line = lineReader.readLine()) != null) { + command = handleLine(command, line); + } + commitConnection(); + checkForMissingLineTerminator(command); + } catch (Exception e) { + String message = "Error executing: " + command + ". Cause: " + e; + printlnError(message); + throw new RuntimeSqlException(message, e); } finally { + rollbackConnection(); flush(); } } @@ -62,105 +84,113 @@ } } - /** - * Runs an SQL script (read in using the Reader parameter) using the connection passed in - * - * @param conn - the connection to use for the script - * @param reader - the source of the script - * @throws java.sql.SQLException if any SQL errors occur - * @throws java.io.IOException if there is an error reading from the Reader - */ - private void runScriptWithConnection(Connection conn, Reader reader) - throws IOException, SQLException { - StringBuffer command = null; + private void setAutoCommit() { try { - BufferedReader lineReader = new BufferedReader(reader); - String line; - while ((line = lineReader.readLine()) != null) { - if (command == null) { - command = new StringBuffer(); - } - String trimmedLine = line.trim(); - if (trimmedLine.length() < 1) { - // do nothing - } else if (trimmedLine.startsWith("//") || trimmedLine.startsWith("--")) { - println(trimmedLine); - } else if (!fullLineDelimiter && trimmedLine.endsWith(delimiter) - || fullLineDelimiter && trimmedLine.equals(delimiter)) { - command.append(line.substring(0, line.lastIndexOf(delimiter))); - command.append(" "); - Statement statement = conn.createStatement(); - - println(command); - - boolean hasResults = false; - if (stopOnError) { - hasResults = statement.execute(command.toString()); - } else { - try { - hasResults = statement.execute(command.toString()); - } catch (SQLException e) { - e.fillInStackTrace(); - printlnError("Error executing: " + command); - printlnError(e); - } - } + if (autoCommit != connection.getAutoCommit()) { + connection.setAutoCommit(autoCommit); + } + } catch (Throwable t) { + throw new RuntimeSqlException("Could not set AutoCommit to " + autoCommit + ". Cause: " + t, t); + } + } - if (autoCommit && !conn.getAutoCommit()) { - conn.commit(); - } + private void commitConnection() { + try { + if (!connection.getAutoCommit()) { + connection.commit(); + } + } catch (Throwable t) { + throw new RuntimeSqlException("Could not commit transaction. Cause: " + t, t); + } + } - if (hasResults) { - ResultSet rs = statement.getResultSet(); - if (rs != null) { - ResultSetMetaData md = rs.getMetaData(); - int cols = md.getColumnCount(); - for (int i = 0; i < cols; i++) { - String name = md.getColumnLabel(i + 1); - print(name + "\t"); - } - println(""); - while (rs.next()) { - for (int i = 0; i < cols; i++) { - String value = rs.getString(i + 1); - print(value + "\t"); - } - println(""); - } - } - } + private void rollbackConnection() { + try { + if (!connection.getAutoCommit()) { + connection.rollback(); + } + } catch (Throwable t) { + // ignore + } + } - command = null; - try { - statement.close(); - } catch (Exception e) { - // Ignore to workaround a bug in Jakarta DBCP + private void checkForMissingLineTerminator(StringBuffer command) { + if (command != null && command.toString().trim().length() > 0) { + throw new RuntimeSqlException("Line missing end-of-line terminator (" + delimiter + ") => " + command); + } + } + + private StringBuffer handleLine(StringBuffer command, String line) throws SQLException { + String trimmedLine = line.trim(); + if (lineIsComment(trimmedLine)) { + println(trimmedLine); + } else if (commandReadyToExecute(trimmedLine)) { + command.append(line.substring(0, line.lastIndexOf(delimiter))); + command.append(" "); + println(command); + executeStatement(command.toString()); + command.setLength(0); + } else if (trimmedLine.length() > 0) { + command.append(line); + command.append(" "); + } + return command; + } + + private boolean lineIsComment(String trimmedLine) { + return trimmedLine.startsWith("//") || trimmedLine.startsWith("--"); + } + + private boolean commandReadyToExecute(String trimmedLine) { + return !fullLineDelimiter && trimmedLine.endsWith(delimiter) + || fullLineDelimiter && trimmedLine.equals(delimiter); + } + + private void executeStatement(String command) throws SQLException { + boolean hasResults = false; + Statement statement = connection.createStatement(); + if (stopOnError) { + hasResults = statement.execute(command); + } else { + try { + hasResults = statement.execute(command); + } catch (SQLException e) { + String message = "Error executing: " + command + ". Cause: " + e; + printlnError(message); + } + } + printResults(statement, hasResults); + try { + statement.close(); + } catch (Exception e) { + // Ignore to workaround a bug in some connection pools + } + commitConnection(); + } + + private void printResults(Statement statement, boolean hasResults) { + try { + if (hasResults) { + ResultSet rs = statement.getResultSet(); + if (rs != null) { + ResultSetMetaData md = rs.getMetaData(); + int cols = md.getColumnCount(); + for (int i = 0; i < cols; i++) { + String name = md.getColumnLabel(i + 1); + print(name + "\t"); + } + println(""); + while (rs.next()) { + for (int i = 0; i < cols; i++) { + String value = rs.getString(i + 1); + print(value + "\t"); + } + println(""); } - Thread.yield(); - } else { - command.append(line); - command.append(" "); } } - if (!autoCommit && !conn.getAutoCommit()) { - conn.commit(); - } - if (command != null && command.toString().trim().length() > 0) { - throw new IOException("Line missing end-of-line terminator ("+delimiter+") => " + command); - } } catch (SQLException e) { - e.fillInStackTrace(); - printlnError("Error executing: " + command); - printlnError(e); - throw e; - } catch (IOException e) { - e.fillInStackTrace(); - printlnError("Error executing: " + command); - printlnError(e); - throw e; - } finally { - conn.rollback(); - flush(); + printlnError("Error printing results: " + e.getMessage()); } } Modified: ibatis/trunk/java/ibatis-3/version.properties URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/version.properties?rev=784653&r1=784652&r2=784653&view=diff ============================================================================== --- ibatis/trunk/java/ibatis-3/version.properties (original) +++ ibatis/trunk/java/ibatis-3/version.properties Mon Jun 15 05:12:23 2009 @@ -1,5 +1,5 @@ #Build version info -#Tue Jun 09 23:51:33 MDT 2009 +#Wed Jun 10 13:21:15 MDT 2009 version=3.0.0 -buildDate=2009/06/09 23\:51 -buildNum=177 +buildDate=2009/06/10 13\:21 +buildNum=179