diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java index 0ada68a12c9..00894080afe 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,8 +48,6 @@ import org.springframework.util.StringUtils; */ public abstract class ScriptUtils { - private static final Log logger = LogFactory.getLog(ScriptUtils.class); - /** * Default statement separator within SQL scripts: {@code ";"}. */ @@ -89,12 +87,8 @@ public abstract class ScriptUtils { public static final String DEFAULT_BLOCK_COMMENT_END_DELIMITER = "*/"; - /** - * Prevent instantiation of this utility class. - */ - private ScriptUtils() { - /* no-op */ - } + private static final Log logger = LogFactory.getLog(ScriptUtils.class); + /** * Split an SQL script into separate statements delimited by the provided @@ -140,7 +134,7 @@ public abstract class ScriptUtils { */ public static void splitSqlScript(String script, String separator, List statements) throws ScriptException { splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIX, DEFAULT_BLOCK_COMMENT_START_DELIMITER, - DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements); + DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements); } /** @@ -276,6 +270,7 @@ public abstract class ScriptUtils { */ private static String readScript(EncodedResource resource, String commentPrefix, String separator) throws IOException { + LineNumberReader lnr = new LineNumberReader(resource.getReader()); try { return readScript(lnr, commentPrefix, separator); @@ -302,6 +297,7 @@ public abstract class ScriptUtils { */ public static String readScript(LineNumberReader lineNumberReader, String commentPrefix, String separator) throws IOException { + String currentStatement = lineNumberReader.readLine(); StringBuilder scriptBuilder = new StringBuilder(); while (currentStatement != null) { @@ -431,9 +427,8 @@ public abstract class ScriptUtils { if (logger.isInfoEnabled()) { logger.info("Executing SQL script from " + resource); } - long startTime = System.currentTimeMillis(); - List statements = new LinkedList(); + String script; try { script = readScript(resource, commentPrefix, separator); @@ -449,13 +444,15 @@ public abstract class ScriptUtils { separator = FALLBACK_STATEMENT_SEPARATOR; } + List statements = new LinkedList(); splitSqlScript(resource, script, separator, commentPrefix, blockCommentStartDelimiter, - blockCommentEndDelimiter, statements); - int lineNumber = 0; + blockCommentEndDelimiter, statements); + + int stmtNumber = 0; Statement stmt = connection.createStatement(); try { for (String statement : statements) { - lineNumber++; + stmtNumber++; try { stmt.execute(statement); int rowsAffected = stmt.getUpdateCount(); @@ -467,12 +464,12 @@ public abstract class ScriptUtils { boolean dropStatement = StringUtils.startsWithIgnoreCase(statement.trim(), "drop"); if (continueOnError || (dropStatement && ignoreFailedDrops)) { if (logger.isDebugEnabled()) { - logger.debug("Failed to execute SQL script statement at line " + lineNumber - + " of resource " + resource + ": " + statement, ex); + logger.debug("Failed to execute SQL script statement #" + stmtNumber + + " of resource " + resource + ": " + statement, ex); } } else { - throw new ScriptStatementFailedException(statement, lineNumber, resource, ex); + throw new ScriptStatementFailedException(statement, stmtNumber, resource, ex); } } } @@ -495,7 +492,6 @@ public abstract class ScriptUtils { if (ex instanceof ScriptException) { throw (ScriptException) ex; } - throw new UncategorizedScriptException( "Failed to execute database script from resource [" + resource + "]", ex); }