improved sql parser for mysql explain. for issue #1645

This commit is contained in:
wenshao
2017-03-22 13:52:30 +08:00
parent a861aeab24
commit c607356412
4 changed files with 42 additions and 0 deletions
@@ -24,6 +24,8 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLExplainStatement extends SQLStatementImpl {
private String type;
protected SQLStatement statement;
private List<SQLCommentHint> hints;
@@ -47,6 +49,14 @@ public class SQLExplainStatement extends SQLStatementImpl {
this.statement = statement;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -2087,6 +2087,15 @@ public class SQLStatementParser extends SQLParser {
explain.setHints(this.exprParser.parseHints());
}
if (JdbcConstants.MYSQL.equals(dbType)) {
if (identifierEquals("FORMAT")
|| identifierEquals("EXTENDED")
|| identifierEquals("PARTITIONS")) {
explain.setType(lexer.stringVal);
lexer.nextToken();
}
}
explain.setStatement(parseStatement());
return explain;
@@ -2631,6 +2631,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
print(' ');
printAndAccept(x.getHints(), " ");
}
if (x.getType() != null) {
print(' ');
print0(x.getType());
}
println();
x.getStatement().accept(this);
return false;
@@ -0,0 +1,18 @@
package com.alibaba.druid.bvt.bug;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
/**
* Created by wenshao on 22/03/2017.
*/
public class Issue1645 extends TestCase {
public void test_for_issue() throws Exception {
String sql = "explain extended select * from foo";
String formatedSql = SQLUtils.format(sql, JdbcConstants.MYSQL);
assertEquals("EXPLAIN extended\n" +
"SELECT *\n" +
"FROM foo", formatedSql);
}
}