Skip to content

Commit 7f5904c

Browse files
committed
Remove internal APIs from ScriptUtils in spring-r2dbc
Prior to this commit, many of the utility methods in ScriptUtils in spring-r2dbc were public by accident: they were copied from ScriptUtils in spring-jdbc without changing the visibility when spring-r2dbc was introduced in Spring Framework. 5.3 GA. This commit corrects this mistake by removing internal APIs from ScriptUtils in spring-r2dbc from the public contract. See gh-26947
1 parent 66e4888 commit 7f5904c

File tree

2 files changed

+23
-157
lines changed

2 files changed

+23
-157
lines changed

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

Lines changed: 8 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,10 @@ public abstract class ScriptUtils {
8585
*/
8686
public static final String EOF_STATEMENT_SEPARATOR = "^^^ END OF SCRIPT ^^^";
8787

88-
/**
89-
* Default prefix for single-line comments within SQL scripts: {@code "--"}.
90-
*/
91-
public static final String DEFAULT_COMMENT_PREFIX = "--";
92-
9388
/**
9489
* Default prefixes for single-line comments within SQL scripts: {@code ["--"]}.
9590
*/
96-
public static final String[] DEFAULT_COMMENT_PREFIXES = {DEFAULT_COMMENT_PREFIX};
91+
public static final String[] DEFAULT_COMMENT_PREFIXES = {"--"};
9792

9893
/**
9994
* Default start delimiter for block comments within SQL scripts: {@code "/*"}.
@@ -109,87 +104,6 @@ public abstract class ScriptUtils {
109104
private static final Log logger = LogFactory.getLog(ScriptUtils.class);
110105

111106

112-
/**
113-
* Split an SQL script into separate statements delimited by the provided
114-
* separator character. Each individual statement will be added to the
115-
* provided {@code List}.
116-
* <p>Within the script, {@value #DEFAULT_COMMENT_PREFIX} will be used as the
117-
* comment prefix; any text beginning with the comment prefix and extending to
118-
* the end of the line will be omitted from the output. Similarly,
119-
* {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and
120-
* {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the
121-
* <em>start</em> and <em>end</em> block comment delimiters: any text enclosed
122-
* in a block comment will be omitted from the output. In addition, multiple
123-
* adjacent whitespace characters will be collapsed into a single space.
124-
* @param script the SQL script
125-
* @param separator character separating each statement (typically a ';')
126-
* @param statements the list that will contain the individual statements
127-
* @throws ScriptException if an error occurred while splitting the SQL script
128-
* @see #splitSqlScript(String, String, List)
129-
* @see #splitSqlScript(EncodedResource, String, String, String, String, String, List)
130-
*/
131-
public static void splitSqlScript(String script, char separator, List<String> statements) throws ScriptException {
132-
splitSqlScript(script, String.valueOf(separator), statements);
133-
}
134-
135-
/**
136-
* Split an SQL script into separate statements delimited by the provided
137-
* separator string. Each individual statement will be added to the
138-
* provided {@code List}.
139-
* <p>Within the script, {@value #DEFAULT_COMMENT_PREFIX} will be used as the
140-
* comment prefix; any text beginning with the comment prefix and extending to
141-
* the end of the line will be omitted from the output. Similarly,
142-
* {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and
143-
* {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the
144-
* <em>start</em> and <em>end</em> block comment delimiters: any text enclosed
145-
* in a block comment will be omitted from the output. In addition, multiple
146-
* adjacent whitespace characters will be collapsed into a single space.
147-
* @param script the SQL script
148-
* @param separator text separating each statement
149-
* (typically a ';' or newline character)
150-
* @param statements the list that will contain the individual statements
151-
* @throws ScriptException if an error occurred while splitting the SQL script
152-
* @see #splitSqlScript(String, char, List)
153-
* @see #splitSqlScript(EncodedResource, String, String, String, String, String, List)
154-
*/
155-
public static void splitSqlScript(String script, String separator, List<String> statements) throws ScriptException {
156-
splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIX, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
157-
DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
158-
}
159-
160-
/**
161-
* Split an SQL script into separate statements delimited by the provided
162-
* separator string. Each individual statement will be added to the provided
163-
* {@code List}.
164-
* <p>Within the script, the provided {@code commentPrefix} will be honored:
165-
* any text beginning with the comment prefix and extending to the end of the
166-
* line will be omitted from the output. Similarly, the provided
167-
* {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter}
168-
* delimiters will be honored: any text enclosed in a block comment will be
169-
* omitted from the output. In addition, multiple adjacent whitespace characters
170-
* will be collapsed into a single space.
171-
* @param resource the resource from which the script was read
172-
* @param script the SQL script
173-
* @param separator text separating each statement
174-
* (typically a ';' or newline character)
175-
* @param commentPrefix the prefix that identifies SQL line comments
176-
* (typically "--")
177-
* @param blockCommentStartDelimiter the <em>start</em> block comment delimiter;
178-
* never {@code null} or empty
179-
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter;
180-
* never {@code null} or empty
181-
* @param statements the list that will contain the individual statements
182-
* @throws ScriptException if an error occurred while splitting the SQL script
183-
*/
184-
public static void splitSqlScript(@Nullable EncodedResource resource, String script,
185-
String separator, String commentPrefix, String blockCommentStartDelimiter,
186-
String blockCommentEndDelimiter, List<String> statements) throws ScriptException {
187-
188-
Assert.hasText(commentPrefix, "'commentPrefix' must not be null or empty");
189-
splitSqlScript(resource, script, separator, new String[] { commentPrefix },
190-
blockCommentStartDelimiter, blockCommentEndDelimiter, statements);
191-
}
192-
193107
/**
194108
* Split an SQL script into separate statements delimited by the provided
195109
* separator string. Each individual statement will be added to the provided
@@ -214,7 +128,7 @@ public static void splitSqlScript(@Nullable EncodedResource resource, String scr
214128
* @param statements the list that will contain the individual statements
215129
* @throws ScriptException if an error occurred while splitting the SQL script
216130
*/
217-
public static void splitSqlScript(@Nullable EncodedResource resource, String script,
131+
static void splitSqlScript(@Nullable EncodedResource resource, String script,
218132
String separator, String[] commentPrefixes, String blockCommentStartDelimiter,
219133
String blockCommentEndDelimiter, List<String> statements) throws ScriptException {
220134

@@ -303,18 +217,6 @@ else if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
303217
}
304218
}
305219

306-
/**
307-
* Read a script from the given resource, using "{@code --}" as the comment prefix
308-
* and "{@code ;}" as the statement separator, and build a String containing the lines.
309-
* @param resource the {@code EncodedResource} to be read
310-
* @param dataBufferFactory the factory to create data buffers with
311-
* @return {@code String} containing the script lines
312-
*/
313-
public static Mono<String> readScript(EncodedResource resource, DataBufferFactory dataBufferFactory) {
314-
return readScript(resource, dataBufferFactory, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_COMMENT_PREFIXES,
315-
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
316-
}
317-
318220
/**
319221
* Read a script from the provided resource, using the supplied comment prefixes
320222
* and statement separator, and build a {@code String} containing the lines.
@@ -331,7 +233,7 @@ public static Mono<String> readScript(EncodedResource resource, DataBufferFactor
331233
* @return a {@link Mono} of {@link String} containing the script lines that
332234
* completes once the resource was loaded
333235
*/
334-
private static Mono<String> readScript(EncodedResource resource, DataBufferFactory dataBufferFactory,
236+
static Mono<String> readScript(EncodedResource resource, DataBufferFactory dataBufferFactory,
335237
@Nullable String separator, @Nullable String[] commentPrefixes, @Nullable String blockCommentEndDelimiter) {
336238

337239
return DataBufferUtils.join(DataBufferUtils.read(resource.getResource(), dataBufferFactory, 8192))
@@ -353,29 +255,6 @@ private static Mono<String> readScript(EncodedResource resource, DataBufferFacto
353255
});
354256
}
355257

356-
/**
357-
* Read a script from the provided {@code LineNumberReader}, using the supplied
358-
* comment prefix and statement separator, and build a {@code String} containing
359-
* the lines.
360-
* <p>Lines <em>beginning</em> with the comment prefix are excluded from the
361-
* results; however, line comments anywhere else &mdash; for example, within
362-
* a statement &mdash; will be included in the results.
363-
* @param lineNumberReader the {@code LineNumberReader} containing the script
364-
* to be processed
365-
* @param commentPrefix the prefix that identifies comments in the SQL script
366-
* (typically "--")
367-
* @param separator the statement separator in the SQL script (typically ";")
368-
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
369-
* @return a {@code String} containing the script lines
370-
* @throws IOException in case of I/O errors
371-
*/
372-
public static String readScript(LineNumberReader lineNumberReader, @Nullable String commentPrefix,
373-
@Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException {
374-
375-
String[] commentPrefixes = (commentPrefix != null) ? new String[] { commentPrefix } : null;
376-
return readScript(lineNumberReader, commentPrefixes, separator, blockCommentEndDelimiter);
377-
}
378-
379258
/**
380259
* Read a script from the provided {@code LineNumberReader}, using the supplied
381260
* comment prefixes and statement separator, and build a {@code String} containing
@@ -392,7 +271,7 @@ public static String readScript(LineNumberReader lineNumberReader, @Nullable Str
392271
* @return a {@code String} containing the script lines
393272
* @throws IOException in case of I/O errors
394273
*/
395-
public static String readScript(LineNumberReader lineNumberReader, @Nullable String[] commentPrefixes,
274+
private static String readScript(LineNumberReader lineNumberReader, @Nullable String[] commentPrefixes,
396275
@Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException {
397276

398277
String currentStatement = lineNumberReader.readLine();
@@ -435,25 +314,6 @@ private static boolean startsWithAny(String script, String[] prefixes, int offse
435314
return false;
436315
}
437316

438-
/**
439-
* Determine if the provided SQL script contains the specified delimiter.
440-
* <p>This method is intended to be used to find the string delimiting each
441-
* SQL statement &mdash; for example, a ';' character.
442-
* <p>Any occurrence of the delimiter within the script will be ignored if it
443-
* is within a <em>literal</em> block of text enclosed in single quotes
444-
* ({@code '}) or double quotes ({@code "}), if it is escaped with a backslash
445-
* ({@code \}), or if it is within a single-line comment or block comment.
446-
* @param script the SQL script to search within
447-
* @param delimiter the statement delimiter to search for
448-
* @see #DEFAULT_COMMENT_PREFIXES
449-
* @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
450-
* @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
451-
*/
452-
public static boolean containsSqlScriptDelimiters(String script, String delimiter) {
453-
return containsStatementSeparator(null, script, delimiter, DEFAULT_COMMENT_PREFIXES,
454-
DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
455-
}
456-
457317
/**
458318
* Determine if the provided SQL script contains the specified statement separator.
459319
* <p>This method is intended to be used to find the string separating each
@@ -474,7 +334,7 @@ public static boolean containsSqlScriptDelimiters(String script, String delimite
474334
* (typically <code>"*&#47;"</code>)
475335
* @since 5.3.8
476336
*/
477-
private static boolean containsStatementSeparator(@Nullable EncodedResource resource, String script,
337+
static boolean containsStatementSeparator(@Nullable EncodedResource resource, String script,
478338
String separator, String[] commentPrefixes, String blockCommentStartDelimiter,
479339
String blockCommentEndDelimiter) throws ScriptException {
480340

@@ -547,7 +407,7 @@ else if (script.startsWith(blockCommentStartDelimiter, i)) {
547407
* @throws ScriptException if an error occurred while executing the SQL script
548408
* @see #executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String)
549409
* @see #DEFAULT_STATEMENT_SEPARATOR
550-
* @see #DEFAULT_COMMENT_PREFIX
410+
* @see #DEFAULT_COMMENT_PREFIXES
551411
* @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
552412
* @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
553413
* @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
@@ -571,15 +431,15 @@ public static Mono<Void> executeSqlScript(Connection connection, Resource resour
571431
* @throws ScriptException if an error occurred while executing the SQL script
572432
* @see #executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String)
573433
* @see #DEFAULT_STATEMENT_SEPARATOR
574-
* @see #DEFAULT_COMMENT_PREFIX
434+
* @see #DEFAULT_COMMENT_PREFIXES
575435
* @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
576436
* @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
577437
* @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
578438
* @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
579439
*/
580440
public static Mono<Void> executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException {
581441
return executeSqlScript(connection, resource, DefaultDataBufferFactory.sharedInstance, false, false,
582-
DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
442+
DEFAULT_COMMENT_PREFIXES, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
583443
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
584444
}
585445

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER;
3434
import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_COMMENT_PREFIXES;
3535
import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_STATEMENT_SEPARATOR;
36-
import static org.springframework.r2dbc.connection.init.ScriptUtils.containsSqlScriptDelimiters;
37-
import static org.springframework.r2dbc.connection.init.ScriptUtils.splitSqlScript;
3836

3937
/**
4038
* Unit tests for {@link ScriptUtils}.
@@ -143,7 +141,7 @@ public void readAndSplitScriptContainingCommentsWithMultiplePrefixes() throws Ex
143141

144142
private void splitScriptContainingComments(String script, String... commentPrefixes) {
145143
List<String> statements = new ArrayList<>();
146-
splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
144+
ScriptUtils.splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
147145
DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
148146

149147
String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')";
@@ -159,7 +157,7 @@ private void splitScriptContainingComments(String script, String... commentPrefi
159157
public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception {
160158
String script = readScript("test-data-with-comments-and-leading-tabs.sql");
161159
List<String> statements = new ArrayList<>();
162-
splitSqlScript(script, ';', statements);
160+
splitSqlScript(script, ";", statements);
163161

164162
String statement1 = "insert into customer (id, name) values (1, 'Sam Brannen')";
165163
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2013-06-08', 1)";
@@ -172,7 +170,7 @@ public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Excepti
172170
public void readAndSplitScriptContainingMultiLineComments() throws Exception {
173171
String script = readScript("test-data-with-multi-line-comments.sql");
174172
List<String> statements = new ArrayList<>();
175-
splitSqlScript(script, ';', statements);
173+
splitSqlScript(script, ";", statements);
176174

177175
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
178176
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
@@ -184,7 +182,7 @@ public void readAndSplitScriptContainingMultiLineComments() throws Exception {
184182
public void readAndSplitScriptContainingMultiLineNestedComments() throws Exception {
185183
String script = readScript("test-data-with-multi-line-nested-comments.sql");
186184
List<String> statements = new ArrayList<>();
187-
splitSqlScript(script, ';', statements);
185+
splitSqlScript(script, ";", statements);
188186

189187
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
190188
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
@@ -225,13 +223,21 @@ public void readAndSplitScriptContainingMultiLineNestedComments() throws Excepti
225223
"'/* double \\\" quotes */\ninsert into colors(color_num) values(42);' # ; # true"
226224
})
227225
public void containsStatementSeparator(String script, String delimiter, boolean expected) {
228-
// Indirectly tests ScriptUtils.containsStatementSeparator(EncodedResource, String, String, String[], String, String).
229-
assertThat(containsSqlScriptDelimiters(script, delimiter)).isEqualTo(expected);
226+
boolean contains = ScriptUtils.containsStatementSeparator(null, script, delimiter, DEFAULT_COMMENT_PREFIXES,
227+
DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
228+
229+
assertThat(contains).isEqualTo(expected);
230230
}
231231

232232
private String readScript(String path) throws Exception {
233233
EncodedResource resource = new EncodedResource(new ClassPathResource(path, getClass()));
234-
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance).block();
234+
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance, DEFAULT_STATEMENT_SEPARATOR,
235+
DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_END_DELIMITER).block();
236+
}
237+
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);
235241
}
236242

237243
}

0 commit comments

Comments
 (0)