Skip to content

Commit c788be3

Browse files
committed
Polish contribution
See gh-23289
1 parent b5e4e02 commit c788be3

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,25 @@ public EmbeddedDatabaseBuilder setSeparator(String separator) {
201201
* @param commentPrefix the prefix for single-line comments
202202
* @return {@code this}, to facilitate method chaining
203203
* @since 4.0.3
204+
* @see #setCommentPrefixes(String...)
204205
*/
205206
public EmbeddedDatabaseBuilder setCommentPrefix(String commentPrefix) {
206207
this.databasePopulator.setCommentPrefix(commentPrefix);
207208
return this;
208209
}
209210

211+
/**
212+
* Specify the prefixes that identify single-line comments within all SQL scripts.
213+
* <p>Defaults to {@code ["--"]}.
214+
* @param commentPrefixes the prefixes for single-line comments
215+
* @return {@code this}, to facilitate method chaining
216+
* @since 5.2
217+
*/
218+
public EmbeddedDatabaseBuilder setCommentPrefixes(String... commentPrefixes) {
219+
this.databasePopulator.setCommentPrefixes(commentPrefixes);
220+
return this;
221+
}
222+
210223
/**
211224
* Specify the start delimiter for block comments in all SQL scripts.
212225
* <p>Defaults to {@code "/*"}.

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* @author Oliver Gierke
4848
* @author Sam Brannen
4949
* @author Chris Baldwin
50+
* @author Phillip Webb
5051
* @since 3.0
5152
* @see DatabasePopulatorUtils
5253
* @see ScriptUtils
@@ -116,7 +117,7 @@ public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDr
116117
* @param script the path to an SQL script (never {@code null})
117118
*/
118119
public void addScript(Resource script) {
119-
Assert.notNull(script, "Script must not be null");
120+
Assert.notNull(script, "'script' must not be null");
120121
this.scripts.add(script);
121122
}
122123

@@ -141,8 +142,8 @@ public void setScripts(Resource... scripts) {
141142
}
142143

143144
private void assertContentsOfScriptArray(Resource... scripts) {
144-
Assert.notNull(scripts, "Scripts array must not be null");
145-
Assert.noNullElements(scripts, "Scripts array must not contain null elements");
145+
Assert.notNull(scripts, "'scripts' must not be null");
146+
Assert.noNullElements(scripts, "'scripts' must not contain null elements");
146147
}
147148

148149
/**
@@ -174,18 +175,19 @@ public void setSeparator(String separator) {
174175
* @see #setCommentPrefixes(String...)
175176
*/
176177
public void setCommentPrefix(String commentPrefix) {
177-
Assert.hasText(commentPrefix, "CommentPrefix must not be null or empty");
178+
Assert.hasText(commentPrefix, "'commentPrefix' must not be null or empty");
178179
this.commentPrefixes = new String[] { commentPrefix };
179180
}
180181

181182
/**
182183
* Set the prefixes that identify single-line comments within the SQL scripts.
183-
* <p>Defaults to {@code "--"}.
184+
* <p>Defaults to {@code ["--"]}.
184185
* @param commentPrefixes the prefixes for single-line comments
185186
* @since 5.2
186187
*/
187188
public void setCommentPrefixes(String... commentPrefixes) {
188-
Assert.notNull(commentPrefixes, "CommentPrefixes must not be null");
189+
Assert.notEmpty(commentPrefixes, "'commentPrefixes' must not be null or empty");
190+
Assert.noNullElements(commentPrefixes, "'commentPrefixes' must not contain null elements");
189191
this.commentPrefixes = commentPrefixes;
190192
}
191193

@@ -199,7 +201,7 @@ public void setCommentPrefixes(String... commentPrefixes) {
199201
* @see #setBlockCommentEndDelimiter
200202
*/
201203
public void setBlockCommentStartDelimiter(String blockCommentStartDelimiter) {
202-
Assert.hasText(blockCommentStartDelimiter, "BlockCommentStartDelimiter must not be null or empty");
204+
Assert.hasText(blockCommentStartDelimiter, "'blockCommentStartDelimiter' must not be null or empty");
203205
this.blockCommentStartDelimiter = blockCommentStartDelimiter;
204206
}
205207

@@ -213,7 +215,7 @@ public void setBlockCommentStartDelimiter(String blockCommentStartDelimiter) {
213215
* @see #setBlockCommentStartDelimiter
214216
*/
215217
public void setBlockCommentEndDelimiter(String blockCommentEndDelimiter) {
216-
Assert.hasText(blockCommentEndDelimiter, "BlockCommentEndDelimiter must not be null or empty");
218+
Assert.hasText(blockCommentEndDelimiter, "'blockCommentEndDelimiter' must not be null or empty");
217219
this.blockCommentEndDelimiter = blockCommentEndDelimiter;
218220
}
219221

@@ -245,7 +247,7 @@ public void setIgnoreFailedDrops(boolean ignoreFailedDrops) {
245247
*/
246248
@Override
247249
public void populate(Connection connection) throws ScriptException {
248-
Assert.notNull(connection, "Connection must not be null");
250+
Assert.notNull(connection, "'connection' must not be null");
249251
for (Resource script : this.scripts) {
250252
EncodedResource encodedScript = new EncodedResource(script, this.sqlScriptEncoding);
251253
ScriptUtils.executeSqlScript(connection, encodedScript, this.continueOnError, this.ignoreFailedDrops,

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @author Oliver Gierke
4949
* @author Chris Baldwin
5050
* @author Nicolas Debeissat
51+
* @author Phillip Webb
5152
* @since 4.0.3
5253
*/
5354
public abstract class ScriptUtils {
@@ -185,9 +186,9 @@ public static void splitSqlScript(@Nullable EncodedResource resource, String scr
185186
* Split an SQL script into separate statements delimited by the provided
186187
* separator string. Each individual statement will be added to the provided
187188
* {@code List}.
188-
* <p>Within the script, the provided {@code commentPrefix} will be honored:
189-
* any text beginning with the comment prefix and extending to the end of the
190-
* line will be omitted from the output. Similarly, the provided
189+
* <p>Within the script, the provided {@code commentPrefixes} will be honored:
190+
* any text beginning with one of the comment prefixes and extending to the
191+
* end of the line will be omitted from the output. Similarly, the provided
191192
* {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter}
192193
* delimiters will be honored: any text enclosed in a block comment will be
193194
* omitted from the output. In addition, multiple adjacent whitespace characters
@@ -212,7 +213,7 @@ public static void splitSqlScript(@Nullable EncodedResource resource, String scr
212213

213214
Assert.hasText(script, "'script' must not be null or empty");
214215
Assert.notNull(separator, "'separator' must not be null");
215-
Assert.notNull(commentPrefixes, "'commentPrefixes' must not be null");
216+
Assert.notEmpty(commentPrefixes, "'commentPrefixes' must not be null or empty");
216217
for (int i = 0; i < commentPrefixes.length; i++) {
217218
Assert.hasText(commentPrefixes[i], "'commentPrefixes' must not contain null or empty elements");
218219
}
@@ -307,14 +308,14 @@ static String readScript(EncodedResource resource) throws IOException {
307308
}
308309

309310
/**
310-
* Read a script from the provided resource, using the supplied comment prefix
311+
* Read a script from the provided resource, using the supplied comment prefixes
311312
* and statement separator, and build a {@code String} containing the lines.
312-
* <p>Lines <em>beginning</em> with the comment prefix are excluded from the
313-
* results; however, line comments anywhere else &mdash; for example, within
314-
* a statement &mdash; will be included in the results.
313+
* <p>Lines <em>beginning</em> with one of the comment prefixes are excluded
314+
* from the results; however, line comments anywhere else &mdash; for example,
315+
* within a statement &mdash; will be included in the results.
315316
* @param resource the {@code EncodedResource} containing the script
316317
* to be processed
317-
* @param commentPrefixes the prefix that identifies comments in the SQL script
318+
* @param commentPrefixes the prefixes that identify comments in the SQL script
318319
* (typically "--")
319320
* @param separator the statement separator in the SQL script (typically ";")
320321
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
@@ -358,11 +359,11 @@ public static String readScript(LineNumberReader lineNumberReader, @Nullable Str
358359

359360
/**
360361
* Read a script from the provided {@code LineNumberReader}, using the supplied
361-
* comment prefix and statement separator, and build a {@code String} containing
362+
* comment prefixes and statement separator, and build a {@code String} containing
362363
* the lines.
363-
* <p>Lines <em>beginning</em> with the comment prefix are excluded from the
364-
* results; however, line comments anywhere else &mdash; for example, within
365-
* a statement &mdash; will be included in the results.
364+
* <p>Lines <em>beginning</em> with one of the comment prefixes are excluded
365+
* from the results; however, line comments anywhere else &mdash; for example,
366+
* within a statement &mdash; will be included in the results.
366367
* @param lineNumberReader the {@code LineNumberReader} containing the script
367368
* to be processed
368369
* @param lineCommentPrefixes the prefixes that identify comments in the SQL script
@@ -407,9 +408,9 @@ private static void appendSeparatorToScriptIfNecessary(StringBuilder scriptBuild
407408
}
408409
}
409410

410-
private static boolean startsWithAny(String script, String[] prefixes, int toffset) {
411+
private static boolean startsWithAny(String script, String[] prefixes, int offset) {
411412
for (String prefix : prefixes) {
412-
if (script.startsWith(prefix, toffset)) {
413+
if (script.startsWith(prefix, offset)) {
413414
return true;
414415
}
415416
}

0 commit comments

Comments
 (0)