mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved mysql parser Kill_Statement support. issue #1326
This commit is contained in:
+13
-6
@@ -15,16 +15,19 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlKillStatement extends MySqlStatementImpl {
|
||||
|
||||
private Type type;
|
||||
private SQLExpr threadId;
|
||||
private Type type;
|
||||
private List<SQLExpr> threadIds = new ArrayList<SQLExpr>();
|
||||
|
||||
public static enum Type {
|
||||
CONNECTION, QUERY
|
||||
CONNECTION, QUERY
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
@@ -36,16 +39,20 @@ public class MySqlKillStatement extends MySqlStatementImpl {
|
||||
}
|
||||
|
||||
public SQLExpr getThreadId() {
|
||||
return threadId;
|
||||
return threadIds.get(0);
|
||||
}
|
||||
|
||||
public void setThreadId(SQLExpr threadId) {
|
||||
this.threadId = threadId;
|
||||
this.threadIds.set(0, threadId);
|
||||
}
|
||||
|
||||
public List<SQLExpr> getThreadIds() {
|
||||
return threadIds;
|
||||
}
|
||||
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, threadId);
|
||||
acceptChild(visitor, threadIds);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
@@ -508,13 +509,13 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
} else if (identifierEquals("QUERY")) {
|
||||
stmt.setType(MySqlKillStatement.Type.QUERY);
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.LITERAL_INT) {
|
||||
// skip
|
||||
} else {
|
||||
throw new ParserException("not support kill type " + lexer.token());
|
||||
}
|
||||
|
||||
SQLExpr threadId = this.exprParser.expr();
|
||||
stmt.setThreadId(threadId);
|
||||
|
||||
this.exprParser.exprList(stmt.getThreadIds(), stmt);
|
||||
return stmt;
|
||||
}
|
||||
|
||||
|
||||
@@ -1492,8 +1492,11 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print0(ucase ? "KILL CONNECTION " : "kill connection ");
|
||||
} else if (MySqlKillStatement.Type.QUERY.equals(x.getType())) {
|
||||
print0(ucase ? "KILL QUERY " : "kill query ");
|
||||
} else {
|
||||
print0(ucase ? "KILL " : "kill ");
|
||||
}
|
||||
x.getThreadId().accept(this);
|
||||
|
||||
printAndAccept(x.getThreadIds(), ", ");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -448,7 +448,7 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlKillStatement x) {
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -48,7 +48,27 @@ public class Kill_Test extends TestCase {
|
||||
Assert.assertEquals("KILL CONNECTION 233;", text);
|
||||
}
|
||||
|
||||
public void test_2() throws Exception {
|
||||
String sql = "KILL 233";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
Assert.assertEquals("KILL 233;", text);
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
String sql = "KILL 233,234";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
Assert.assertEquals("KILL 233, 234;", text);
|
||||
}
|
||||
private String output(List<SQLStatement> stmtList) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user