mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -16,5 +16,5 @@
|
||||
package com.alibaba.druid.sql.ast;
|
||||
|
||||
public interface SQLName extends SQLExpr {
|
||||
|
||||
String getSimleName();
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSimleName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
|
||||
@@ -36,6 +36,10 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
|
||||
public SQLPropertyExpr(){
|
||||
|
||||
}
|
||||
|
||||
public String getSimleName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public SQLExpr getOwner() {
|
||||
return this.owner;
|
||||
|
||||
@@ -30,6 +30,10 @@ public class OracleDbLinkExpr extends SQLExprImpl implements SQLName, OracleExpr
|
||||
public OracleDbLinkExpr(){
|
||||
|
||||
}
|
||||
|
||||
public String getSimleName() {
|
||||
return dbLink;
|
||||
}
|
||||
|
||||
public SQLExpr getExpr() {
|
||||
return this.expr;
|
||||
|
||||
+26
-32
@@ -14,31 +14,39 @@ public class SQLServerObjectReferenceExpr extends SQLServerObjectImpl implements
|
||||
private String server;
|
||||
private String database;
|
||||
private String schema;
|
||||
private String object;
|
||||
|
||||
public SQLServerObjectReferenceExpr() {
|
||||
|
||||
|
||||
public SQLServerObjectReferenceExpr(){
|
||||
|
||||
}
|
||||
|
||||
public SQLServerObjectReferenceExpr(SQLExpr owner, String name) {
|
||||
|
||||
public SQLServerObjectReferenceExpr(SQLExpr owner){
|
||||
if (owner instanceof SQLIdentifierExpr) {
|
||||
this.database = ((SQLIdentifierExpr) owner).getName();
|
||||
this.object = name;
|
||||
} else if (owner instanceof SQLPropertyExpr) {
|
||||
SQLPropertyExpr propExpr = (SQLPropertyExpr) owner;
|
||||
|
||||
|
||||
this.server = ((SQLIdentifierExpr) propExpr.getOwner()).getName();
|
||||
this.database = propExpr.getName();
|
||||
this.object = name;
|
||||
} else {
|
||||
throw new IllegalArgumentException(owner.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public String getSimleName() {
|
||||
if (schema != null) {
|
||||
return schema;
|
||||
}
|
||||
|
||||
if (database != null) {
|
||||
return database;
|
||||
}
|
||||
return server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
|
||||
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
@@ -47,31 +55,25 @@ public class SQLServerObjectReferenceExpr extends SQLServerObjectImpl implements
|
||||
boolean flag = false;
|
||||
if (server != null) {
|
||||
buf.append(server);
|
||||
buf.append('.');
|
||||
flag = true;
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
buf.append('.');
|
||||
}
|
||||
if (database != null) {
|
||||
buf.append(database);
|
||||
buf.append('.');
|
||||
flag = true;
|
||||
} else {
|
||||
if (flag) {
|
||||
buf.append('.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
buf.append('.');
|
||||
}
|
||||
|
||||
if (schema != null) {
|
||||
buf.append(schema);
|
||||
buf.append('.');
|
||||
flag = true;
|
||||
} else {
|
||||
if (flag) {
|
||||
buf.append('.');
|
||||
}
|
||||
}
|
||||
|
||||
buf.append(object);
|
||||
}
|
||||
|
||||
public String getServer() {
|
||||
@@ -98,12 +100,4 @@ public class SQLServerObjectReferenceExpr extends SQLServerObjectImpl implements
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
public String getObject() {
|
||||
return object;
|
||||
}
|
||||
|
||||
public void setObject(String object) {
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-3
@@ -2,6 +2,7 @@ package com.alibaba.druid.sql.dialect.sqlserver.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
@@ -27,15 +28,16 @@ public class SQLServerExprParser extends SQLExprParser {
|
||||
return super.primaryRest(expr);
|
||||
}
|
||||
|
||||
public SQLName nameRest(SQLName name) throws ParserException {
|
||||
public SQLName nameRest(SQLName expr) throws ParserException {
|
||||
if (lexer.token() == Token.DOTDOT) {
|
||||
lexer.nextToken();
|
||||
String text = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
|
||||
name = new SQLServerObjectReferenceExpr(name, text);
|
||||
SQLServerObjectReferenceExpr owner = new SQLServerObjectReferenceExpr(expr);
|
||||
expr = new SQLPropertyExpr(owner, text);
|
||||
}
|
||||
|
||||
return super.nameRest(name);
|
||||
return super.nameRest(expr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitorAdapter;
|
||||
import com.alibaba.druid.wall.IllegalSQLObjectViolation;
|
||||
@@ -178,4 +179,12 @@ public class SQLServerWallVisitor extends SQLServerASTVisitorAdapter implements
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLServerObjectReferenceExpr x) {
|
||||
if (config.isPermitSchema(x.getSchema()) || config.isPermitSchema(x.getDatabase())) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -464,8 +464,8 @@ public class WallVisitorUtils {
|
||||
if (expr instanceof SQLPropertyExpr) {
|
||||
SQLPropertyExpr propExpr = (SQLPropertyExpr) expr;
|
||||
|
||||
if (propExpr.getOwner() instanceof SQLIdentifierExpr) {
|
||||
String ownerName = ((SQLIdentifierExpr) propExpr.getOwner()).getName();
|
||||
if (propExpr.getOwner() instanceof SQLName) {
|
||||
String ownerName = ((SQLName) propExpr.getOwner()).getSimleName();
|
||||
|
||||
ownerName = form(ownerName);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user