mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved mysql parser, bug fixed for mysql grant syntax support. for issue #2338
This commit is contained in:
@@ -27,6 +27,7 @@ public class MySqlUserName extends MySqlExprImpl implements SQLName, Cloneable {
|
||||
|
||||
private String userName;
|
||||
private String host;
|
||||
private String identifiedBy;
|
||||
|
||||
private long userNameHashCod64;
|
||||
private long hashCode64;
|
||||
@@ -63,6 +64,14 @@ public class MySqlUserName extends MySqlExprImpl implements SQLName, Cloneable {
|
||||
return userName + '@' + host;
|
||||
}
|
||||
|
||||
public String getIdentifiedBy() {
|
||||
return identifiedBy;
|
||||
}
|
||||
|
||||
public void setIdentifiedBy(String identifiedBy) {
|
||||
this.identifiedBy = identifiedBy;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getSimpleName();
|
||||
}
|
||||
@@ -70,8 +79,9 @@ public class MySqlUserName extends MySqlExprImpl implements SQLName, Cloneable {
|
||||
public MySqlUserName clone() {
|
||||
MySqlUserName x = new MySqlUserName();
|
||||
|
||||
x.userName = userName;
|
||||
x.host = host;
|
||||
x.userName = userName;
|
||||
x.host = host;
|
||||
x.identifiedBy = identifiedBy;
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -348,6 +348,14 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
userName.setHost(lexer.stringVal());
|
||||
}
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIED) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
userName.setIdentifiedBy(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
return userName;
|
||||
}
|
||||
|
||||
@@ -696,6 +704,14 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
userName.setHost(lexer.stringVal());
|
||||
}
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIED) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
userName.setIdentifiedBy(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
return userName;
|
||||
}
|
||||
return super.nameRest(name);
|
||||
|
||||
@@ -2047,10 +2047,19 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
@Override
|
||||
public boolean visit(MySqlUserName x) {
|
||||
print0(x.getUserName());
|
||||
if (x.getHost() != null) {
|
||||
|
||||
String host = x.getHost();
|
||||
if (host != null) {
|
||||
print('@');
|
||||
print0(x.getHost());
|
||||
print0(host);
|
||||
}
|
||||
|
||||
String identifiedBy = x.getIdentifiedBy();
|
||||
if (identifiedBy != null) {
|
||||
print0(ucase ? " IDENTIFIED BY " : " identified by ");
|
||||
print0(identifiedBy);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -693,7 +693,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else {
|
||||
expr = this.exprParser.expr();
|
||||
}
|
||||
|
||||
|
||||
if (stmt.getObjectType() == SQLObjectType.TABLE || stmt.getObjectType() == null) {
|
||||
stmt.setOn(new SQLExprTableSource(expr));
|
||||
} else {
|
||||
@@ -709,6 +709,11 @@ public class SQLStatementParser extends SQLParser {
|
||||
if (lexer.token == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token == Token.GRANT) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("OPTION");
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (lexer.identifierEquals("MAX_QUERIES_PER_HOUR")) {
|
||||
lexer.nextToken();
|
||||
@@ -862,6 +867,9 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else if (lexer.token == Token.SESSION) {
|
||||
privilege = "ALTER SESSION";
|
||||
lexer.nextToken();
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.ROUTINE)) {
|
||||
privilege = "ALTER ROUTINE";
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token == Token.ANY) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -875,6 +883,8 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else {
|
||||
throw new ParserException("TODO : " + lexer.token + " " + lexer.stringVal());
|
||||
}
|
||||
} else if (lexer.token == Token.ON) {
|
||||
privilege = "ALTER";
|
||||
} else {
|
||||
throw new ParserException("TODO : " + lexer.token + " " + lexer.stringVal());
|
||||
}
|
||||
|
||||
@@ -639,5 +639,6 @@ public final class FnvHash {
|
||||
long COMPRESSION = fnv1a_64_lower("COMPRESSION");
|
||||
long KEY_BLOCK_SIZE = fnv1a_64_lower("KEY_BLOCK_SIZE");
|
||||
long CHECKSUM = fnv1a_64_lower("CHECKSUM");
|
||||
long ROUTINE = fnv1a_64_lower("ROUTINE");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ public class MySqlGrantTest_35 extends MysqlTest {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
// print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
@@ -42,18 +41,92 @@ public class MySqlGrantTest_35 extends MysqlTest {
|
||||
assertEquals("GRANT CREATE ON * TO hello@'%';", //
|
||||
output);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
}
|
||||
|
||||
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("City")));
|
||||
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));
|
||||
|
||||
// assertTrue(visitor.getColumns().contains(new Column("t2", "id")));
|
||||
public void test_1() throws Exception {
|
||||
String sql = "GRANT ALTER ON . TO hello@'%'; ";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("GRANT ALTER ON * TO hello@'%';", //
|
||||
output);
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
}
|
||||
|
||||
public void test_2() throws Exception {
|
||||
String sql = "GRANT CREATE TEMPORARY TABLE ON . TO hello@'%';";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("GRANT CREATE TEMPORARY TABLE ON * TO hello@'%';", //
|
||||
output);
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
String sql = "GRANT ALTER ROUTINE ON . TO hello@'%';";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("GRANT ALTER ROUTINE ON * TO hello@'%';", //
|
||||
output);
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
}
|
||||
|
||||
public void test_4() throws Exception {
|
||||
String sql = "grant all on . to hello@'%' identified by 'helloPassword' with grant option MAX_QUERIES_PER_HOUR 90;";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("GRANT ALL ON * TO hello@'%' IDENTIFIED BY helloPassword WITH MAX_QUERIES_PER_HOUR 90;", //
|
||||
output);
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user