ScriptUtils properly refers to statement numbers instead of line numbers

Issue: SPR-12752
This commit is contained in:
Juergen Hoeller 2015-02-25 18:28:23 +01:00
parent cdc04e55fe
commit 8f228d1db4
1 changed files with 15 additions and 19 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { public abstract class ScriptUtils {
private static final Log logger = LogFactory.getLog(ScriptUtils.class);
/** /**
* Default statement separator within SQL scripts: {@code ";"}. * Default statement separator within SQL scripts: {@code ";"}.
*/ */
@ -89,12 +87,8 @@ public abstract class ScriptUtils {
public static final String DEFAULT_BLOCK_COMMENT_END_DELIMITER = "*/"; public static final String DEFAULT_BLOCK_COMMENT_END_DELIMITER = "*/";
/** private static final Log logger = LogFactory.getLog(ScriptUtils.class);
* Prevent instantiation of this utility class.
*/
private ScriptUtils() {
/* no-op */
}
/** /**
* Split an SQL script into separate statements delimited by the provided * 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<String> statements) throws ScriptException { public static void splitSqlScript(String script, String separator, List<String> statements) throws ScriptException {
splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIX, DEFAULT_BLOCK_COMMENT_START_DELIMITER, 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) private static String readScript(EncodedResource resource, String commentPrefix, String separator)
throws IOException { throws IOException {
LineNumberReader lnr = new LineNumberReader(resource.getReader()); LineNumberReader lnr = new LineNumberReader(resource.getReader());
try { try {
return readScript(lnr, commentPrefix, separator); return readScript(lnr, commentPrefix, separator);
@ -302,6 +297,7 @@ public abstract class ScriptUtils {
*/ */
public static String readScript(LineNumberReader lineNumberReader, String commentPrefix, String separator) public static String readScript(LineNumberReader lineNumberReader, String commentPrefix, String separator)
throws IOException { throws IOException {
String currentStatement = lineNumberReader.readLine(); String currentStatement = lineNumberReader.readLine();
StringBuilder scriptBuilder = new StringBuilder(); StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) { while (currentStatement != null) {
@ -431,9 +427,8 @@ public abstract class ScriptUtils {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("Executing SQL script from " + resource); logger.info("Executing SQL script from " + resource);
} }
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
List<String> statements = new LinkedList<String>();
String script; String script;
try { try {
script = readScript(resource, commentPrefix, separator); script = readScript(resource, commentPrefix, separator);
@ -449,13 +444,15 @@ public abstract class ScriptUtils {
separator = FALLBACK_STATEMENT_SEPARATOR; separator = FALLBACK_STATEMENT_SEPARATOR;
} }
List<String> statements = new LinkedList<String>();
splitSqlScript(resource, script, separator, commentPrefix, blockCommentStartDelimiter, splitSqlScript(resource, script, separator, commentPrefix, blockCommentStartDelimiter,
blockCommentEndDelimiter, statements); blockCommentEndDelimiter, statements);
int lineNumber = 0;
int stmtNumber = 0;
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
try { try {
for (String statement : statements) { for (String statement : statements) {
lineNumber++; stmtNumber++;
try { try {
stmt.execute(statement); stmt.execute(statement);
int rowsAffected = stmt.getUpdateCount(); int rowsAffected = stmt.getUpdateCount();
@ -467,12 +464,12 @@ public abstract class ScriptUtils {
boolean dropStatement = StringUtils.startsWithIgnoreCase(statement.trim(), "drop"); boolean dropStatement = StringUtils.startsWithIgnoreCase(statement.trim(), "drop");
if (continueOnError || (dropStatement && ignoreFailedDrops)) { if (continueOnError || (dropStatement && ignoreFailedDrops)) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Failed to execute SQL script statement at line " + lineNumber logger.debug("Failed to execute SQL script statement #" + stmtNumber +
+ " of resource " + resource + ": " + statement, ex); " of resource " + resource + ": " + statement, ex);
} }
} }
else { 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) { if (ex instanceof ScriptException) {
throw (ScriptException) ex; throw (ScriptException) ex;
} }
throw new UncategorizedScriptException( throw new UncategorizedScriptException(
"Failed to execute database script from resource [" + resource + "]", ex); "Failed to execute database script from resource [" + resource + "]", ex);
} }