mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve WallFilter
This commit is contained in:
@@ -118,7 +118,9 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
public static boolean visit(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
|
||||
if ("concat".equalsIgnoreCase(x.getMethodName())) {
|
||||
String methodName = x.getMethodName().toLowerCase();
|
||||
|
||||
if ("concat".equals(methodName)) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
for (SQLExpr item : x.getParameters()) {
|
||||
@@ -132,9 +134,9 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
x.getAttributes().put(EVAL_VALUE, buf.toString());
|
||||
} else if ("now".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("now".equals(methodName)) {
|
||||
x.getAttributes().put(EVAL_VALUE, new Date());
|
||||
} else if ("ascii".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("ascii".equals(methodName)) {
|
||||
if (x.getParameters().size() == 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -153,7 +155,7 @@ public class SQLEvalVisitorUtils {
|
||||
|
||||
int ascii = strValue.charAt(0);
|
||||
x.getAttributes().put(EVAL_VALUE, ascii);
|
||||
} else if ("instr".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("instr".equals(methodName)) {
|
||||
if (x.getParameters().size() != 2) {
|
||||
return false;
|
||||
}
|
||||
@@ -175,7 +177,7 @@ public class SQLEvalVisitorUtils {
|
||||
int result = strValue0.indexOf(strValue1) + 1;
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("left".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("left".equals(methodName)) {
|
||||
if (x.getParameters().size() != 2) {
|
||||
return false;
|
||||
}
|
||||
@@ -197,7 +199,7 @@ public class SQLEvalVisitorUtils {
|
||||
String result = strValue.substring(0, intValue);
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("right".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("right".equals(methodName)) {
|
||||
if (x.getParameters().size() != 2) {
|
||||
return false;
|
||||
}
|
||||
@@ -219,7 +221,7 @@ public class SQLEvalVisitorUtils {
|
||||
String result = strValue.substring(strValue.length() - intValue, strValue.length());
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("reverse".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("reverse".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -241,7 +243,7 @@ public class SQLEvalVisitorUtils {
|
||||
String result = buf.toString();
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("trim".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("trim".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -258,7 +260,7 @@ public class SQLEvalVisitorUtils {
|
||||
String result = strValue.trim();
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("length".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("length".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -276,7 +278,7 @@ public class SQLEvalVisitorUtils {
|
||||
int result = strValue.length();
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("ucase".equalsIgnoreCase(x.getMethodName()) || "upper".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("ucase".equals(methodName) || "upper".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -294,7 +296,7 @@ public class SQLEvalVisitorUtils {
|
||||
String result = strValue.toUpperCase();
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("lcase".equalsIgnoreCase(x.getMethodName()) || "lower".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("lcase".equals(methodName) || "lower".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -313,7 +315,7 @@ public class SQLEvalVisitorUtils {
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
|
||||
} else if ("mod".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("mod".equals(methodName)) {
|
||||
if (x.getParameters().size() != 2) {
|
||||
return false;
|
||||
}
|
||||
@@ -335,7 +337,7 @@ public class SQLEvalVisitorUtils {
|
||||
int result = intValue0 % intValue1;
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("abs".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("abs".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -358,7 +360,7 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
} else if ("acos".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("acos".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -379,7 +381,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("asin".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("asin".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -400,7 +402,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("atan".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("atan".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -421,7 +423,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("atan2".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("atan2".equals(methodName)) {
|
||||
if (x.getParameters().size() != 2) {
|
||||
return false;
|
||||
}
|
||||
@@ -446,7 +448,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("ceil".equalsIgnoreCase(x.getMethodName()) || "ceiling".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("ceil".equals(methodName) || "ceiling".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -467,7 +469,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("cos".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("cos".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -488,7 +490,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("sin".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("sin".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -509,7 +511,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("log".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("log".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -530,7 +532,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("log10".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("log10".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -551,7 +553,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("tan".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("tan".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -572,7 +574,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("sqrt".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("sqrt".equals(methodName)) {
|
||||
if (x.getParameters().size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -593,7 +595,7 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("power".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("power".equals(methodName)) {
|
||||
if (x.getParameters().size() != 2) {
|
||||
return false;
|
||||
}
|
||||
@@ -618,10 +620,18 @@ public class SQLEvalVisitorUtils {
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("pi".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("pi".equals(methodName)) {
|
||||
x.putAttribute(EVAL_VALUE, Math.PI);
|
||||
} else if ("rand".equalsIgnoreCase(x.getMethodName())) {
|
||||
} else if ("rand".equals(methodName)) {
|
||||
x.putAttribute(EVAL_VALUE, Math.random());
|
||||
} else if ("chr".equals(methodName) && x.getParameters().size() == 1) {
|
||||
SQLExpr first = x.getParameters().get(0);
|
||||
Object firstResult = getValue(first);
|
||||
if (firstResult instanceof Number) {
|
||||
int intValue = ((Number) firstResult).intValue();
|
||||
char ch = (char) intValue;
|
||||
x.putAttribute(EVAL_VALUE, Character.toString(ch));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -790,11 +800,11 @@ public class SQLEvalVisitorUtils {
|
||||
left.accept(visitor);
|
||||
right.accept(visitor);
|
||||
|
||||
final WallConditionContext wallConditionContext = WallVisitorUtils.getWallConditionContext();
|
||||
if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
|
||||
final WallConditionContext wallContext = WallVisitorUtils.getWallConditionContext();
|
||||
if (wallContext != null) {
|
||||
if (wallConditionContext != null) {
|
||||
if (left.getAttribute(EVAL_VALUE) == Boolean.TRUE || right.getAttribute(EVAL_VALUE) == Boolean.TRUE) {
|
||||
wallContext.setPartAllowTrue(true);
|
||||
wallConditionContext.setPartAlwayTrue(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -807,52 +817,68 @@ public class SQLEvalVisitorUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wallConditionContext != null) {
|
||||
switch (x.getOperator()) {
|
||||
case Add:
|
||||
case Subtract:
|
||||
case Multiply:
|
||||
case Divide:
|
||||
case Modulus:
|
||||
wallConditionContext.setConstArithmetic(true);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Object value = null;
|
||||
Object leftValue = left.getAttribute(EVAL_VALUE);
|
||||
Object rightValue = right.getAttributes().get(EVAL_VALUE);
|
||||
switch (x.getOperator()) {
|
||||
case Add:
|
||||
value = add(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = add(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case Subtract:
|
||||
value = sub(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = sub(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case Multiply:
|
||||
value = multi(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = multi(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case Divide:
|
||||
value = div(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = div(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case GreaterThan:
|
||||
value = gt(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = gt(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case GreaterThanOrEqual:
|
||||
value = gteq(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = gteq(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case LessThan:
|
||||
value = lt(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = lt(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case LessThanOrEqual:
|
||||
value = lteq(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = lteq(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case Is:
|
||||
case Equality:
|
||||
value = eq(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = eq(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case IsNot:
|
||||
value = !eq(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE));
|
||||
value = !eq(leftValue, rightValue);
|
||||
x.putAttribute(EVAL_VALUE, value);
|
||||
break;
|
||||
case RegExp:
|
||||
case RLike: {
|
||||
String pattern = castToString(right.getAttributes().get(EVAL_VALUE));
|
||||
String pattern = castToString(rightValue);
|
||||
String input = castToString(left.getAttributes().get(EVAL_VALUE));
|
||||
boolean matchResult = Pattern.matches(pattern, input);
|
||||
x.putAttribute(EVAL_VALUE, matchResult);
|
||||
@@ -860,26 +886,31 @@ public class SQLEvalVisitorUtils {
|
||||
break;
|
||||
case NotRegExp:
|
||||
case NotRLike: {
|
||||
String pattern = castToString(right.getAttributes().get(EVAL_VALUE));
|
||||
String pattern = castToString(rightValue);
|
||||
String input = castToString(left.getAttributes().get(EVAL_VALUE));
|
||||
boolean matchResult = !Pattern.matches(pattern, input);
|
||||
x.putAttribute(EVAL_VALUE, matchResult);
|
||||
}
|
||||
break;
|
||||
case Like: {
|
||||
String pattern = castToString(right.getAttributes().get(EVAL_VALUE));
|
||||
String pattern = castToString(rightValue);
|
||||
String input = castToString(left.getAttributes().get(EVAL_VALUE));
|
||||
boolean matchResult = like(input, pattern);
|
||||
x.putAttribute(EVAL_VALUE, matchResult);
|
||||
}
|
||||
break;
|
||||
case NotLike: {
|
||||
String pattern = castToString(right.getAttributes().get(EVAL_VALUE));
|
||||
String pattern = castToString(rightValue);
|
||||
String input = castToString(left.getAttributes().get(EVAL_VALUE));
|
||||
boolean matchResult = !like(input, pattern);
|
||||
x.putAttribute(EVAL_VALUE, matchResult);
|
||||
}
|
||||
break;
|
||||
case Concat: {
|
||||
String result = leftValue.toString() + rightValue.toString();
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,6 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
|
||||
Reference in New Issue
Block a user