diff --git a/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java b/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java index 8a0d28374e..e80709e84b 100644 --- a/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java +++ b/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java @@ -531,7 +531,7 @@ public class WallVisitorUtils { Object result = getValue(visitor, groupList.get(i)); if (Boolean.TRUE == result) { final WallConditionContext wallContext = WallVisitorUtils.getWallConditionContext(); - if (wallContext != null) { + if (wallContext != null && i != 0) { wallContext.setPartAlwayTrue(true); } return true; @@ -627,14 +627,14 @@ public class WallVisitorUtils { if (x == null) { return false; } - + for (;;) { SQLObject parent = x.getParent(); if (parent == null) { return false; } - + if (parent instanceof SQLSelectQueryBlock) { SQLSelectQueryBlock query = (SQLSelectQueryBlock) parent; if (query.getWhere() == x) { @@ -643,7 +643,7 @@ public class WallVisitorUtils { return false; } } - + if (parent instanceof SQLSelectGroupByClause) { SQLSelectGroupByClause groupBy = (SQLSelectGroupByClause) parent; if (x == groupBy.getHaving()) { @@ -652,7 +652,7 @@ public class WallVisitorUtils { return false; } } - + x = parent; } } diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_having.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_having.java new file mode 100644 index 0000000000..7222220da7 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_having.java @@ -0,0 +1,47 @@ +/* + * 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 MySqlWallTest_having extends TestCase { + + public void test_having() throws Exception { + Assert.assertTrue(WallUtils.isValidateMySql(// + "select id, count(*) from t group by id having 1 = 1")); + } + + public void test_having_true_first() throws Exception { + Assert.assertTrue(WallUtils.isValidateMySql(// + "select id, count(*) from t group by id having 1 = 1 AND count(*) > 2")); + } + + public void test_having_false() throws Exception { + Assert.assertFalse(WallUtils.isValidateMySql(// + "select id, count(*) from t group by id having count(*) > 2 OR 1 = 1")); + } +}