Skip to content

Commit fab7b1c

Browse files
committed
Polish ScriptUtils internals in spring-r2dbc
See gh-26947
1 parent 101ed17 commit fab7b1c

File tree

2 files changed

+32
-49
lines changed

2 files changed

+32
-49
lines changed

spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public abstract class ScriptUtils {
106106

107107
/**
108108
* Split an SQL script into separate statements delimited by the provided
109-
* separator string. Each individual statement will be added to the provided
110-
* {@code List}.
109+
* separator string and return a {@code List} containing each individual
110+
* statement.
111111
* <p>Within the script, the provided {@code commentPrefixes} will be honored:
112112
* any text beginning with one of the comment prefixes and extending to the
113113
* end of the line will be omitted from the output. Similarly, the provided
@@ -125,12 +125,12 @@ public abstract class ScriptUtils {
125125
* never {@code null} or empty
126126
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter;
127127
* never {@code null} or empty
128-
* @param statements the list that will contain the individual statements
128+
* @return a list of statements
129129
* @throws ScriptException if an error occurred while splitting the SQL script
130130
*/
131-
static void splitSqlScript(@Nullable EncodedResource resource, String script,
131+
static List<String> splitSqlScript(EncodedResource resource, String script,
132132
String separator, String[] commentPrefixes, String blockCommentStartDelimiter,
133-
String blockCommentEndDelimiter, List<String> statements) throws ScriptException {
133+
String blockCommentEndDelimiter) throws ScriptException {
134134

135135
Assert.hasText(script, "'script' must not be null or empty");
136136
Assert.notNull(separator, "'separator' must not be null");
@@ -141,6 +141,7 @@ static void splitSqlScript(@Nullable EncodedResource resource, String script,
141141
Assert.hasText(blockCommentStartDelimiter, "'blockCommentStartDelimiter' must not be null or empty");
142142
Assert.hasText(blockCommentEndDelimiter, "'blockCommentEndDelimiter' must not be null or empty");
143143

144+
List<String> statements = new ArrayList<>();
144145
StringBuilder sb = new StringBuilder();
145146
boolean inSingleQuote = false;
146147
boolean inDoubleQuote = false;
@@ -215,26 +216,22 @@ else if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
215216
if (StringUtils.hasText(sb)) {
216217
statements.add(sb.toString());
217218
}
219+
220+
return statements;
218221
}
219222

220223
/**
221-
* Read a script from the provided resource, using the supplied comment prefixes
222-
* and statement separator, and build a {@code String} containing the lines.
223-
* <p>Lines <em>beginning</em> with one of the comment prefixes are excluded
224-
* from the results; however, line comments anywhere else &mdash; for example,
225-
* within a statement &mdash; will be included in the results.
226-
* @param resource the {@code EncodedResource} containing the script
227-
* to be processed
224+
* Read a script from the provided resource, using the supplied statement
225+
* separator, and build a {@code String} containing the lines.
226+
* @param resource the {@code EncodedResource} containing the script to be
227+
* processed
228228
* @param dataBufferFactory the factory to create data buffers with
229229
* @param separator the statement separator in the SQL script (typically ";")
230-
* @param commentPrefixes the prefixes that identify comments in the SQL script
231-
* (typically "--")
232-
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
233230
* @return a {@link Mono} of {@link String} containing the script lines that
234-
* completes once the resource was loaded
231+
* completes once the resource has been loaded
235232
*/
236233
static Mono<String> readScript(EncodedResource resource, DataBufferFactory dataBufferFactory,
237-
@Nullable String separator, @Nullable String[] commentPrefixes, @Nullable String blockCommentEndDelimiter) {
234+
@Nullable String separator) {
238235

239236
return DataBufferUtils.join(DataBufferUtils.read(resource.getResource(), dataBufferFactory, 8192))
240237
.handle((it, sink) -> {
@@ -322,7 +319,7 @@ private static boolean startsWithAny(String script, String[] prefixes, int offse
322319
* (typically <code>"*&#47;"</code>)
323320
* @since 5.3.8
324321
*/
325-
static boolean containsStatementSeparator(@Nullable EncodedResource resource, String script,
322+
static boolean containsStatementSeparator(EncodedResource resource, String script,
326323
String separator, String[] commentPrefixes, String blockCommentStartDelimiter,
327324
String blockCommentEndDelimiter) throws ScriptException {
328325

@@ -514,13 +511,12 @@ public static Mono<Void> executeSqlScript(Connection connection, EncodedResource
514511

515512
long startTime = System.currentTimeMillis();
516513

517-
Mono<String> inputScript = readScript(resource, dataBufferFactory, separator, commentPrefixes, blockCommentEndDelimiter)
514+
Mono<String> inputScript = readScript(resource, dataBufferFactory, separator)
518515
.onErrorMap(IOException.class, ex -> new CannotReadScriptException(resource, ex));
519516

520517
AtomicInteger statementNumber = new AtomicInteger();
521518

522519
Flux<Void> executeScript = inputScript.flatMapIterable(script -> {
523-
List<String> statements = new ArrayList<>();
524520
String separatorToUse = separator;
525521
if (separatorToUse == null) {
526522
separatorToUse = DEFAULT_STATEMENT_SEPARATOR;
@@ -530,9 +526,8 @@ public static Mono<Void> executeSqlScript(Connection connection, EncodedResource
530526
blockCommentStartDelimiter, blockCommentEndDelimiter)) {
531527
separatorToUse = FALLBACK_STATEMENT_SEPARATOR;
532528
}
533-
splitSqlScript(resource, script, separatorToUse, commentPrefixes, blockCommentStartDelimiter,
534-
blockCommentEndDelimiter, statements);
535-
return statements;
529+
return splitSqlScript(resource, script, separatorToUse, commentPrefixes,
530+
blockCommentStartDelimiter, blockCommentEndDelimiter);
536531
}).concatMap(statement -> {
537532
statementNumber.incrementAndGet();
538533
return runStatement(statement, connection, resource, continueOnError, ignoreFailedDrops, statementNumber);

spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ScriptUtilsUnitTests.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.r2dbc.connection.init;
1818

19-
import java.util.ArrayList;
2019
import java.util.List;
2120

2221
import org.assertj.core.util.Strings;
@@ -60,8 +59,7 @@ public void splitSqlScriptDelimitedWithSemicolon() {
6059
String delimiter = ";";
6160
String script = Strings.join(rawStatement1, rawStatement2, rawStatement3).with(delimiter);
6261

63-
List<String> statements = new ArrayList<>();
64-
splitSqlScript(script, delimiter, statements);
62+
List<String> statements = splitSqlScript(script, delimiter);
6563

6664
assertThat(statements).containsExactly(cleanedStatement1, cleanedStatement2, cleanedStatement3);
6765
}
@@ -75,8 +73,7 @@ public void splitSqlScriptDelimitedWithNewLine() {
7573
String delimiter = "\n";
7674
String script = Strings.join(statement1, statement2, statement3).with(delimiter);
7775

78-
List<String> statements = new ArrayList<>();
79-
splitSqlScript(script, delimiter, statements);
76+
List<String> statements = splitSqlScript(script, delimiter);
8077

8178
assertThat(statements).containsExactly(statement1, statement2, statement3);
8279
}
@@ -88,9 +85,7 @@ public void splitSqlScriptDelimitedWithNewLineButDefaultDelimiterSpecified() {
8885

8986
String script = Strings.join(statement1, statement2).with("\n");
9087

91-
List<String> statements = new ArrayList<>();
92-
93-
splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR, statements);
88+
List<String> statements = splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR);
9489

9590
assertThat(statements).as("stripped but not split statements").containsExactly(script.replace('\n', ' '));
9691
}
@@ -103,17 +98,15 @@ public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() {
10398
String delimiter = ";";
10499
String script = Strings.join(statement1, statement2).with(delimiter);
105100

106-
List<String> statements = new ArrayList<>();
107-
splitSqlScript(script, delimiter, statements);
101+
List<String> statements = splitSqlScript(script, delimiter);
108102

109103
assertThat(statements).containsExactly(statement1, statement2);
110104
}
111105

112106
@Test // SPR-11560
113107
public void readAndSplitScriptWithMultipleNewlinesAsSeparator() throws Exception {
114108
String script = readScript("db-test-data-multi-newline.sql");
115-
List<String> statements = new ArrayList<>();
116-
splitSqlScript(script, "\n\n", statements);
109+
List<String> statements = splitSqlScript(script, "\n\n");
117110

118111
String statement1 = "insert into T_TEST (NAME) values ('Keith')";
119112
String statement2 = "insert into T_TEST (NAME) values ('Dave')";
@@ -140,9 +133,8 @@ public void readAndSplitScriptContainingCommentsWithMultiplePrefixes() throws Ex
140133
}
141134

142135
private void splitScriptContainingComments(String script, String... commentPrefixes) {
143-
List<String> statements = new ArrayList<>();
144-
ScriptUtils.splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
145-
DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
136+
List<String> statements = ScriptUtils.splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
137+
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
146138

147139
String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')";
148140
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
@@ -156,8 +148,7 @@ private void splitScriptContainingComments(String script, String... commentPrefi
156148
@Test // SPR-10330
157149
public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception {
158150
String script = readScript("test-data-with-comments-and-leading-tabs.sql");
159-
List<String> statements = new ArrayList<>();
160-
splitSqlScript(script, ";", statements);
151+
List<String> statements = splitSqlScript(script, ";");
161152

162153
String statement1 = "insert into customer (id, name) values (1, 'Sam Brannen')";
163154
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2013-06-08', 1)";
@@ -169,8 +160,7 @@ public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Excepti
169160
@Test // SPR-9531
170161
public void readAndSplitScriptContainingMultiLineComments() throws Exception {
171162
String script = readScript("test-data-with-multi-line-comments.sql");
172-
List<String> statements = new ArrayList<>();
173-
splitSqlScript(script, ";", statements);
163+
List<String> statements = splitSqlScript(script, ";");
174164

175165
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
176166
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
@@ -181,8 +171,7 @@ public void readAndSplitScriptContainingMultiLineComments() throws Exception {
181171
@Test
182172
public void readAndSplitScriptContainingMultiLineNestedComments() throws Exception {
183173
String script = readScript("test-data-with-multi-line-nested-comments.sql");
184-
List<String> statements = new ArrayList<>();
185-
splitSqlScript(script, ";", statements);
174+
List<String> statements = splitSqlScript(script, ";");
186175

187176
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
188177
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
@@ -231,13 +220,12 @@ public void containsStatementSeparator(String script, String delimiter, boolean
231220

232221
private String readScript(String path) throws Exception {
233222
EncodedResource resource = new EncodedResource(new ClassPathResource(path, getClass()));
234-
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance, DEFAULT_STATEMENT_SEPARATOR,
235-
DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_END_DELIMITER).block();
223+
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance, DEFAULT_STATEMENT_SEPARATOR).block();
236224
}
237225

238-
private static void splitSqlScript(String script, String separator, List<String> statements) throws ScriptException {
239-
ScriptUtils.splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
240-
DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
226+
private static List<String> splitSqlScript(String script, String separator) throws ScriptException {
227+
return ScriptUtils.splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
228+
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
241229
}
242230

243231
}

0 commit comments

Comments
 (0)