Skip to content

Commit

Permalink
Fix ScriptUtils splitting with Windows EOF (#1467, #1465)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppfeiler authored and rnorth committed May 18, 2019
1 parent 03f8caa commit e4ce8f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Expand Up @@ -206,7 +206,7 @@ else if (script.startsWith(blockCommentStartDelimiter, i)) {
blockCommentEndDelimiter), resource);
}
}
else if (c == ' ' || c == '\n' || c == '\t') {
else if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {
// avoid multiple adjacent whitespace characters
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') {
c = ' ';
Expand Down
Expand Up @@ -28,4 +28,21 @@ public void testSplit() throws IOException {
assertEquals("SELECT * from `bar`", statements.get(4));
assertEquals("INSERT INTO bar (foo) VALUES ('hello world')", statements.get(6));
}

/*
* Test ScriptUtils script splitting with some ugly/hard-to-split cases and linux line endings
*/
@Test
public void testSplitWithWidnwosLineEnding() throws IOException {
final String script = Resources.toString(Resources.getResource("splittable.sql"), Charsets.UTF_8);
final String scriptWithWindowsLineEndings = script.replaceAll("\n", "\r\n");
final List<String> statements = new ArrayList<>();
ScriptUtils.splitSqlScript("resourcename", scriptWithWindowsLineEndings, ";", "--", "/*", "*/", statements);

assertEquals(7, statements.size());
assertEquals("SELECT \"a /* string literal containing comment characters like -- here\"", statements.get(2));
assertEquals("SELECT \"a 'quoting' \\\"scenario ` involving BEGIN keyword\\\" here\"", statements.get(3));
assertEquals("SELECT * from `bar`", statements.get(4));
assertEquals("INSERT INTO bar (foo) VALUES ('hello world')", statements.get(6));
}
}

0 comments on commit e4ce8f5

Please sign in to comment.