mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql desensitize support.
This commit is contained in:
@@ -24,6 +24,7 @@ import java.sql.NClob;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
@@ -69,6 +70,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
protected transient int lines = 0;
|
||||
|
||||
protected boolean desensitize = false;
|
||||
|
||||
public SQLASTOutputVisitor(Appendable appender){
|
||||
this.appender = appender;
|
||||
}
|
||||
@@ -118,6 +121,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public boolean isDesensitize() {
|
||||
return desensitize;
|
||||
}
|
||||
|
||||
public void setDesensitize(boolean desensitize) {
|
||||
this.desensitize = desensitize;
|
||||
}
|
||||
|
||||
public Set<String> getTables() {
|
||||
return this.tables;
|
||||
}
|
||||
@@ -1423,6 +1434,26 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
addTable(expr.toString());
|
||||
}
|
||||
|
||||
if (desensitize) {
|
||||
SQLExpr owner = null;
|
||||
String ident = null;
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
ident = ((SQLIdentifierExpr) expr).getName();
|
||||
} else if (expr instanceof SQLPropertyExpr) {
|
||||
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
|
||||
propertyExpr.getOwner().accept(this);
|
||||
print('.');
|
||||
|
||||
ident = propertyExpr.getName();
|
||||
}
|
||||
|
||||
if (ident != null) {
|
||||
String desensitizeTable = SQLUtils.desensitizeTable(ident);
|
||||
print0(desensitizeTable);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (tableMapping != null && expr instanceof SQLName) {
|
||||
String tableName = expr.toString();
|
||||
|
||||
@@ -2446,7 +2477,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
print0(ucase ? "IF NOT EXISTS " : "if not exists ");
|
||||
}
|
||||
|
||||
x.getName().accept(this);
|
||||
x.getTableSource().accept(this);
|
||||
|
||||
if (x.getColumns().size() > 0) {
|
||||
println();
|
||||
|
||||
@@ -426,7 +426,11 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (x.getOperator()) {
|
||||
final SQLBinaryOperator op = x.getOperator();
|
||||
final SQLExpr left = x.getLeft();
|
||||
final SQLExpr right = x.getRight();
|
||||
|
||||
switch (op) {
|
||||
case Equality:
|
||||
case NotEqual:
|
||||
case GreaterThan:
|
||||
@@ -439,15 +443,32 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
case NotLike:
|
||||
case Is:
|
||||
case IsNot:
|
||||
handleCondition(x.getLeft(), x.getOperator().name, x.getRight());
|
||||
handleCondition(x.getRight(), x.getOperator().name, x.getLeft());
|
||||
handleCondition(left, x.getOperator().name, right);
|
||||
handleCondition(right, x.getOperator().name, left);
|
||||
|
||||
handleRelationship(x.getLeft(), x.getOperator().name, x.getRight());
|
||||
handleRelationship(left, x.getOperator().name, right);
|
||||
break;
|
||||
case BooleanOr:{
|
||||
List<SQLExpr> list = SQLBinaryOpExpr.split(x, op);
|
||||
|
||||
for (SQLExpr item : list) {
|
||||
if (item instanceof SQLBinaryOpExpr) {
|
||||
visit((SQLBinaryOpExpr) item);
|
||||
} else {
|
||||
item.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
|
||||
left.accept(this);
|
||||
right.accept(this);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void handleRelationship(SQLExpr left, String operator, SQLExpr right) {
|
||||
|
||||
Reference in New Issue
Block a user