Skip to content

Commit 9ae6268

Browse files
committed
Polish ScriptUtils implementation
1 parent e4d843e commit 9ae6268

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,6 @@ else if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
296296
}
297297
}
298298

299-
/**
300-
* Read a script from the given resource, using "{@code --}" as the comment prefix
301-
* and "{@code ;}" as the statement separator, and build a String containing the lines.
302-
* @param resource the {@code EncodedResource} to be read
303-
* @return {@code String} containing the script lines
304-
* @throws IOException in case of I/O errors
305-
*/
306-
static String readScript(EncodedResource resource) throws IOException {
307-
return readScript(resource, DEFAULT_COMMENT_PREFIXES, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
308-
}
309-
310299
/**
311300
* Read a script from the provided resource, using the supplied comment prefixes
312301
* and statement separator, and build a {@code String} containing the lines.
@@ -315,15 +304,15 @@ static String readScript(EncodedResource resource) throws IOException {
315304
* within a statement — will be included in the results.
316305
* @param resource the {@code EncodedResource} containing the script
317306
* to be processed
307+
* @param separator the statement separator in the SQL script (typically ";")
318308
* @param commentPrefixes the prefixes that identify comments in the SQL script
319309
* (typically "--")
320-
* @param separator the statement separator in the SQL script (typically ";")
321310
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
322311
* @return a {@code String} containing the script lines
323312
* @throws IOException in case of I/O errors
324313
*/
325-
private static String readScript(EncodedResource resource, @Nullable String[] commentPrefixes,
326-
@Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException {
314+
static String readScript(EncodedResource resource, @Nullable String separator,
315+
@Nullable String[] commentPrefixes, @Nullable String blockCommentEndDelimiter) throws IOException {
327316

328317
try (LineNumberReader lnr = new LineNumberReader(resource.getReader())) {
329318
return readScript(lnr, commentPrefixes, separator, blockCommentEndDelimiter);
@@ -339,18 +328,18 @@ private static String readScript(EncodedResource resource, @Nullable String[] co
339328
* a statement &mdash; will be included in the results.
340329
* @param lineNumberReader the {@code LineNumberReader} containing the script
341330
* to be processed
342-
* @param lineCommentPrefix the prefix that identifies comments in the SQL script
331+
* @param commentPrefix the prefix that identifies comments in the SQL script
343332
* (typically "--")
344333
* @param separator the statement separator in the SQL script (typically ";")
345334
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
346335
* @return a {@code String} containing the script lines
347336
* @throws IOException in case of I/O errors
348337
*/
349-
public static String readScript(LineNumberReader lineNumberReader, @Nullable String lineCommentPrefix,
338+
public static String readScript(LineNumberReader lineNumberReader, @Nullable String commentPrefix,
350339
@Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException {
351340

352-
String[] lineCommentPrefixes = (lineCommentPrefix != null) ? new String[] { lineCommentPrefix } : null;
353-
return readScript(lineNumberReader, lineCommentPrefixes, separator, blockCommentEndDelimiter);
341+
String[] commentPrefixes = (commentPrefix != null) ? new String[] { commentPrefix } : null;
342+
return readScript(lineNumberReader, commentPrefixes, separator, blockCommentEndDelimiter);
354343
}
355344

356345
/**
@@ -362,22 +351,22 @@ public static String readScript(LineNumberReader lineNumberReader, @Nullable Str
362351
* within a statement &mdash; will be included in the results.
363352
* @param lineNumberReader the {@code LineNumberReader} containing the script
364353
* to be processed
365-
* @param lineCommentPrefixes the prefixes that identify comments in the SQL script
354+
* @param commentPrefixes the prefixes that identify comments in the SQL script
366355
* (typically "--")
367356
* @param separator the statement separator in the SQL script (typically ";")
368357
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
369358
* @return a {@code String} containing the script lines
370359
* @throws IOException in case of I/O errors
371360
* @since 5.2
372361
*/
373-
public static String readScript(LineNumberReader lineNumberReader, @Nullable String[] lineCommentPrefixes,
362+
public static String readScript(LineNumberReader lineNumberReader, @Nullable String[] commentPrefixes,
374363
@Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException {
375364

376365
String currentStatement = lineNumberReader.readLine();
377366
StringBuilder scriptBuilder = new StringBuilder();
378367
while (currentStatement != null) {
379368
if ((blockCommentEndDelimiter != null && currentStatement.contains(blockCommentEndDelimiter)) ||
380-
(lineCommentPrefixes != null && !startsWithAny(currentStatement, lineCommentPrefixes, 0))) {
369+
(commentPrefixes != null && !startsWithAny(currentStatement, commentPrefixes, 0))) {
381370
if (scriptBuilder.length() > 0) {
382371
scriptBuilder.append('\n');
383372
}
@@ -642,7 +631,7 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
642631

643632
String script;
644633
try {
645-
script = readScript(resource, commentPrefixes, separator, blockCommentEndDelimiter);
634+
script = readScript(resource, separator, commentPrefixes, blockCommentEndDelimiter);
646635
}
647636
catch (IOException ex) {
648637
throw new CannotReadScriptException(resource, ex);

spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ public void containsStatementSeparator(String script, String delimiter, boolean
206206

207207
private String readScript(String path) throws Exception {
208208
EncodedResource resource = new EncodedResource(new ClassPathResource(path, getClass()));
209-
return ScriptUtils.readScript(resource);
209+
return ScriptUtils.readScript(resource, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_COMMENT_PREFIXES,
210+
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
210211
}
211212

212213
}

0 commit comments

Comments
 (0)