diff --git a/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java b/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java index 87d33221bf7c..8c2370f7a7e2 100644 --- a/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java +++ b/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java @@ -120,7 +120,7 @@ public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) { /** * Execute the given SQL script. *
The script will typically be loaded from the classpath. There should - * be one statement per line. Any semicolons will be removed. + * be one statement per line. Any semicolons and line comments will be removed. *
Do not use this method to execute DDL if you expect rollback. * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param resourceLoader the resource loader with which to load the SQL script @@ -130,6 +130,7 @@ public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) { * @throws DataAccessException if there is an error executing a statement * and {@code continueOnError} is {@code false} * @see ResourceDatabasePopulator + * @see #executeSqlScript(JdbcTemplate, Resource, boolean) */ public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader, String sqlResourcePath, boolean continueOnError) throws DataAccessException { @@ -142,7 +143,8 @@ public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader re *
The script will typically be loaded from the classpath. Statements * should be delimited with a semicolon. If statements are not delimited with * a semicolon then there should be one statement per line. Statements are - * allowed to span lines only if they are delimited with a semicolon. + * allowed to span lines only if they are delimited with a semicolon. Any + * line comments will be removed. *
Do not use this method to execute DDL if you expect rollback. * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param resource the resource to load the SQL script from @@ -151,6 +153,7 @@ public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader re * @throws DataAccessException if there is an error executing a statement * and {@code continueOnError} is {@code false} * @see ResourceDatabasePopulator + * @see #executeSqlScript(JdbcTemplate, EncodedResource, boolean) */ public static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource, boolean continueOnError) throws DataAccessException { @@ -160,7 +163,7 @@ public static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource /** * Execute the given SQL script. *
The script will typically be loaded from the classpath. There should - * be one statement per line. Any semicolons will be removed. + * be one statement per line. Any semicolons and line comments will be removed. *
Do not use this method to execute DDL if you expect rollback. * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param resource the resource (potentially associated with a specific encoding) @@ -245,9 +248,12 @@ public static String readScript(LineNumberReader lineNumberReader) throws IOExce /** * Read a script from the provided {@code LineNumberReader}, using the supplied * comment prefix, and build a {@code String} containing the lines. + *
Lines beginning with the comment prefix are excluded from the
+ * results; however, line comments anywhere else — for example, within
+ * a statement — will be included in the results.
* @param lineNumberReader the {@code LineNumberReader} containing the script
* to be processed
- * @param commentPrefix the line prefix that identifies comments in the SQL script
+ * @param commentPrefix the prefix that identifies comments in the SQL script — typically "--"
* @return a {@code String} containing the script lines
*/
public static String readScript(LineNumberReader lineNumberReader, String commentPrefix) throws IOException {
@@ -287,25 +293,35 @@ public static boolean containsSqlScriptDelimiters(String script, char delim) {
}
/**
- * Split an SQL script into separate statements delimited with the provided
+ * Split an SQL script into separate statements delimited by the provided
* delimiter character. Each individual statement will be added to the
* provided List
.
+ *
Within a statement, "{@code --}" will be used as the comment prefix;
+ * any text beginning with the comment prefix and extending to the end of
+ * the line will be omitted from the statement. In addition, multiple adjacent
+ * whitespace characters will be collapsed into a single space.
* @param script the SQL script
* @param delim character delimiting each statement — typically a ';' character
* @param statements the list that will contain the individual statements
*/
public static void splitSqlScript(String script, char delim, List Within a statement, the provided {@code commentPrefix} will be honored;
+ * any text beginning with the comment prefix and extending to the end of the
+ * line will be omitted from the statement. In addition, multiple adjacent
+ * whitespace characters will be collapsed into a single space.
* @param script the SQL script
* @param delim character delimiting each statement — typically a ';' character
+ * @param commentPrefix the prefix that identifies line comments in the SQL script — typically "--"
* @param statements the List that will contain the individual statements
*/
- private static void splitSqlScript(String script, String delim, List