improve wall filter

This commit is contained in:
wenshao
2013-04-11 19:30:32 +08:00
parent eddb42ae53
commit 76a963ac46
7 changed files with 89 additions and 14 deletions
@@ -34,8 +34,8 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements Serializable {
public SQLBinaryOpExpr(SQLExpr left, SQLBinaryOperator operator, SQLExpr right){
this.left = left;
this.right = right;
setLeft(left);
setRight(right);
this.operator = operator;
}
@@ -51,6 +51,9 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements Serializable {
}
public void setLeft(SQLExpr left) {
if (left != null) {
left.setParent(this);
}
this.left = left;
}
@@ -59,6 +62,9 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements Serializable {
}
public void setRight(SQLExpr right) {
if (right != null) {
right.setParent(this);
}
this.right = right;
}
@@ -61,6 +61,51 @@ public class MySqlLexer extends Lexer {
super(input);
super.keywods = DEFAULT_MYSQL_KEYWORDS;
}
public void scanSharp() {
if (ch != '#') {
throw new ParserException("illegal stat");
}
Token lastToken = this.token;
scanChar();
mark = pos;
bufPos = 0;
for (;;) {
if (ch == '\r') {
if (charAt(pos + 1) == '\n') {
bufPos += 2;
scanChar();
break;
}
bufPos++;
break;
} else if (ch == EOI) {
break;
}
if (ch == '\n') {
scanChar();
bufPos++;
break;
}
scanChar();
bufPos++;
}
stringVal = subString(mark, bufPos);
token = Token.LINE_COMMENT;
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
return;
}
if (!isAllowComment()) {
throw new NotAllowCommentException();
}
}
public void scanVariable() {
if (ch != '@' && ch != ':' && ch != '#' && ch != '$') {
@@ -360,7 +360,11 @@ public class Lexer {
}
return;
case '#':
scanVariable();
scanSharp();
if ((token() == Token.LINE_COMMENT || token() == Token.MULTI_LINE_COMMENT) && skipComment) {
bufPos = 0;
continue;
}
return;
case '.':
scanChar();
@@ -648,6 +652,10 @@ public class Lexer {
stringVal = subString(mark + 1, bufPos);
}
public void scanSharp() {
scanVariable();
}
public void scanVariable() {
if (ch != '@' && ch != ':' && ch != '#' && ch != '$') {
@@ -25,6 +25,7 @@ public interface SQLEvalVisitor extends SQLASTVisitor {
public static final String EVAL_EXPR = "eval.expr";
public static final Object EVAL_ERROR = new Object();
public static final Object EVAL_VALUE_COUNT = new Object();
public static final Object EVAL_VALUE_NULL = new Object();
Function getFunction(String funcName);
@@ -62,10 +62,14 @@ import com.alibaba.druid.sql.visitor.functions.Char;
import com.alibaba.druid.sql.visitor.functions.Concat;
import com.alibaba.druid.sql.visitor.functions.Elt;
import com.alibaba.druid.sql.visitor.functions.Function;
import com.alibaba.druid.sql.visitor.functions.Greatest;
import com.alibaba.druid.sql.visitor.functions.Hex;
import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.druid.sql.visitor.functions.Insert;
import com.alibaba.druid.sql.visitor.functions.Instr;
import com.alibaba.druid.sql.visitor.functions.Isnull;
import com.alibaba.druid.sql.visitor.functions.Lcase;
import com.alibaba.druid.sql.visitor.functions.Least;
import com.alibaba.druid.sql.visitor.functions.Left;
import com.alibaba.druid.sql.visitor.functions.Length;
import com.alibaba.druid.sql.visitor.functions.Locate;
@@ -185,6 +189,10 @@ public class SQLEvalVisitorUtils {
functions.put("lower", Lcase.instance);
functions.put("hex", Hex.instance);
functions.put("unhex", Unhex.instance);
functions.put("greatest", Greatest.instance);
functions.put("least", Least.instance);
functions.put("isnull", Isnull.instance);
functions.put("if", If.instance);
}
public static boolean visit(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
@@ -518,6 +526,8 @@ public class SQLEvalVisitorUtils {
char ch = (char) intValue;
x.putAttribute(EVAL_VALUE, Character.toString(ch));
}
} else if ("CURRENT_USER".equals(methodName)) {
x.putAttribute(EVAL_VALUE, "CURRENT_USER");
}
return false;
}
@@ -954,7 +964,7 @@ public class SQLEvalVisitorUtils {
if (val instanceof Short) {
return (Short) val;
}
if (val instanceof String) {
return Short.parseShort((String) val);
}
@@ -975,7 +985,7 @@ public class SQLEvalVisitorUtils {
if (val instanceof String) {
return Integer.parseInt((String) val);
}
if (val instanceof List) {
List list = (List) val;
if (list.size() == 1) {
@@ -983,6 +993,14 @@ public class SQLEvalVisitorUtils {
}
}
if (val instanceof Boolean) {
if (((Boolean) val).booleanValue()) {
return 1;
} else {
return 0;
}
}
return ((Number) val).intValue();
}
@@ -995,12 +1013,11 @@ public class SQLEvalVisitorUtils {
if (val instanceof Long) {
return (Long) val;
}
if (val instanceof String) {
return Long.parseLong((String) val);
}
if (val instanceof List) {
List list = (List) val;
if (list.size() == 1) {
@@ -1424,7 +1441,7 @@ public class SQLEvalVisitorUtils {
if (b == null) {
return a;
}
if (a instanceof Date || b instanceof Date) {
return SQLEvalVisitor.EVAL_ERROR;
}
@@ -1460,8 +1477,8 @@ public class SQLEvalVisitorUtils {
if (a instanceof Byte || b instanceof Byte) {
return castToByte(a) - castToByte(b);
}
if(a instanceof String && b instanceof String) {
if (a instanceof String && b instanceof String) {
return castToLong(a) - castToLong(b);
}
@@ -480,9 +480,6 @@ public class WallVisitorUtils {
}
public static Object getValue(SQLBinaryOpExpr x) {
x.getLeft().setParent(x);
x.getRight().setParent(x);
if (x.getLeft() instanceof SQLName && x.getRight() instanceof SQLName) {
if (x.getLeft().toString().equalsIgnoreCase(x.getRight().toString())) {
if (x.getOperator() == SQLBinaryOperator.Equality) {