Skip to content
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
46 changes: 39 additions & 7 deletions jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,19 +506,36 @@ private boolean isDDLCommand(int updatedCount, int columnCount) throws SQLExcept
protected ArrayList<String> splitSqlQueries(String sql) {
ArrayList<String> queries = new ArrayList<>();
StringBuilder query = new StringBuilder();
Character character;
char character;

Boolean antiSlash = false;
Boolean multiLineComment = false;
Boolean singleLineComment = false;
Boolean quoteString = false;
Boolean doubleQuoteString = false;

for (int item = 0; item < sql.length(); item++) {
character = sql.charAt(item);

if (character.equals('\\')) {
if ((singleLineComment && (character == '\n' || item == sql.length() - 1))
|| (multiLineComment && character == '/' && sql.charAt(item - 1) == '*')) {
singleLineComment = false;
multiLineComment = false;
if (item == sql.length() - 1 && query.length() > 0) {
queries.add(StringUtils.trim(query.toString()));
}
continue;
}

if (singleLineComment || multiLineComment) {
continue;
}

if (character == '\\') {
antiSlash = true;
}
if (character.equals('\'')) {

if (character == '\'') {
if (antiSlash) {
antiSlash = false;
} else if (quoteString) {
Expand All @@ -527,7 +544,8 @@ protected ArrayList<String> splitSqlQueries(String sql) {
quoteString = true;
}
}
if (character.equals('"')) {

if (character == '"') {
if (antiSlash) {
antiSlash = false;
} else if (doubleQuoteString) {
Expand All @@ -537,16 +555,30 @@ protected ArrayList<String> splitSqlQueries(String sql) {
}
}

if (character.equals(';') && !antiSlash && !quoteString && !doubleQuoteString) {
queries.add(query.toString());
if (!quoteString && !doubleQuoteString && !multiLineComment && !singleLineComment
&& sql.length() > item + 1) {
if (character == '-' && sql.charAt(item + 1) == '-') {
singleLineComment = true;
continue;
}

if (character == '/' && sql.charAt(item + 1) == '*') {
multiLineComment = true;
continue;
}
}

if (character == ';' && !antiSlash && !quoteString && !doubleQuoteString) {
queries.add(StringUtils.trim(query.toString()));
query = new StringBuilder();
} else if (item == sql.length() - 1) {
query.append(character);
queries.add(query.toString());
queries.add(StringUtils.trim(query.toString()));
} else {
query.append(character);
}
}

return queries;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,32 @@ public void testPrecodeWithAnotherPrefix() throws SQLException, IOException {
assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType());
assertEquals("@TESTVARIABLE\n2\n", interpreterResult.message().get(0).getData());
}

@Test
public void testExcludingComments() throws SQLException, IOException {
Properties properties = new Properties();
properties.setProperty("common.max_count", "1000");
properties.setProperty("common.max_retry", "3");
properties.setProperty("default.driver", "org.h2.Driver");
properties.setProperty("default.url", getJdbcConnection());
properties.setProperty("default.user", "");
properties.setProperty("default.password", "");
JDBCInterpreter t = new JDBCInterpreter(properties);
t.open();

String sqlQuery = "/* ; */\n" +
"-- /* comment\n" +
"--select * from test_table\n" +
"select * from test_table; /* some comment ; */\n" +
"/*\n" +
"select * from test_table;\n" +
"*/\n" +
"-- a ; b\n" +
"select * from test_table WHERE ID = ';--';\n" +
"select * from test_table WHERE ID = '/*' -- test";

InterpreterResult interpreterResult = t.interpret(sqlQuery, interpreterContext);
assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
assertEquals(3, interpreterResult.message().size());
}
}