Skip to content

Commit

Permalink
improved mysql parser Kill_Statement support. issue #1326
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 13, 2016
1 parent f526bf5 commit 11ff35f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -36,16 +39,20 @@ public void setType(Type type) {
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -508,13 +509,13 @@ public SQLStatement parseKill() {
} 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1492,8 +1492,11 @@ public boolean visit(MySqlKillStatement x) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public void endVisit(MySqlKillStatement x) {

@Override
public boolean visit(MySqlKillStatement x) {
return true;
return false;
}

@Override
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/alibaba/druid/bvt/sql/mysql/Kill_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,27 @@ public void test_1() throws Exception {
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();

Expand Down

0 comments on commit 11ff35f

Please sign in to comment.