From 9ae6268a1a891f67ef3443bc17594248da09c75e Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 14 May 2021 15:15:12 +0200 Subject: [PATCH] Polish ScriptUtils implementation --- .../jdbc/datasource/init/ScriptUtils.java | 33 +++++++------------ .../datasource/init/ScriptUtilsUnitTests.java | 3 +- 2 files changed, 13 insertions(+), 23 deletions(-) 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 6397c45d48..789007521a 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 @@ -296,17 +296,6 @@ public abstract class ScriptUtils { } } - /** - * Read a script from the given resource, using "{@code --}" as the comment prefix - * and "{@code ;}" as the statement separator, and build a String containing the lines. - * @param resource the {@code EncodedResource} to be read - * @return {@code String} containing the script lines - * @throws IOException in case of I/O errors - */ - static String readScript(EncodedResource resource) throws IOException { - return readScript(resource, DEFAULT_COMMENT_PREFIXES, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_END_DELIMITER); - } - /** * Read a script from the provided resource, using the supplied comment prefixes * and statement separator, and build a {@code String} containing the lines. @@ -315,15 +304,15 @@ public abstract class ScriptUtils { * within a statement — will be included in the results. * @param resource the {@code EncodedResource} containing the script * to be processed + * @param separator the statement separator in the SQL script (typically ";") * @param commentPrefixes the prefixes that identify comments in the SQL script * (typically "--") - * @param separator the statement separator in the SQL script (typically ";") * @param blockCommentEndDelimiter the end block comment delimiter * @return a {@code String} containing the script lines * @throws IOException in case of I/O errors */ - private static String readScript(EncodedResource resource, @Nullable String[] commentPrefixes, - @Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException { + static String readScript(EncodedResource resource, @Nullable String separator, + @Nullable String[] commentPrefixes, @Nullable String blockCommentEndDelimiter) throws IOException { try (LineNumberReader lnr = new LineNumberReader(resource.getReader())) { return readScript(lnr, commentPrefixes, separator, blockCommentEndDelimiter); @@ -339,18 +328,18 @@ public abstract class ScriptUtils { * a statement — will be included in the results. * @param lineNumberReader the {@code LineNumberReader} containing the script * to be processed - * @param lineCommentPrefix the prefix that identifies comments in the SQL script + * @param commentPrefix the prefix that identifies comments in the SQL script * (typically "--") * @param separator the statement separator in the SQL script (typically ";") * @param blockCommentEndDelimiter the end block comment delimiter * @return a {@code String} containing the script lines * @throws IOException in case of I/O errors */ - public static String readScript(LineNumberReader lineNumberReader, @Nullable String lineCommentPrefix, + public static String readScript(LineNumberReader lineNumberReader, @Nullable String commentPrefix, @Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException { - String[] lineCommentPrefixes = (lineCommentPrefix != null) ? new String[] { lineCommentPrefix } : null; - return readScript(lineNumberReader, lineCommentPrefixes, separator, blockCommentEndDelimiter); + String[] commentPrefixes = (commentPrefix != null) ? new String[] { commentPrefix } : null; + return readScript(lineNumberReader, commentPrefixes, separator, blockCommentEndDelimiter); } /** @@ -362,7 +351,7 @@ public abstract class ScriptUtils { * within a statement — will be included in the results. * @param lineNumberReader the {@code LineNumberReader} containing the script * to be processed - * @param lineCommentPrefixes the prefixes that identify comments in the SQL script + * @param commentPrefixes the prefixes that identify comments in the SQL script * (typically "--") * @param separator the statement separator in the SQL script (typically ";") * @param blockCommentEndDelimiter the end block comment delimiter @@ -370,14 +359,14 @@ public abstract class ScriptUtils { * @throws IOException in case of I/O errors * @since 5.2 */ - public static String readScript(LineNumberReader lineNumberReader, @Nullable String[] lineCommentPrefixes, + public static String readScript(LineNumberReader lineNumberReader, @Nullable String[] commentPrefixes, @Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException { String currentStatement = lineNumberReader.readLine(); StringBuilder scriptBuilder = new StringBuilder(); while (currentStatement != null) { if ((blockCommentEndDelimiter != null && currentStatement.contains(blockCommentEndDelimiter)) || - (lineCommentPrefixes != null && !startsWithAny(currentStatement, lineCommentPrefixes, 0))) { + (commentPrefixes != null && !startsWithAny(currentStatement, commentPrefixes, 0))) { if (scriptBuilder.length() > 0) { scriptBuilder.append('\n'); } @@ -642,7 +631,7 @@ public abstract class ScriptUtils { String script; try { - script = readScript(resource, commentPrefixes, separator, blockCommentEndDelimiter); + script = readScript(resource, separator, commentPrefixes, blockCommentEndDelimiter); } catch (IOException ex) { throw new CannotReadScriptException(resource, ex); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java index 1abda8e0d5..ec5c832301 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java @@ -206,7 +206,8 @@ public class ScriptUtilsUnitTests { private String readScript(String path) throws Exception { EncodedResource resource = new EncodedResource(new ClassPathResource(path, getClass())); - return ScriptUtils.readScript(resource); + return ScriptUtils.readScript(resource, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_COMMENT_PREFIXES, + DEFAULT_BLOCK_COMMENT_END_DELIMITER); } }