Skip to content

Commit 448c99f

Browse files
committed
Introduce buildErrorMessage() utility in ScriptStatementFailedException
To eliminate code duplication, ScriptStatementFailedException and ScriptUtils now delegate to a new buildErrorMessage() utility method. Issue: SPR-12752
1 parent fe8289b commit 448c99f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptStatementFailedException.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
@SuppressWarnings("serial")
3030
public class ScriptStatementFailedException extends ScriptException {
3131

32+
/**
33+
* Build an error message based on the supplied parameters.
34+
* @param statement the actual SQL statement that failed
35+
* @param statementNumber the statement number in the SQL script (i.e.,
36+
* the nth statement present in the resource)
37+
* @param encodedResource the resource from which the SQL statement was read
38+
*/
39+
public static String buildErrorMessage(String statement, int statementNumber, EncodedResource encodedResource) {
40+
return String.format("Failed to execute SQL script statement #%s of %s: %s",
41+
statementNumber, encodedResource, statement);
42+
}
43+
3244
/**
3345
* Construct a new {@code ScriptStatementFailedException}.
3446
* @param statement the actual SQL statement that failed
@@ -38,8 +50,7 @@ public class ScriptStatementFailedException extends ScriptException {
3850
* @param cause the underlying cause of the failure
3951
*/
4052
public ScriptStatementFailedException(String statement, int statementNumber, EncodedResource resource, Throwable cause) {
41-
super("Failed to execute SQL script statement #" + statementNumber + " of resource " + resource + ": "
42-
+ statement, cause);
53+
super(buildErrorMessage(statement, statementNumber, resource), cause);
4354
}
4455

4556
}

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
464464
boolean dropStatement = StringUtils.startsWithIgnoreCase(statement.trim(), "drop");
465465
if (continueOnError || (dropStatement && ignoreFailedDrops)) {
466466
if (logger.isDebugEnabled()) {
467-
logger.debug("Failed to execute SQL script statement #" + stmtNumber +
468-
" of resource " + resource + ": " + statement, ex);
467+
logger.debug(ScriptStatementFailedException.buildErrorMessage(statement, stmtNumber, resource), ex);
469468
}
470469
}
471470
else {

0 commit comments

Comments
 (0)