diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java index 3e6e99c6c1..3a2c339209 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java @@ -85,15 +85,10 @@ public abstract class ScriptUtils { */ public static final String EOF_STATEMENT_SEPARATOR = "^^^ END OF SCRIPT ^^^"; - /** - * Default prefix for single-line comments within SQL scripts: {@code "--"}. - */ - public static final String DEFAULT_COMMENT_PREFIX = "--"; - /** * Default prefixes for single-line comments within SQL scripts: {@code ["--"]}. */ - public static final String[] DEFAULT_COMMENT_PREFIXES = {DEFAULT_COMMENT_PREFIX}; + public static final String[] DEFAULT_COMMENT_PREFIXES = {"--"}; /** * Default start delimiter for block comments within SQL scripts: {@code "/*"}. @@ -109,87 +104,6 @@ public abstract class ScriptUtils { private static final Log logger = LogFactory.getLog(ScriptUtils.class); - /** - * Split an SQL script into separate statements delimited by the provided - * separator character. Each individual statement will be added to the - * provided {@code List}. - *

Within the script, {@value #DEFAULT_COMMENT_PREFIX} 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 output. Similarly, - * {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and - * {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the - * start and end block comment delimiters: any text enclosed - * in a block comment will be omitted from the output. In addition, multiple - * adjacent whitespace characters will be collapsed into a single space. - * @param script the SQL script - * @param separator character separating each statement (typically a ';') - * @param statements the list that will contain the individual statements - * @throws ScriptException if an error occurred while splitting the SQL script - * @see #splitSqlScript(String, String, List) - * @see #splitSqlScript(EncodedResource, String, String, String, String, String, List) - */ - public static void splitSqlScript(String script, char separator, List statements) throws ScriptException { - splitSqlScript(script, String.valueOf(separator), statements); - } - - /** - * Split an SQL script into separate statements delimited by the provided - * separator string. Each individual statement will be added to the - * provided {@code List}. - *

Within the script, {@value #DEFAULT_COMMENT_PREFIX} 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 output. Similarly, - * {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and - * {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the - * start and end block comment delimiters: any text enclosed - * in a block comment will be omitted from the output. In addition, multiple - * adjacent whitespace characters will be collapsed into a single space. - * @param script the SQL script - * @param separator text separating each statement - * (typically a ';' or newline character) - * @param statements the list that will contain the individual statements - * @throws ScriptException if an error occurred while splitting the SQL script - * @see #splitSqlScript(String, char, List) - * @see #splitSqlScript(EncodedResource, String, String, String, String, String, List) - */ - public static void splitSqlScript(String script, String separator, List statements) throws ScriptException { - splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIX, DEFAULT_BLOCK_COMMENT_START_DELIMITER, - DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements); - } - - /** - * Split an SQL script into separate statements delimited by the provided - * separator string. Each individual statement will be added to the provided - * {@code List}. - *

Within the script, 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 output. Similarly, the provided - * {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter} - * delimiters will be honored: any text enclosed in a block comment will be - * omitted from the output. In addition, multiple adjacent whitespace characters - * will be collapsed into a single space. - * @param resource the resource from which the script was read - * @param script the SQL script - * @param separator text separating each statement - * (typically a ';' or newline character) - * @param commentPrefix the prefix that identifies SQL line comments - * (typically "--") - * @param blockCommentStartDelimiter the start block comment delimiter; - * never {@code null} or empty - * @param blockCommentEndDelimiter the end block comment delimiter; - * never {@code null} or empty - * @param statements the list that will contain the individual statements - * @throws ScriptException if an error occurred while splitting the SQL script - */ - public static void splitSqlScript(@Nullable EncodedResource resource, String script, - String separator, String commentPrefix, String blockCommentStartDelimiter, - String blockCommentEndDelimiter, List statements) throws ScriptException { - - Assert.hasText(commentPrefix, "'commentPrefix' must not be null or empty"); - splitSqlScript(resource, script, separator, new String[] { commentPrefix }, - blockCommentStartDelimiter, blockCommentEndDelimiter, statements); - } - /** * Split an SQL script into separate statements delimited by the provided * separator string. Each individual statement will be added to the provided @@ -214,7 +128,7 @@ public abstract class ScriptUtils { * @param statements the list that will contain the individual statements * @throws ScriptException if an error occurred while splitting the SQL script */ - public static void splitSqlScript(@Nullable EncodedResource resource, String script, + static void splitSqlScript(@Nullable EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List statements) throws ScriptException { @@ -303,18 +217,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 - * @param dataBufferFactory the factory to create data buffers with - * @return {@code String} containing the script lines - */ - public static Mono readScript(EncodedResource resource, DataBufferFactory dataBufferFactory) { - return readScript(resource, dataBufferFactory, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_COMMENT_PREFIXES, - 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. @@ -331,7 +233,7 @@ public abstract class ScriptUtils { * @return a {@link Mono} of {@link String} containing the script lines that * completes once the resource was loaded */ - private static Mono readScript(EncodedResource resource, DataBufferFactory dataBufferFactory, + static Mono readScript(EncodedResource resource, DataBufferFactory dataBufferFactory, @Nullable String separator, @Nullable String[] commentPrefixes, @Nullable String blockCommentEndDelimiter) { return DataBufferUtils.join(DataBufferUtils.read(resource.getResource(), dataBufferFactory, 8192)) @@ -353,29 +255,6 @@ public abstract class ScriptUtils { }); } - /** - * Read a script from the provided {@code LineNumberReader}, using the supplied - * comment prefix and statement separator, 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 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 commentPrefix, - @Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException { - - String[] commentPrefixes = (commentPrefix != null) ? new String[] { commentPrefix } : null; - return readScript(lineNumberReader, commentPrefixes, separator, blockCommentEndDelimiter); - } - /** * Read a script from the provided {@code LineNumberReader}, using the supplied * comment prefixes and statement separator, and build a {@code String} containing @@ -392,7 +271,7 @@ public abstract class ScriptUtils { * @return a {@code String} containing the script lines * @throws IOException in case of I/O errors */ - public static String readScript(LineNumberReader lineNumberReader, @Nullable String[] commentPrefixes, + private static String readScript(LineNumberReader lineNumberReader, @Nullable String[] commentPrefixes, @Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException { String currentStatement = lineNumberReader.readLine(); @@ -435,25 +314,6 @@ public abstract class ScriptUtils { return false; } - /** - * Determine if the provided SQL script contains the specified delimiter. - *

This method is intended to be used to find the string delimiting each - * SQL statement — for example, a ';' character. - *

Any occurrence of the delimiter within the script will be ignored if it - * is within a literal block of text enclosed in single quotes - * ({@code '}) or double quotes ({@code "}), if it is escaped with a backslash - * ({@code \}), or if it is within a single-line comment or block comment. - * @param script the SQL script to search within - * @param delimiter the statement delimiter to search for - * @see #DEFAULT_COMMENT_PREFIXES - * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER - * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER - */ - public static boolean containsSqlScriptDelimiters(String script, String delimiter) { - return containsStatementSeparator(null, script, delimiter, DEFAULT_COMMENT_PREFIXES, - DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); - } - /** * Determine if the provided SQL script contains the specified statement separator. *

This method is intended to be used to find the string separating each @@ -474,7 +334,7 @@ public abstract class ScriptUtils { * (typically "*/") * @since 5.3.8 */ - private static boolean containsStatementSeparator(@Nullable EncodedResource resource, String script, + static boolean containsStatementSeparator(@Nullable EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException { @@ -547,7 +407,7 @@ public abstract class ScriptUtils { * @throws ScriptException if an error occurred while executing the SQL script * @see #executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String) * @see #DEFAULT_STATEMENT_SEPARATOR - * @see #DEFAULT_COMMENT_PREFIX + * @see #DEFAULT_COMMENT_PREFIXES * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection @@ -571,7 +431,7 @@ public abstract class ScriptUtils { * @throws ScriptException if an error occurred while executing the SQL script * @see #executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String) * @see #DEFAULT_STATEMENT_SEPARATOR - * @see #DEFAULT_COMMENT_PREFIX + * @see #DEFAULT_COMMENT_PREFIXES * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection @@ -579,7 +439,7 @@ public abstract class ScriptUtils { */ public static Mono executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException { return executeSqlScript(connection, resource, DefaultDataBufferFactory.sharedInstance, false, false, - DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, + DEFAULT_COMMENT_PREFIXES, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); } diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ScriptUtilsUnitTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ScriptUtilsUnitTests.java index 334afe6e3a..bc9775ad9c 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ScriptUtilsUnitTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ScriptUtilsUnitTests.java @@ -33,8 +33,6 @@ import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_BLOC import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER; import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_COMMENT_PREFIXES; import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_STATEMENT_SEPARATOR; -import static org.springframework.r2dbc.connection.init.ScriptUtils.containsSqlScriptDelimiters; -import static org.springframework.r2dbc.connection.init.ScriptUtils.splitSqlScript; /** * Unit tests for {@link ScriptUtils}. @@ -143,7 +141,7 @@ public class ScriptUtilsUnitTests { private void splitScriptContainingComments(String script, String... commentPrefixes) { List statements = new ArrayList<>(); - splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER, + ScriptUtils.splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements); String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')"; @@ -159,7 +157,7 @@ public class ScriptUtilsUnitTests { public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception { String script = readScript("test-data-with-comments-and-leading-tabs.sql"); List statements = new ArrayList<>(); - splitSqlScript(script, ';', statements); + splitSqlScript(script, ";", statements); String statement1 = "insert into customer (id, name) values (1, 'Sam Brannen')"; String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2013-06-08', 1)"; @@ -172,7 +170,7 @@ public class ScriptUtilsUnitTests { public void readAndSplitScriptContainingMultiLineComments() throws Exception { String script = readScript("test-data-with-multi-line-comments.sql"); List statements = new ArrayList<>(); - splitSqlScript(script, ';', statements); + splitSqlScript(script, ";", statements); String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')"; String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )"; @@ -184,7 +182,7 @@ public class ScriptUtilsUnitTests { public void readAndSplitScriptContainingMultiLineNestedComments() throws Exception { String script = readScript("test-data-with-multi-line-nested-comments.sql"); List statements = new ArrayList<>(); - splitSqlScript(script, ';', statements); + splitSqlScript(script, ";", statements); String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')"; String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )"; @@ -225,13 +223,21 @@ public class ScriptUtilsUnitTests { "'/* double \\\" quotes */\ninsert into colors(color_num) values(42);' # ; # true" }) public void containsStatementSeparator(String script, String delimiter, boolean expected) { - // Indirectly tests ScriptUtils.containsStatementSeparator(EncodedResource, String, String, String[], String, String). - assertThat(containsSqlScriptDelimiters(script, delimiter)).isEqualTo(expected); + boolean contains = ScriptUtils.containsStatementSeparator(null, script, delimiter, DEFAULT_COMMENT_PREFIXES, + DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); + + assertThat(contains).isEqualTo(expected); } private String readScript(String path) throws Exception { EncodedResource resource = new EncodedResource(new ClassPathResource(path, getClass())); - return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance).block(); + return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance, DEFAULT_STATEMENT_SEPARATOR, + DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_END_DELIMITER).block(); + } + + private static void splitSqlScript(String script, String separator, List statements) throws ScriptException { + ScriptUtils.splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_START_DELIMITER, + DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements); } }