From 192a683339ceeb44c2e87b017760badd20b93d7e Mon Sep 17 00:00:00 2001 From: Yako Date: Tue, 26 Aug 2014 14:23:01 +0800 Subject: [PATCH] improve sqlwall --- .../druid/wall/spi/WallVisitorUtils.java | 13 ++++++- .../bvt/filter/wall/MySqlWallTest_union.java | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_union.java 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 d8c5cbd1a6..40296c3ffb 100644 --- a/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java +++ b/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java @@ -2153,7 +2153,18 @@ public class WallVisitorUtils { SQLTableSource from = queryBlock.getFrom(); if (from == null) { - return true; + boolean itemIsConst = true; + for (SQLSelectItem item : queryBlock.getSelectList()) { + if (item.getExpr() instanceof SQLIdentifierExpr || item.getExpr() instanceof SQLPropertyExpr) { + itemIsConst = false; + break; + } + } + if (itemIsConst) { + return true; + } else { + return false; + } } if (from instanceof SQLExprTableSource) { diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_union.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_union.java new file mode 100644 index 0000000000..1ac15282fd --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_union.java @@ -0,0 +1,38 @@ +/* + * 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.WallConfig; +import com.alibaba.druid.wall.WallUtils; + +public class MySqlWallTest_union extends TestCase { + + public void testUnion() throws Exception { + WallConfig config = new WallConfig(); + config.setSelectUnionCheck(true); + + Assert.assertFalse(WallUtils.isValidateMySql("select f1, f2 from t union select 1, 2", config)); + + Assert.assertFalse(WallUtils.isValidateMySql("select f1, f2 from t union select null, '1', 2", config)); + + Assert.assertTrue(WallUtils.isValidateMySql("select f1, f2 from t union select c1, c2", config)); + + } +}