mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser support revoke
This commit is contained in:
@@ -19,6 +19,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -26,27 +27,27 @@ public class SQLGrantStatement extends SQLStatementImpl {
|
||||
|
||||
private final List<SQLExpr> privileges = new ArrayList<SQLExpr>();
|
||||
|
||||
private SQLExpr on;
|
||||
private SQLObject on;
|
||||
private SQLExpr to;
|
||||
|
||||
public SQLGrantStatement() {
|
||||
|
||||
|
||||
public SQLGrantStatement(){
|
||||
|
||||
}
|
||||
|
||||
public SQLGrantStatement(String dbType) {
|
||||
super (dbType);
|
||||
|
||||
public SQLGrantStatement(String dbType){
|
||||
super(dbType);
|
||||
}
|
||||
|
||||
// mysql
|
||||
private ObjectType objectType;
|
||||
private SQLExpr maxQueriesPerHour;
|
||||
private SQLExpr maxUpdatesPerHour;
|
||||
private SQLExpr maxConnectionsPerHour;
|
||||
private SQLExpr maxUserConnections;
|
||||
private SQLObjectType objectType;
|
||||
private SQLExpr maxQueriesPerHour;
|
||||
private SQLExpr maxUpdatesPerHour;
|
||||
private SQLExpr maxConnectionsPerHour;
|
||||
private SQLExpr maxUserConnections;
|
||||
|
||||
private boolean adminOption;
|
||||
private boolean adminOption;
|
||||
|
||||
private SQLExpr identifiedBy;
|
||||
private SQLExpr identifiedBy;
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
@@ -58,20 +59,21 @@ public class SQLGrantStatement extends SQLStatementImpl {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public ObjectType getObjectType() {
|
||||
public SQLObjectType getObjectType() {
|
||||
return objectType;
|
||||
}
|
||||
|
||||
public void setObjectType(ObjectType objectType) {
|
||||
public void setObjectType(SQLObjectType objectType) {
|
||||
this.objectType = objectType;
|
||||
}
|
||||
|
||||
public SQLExpr getOn() {
|
||||
public SQLObject getOn() {
|
||||
return on;
|
||||
}
|
||||
|
||||
public void setOn(SQLExpr on) {
|
||||
public void setOn(SQLObject on) {
|
||||
this.on = on;
|
||||
on.setParent(this);
|
||||
}
|
||||
|
||||
public SQLExpr getTo() {
|
||||
@@ -133,8 +135,4 @@ public class SQLGrantStatement extends SQLStatementImpl {
|
||||
public void setIdentifiedBy(SQLExpr identifiedBy) {
|
||||
this.identifiedBy = identifiedBy;
|
||||
}
|
||||
|
||||
public static enum ObjectType {
|
||||
TABLE, FUNCTION, PROCEDURE, USER
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
public enum SQLObjectType {
|
||||
TABLE, FUNCTION, PROCEDURE, USER
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLRevokeStatement extends SQLStatementImpl {
|
||||
|
||||
private final List<SQLExpr> privileges = new ArrayList<SQLExpr>();
|
||||
|
||||
private SQLObject on;
|
||||
private SQLExpr from;
|
||||
// mysql
|
||||
private SQLObjectType objectType;
|
||||
|
||||
public SQLRevokeStatement(){
|
||||
|
||||
}
|
||||
|
||||
public SQLRevokeStatement(String dbType){
|
||||
super(dbType);
|
||||
}
|
||||
|
||||
public SQLObject getOn() {
|
||||
return on;
|
||||
}
|
||||
|
||||
public void setOn(SQLObject on) {
|
||||
this.on = on;
|
||||
}
|
||||
|
||||
public SQLExpr getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(SQLExpr from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public List<SQLExpr> getPrivileges() {
|
||||
return privileges;
|
||||
}
|
||||
|
||||
public SQLObjectType getObjectType() {
|
||||
return objectType;
|
||||
}
|
||||
|
||||
public void setObjectType(SQLObjectType objectType) {
|
||||
this.objectType = objectType;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, on);
|
||||
acceptChild(visitor, from);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
@@ -329,6 +329,12 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
statementList.add(this.parseGrant());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.REVOKE) {
|
||||
statementList.add(this.parseRevoke());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.COMMENT) {
|
||||
statementList.add(this.parseComment());
|
||||
continue;
|
||||
|
||||
@@ -19,7 +19,6 @@ import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLTimestampExpr;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleDateExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGOrderBy;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGArrayExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGTypeCastExpr;
|
||||
|
||||
@@ -133,6 +133,7 @@ public class Keywords {
|
||||
map.put("COMPUTE", Token.COMPUTE);
|
||||
map.put("WITH", Token.WITH);
|
||||
map.put("GRANT", Token.GRANT);
|
||||
map.put("REVOKE", Token.REVOKE);
|
||||
|
||||
DEFAULT_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
@@ -846,6 +846,7 @@ public class SQLExprParser extends SQLParser {
|
||||
case ANALYZE:
|
||||
case OPTIMIZE:
|
||||
case GRANT:
|
||||
case REVOKE:
|
||||
identName = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
break;
|
||||
|
||||
@@ -119,6 +119,7 @@ public class SQLParser {
|
||||
case ANALYZE:
|
||||
case OPTIMIZE:
|
||||
case GRANT:
|
||||
case REVOKE:
|
||||
case FULL:
|
||||
case TO:
|
||||
case NEW:
|
||||
|
||||
@@ -63,11 +63,12 @@ import com.alibaba.druid.sql.ast.statement.SQLDropViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExplainStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLGrantStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLGrantStatement.ObjectType;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertInto;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLObjectType;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRevokeStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRollbackStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSavePointStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
@@ -241,6 +242,12 @@ public class SQLStatementParser extends SQLParser {
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.REVOKE) {
|
||||
SQLStatement stmt = parseRevoke();
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) {
|
||||
SQLCallStatement stmt = parseCall();
|
||||
@@ -355,6 +362,86 @@ public class SQLStatementParser extends SQLParser {
|
||||
accept(Token.GRANT);
|
||||
SQLGrantStatement stmt = new SQLGrantStatement(getDbType());
|
||||
|
||||
parsePrivileages(stmt.getPrivileges(), stmt);
|
||||
|
||||
if (lexer.token() == Token.ON) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.PROCEDURE) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(SQLObjectType.PROCEDURE);
|
||||
} else if (lexer.token() == Token.FUNCTION) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(SQLObjectType.FUNCTION);
|
||||
} else if (lexer.token() == Token.TABLE) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(SQLObjectType.TABLE);
|
||||
} else if (lexer.token() == Token.USER) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(SQLObjectType.USER);
|
||||
}
|
||||
|
||||
SQLExpr expr = this.exprParser.expr();
|
||||
if (stmt.getObjectType() == SQLObjectType.TABLE || stmt.getObjectType() == null) {
|
||||
stmt.setOn(new SQLExprTableSource(expr));
|
||||
} else {
|
||||
stmt.setOn(expr);
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.TO) {
|
||||
lexer.nextToken();
|
||||
stmt.setTo(this.exprParser.expr());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
|
||||
for (;;) {
|
||||
if (identifierEquals("MAX_QUERIES_PER_HOUR")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxQueriesPerHour(this.exprParser.primary());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identifierEquals("MAX_UPDATES_PER_HOUR")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxUpdatesPerHour(this.exprParser.primary());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identifierEquals("MAX_CONNECTIONS_PER_HOUR")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxConnectionsPerHour(this.exprParser.primary());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identifierEquals("MAX_USER_CONNECTIONS")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxUserConnections(this.exprParser.primary());
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (identifierEquals("ADMIN")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("OPTION");
|
||||
stmt.setAdminOption(true);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIED) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
stmt.setIdentifiedBy(this.exprParser.expr());
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
protected void parsePrivileages(List<SQLExpr> privileges, SQLObject parent) {
|
||||
for (;;) {
|
||||
String privilege = null;
|
||||
if (lexer.token() == Token.ALL) {
|
||||
@@ -553,8 +640,8 @@ public class SQLStatementParser extends SQLParser {
|
||||
expr = this.exprParser.primaryRest(expr);
|
||||
}
|
||||
|
||||
expr.setParent(stmt);
|
||||
stmt.getPrivileges().add(expr);
|
||||
expr.setParent(parent);
|
||||
privileges.add(expr);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
@@ -563,74 +650,43 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public SQLRevokeStatement parseRevoke() {
|
||||
accept(Token.REVOKE);
|
||||
|
||||
SQLRevokeStatement stmt = new SQLRevokeStatement(getDbType());
|
||||
|
||||
parsePrivileages(stmt.getPrivileges(), stmt);
|
||||
|
||||
if (lexer.token() == Token.ON) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.PROCEDURE) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(ObjectType.PROCEDURE);
|
||||
stmt.setObjectType(SQLObjectType.PROCEDURE);
|
||||
} else if (lexer.token() == Token.FUNCTION) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(ObjectType.FUNCTION);
|
||||
stmt.setObjectType(SQLObjectType.FUNCTION);
|
||||
} else if (lexer.token() == Token.TABLE) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(ObjectType.TABLE);
|
||||
stmt.setObjectType(SQLObjectType.TABLE);
|
||||
} else if (lexer.token() == Token.USER) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(ObjectType.USER);
|
||||
stmt.setObjectType(SQLObjectType.USER);
|
||||
}
|
||||
|
||||
stmt.setOn(this.exprParser.expr());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.TO) {
|
||||
lexer.nextToken();
|
||||
stmt.setTo(this.exprParser.expr());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
|
||||
for (;;) {
|
||||
if (identifierEquals("MAX_QUERIES_PER_HOUR")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxQueriesPerHour(this.exprParser.primary());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identifierEquals("MAX_UPDATES_PER_HOUR")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxUpdatesPerHour(this.exprParser.primary());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identifierEquals("MAX_CONNECTIONS_PER_HOUR")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxConnectionsPerHour(this.exprParser.primary());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identifierEquals("MAX_USER_CONNECTIONS")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxUserConnections(this.exprParser.primary());
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
SQLExpr expr = this.exprParser.expr();
|
||||
if (stmt.getObjectType() == SQLObjectType.TABLE || stmt.getObjectType() == null) {
|
||||
stmt.setOn(new SQLExprTableSource(expr));
|
||||
} else {
|
||||
stmt.setOn(expr);
|
||||
}
|
||||
}
|
||||
|
||||
if (identifierEquals("ADMIN")) {
|
||||
if (lexer.token() == Token.FROM) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("OPTION");
|
||||
stmt.setAdminOption(true);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIED) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
stmt.setIdentifiedBy(this.exprParser.expr());
|
||||
stmt.setFrom(this.exprParser.expr());
|
||||
}
|
||||
|
||||
return stmt;
|
||||
|
||||
@@ -177,6 +177,7 @@ public enum Token {
|
||||
DECLARE("DECLARE"),
|
||||
EXCEPTION("EXCEPTION"),
|
||||
GRANT("GRANT"),
|
||||
REVOKE("REVOKE"),
|
||||
LOOP("LOOP"),
|
||||
GOTO("GOTO"),
|
||||
COMMIT("COMMIT"),
|
||||
|
||||
@@ -124,6 +124,7 @@ import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKeyImpl;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRevokeStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRollbackStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSavePointStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
@@ -1851,6 +1852,30 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLRevokeStatement x) {
|
||||
print("ROVOKE ");
|
||||
printAndAccept(x.getPrivileges(), ", ");
|
||||
|
||||
if (x.getOn() != null) {
|
||||
print(" ON ");
|
||||
|
||||
if (x.getObjectType() != null) {
|
||||
print(x.getObjectType().name());
|
||||
print(' ');
|
||||
}
|
||||
|
||||
x.getOn().accept(this);
|
||||
}
|
||||
|
||||
if (x.getFrom() != null) {
|
||||
print(" FROM ");
|
||||
x.getFrom().accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropDatabaseStatement x) {
|
||||
|
||||
@@ -101,6 +101,7 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKeyImpl;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRevokeStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRollbackStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSavePointStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
@@ -550,4 +551,8 @@ public interface SQLASTVisitor {
|
||||
|
||||
boolean visit(SQLTimestampExpr x);
|
||||
|
||||
void endVisit(SQLRevokeStatement x);
|
||||
|
||||
boolean visit(SQLRevokeStatement x);
|
||||
|
||||
}
|
||||
|
||||
@@ -103,6 +103,7 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKeyImpl;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRevokeStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRollbackStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSavePointStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
@@ -1075,4 +1076,14 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLRevokeStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLRevokeStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -70,6 +70,8 @@ import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLGrantStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLObjectType;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRevokeStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLRollbackStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
|
||||
@@ -1301,6 +1303,17 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLGrantStatement x) {
|
||||
if (x.getOn() != null && (x.getObjectType() == null || x.getObjectType() == SQLObjectType.TABLE)) {
|
||||
x.getOn().accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLRevokeStatement x) {
|
||||
if (x.getOn() != null) {
|
||||
x.getOn().accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user