1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -107,7 +107,7 @@ public abstract class ScriptUtils {
107
107
* in a block comment will be omitted from the output. In addition, multiple
108
108
* adjacent whitespace characters will be collapsed into a single space.
109
109
* @param script the SQL script
110
- * @param separator character separating each statement — typically a ';'
110
+ * @param separator character separating each statement ( typically a ';')
111
111
* @param statements the list that will contain the individual statements
112
112
* @throws ScriptException if an error occurred while splitting the SQL script
113
113
* @see #splitSqlScript(String, String, List)
@@ -130,7 +130,8 @@ public static void splitSqlScript(String script, char separator, List<String> st
130
130
* in a block comment will be omitted from the output. In addition, multiple
131
131
* adjacent whitespace characters will be collapsed into a single space.
132
132
* @param script the SQL script
133
- * @param separator text separating each statement — typically a ';' or newline character
133
+ * @param separator text separating each statement
134
+ * (typically a ';' or newline character)
134
135
* @param statements the list that will contain the individual statements
135
136
* @throws ScriptException if an error occurred while splitting the SQL script
136
137
* @see #splitSqlScript(String, char, List)
@@ -153,11 +154,11 @@ public static void splitSqlScript(String script, String separator, List<String>
153
154
* omitted from the output. In addition, multiple adjacent whitespace characters
154
155
* will be collapsed into a single space.
155
156
* @param resource the resource from which the script was read
156
- * @param script the SQL script; never {@code null} or empty
157
- * @param separator text separating each statement — typically a ';' or
158
- * newline character; never {@code null}
159
- * @param commentPrefix the prefix that identifies SQL line comments —
160
- * typically "--"; never {@code null} or empty
157
+ * @param script the SQL script
158
+ * @param separator text separating each statement
159
+ * (typically a ';' or newline character)
160
+ * @param commentPrefix the prefix that identifies SQL line comments
161
+ * ( typically "--")
161
162
* @param blockCommentStartDelimiter the <em>start</em> block comment delimiter;
162
163
* never {@code null} or empty
163
164
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter;
@@ -259,7 +260,7 @@ else if (c == ' ' || c == '\n' || c == '\t') {
259
260
* @throws IOException in case of I/O errors
260
261
*/
261
262
static String readScript (EncodedResource resource ) throws IOException {
262
- return readScript (resource , DEFAULT_COMMENT_PREFIX , DEFAULT_STATEMENT_SEPARATOR );
263
+ return readScript (resource , DEFAULT_COMMENT_PREFIX , DEFAULT_STATEMENT_SEPARATOR , DEFAULT_BLOCK_COMMENT_END_DELIMITER );
263
264
}
264
265
265
266
/**
@@ -270,18 +271,19 @@ static String readScript(EncodedResource resource) throws IOException {
270
271
* a statement — will be included in the results.
271
272
* @param resource the {@code EncodedResource} containing the script
272
273
* to be processed
273
- * @param commentPrefix the prefix that identifies comments in the SQL script —
274
- * typically "--"
275
- * @param separator the statement separator in the SQL script — typically ";"
274
+ * @param commentPrefix the prefix that identifies comments in the SQL script
275
+ * (typically "--")
276
+ * @param separator the statement separator in the SQL script (typically ";")
277
+ * @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
276
278
* @return a {@code String} containing the script lines
277
279
* @throws IOException in case of I/O errors
278
280
*/
279
281
private static String readScript (EncodedResource resource , @ Nullable String commentPrefix ,
280
- @ Nullable String separator ) throws IOException {
282
+ @ Nullable String separator , @ Nullable String blockCommentEndDelimiter ) throws IOException {
281
283
282
284
LineNumberReader lnr = new LineNumberReader (resource .getReader ());
283
285
try {
284
- return readScript (lnr , commentPrefix , separator );
286
+ return readScript (lnr , commentPrefix , separator , blockCommentEndDelimiter );
285
287
}
286
288
finally {
287
289
lnr .close ();
@@ -297,19 +299,21 @@ private static String readScript(EncodedResource resource, @Nullable String comm
297
299
* a statement — will be included in the results.
298
300
* @param lineNumberReader the {@code LineNumberReader} containing the script
299
301
* to be processed
300
- * @param commentPrefix the prefix that identifies comments in the SQL script —
301
- * typically "--"
302
- * @param separator the statement separator in the SQL script — typically ";"
302
+ * @param lineCommentPrefix the prefix that identifies comments in the SQL script
303
+ * (typically "--")
304
+ * @param separator the statement separator in the SQL script (typically ";")
305
+ * @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
303
306
* @return a {@code String} containing the script lines
304
307
* @throws IOException in case of I/O errors
305
308
*/
306
- public static String readScript (LineNumberReader lineNumberReader , @ Nullable String commentPrefix ,
307
- @ Nullable String separator ) throws IOException {
309
+ public static String readScript (LineNumberReader lineNumberReader , @ Nullable String lineCommentPrefix ,
310
+ @ Nullable String separator , @ Nullable String blockCommentEndDelimiter ) throws IOException {
308
311
309
312
String currentStatement = lineNumberReader .readLine ();
310
313
StringBuilder scriptBuilder = new StringBuilder ();
311
314
while (currentStatement != null ) {
312
- if (commentPrefix != null && !currentStatement .startsWith (commentPrefix )) {
315
+ if ((blockCommentEndDelimiter != null && currentStatement .contains (blockCommentEndDelimiter )) ||
316
+ (lineCommentPrefix != null && !currentStatement .startsWith (lineCommentPrefix ))) {
313
317
if (scriptBuilder .length () > 0 ) {
314
318
scriptBuilder .append ('\n' );
315
319
}
@@ -431,16 +435,14 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
431
435
* @param ignoreFailedDrops whether or not to continue in the event of specifically
432
436
* an error on a {@code DROP} statement
433
437
* @param commentPrefix the prefix that identifies single-line comments in the
434
- * SQL script — typically "--"
438
+ * SQL script ( typically "--")
435
439
* @param separator the script statement separator; defaults to
436
440
* {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
437
441
* {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
438
442
* {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
439
443
* single statement without a separator
440
- * @param blockCommentStartDelimiter the <em>start</em> block comment delimiter; never
441
- * {@code null} or empty
442
- * @param blockCommentEndDelimiter the <em>end</em> block comment delimiter; never
443
- * {@code null} or empty
444
+ * @param blockCommentStartDelimiter the <em>start</em> block comment delimiter
445
+ * @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
444
446
* @throws ScriptException if an error occurred while executing the SQL script
445
447
* @see #DEFAULT_STATEMENT_SEPARATOR
446
448
* @see #FALLBACK_STATEMENT_SEPARATOR
@@ -460,7 +462,7 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
460
462
461
463
String script ;
462
464
try {
463
- script = readScript (resource , commentPrefix , separator );
465
+ script = readScript (resource , commentPrefix , separator , blockCommentEndDelimiter );
464
466
}
465
467
catch (IOException ex ) {
466
468
throw new CannotReadScriptException (resource , ex );
0 commit comments