mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
[ZEPPELIN-2554] sql parser fix (backslash)
This commit is contained in:
parent
5fde8c21d6
commit
3951aa7053
2 changed files with 25 additions and 10 deletions
|
|
@ -499,7 +499,7 @@ public class JDBCInterpreter extends Interpreter {
|
|||
StringBuilder query = new StringBuilder();
|
||||
char character;
|
||||
|
||||
Boolean antiSlash = false;
|
||||
Boolean backslash = false;
|
||||
Boolean multiLineComment = false;
|
||||
Boolean singleLineComment = false;
|
||||
Boolean quoteString = false;
|
||||
|
|
@ -508,6 +508,12 @@ public class JDBCInterpreter extends Interpreter {
|
|||
for (int item = 0; item < sql.length(); item++) {
|
||||
character = sql.charAt(item);
|
||||
|
||||
if (backslash) {
|
||||
query.append(character);
|
||||
backslash = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((singleLineComment && (character == '\n' || item == sql.length() - 1))
|
||||
|| (multiLineComment && character == '/' && sql.charAt(item - 1) == '*')) {
|
||||
singleLineComment = false;
|
||||
|
|
@ -523,13 +529,11 @@ public class JDBCInterpreter extends Interpreter {
|
|||
}
|
||||
|
||||
if (character == '\\') {
|
||||
antiSlash = true;
|
||||
backslash = true;
|
||||
}
|
||||
|
||||
if (character == '\'') {
|
||||
if (antiSlash) {
|
||||
antiSlash = false;
|
||||
} else if (quoteString) {
|
||||
if (quoteString) {
|
||||
quoteString = false;
|
||||
} else if (!doubleQuoteString) {
|
||||
quoteString = true;
|
||||
|
|
@ -537,9 +541,7 @@ public class JDBCInterpreter extends Interpreter {
|
|||
}
|
||||
|
||||
if (character == '"') {
|
||||
if (antiSlash) {
|
||||
antiSlash = false;
|
||||
} else if (doubleQuoteString) {
|
||||
if (doubleQuoteString) {
|
||||
doubleQuoteString = false;
|
||||
} else if (!quoteString) {
|
||||
doubleQuoteString = true;
|
||||
|
|
@ -559,7 +561,7 @@ public class JDBCInterpreter extends Interpreter {
|
|||
}
|
||||
}
|
||||
|
||||
if (character == ';' && !antiSlash && !quoteString && !doubleQuoteString) {
|
||||
if (character == ';' && !backslash && !quoteString && !doubleQuoteString) {
|
||||
queries.add(StringUtils.trim(query.toString()));
|
||||
query = new StringBuilder();
|
||||
} else if (item == sql.length() - 1) {
|
||||
|
|
@ -597,7 +599,6 @@ public class JDBCInterpreter extends Interpreter {
|
|||
String user = interpreterContext.getAuthenticationInfo().getUser();
|
||||
|
||||
InterpreterResult interpreterResult = new InterpreterResult(InterpreterResult.Code.SUCCESS);
|
||||
|
||||
try {
|
||||
connection = getConnection(propertyKey, interpreterContext);
|
||||
if (connection == null) {
|
||||
|
|
|
|||
|
|
@ -188,6 +188,20 @@ public class JDBCInterpreterTest extends BasicJDBCTestCaseAdapter {
|
|||
assertEquals("select * from test_table WHERE ID = ';'", multipleSqlArray.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSqlQueryWithBackslash() throws SQLException, IOException {
|
||||
String sqlQuery = "select '\\n', ';';" +
|
||||
"select replace('A\\;B', '\\', 'text')";
|
||||
|
||||
Properties properties = new Properties();
|
||||
JDBCInterpreter t = new JDBCInterpreter(properties);
|
||||
t.open();
|
||||
ArrayList<String> multipleSqlArray = t.splitSqlQueries(sqlQuery);
|
||||
assertEquals(2, multipleSqlArray.size());
|
||||
assertEquals("select '\\n', ';'", multipleSqlArray.get(0));
|
||||
assertEquals("select replace('A\\;B', '\\', 'text')", multipleSqlArray.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectMultipleQuries() throws SQLException, IOException {
|
||||
Properties properties = new Properties();
|
||||
|
|
|
|||
Loading…
Reference in a new issue