[ZEPPELIN-2554] sql parser fix (backslash)

This commit is contained in:
Tinkoff DWH 2017-05-16 12:29:56 +05:00
parent 5fde8c21d6
commit 3951aa7053
2 changed files with 25 additions and 10 deletions

View file

@ -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) {

View file

@ -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();