diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java index b1571f31fa..d479e2a50d 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java @@ -24,6 +24,7 @@ import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr; import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; import com.alibaba.druid.sql.ast.expr.SQLCaseExpr; import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLInListExpr; import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; @@ -147,4 +148,8 @@ public class MySqlEvalVisitorImpl extends MySqlASTVisitorAdapter implements SQLE public void registerFunction(String funcName, Function function) { functions.put(funcName, function); } + + public boolean visit(SQLIdentifierExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/oracle/visitor/OracleEvalVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/oracle/visitor/OracleEvalVisitor.java index 72fa05fca5..d1e03a5208 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/oracle/visitor/OracleEvalVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/oracle/visitor/OracleEvalVisitor.java @@ -23,6 +23,7 @@ import java.util.Map; import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; import com.alibaba.druid.sql.ast.expr.SQLCaseExpr; import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLInListExpr; import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; @@ -134,4 +135,8 @@ public class OracleEvalVisitor extends OracleASTVisitorAdapter implements SQLEva public void registerFunction(String funcName, Function function) { functions.put(funcName, function); } + + public boolean visit(SQLIdentifierExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGEvalVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGEvalVisitor.java index dbd13b365a..1efd330256 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGEvalVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGEvalVisitor.java @@ -23,6 +23,7 @@ import java.util.Map; import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; import com.alibaba.druid.sql.ast.expr.SQLCaseExpr; import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLInListExpr; import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; @@ -134,4 +135,8 @@ public class PGEvalVisitor extends PGASTVisitorAdapter implements SQLEvalVisitor public void registerFunction(String funcName, Function function) { functions.put(funcName, function); } + + public boolean visit(SQLIdentifierExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/visitor/SQLServerEvalVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/visitor/SQLServerEvalVisitor.java index 986f0dc6ae..d24e664b2c 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/visitor/SQLServerEvalVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/visitor/SQLServerEvalVisitor.java @@ -23,6 +23,7 @@ import java.util.Map; import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; import com.alibaba.druid.sql.ast.expr.SQLCaseExpr; import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLInListExpr; import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; @@ -134,4 +135,8 @@ public class SQLServerEvalVisitor extends SQLServerASTVisitorAdapter implements public void registerFunction(String funcName, Function function) { functions.put(funcName, function); } + + public boolean visit(SQLIdentifierExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } } diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitor.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitor.java index 81a4404184..5c3d44d5b7 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitor.java @@ -22,6 +22,7 @@ import com.alibaba.druid.sql.visitor.functions.Function; public interface SQLEvalVisitor extends SQLASTVisitor { public static final String EVAL_VALUE = "eval.value"; + public static final String EVAL_EXPR = "eval.expr"; public static final Object EVAL_ERROR = new Object(); Function getFunction(String funcName); diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorImpl.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorImpl.java index 013afb6af7..2ec6156c09 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorImpl.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorImpl.java @@ -23,6 +23,7 @@ import java.util.Map; import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; import com.alibaba.druid.sql.ast.expr.SQLCaseExpr; import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLInListExpr; import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; @@ -128,4 +129,8 @@ public class SQLEvalVisitorImpl extends SQLASTVisitorAdapter implements SQLEvalV public void registerFunction(String funcName, Function function) { functions.put(funcName, function); } + + public boolean visit(SQLIdentifierExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } } diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java index 3365af2f9b..9596ff4159 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java @@ -15,6 +15,7 @@ */ package com.alibaba.druid.sql.visitor; +import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_EXPR; import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VALUE; import java.math.BigDecimal; @@ -57,6 +58,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerEvalVisitor; import com.alibaba.druid.sql.visitor.functions.Ascii; import com.alibaba.druid.sql.visitor.functions.Concat; import com.alibaba.druid.sql.visitor.functions.Function; +import com.alibaba.druid.sql.visitor.functions.Hex; import com.alibaba.druid.sql.visitor.functions.Instr; import com.alibaba.druid.sql.visitor.functions.Lcase; import com.alibaba.druid.sql.visitor.functions.Left; @@ -66,6 +68,7 @@ import com.alibaba.druid.sql.visitor.functions.Reverse; import com.alibaba.druid.sql.visitor.functions.Right; import com.alibaba.druid.sql.visitor.functions.Trim; import com.alibaba.druid.sql.visitor.functions.Ucase; +import com.alibaba.druid.sql.visitor.functions.Unhex; import com.alibaba.druid.util.JdbcUtils; import com.alibaba.druid.wall.spi.WallVisitorUtils; import com.alibaba.druid.wall.spi.WallVisitorUtils.WallConditionContext; @@ -158,6 +161,8 @@ public class SQLEvalVisitorUtils { functions.put("upper", Ucase.instance); functions.put("ucase", Lcase.instance); functions.put("lower", Lcase.instance); + functions.put("hex", Hex.instance); + functions.put("unhex", Unhex.instance); } public static boolean visit(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { @@ -709,11 +714,40 @@ public class SQLEvalVisitorUtils { } } - if (!left.getAttributes().containsKey(EVAL_VALUE)) { + boolean leftHasValue = left.getAttributes().containsKey(EVAL_VALUE); + boolean rightHasValue = right.getAttributes().containsKey(EVAL_VALUE); + + if ((!leftHasValue) && !rightHasValue) { + SQLExpr leftEvalExpr = (SQLExpr) left.getAttribute(EVAL_EXPR); + SQLExpr rightEvalExpr = (SQLExpr) right.getAttribute(EVAL_EXPR); + + if (leftEvalExpr.equals(rightEvalExpr)) { + switch (x.getOperator()) { + case Like: + case Equality: + case GreaterThanOrEqual: + case LessThanOrEqual: + case NotLessThan: + case NotGreaterThan: + x.putAttribute(EVAL_VALUE, Boolean.TRUE); + return false; + case NotEqual: + case NotLike: + case GreaterThan: + case LessThan: + x.putAttribute(EVAL_VALUE, Boolean.FALSE); + return false; + default: + break; + } + } + } + + if (!leftHasValue) { return false; } - if (!right.getAttributes().containsKey(EVAL_VALUE)) { + if (!rightHasValue) { return false; } @@ -1473,4 +1507,8 @@ public class SQLEvalVisitorUtils { return Pattern.matches(regexpr, input); } + public static boolean visit(SQLEvalVisitor visitor, SQLIdentifierExpr x) { + x.putAttribute(EVAL_EXPR, x); + return false; + } } diff --git a/src/main/java/com/alibaba/druid/sql/visitor/functions/Hex.java b/src/main/java/com/alibaba/druid/sql/visitor/functions/Hex.java index b749b6d4ae..8f679c6d0f 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/functions/Hex.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/functions/Hex.java @@ -15,19 +15,44 @@ */ package com.alibaba.druid.sql.visitor.functions; +import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VALUE; + +import org.apache.xerces.impl.dv.util.HexBin; + +import com.alibaba.druid.sql.ast.SQLExpr; import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; import com.alibaba.druid.sql.parser.ParserException; import com.alibaba.druid.sql.visitor.SQLEvalVisitor; - public class Hex implements Function { + public final static Hex instance = new Hex(); - + public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { - if(x.getParameters().size() != 1) { + if (x.getParameters().size() != 1) { throw new ParserException("argument's != 1, " + x.getParameters().size()); } - - return null; + + SQLExpr param0 = x.getParameters().get(0); + param0.accept(visitor); + + Object param0Value = param0.getAttributes().get(EVAL_VALUE); + if (param0Value == null) { + return SQLEvalVisitor.EVAL_ERROR; + } + + if (param0Value instanceof String) { + byte[] bytes = ((String) param0Value).getBytes(); + String result = HexBin.encode(bytes); + return result; + } + + if (param0Value instanceof Number) { + long value = ((Number) param0Value).longValue(); + String result = Long.toHexString(value).toUpperCase(); + return result; + } + + return SQLEvalVisitor.EVAL_ERROR; } } diff --git a/src/main/java/com/alibaba/druid/sql/visitor/functions/Unhex.java b/src/main/java/com/alibaba/druid/sql/visitor/functions/Unhex.java new file mode 100644 index 0000000000..de1771401c --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/visitor/functions/Unhex.java @@ -0,0 +1,77 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.sql.visitor.functions; + +import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VALUE; +import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_EXPR; + +import java.io.UnsupportedEncodingException; + +import org.apache.xerces.impl.dv.util.HexBin; + +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; +import com.alibaba.druid.sql.parser.ParserException; +import com.alibaba.druid.sql.visitor.SQLEvalVisitor; + +public class Unhex implements Function { + + public final static Unhex instance = new Unhex(); + + public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { + if (x.getParameters().size() != 1) { + throw new ParserException("argument's != 1, " + x.getParameters().size()); + } + + SQLExpr param0 = x.getParameters().get(0); + + if (param0 instanceof SQLMethodInvokeExpr) { + SQLMethodInvokeExpr paramMethodExpr = (SQLMethodInvokeExpr) param0; + if (paramMethodExpr.getMethodName().equalsIgnoreCase("hex")) { + SQLExpr subParamExpr = paramMethodExpr.getParameters().get(0); + subParamExpr.accept(visitor); + + Object param0Value = subParamExpr.getAttributes().get(EVAL_VALUE); + if (param0Value == null) { + x.putAttribute(EVAL_EXPR, subParamExpr); + return SQLEvalVisitor.EVAL_ERROR; + } + + return param0Value; + } + } + + param0.accept(visitor); + + Object param0Value = param0.getAttributes().get(EVAL_VALUE); + if (param0Value == null) { + return SQLEvalVisitor.EVAL_ERROR; + } + + if (param0Value instanceof String) { + byte[] bytes = HexBin.decode((String) param0Value); + String result; + try { + result = new String(bytes, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException(e.getMessage(), e); + } + return result; + } + + return SQLEvalVisitor.EVAL_ERROR; + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest27.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest27.java new file mode 100644 index 0000000000..066312ee9d --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest27.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.filter.wall; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.wall.WallUtils; + +/** + * SQLServerWallTest + * + * @author RaymondXiu + * @version 1.0, 2012-3-18 + * @see + */ +public class MySqlWallTest27 extends TestCase { + + public void test_false() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where hex('abc') = '616263'")); // + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest28.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest28.java new file mode 100644 index 0000000000..28f851b2a1 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest28.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.filter.wall; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.wall.WallUtils; + +/** + * SQLServerWallTest + * + * @author RaymondXiu + * @version 1.0, 2012-3-18 + * @see + */ +public class MySqlWallTest28 extends TestCase { + + public void test_false() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where hex(123) = '7B'")); // + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest29.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest29.java new file mode 100644 index 0000000000..0fd63ebcb4 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest29.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.filter.wall; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.wall.WallUtils; + +/** + * SQLServerWallTest + * + * @author RaymondXiu + * @version 1.0, 2012-3-18 + * @see + */ +public class MySqlWallTest29 extends TestCase { + + public void test_false() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where UNHEX('4D7953514C') = 'MySQL'")); // + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest30.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest30.java new file mode 100644 index 0000000000..fdbd482f66 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest30.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.filter.wall; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.wall.WallUtils; + +/** + * SQLServerWallTest + * + * @author RaymondXiu + * @version 1.0, 2012-3-18 + * @see + */ +public class MySqlWallTest30 extends TestCase { + + public void test_false() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where UNHEX(HEX('MySQL')) = 'MySQL'")); // + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest31.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest31.java new file mode 100644 index 0000000000..744fbfd7b2 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest31.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.filter.wall; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.wall.WallUtils; + +/** + * SQLServerWallTest + * + * @author RaymondXiu + * @version 1.0, 2012-3-18 + * @see + */ +public class MySqlWallTest31 extends TestCase { + + public void test_false() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where UNHEX(HEX(FNAME)) = FNAME")); // + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest32.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest32.java new file mode 100644 index 0000000000..6fe5b9e82d --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest32.java @@ -0,0 +1,52 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.filter.wall; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.wall.WallUtils; + +/** + * SQLServerWallTest + * + * @author RaymondXiu + * @version 1.0, 2012-3-18 + * @see + */ +public class MySqlWallTest32 extends TestCase { + + public void test_false() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where NOT(UNHEX(HEX(FNAME)) != FNAME)")); // + } + + public void test_false_1() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where NOT(UNHEX(HEX(FNAME)) > FNAME)")); // + } + + public void test_false_2() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where NOT(UNHEX(HEX(FNAME)) < FNAME)")); // + } + + public void test_false_3() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select * from t where NOT(UNHEX(HEX(FNAME)) NOT LIKE FNAME)")); // + } +} diff --git a/src/test/java/com/alibaba/druid/mysql/MySqlHexTest.java b/src/test/java/com/alibaba/druid/mysql/MySqlHexTest.java new file mode 100644 index 0000000000..ace0aa96b6 --- /dev/null +++ b/src/test/java/com/alibaba/druid/mysql/MySqlHexTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.mysql; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.util.JdbcUtils; + +public class MySqlHexTest extends TestCase { + final int COUNT = 800; + + private String jdbcUrl; + private String user; + private String password; + private String driverClass; + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + jdbcUrl = "jdbc:mysql://10.20.149.16:3306/circe0_test"; + user = "circe"; + password = "circe"; + driverClass = "com.mysql.jdbc.Driver"; + + dataSource = new DruidDataSource(); + dataSource.setDriverClassName(driverClass); + dataSource.setUrl(jdbcUrl); + dataSource.setPoolPreparedStatements(true); + dataSource.setUsername(user); + dataSource.setPassword(password); + + } + + + public void test_0() throws Exception { + + Connection conn = dataSource.getConnection(); + + String sql = "select hex('123'),hex(123)"; + + PreparedStatement stmt = conn.prepareStatement(sql); + + ResultSet rs = stmt.executeQuery(); + JdbcUtils.printResultSet(rs); + rs.close(); + stmt.close(); + + conn.close(); + } + +}