Add multi-prefix comment support for SQL scripts
Update `ResourceDatabasePopulator` and `ScriptUtils` so that more than one comment prefix can be used when processing SQL scripts. This feature is particularly useful when dealing with scripts provided by Quartz since they often use a mix `--` and `#`. Closes gh-23289
This commit is contained in:
committed by
Sam Brannen
parent
1ff29b0f6b
commit
17914fc44b
@@ -25,6 +25,9 @@ import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER;
|
||||
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER;
|
||||
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_COMMENT_PREFIXES;
|
||||
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_STATEMENT_SEPARATOR;
|
||||
import static org.springframework.jdbc.datasource.init.ScriptUtils.containsSqlScriptDelimiters;
|
||||
import static org.springframework.jdbc.datasource.init.ScriptUtils.splitSqlScript;
|
||||
@@ -117,18 +120,25 @@ public class ScriptUtilsUnitTests {
|
||||
@Test
|
||||
public void readAndSplitScriptContainingComments() throws Exception {
|
||||
String script = readScript("test-data-with-comments.sql");
|
||||
splitScriptContainingComments(script);
|
||||
splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAndSplitScriptContainingCommentsWithWindowsLineEnding() throws Exception {
|
||||
String script = readScript("test-data-with-comments.sql").replaceAll("\n", "\r\n");
|
||||
splitScriptContainingComments(script);
|
||||
splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES);
|
||||
}
|
||||
|
||||
private void splitScriptContainingComments(String script) throws Exception {
|
||||
@Test
|
||||
public void readAndSplitScriptContainingCommentsWithMultiplePrefixes() throws Exception {
|
||||
String script = readScript("test-data-with-multi-prefix-comments.sql");
|
||||
splitScriptContainingComments(script, "--", "#", "^");
|
||||
}
|
||||
|
||||
private void splitScriptContainingComments(String script, String... commentPrefixes) throws Exception {
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
|
||||
DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
|
||||
|
||||
String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')";
|
||||
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
-- The next comment line has no text after the '--' prefix.
|
||||
--
|
||||
-- The next comment line starts with a space.
|
||||
-- x, y, z...
|
||||
|
||||
insert into customer (id, name)
|
||||
values (1, 'Rod; Johnson'), (2, 'Adrian Collier');
|
||||
-- This is also a comment.
|
||||
insert into orders(id, order_date, customer_id)
|
||||
values (1, '2008-01-02', 2);
|
||||
# A comment with a different prefix
|
||||
insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2);
|
||||
INSERT INTO persons( person_id--
|
||||
, name)
|
||||
^ A comment with yet another different prefix
|
||||
VALUES( 1 -- person_id
|
||||
, 'Name' --name
|
||||
);--
|
||||
Reference in New Issue
Block a user