Author: cbegin Date: Sun Nov 6 10:30:30 2005 New Revision: 331131 URL: http://svn.apache.org/viewcvs?rev=331131&view=rev Log: Improved scriptrunner design, removed deprication. Added case ignorance to entity resolver
Modified: ibatis/trunk/java/mapper/mapper2/build/version.properties ibatis/trunk/java/mapper/mapper2/src/com/ibatis/common/jdbc/ScriptRunner.java ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapClasspathEntityResolver.java ibatis/trunk/java/mapper/mapper2/test/com/ibatis/dao/BaseDaoTest.java ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/BaseSqlMapTest.java ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/builder/xml/SqlMapClasspathEntityResolverTest.java ibatis/trunk/java/mapper/mapper2/test/compatibility/scriptrunner/ScriptRunnerCompat.java Modified: ibatis/trunk/java/mapper/mapper2/build/version.properties URL: http://svn.apache.org/viewcvs/ibatis/trunk/java/mapper/mapper2/build/version.properties?rev=331131&r1=331130&r2=331131&view=diff ============================================================================== --- ibatis/trunk/java/mapper/mapper2/build/version.properties (original) +++ ibatis/trunk/java/mapper/mapper2/build/version.properties Sun Nov 6 10:30:30 2005 @@ -1,5 +1,5 @@ #Build version info -#Sun Nov 06 09:57:40 MST 2005 +#Sun Nov 06 11:16:16 MST 2005 version=2.1.6 -buildDate=2005/11/06 09\:57 -buildNum=590 +buildDate=2005/11/06 11\:16 +buildNum=591 Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/common/jdbc/ScriptRunner.java URL: http://svn.apache.org/viewcvs/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/common/jdbc/ScriptRunner.java?rev=331131&r1=331130&r2=331131&view=diff ============================================================================== --- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/common/jdbc/ScriptRunner.java (original) +++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/common/jdbc/ScriptRunner.java Sun Nov 6 10:30:30 2005 @@ -16,98 +16,50 @@ package com.ibatis.common.jdbc; import com.ibatis.common.resources.Resources; +import com.ibatis.common.exception.NestedRuntimeException; import java.io.IOException; import java.io.LineNumberReader; import java.io.PrintWriter; import java.io.Reader; import java.sql.*; -import java.util.Map; /** * Tool to run database scripts - * @deprecated There are better tools available for running scripts. This - * class has become out of scope for iBATIS. */ public class ScriptRunner { //private static final Log log = LogFactory.getLog(ScriptRunner.class); + private Connection connection; private String driver; private String url; private String username; private String password; + private boolean stopOnError; private boolean autoCommit; + private PrintWriter logWriter = new PrintWriter(System.out); private PrintWriter errorLogWriter = new PrintWriter(System.err); /** * Default constructor */ - public ScriptRunner() { - stopOnError = false; - autoCommit = false; - } - - /** - * Constructor to allow passing in a Map with configuration data - * - * @param props - the configuration properties - */ - public ScriptRunner(Map props) { - setDriver((String) props.get("driver")); - setUrl((String) props.get("url")); - setUsername((String) props.get("username")); - setPassword((String) props.get("password")); - setStopOnError("true".equals(props.get("stopOnError"))); - setAutoCommit("true".equals(props.get("autoCommit"))); - } - - /** - * Getter for stopOnError property - * - * @return The value of the stopOnError property - */ - public boolean isStopOnError() { - return stopOnError; - } - - /** - * Setter for stopOnError property - * - * @param stopOnError - the new value of the stopOnError property - */ - public void setStopOnError(boolean stopOnError) { + public ScriptRunner(Connection connection, boolean autoCommit, boolean stopOnError) { + this.connection = connection; + this.autoCommit = autoCommit; this.stopOnError = stopOnError; } - /** - * Getter for autoCommit property - * - * @return The value of the autoCommit property - */ - public boolean isAutoCommit() { - return autoCommit; - } - - /** - * Setter for autoCommit property - * - * @param autoCommit - the new value of the autoCommit property - */ - public void setAutoCommit(boolean autoCommit) { + public ScriptRunner(String driver, String url, String username, String password, boolean autoCommit, boolean stopOnError) { + this.driver = driver; + this.url = url; + this.username = username; + this.password = password; this.autoCommit = autoCommit; - } - - /** - * Getter for logWriter property - * - * @return The value of the logWriter property - */ - public PrintWriter getLogWriter() { - return logWriter; + this.stopOnError = stopOnError; } /** @@ -120,15 +72,6 @@ } /** - * Getter for errorLogWriter property - * - * @return The value of the errorLogWriter property - */ - public PrintWriter getErrorLogWriter() { - return errorLogWriter; - } - - /** * Setter for errorLogWriter property * * @param errorLogWriter - the new value of the errorLogWriter property @@ -138,97 +81,41 @@ } /** - * Getter for driver property - * - * @return The value of the driver property - */ - public String getDriver() { - return driver; - } - - /** - * Setter for driver property - * - * @param driver - the new value of the driver property - */ - public void setDriver(String driver) { - this.driver = driver; - } - - /** - * Getter for url property - * - * @return The value of the url property - */ - public String getUrl() { - return url; - } - - /** - * Setter for url property - * - * @param url - the new value of the url property - */ - public void setUrl(String url) { - this.url = url; - } - - /** - * Getter for username property - * - * @return The value of the username property - */ - public String getUsername() { - return username; - } - - /** - * Setter for username property - * - * @param username - the new value of the username property - */ - public void setUsername(String username) { - this.username = username; - } - - /** - * Getter for password property - * - * @return The value of the password property - */ - public String getPassword() { - return password; - } - - /** - * Setter for password property - * - * @param password - the new value of the password property - */ - public void setPassword(String password) { - this.password = password; - } - - /** * Runs an SQL script (read in using the Reader parameter) * * @param reader - the source of the script - * @throws ClassNotFoundException if the driver class cannot be found - * @throws SQLException if any SQL errors occur - * @throws IOException if there is an error reading from the Reader - * @throws IllegalAccessException if there are problems creating the driver class - * @throws InstantiationException if there are problems creating the driver class */ - public void runScript(Reader reader) - throws ClassNotFoundException, SQLException, IOException, - IllegalAccessException, InstantiationException { - DriverManager.registerDriver((Driver) Resources.classForName(driver).newInstance()); - Connection conn = DriverManager.getConnection(url, username, password); - if (conn.getAutoCommit() != autoCommit) { - conn.setAutoCommit(autoCommit); + public void runScript(Reader reader) throws IOException, SQLException{ + try { + if (connection == null) { + DriverManager.registerDriver((Driver) Resources.classForName(driver).newInstance()); + Connection conn = DriverManager.getConnection(url, username, password); + try { + if (conn.getAutoCommit() != autoCommit) { + conn.setAutoCommit(autoCommit); + } + runScript(conn, reader); + } finally { + conn.close(); + } + } else { + boolean originalAutoCommit = connection.getAutoCommit(); + try { + if (originalAutoCommit != this.autoCommit) { + connection.setAutoCommit(this.autoCommit); + } + runScript(connection, reader); + } finally { + connection.setAutoCommit(originalAutoCommit); + } + } + } catch (IOException e) { + throw e; + } catch (SQLException e) { + throw e; + } catch (Exception e) { + throw new NestedRuntimeException("Error running script. Cause: " + e, e); } - runScript(conn, reader); - conn.close(); } /** @@ -239,7 +126,7 @@ * @throws SQLException if any SQL errors occur * @throws IOException if there is an error reading from the Reader */ - public void runScript(Connection conn, Reader reader) + private void runScript(Connection conn, Reader reader) throws IOException, SQLException { StringBuffer command = null; try { @@ -252,20 +139,16 @@ String trimmedLine = line.trim(); if (trimmedLine.startsWith("--")) { println(trimmedLine); -// if (log.isDebugEnabled()) { -// log.debug(trimmedLine); -// } } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//")) { //Do nothing + } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--")) { + //Do nothing } else if (trimmedLine.endsWith(";")) { command.append(line.substring(0, line.lastIndexOf(";"))); command.append(" "); Statement statement = conn.createStatement(); println(command); -// if (log.isDebugEnabled()) { -// log.debug(command); -// } boolean hasResults = false; if (stopOnError) { @@ -321,13 +204,11 @@ e.fillInStackTrace(); printlnError("Error executing: " + command); printlnError(e); -// log.error("Error executing: " + command, e); throw e; } catch (IOException e) { e.fillInStackTrace(); printlnError("Error executing: " + command); printlnError(e); -// log.error("Error executing: " + command, e); throw e; } finally { conn.rollback(); Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapClasspathEntityResolver.java URL: http://svn.apache.org/viewcvs/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapClasspathEntityResolver.java?rev=331131&r1=331130&r2=331131&view=diff ============================================================================== --- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapClasspathEntityResolver.java (original) +++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapClasspathEntityResolver.java Sun Nov 6 10:30:30 2005 @@ -36,15 +36,15 @@ private static final Map doctypeMap = new HashMap(); static { - doctypeMap.put("http://www.ibatis.com/dtd/sql-map-config-2.dtd", SQL_MAP_CONFIG_DTD); - doctypeMap.put("http://ibatis.apache.org/dtd/sql-map-config-2.dtd", SQL_MAP_CONFIG_DTD); - doctypeMap.put("-//iBATIS.com//DTD SQL Map Config 2.0//EN", SQL_MAP_CONFIG_DTD); - doctypeMap.put("-//ibatis.apache.org//DTD SQL Map Config 2.0//EN", SQL_MAP_CONFIG_DTD); + doctypeMap.put("http://www.ibatis.com/dtd/sql-map-config-2.dtd".toUpperCase(), SQL_MAP_CONFIG_DTD); + doctypeMap.put("http://ibatis.apache.org/dtd/sql-map-config-2.dtd".toUpperCase(), SQL_MAP_CONFIG_DTD); + doctypeMap.put("-//iBATIS.com//DTD SQL Map Config 2.0//EN".toUpperCase(), SQL_MAP_CONFIG_DTD); + doctypeMap.put("-//ibatis.apache.org//DTD SQL Map Config 2.0//EN".toUpperCase(), SQL_MAP_CONFIG_DTD); - doctypeMap.put("http://www.ibatis.com/dtd/sql-map-2.dtd", SQL_MAP_DTD); - doctypeMap.put("http://ibatis.apache.org/dtd/sql-map-2.dtd", SQL_MAP_DTD); - doctypeMap.put("-//iBATIS.com//DTD SQL Map 2.0//EN", SQL_MAP_DTD); - doctypeMap.put("-//ibatis.apache.org//DTD SQL Map 2.0//EN", SQL_MAP_DTD); + doctypeMap.put("http://www.ibatis.com/dtd/sql-map-2.dtd".toUpperCase(), SQL_MAP_DTD); + doctypeMap.put("http://ibatis.apache.org/dtd/sql-map-2.dtd".toUpperCase(), SQL_MAP_DTD); + doctypeMap.put("-//iBATIS.com//DTD SQL Map 2.0//EN".toUpperCase(), SQL_MAP_DTD); + doctypeMap.put("-//ibatis.apache.org//DTD SQL Map 2.0//EN".toUpperCase(), SQL_MAP_DTD); } @@ -58,6 +58,10 @@ */ public InputSource resolveEntity(String publicId, String systemId) throws SAXException { + + if (publicId != null) publicId = publicId.toUpperCase(); + if (systemId != null) systemId = systemId.toUpperCase(); + InputSource source = null; try { String path = (String) doctypeMap.get(publicId); Modified: ibatis/trunk/java/mapper/mapper2/test/com/ibatis/dao/BaseDaoTest.java URL: http://svn.apache.org/viewcvs/ibatis/trunk/java/mapper/mapper2/test/com/ibatis/dao/BaseDaoTest.java?rev=331131&r1=331130&r2=331131&view=diff ============================================================================== --- ibatis/trunk/java/mapper/mapper2/test/com/ibatis/dao/BaseDaoTest.java (original) +++ ibatis/trunk/java/mapper/mapper2/test/com/ibatis/dao/BaseDaoTest.java Sun Nov 6 10:30:30 2005 @@ -1,8 +1,8 @@ package com.ibatis.dao; +import com.ibatis.common.exception.NestedRuntimeException; import com.ibatis.common.jdbc.ScriptRunner; import com.ibatis.common.resources.Resources; -import com.ibatis.common.exception.NestedRuntimeException; import com.ibatis.dao.client.DaoManager; import com.ibatis.dao.client.DaoTransaction; import com.ibatis.dao.engine.transaction.jdbc.JdbcDaoTransaction; @@ -15,8 +15,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.UndeclaredThrowableException; import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; public abstract class BaseDaoTest extends TestCase { @@ -141,12 +139,11 @@ Reader reader = Resources.getResourceAsReader(script); - ScriptRunner runner = new ScriptRunner(); - runner.setStopOnError(false); + ScriptRunner runner = new ScriptRunner(conn, false, false); runner.setLogWriter(null); runner.setErrorLogWriter(null); - runner.runScript(conn, reader); + runner.runScript(reader); daoManager.commitTransaction(); } Modified: ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/BaseSqlMapTest.java URL: http://svn.apache.org/viewcvs/ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/BaseSqlMapTest.java?rev=331131&r1=331130&r2=331131&view=diff ============================================================================== --- ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/BaseSqlMapTest.java (original) +++ ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/BaseSqlMapTest.java Sun Nov 6 10:30:30 2005 @@ -10,10 +10,7 @@ import javax.sql.DataSource; import java.io.Reader; -import java.io.PrintWriter; import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; import java.util.*; public class BaseSqlMapTest extends TestCase { @@ -33,12 +30,11 @@ Reader reader = Resources.getResourceAsReader(script); - ScriptRunner runner = new ScriptRunner(); - runner.setStopOnError(false); + ScriptRunner runner = new ScriptRunner(conn, false, false); runner.setLogWriter(null); runner.setErrorLogWriter(null); - runner.runScript(conn, reader); + runner.runScript(reader); conn.commit(); conn.close(); reader.close(); Modified: ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/builder/xml/SqlMapClasspathEntityResolverTest.java URL: http://svn.apache.org/viewcvs/ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/builder/xml/SqlMapClasspathEntityResolverTest.java?rev=331131&r1=331130&r2=331131&view=diff ============================================================================== --- ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/builder/xml/SqlMapClasspathEntityResolverTest.java (original) +++ ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/builder/xml/SqlMapClasspathEntityResolverTest.java Sun Nov 6 10:30:30 2005 @@ -51,6 +51,11 @@ assertPublicIdCanBeResolved(id); } + public void testOddCase() { + String id = "-//iBATIS.apache.org//DTD SQL Map 2.0//EN"; + assertPublicIdCanBeResolved(id); + } + private void assertSystemIdCanBeResolved(String id) { SqlMapClasspathEntityResolver resolver = new SqlMapClasspathEntityResolver(); try { Modified: ibatis/trunk/java/mapper/mapper2/test/compatibility/scriptrunner/ScriptRunnerCompat.java URL: http://svn.apache.org/viewcvs/ibatis/trunk/java/mapper/mapper2/test/compatibility/scriptrunner/ScriptRunnerCompat.java?rev=331131&r1=331130&r2=331131&view=diff ============================================================================== --- ibatis/trunk/java/mapper/mapper2/test/compatibility/scriptrunner/ScriptRunnerCompat.java (original) +++ ibatis/trunk/java/mapper/mapper2/test/compatibility/scriptrunner/ScriptRunnerCompat.java Sun Nov 6 10:30:30 2005 @@ -11,13 +11,10 @@ public class ScriptRunnerCompat { public static void runInitializationScript(Connection conn, String script) - throws SQLException, IOException { + throws IOException, SQLException { // -- Create a new ScriptRunner instance using default constructor. - ScriptRunner runner = new ScriptRunner(); - - // -- Another constructor can accept database configuration information as a property map. - // ScriptRunner runner = new ScriptRunner (properties); + ScriptRunner runner = new ScriptRunner(conn, false, false); // -- You can configure logwriters for progress and error reporting. // -- Default is System.out and System.err, we'll keep them quiet for the demo. @@ -28,10 +25,8 @@ Reader reader = Resources.getResourceAsReader(script); // -- Run the script from the Reader - runner.runScript(conn, reader); + runner.runScript(reader); - // -- If the ScriptRunner was configured with a properties file, you don't need to pass in a connection - // runner.runInitializationScript(reader); }