ScriptUtils.executeSqlScript logs SQLWarnings at debug level

Issue: SPR-13959
This commit is contained in:
Juergen Hoeller 2016-02-18 17:37:22 +01:00
parent 8495fcf109
commit 923534595c
2 changed files with 31 additions and 21 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 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.
@ -80,8 +80,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
/** /**
* Construct a new {@code ResourceDatabasePopulator} with default settings * Construct a new {@code ResourceDatabasePopulator} with default settings
* for the supplied scripts. * for the supplied scripts.
* @param scripts the scripts to execute to initialize or clean up the database; * @param scripts the scripts to execute to initialize or clean up the database
* never {@code null} * (never {@code null})
* @since 4.0.3 * @since 4.0.3
*/ */
public ResourceDatabasePopulator(Resource... scripts) { public ResourceDatabasePopulator(Resource... scripts) {
@ -97,21 +97,23 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* statement can be ignored * statement can be ignored
* @param sqlScriptEncoding the encoding for the supplied SQL scripts; may * @param sqlScriptEncoding the encoding for the supplied SQL scripts; may
* be {@code null} or <em>empty</em> to indicate platform encoding * be {@code null} or <em>empty</em> to indicate platform encoding
* @param scripts the scripts to execute to initialize or clean up the database; * @param scripts the scripts to execute to initialize or clean up the database
* never {@code null} * (never {@code null})
* @since 4.0.3 * @since 4.0.3
*/ */
public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, String sqlScriptEncoding, public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops,
Resource... scripts) { String sqlScriptEncoding, Resource... scripts) {
this(scripts); this(scripts);
this.continueOnError = continueOnError; this.continueOnError = continueOnError;
this.ignoreFailedDrops = ignoreFailedDrops; this.ignoreFailedDrops = ignoreFailedDrops;
setSqlScriptEncoding(sqlScriptEncoding); setSqlScriptEncoding(sqlScriptEncoding);
} }
/** /**
* Add a script to execute to initialize or clean up the database. * Add a script to execute to initialize or clean up the database.
* @param script the path to an SQL script; never {@code null} * @param script the path to an SQL script (never {@code null})
*/ */
public void addScript(Resource script) { public void addScript(Resource script) {
Assert.notNull(script, "Script must not be null"); Assert.notNull(script, "Script must not be null");
@ -120,7 +122,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
/** /**
* Add multiple scripts to execute to initialize or clean up the database. * Add multiple scripts to execute to initialize or clean up the database.
* @param scripts the scripts to execute; never {@code null} * @param scripts the scripts to execute (never {@code null})
*/ */
public void addScripts(Resource... scripts) { public void addScripts(Resource... scripts) {
assertContentsOfScriptArray(scripts); assertContentsOfScriptArray(scripts);
@ -130,7 +132,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
/** /**
* Set the scripts to execute to initialize or clean up the database, * Set the scripts to execute to initialize or clean up the database,
* replacing any previously added scripts. * replacing any previously added scripts.
* @param scripts the scripts to execute; never {@code null} * @param scripts the scripts to execute (never {@code null})
*/ */
public void setScripts(Resource... scripts) { public void setScripts(Resource... scripts) {
assertContentsOfScriptArray(scripts); assertContentsOfScriptArray(scripts);
@ -173,8 +175,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* Set the start delimiter that identifies block comments within the SQL * Set the start delimiter that identifies block comments within the SQL
* scripts. * scripts.
* <p>Defaults to {@code "/*"}. * <p>Defaults to {@code "/*"}.
* @param blockCommentStartDelimiter the start delimiter for block comments; * @param blockCommentStartDelimiter the start delimiter for block comments
* never {@code null} or empty * (never {@code null} or empty)
* @since 4.0.3 * @since 4.0.3
* @see #setBlockCommentEndDelimiter * @see #setBlockCommentEndDelimiter
*/ */
@ -187,8 +189,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* Set the end delimiter that identifies block comments within the SQL * Set the end delimiter that identifies block comments within the SQL
* scripts. * scripts.
* <p>Defaults to <code>"*&#47;"</code>. * <p>Defaults to <code>"*&#47;"</code>.
* @param blockCommentEndDelimiter the end delimiter for block comments; * @param blockCommentEndDelimiter the end delimiter for block comments
* never {@code null} or empty * (never {@code null} or empty)
* @since 4.0.3 * @since 4.0.3
* @see #setBlockCommentStartDelimiter * @see #setBlockCommentStartDelimiter
*/ */
@ -227,8 +229,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
Assert.notNull(connection, "Connection must not be null"); Assert.notNull(connection, "Connection must not be null");
for (Resource script : getScripts()) { for (Resource script : getScripts()) {
ScriptUtils.executeSqlScript(connection, encodeScript(script), this.continueOnError, ScriptUtils.executeSqlScript(connection, encodeScript(script), this.continueOnError,
this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter, this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter,
this.blockCommentEndDelimiter); this.blockCommentEndDelimiter);
} }
} }
@ -236,7 +238,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* Execute this {@code ResourceDatabasePopulator} against the given * Execute this {@code ResourceDatabasePopulator} against the given
* {@link DataSource}. * {@link DataSource}.
* <p>Delegates to {@link DatabasePopulatorUtils#execute}. * <p>Delegates to {@link DatabasePopulatorUtils#execute}.
* @param dataSource the {@code DataSource} to execute against; never {@code null} * @param dataSource the {@code DataSource} to execute against (never {@code null})
* @throws ScriptException if an error occurs * @throws ScriptException if an error occurs
* @since 4.1 * @since 4.1
* @see #populate(Connection) * @see #populate(Connection)
@ -254,7 +256,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* {@link EncodedResource} is not a sub-type of {@link Resource}. Thus we * {@link EncodedResource} is not a sub-type of {@link Resource}. Thus we
* always need to wrap each script resource in an {@code EncodedResource} * always need to wrap each script resource in an {@code EncodedResource}
* using the configured {@linkplain #setSqlScriptEncoding encoding}. * using the configured {@linkplain #setSqlScriptEncoding encoding}.
* @param script the script to wrap; never {@code null} * @param script the script to wrap (never {@code null})
*/ */
private EncodedResource encodeScript(Resource script) { private EncodedResource encodeScript(Resource script) {
Assert.notNull(script, "Script must not be null"); Assert.notNull(script, "Script must not be null");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement; import java.sql.Statement;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -399,7 +400,7 @@ public abstract class ScriptUtils {
*/ */
public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException { public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException {
executeSqlScript(connection, resource, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, executeSqlScript(connection, resource, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR,
DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
} }
/** /**
@ -472,7 +473,14 @@ public abstract class ScriptUtils {
stmt.execute(statement); stmt.execute(statement);
int rowsAffected = stmt.getUpdateCount(); int rowsAffected = stmt.getUpdateCount();
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(rowsAffected + " returned as updateCount for SQL: " + statement); logger.debug(rowsAffected + " returned as update count for SQL: " + statement);
SQLWarning warningToLog = stmt.getWarnings();
while (warningToLog != null) {
logger.debug("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() +
"', error code '" + warningToLog.getErrorCode() +
"', message [" + warningToLog.getMessage() + "]");
warningToLog = warningToLog.getNextWarning();
}
} }
} }
catch (SQLException ex) { catch (SQLException ex) {