Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix-15374][SQL Task]Remove trailing semicolons from a SQL sentence. #15376

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@
import java.util.List;
import java.util.stream.Collectors;

import com.google.common.base.CharMatcher;

public class SqlSplitUtils {

private static final String UNIX_SQL_SEPARATOR = ";\n";
private static final String WINDOWS_SQL_SEPARATOR = ";\r\n";

/**
* remove trailing ; from a sql sentence
*/
public static String removeTrailingSemicolons(String sql) {
return CharMatcher.is(';').trimTrailingFrom(sql);
}

/**
* split sql to submit sql.
* e.g.
Expand All @@ -41,10 +50,9 @@ public class SqlSplitUtils {
public static List<String> splitSql(String sql) {

return Arrays.stream(sql.replaceAll(WINDOWS_SQL_SEPARATOR, UNIX_SQL_SEPARATOR).split(UNIX_SQL_SEPARATOR))
.filter(subSql -> {
String trim = subSql.trim();
return !trim.isEmpty() && !trim.startsWith("--");
}).collect(Collectors.toList());
.map(subSql -> removeTrailingSemicolons(subSql.trim()))
.filter(subSql -> !subSql.isEmpty() && !subSql.startsWith("--"))
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ void splitSql() {
Lists.newArrayList("select * from table1", "select * from table2", "select * from table3"),
SqlSplitUtils.splitSql(sql));
}

@Test
void splitSqlTrailingSemicolons() {
String sql = "select * from table1;";
Assertions.assertEquals(
Lists.newArrayList("select * from table1"),
SqlSplitUtils.splitSql(sql));
}
}
Loading