diff --git a/pom.xml b/pom.xml index 8bb15ad0e6..7cccdf2648 100644 --- a/pom.xml +++ b/pom.xml @@ -527,5 +527,11 @@ 2.0.2 test + + com.alibaba + fastjson + 1.1.29 + test + diff --git a/src/main/java/com/alibaba/druid/util/JdbcSqlStatUtils.java b/src/main/java/com/alibaba/druid/util/JdbcSqlStatUtils.java index 2a951b5c2c..545ad66345 100644 --- a/src/main/java/com/alibaba/druid/util/JdbcSqlStatUtils.java +++ b/src/main/java/com/alibaba/druid/util/JdbcSqlStatUtils.java @@ -18,7 +18,9 @@ package com.alibaba.druid.util; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLongFieldUpdater; import com.alibaba.druid.stat.JdbcSqlStat; @@ -75,4 +77,20 @@ public class JdbcSqlStatUtils { return updater.get(stat); } } + + public static long get(AtomicLong counter, boolean reset) { + if (reset) { + return counter.getAndSet(0); + } else { + return counter.get(); + } + } + + public static int get(AtomicInteger counter, boolean reset) { + if (reset) { + return counter.getAndSet(0); + } else { + return counter.get(); + } + } } diff --git a/src/main/java/com/alibaba/druid/wall/WallFunctionStat.java b/src/main/java/com/alibaba/druid/wall/WallFunctionStat.java index 4b589b3f00..e8a3842425 100644 --- a/src/main/java/com/alibaba/druid/wall/WallFunctionStat.java +++ b/src/main/java/com/alibaba/druid/wall/WallFunctionStat.java @@ -15,6 +15,8 @@ */ package com.alibaba.druid.wall; +import static com.alibaba.druid.util.JdbcSqlStatUtils.get; + import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicLongFieldUpdater; @@ -47,4 +49,9 @@ public class WallFunctionStat { return map; } + public WallFunctionStatValue getStatValue(boolean reset) { + WallFunctionStatValue statValue = new WallFunctionStatValue(); + statValue.setInvokeCount(get(this, invokeCountUpdater, reset)); + return statValue; + } } diff --git a/src/main/java/com/alibaba/druid/wall/WallFunctionStatValue.java b/src/main/java/com/alibaba/druid/wall/WallFunctionStatValue.java new file mode 100644 index 0000000000..db412373e2 --- /dev/null +++ b/src/main/java/com/alibaba/druid/wall/WallFunctionStatValue.java @@ -0,0 +1,56 @@ +/* + * 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.wall; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class WallFunctionStatValue { + + private String name; + private long invokeCount; + + public WallFunctionStatValue(){ + + } + + public WallFunctionStatValue(String name){ + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getInvokeCount() { + return invokeCount; + } + + public void setInvokeCount(long invokeCount) { + this.invokeCount = invokeCount; + } + + public Map toMap() { + Map map = new LinkedHashMap(2); + map.put("name", name); + map.put("invokeCount", invokeCount); + return map; + } +} diff --git a/src/main/java/com/alibaba/druid/wall/WallProvider.java b/src/main/java/com/alibaba/druid/wall/WallProvider.java index 79ff7e4f04..a2d4c83530 100644 --- a/src/main/java/com/alibaba/druid/wall/WallProvider.java +++ b/src/main/java/com/alibaba/druid/wall/WallProvider.java @@ -15,17 +15,19 @@ */ package com.alibaba.druid.wall; +import static com.alibaba.druid.util.JdbcSqlStatUtils.get; + import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; import com.alibaba.druid.sql.SQLUtils; @@ -45,6 +47,11 @@ import com.alibaba.druid.wall.violation.SyntaxErrorViolation; public abstract class WallProvider { + private final Map attributes = new ConcurrentHashMap( + 1, + 0.75f, + 1); + private LRUCache whiteList; private LRUCache whiteMergedList; @@ -92,6 +99,10 @@ public abstract class WallProvider { this.dbType = dbType; } + public Map getAttributes() { + return attributes; + } + public void reset() { this.checkCount.set(0); this.hardCheckCount.set(0); @@ -238,6 +249,7 @@ public abstract class WallProvider { if (wallStat == null) { wallStat = new WallSqlStat(tableStats, functionStats, syntaxError); whiteMergedList.put(mergedSql, wallStat); + wallStat.setSample(sql); } wallStat.incrementAndGetExecuteCount(); @@ -275,6 +287,7 @@ public abstract class WallProvider { if (wallStat == null) { wallStat = new WallSqlStat(tableStats, functionStats, violations, syntaxError); blackMergedList.put(mergedSql, wallStat); + wallStat.setSample(sql); } wallStat.incrementAndGetExecuteCount(); @@ -332,34 +345,6 @@ public abstract class WallProvider { return Collections. unmodifiableSet(hashSet); } - public List> getBlackListStat() { - List> map = new ArrayList>(); - lock.readLock().lock(); - try { - if (blackList != null) { - for (Map.Entry entry : this.blackList.entrySet()) { - WallSqlStat sqlStat = entry.getValue(); - - Map sqlStatMap = new LinkedHashMap(); - sqlStatMap.put("sql", entry.getKey()); - sqlStatMap.put("executeCount", sqlStat.getExecuteCount()); - sqlStatMap.put("effectRowCount", sqlStat.getEffectRowCount()); - - List violations = sqlStat.getViolations(); - if (violations.size() > 0) { - sqlStatMap.put("violationMessage", violations.get(0).getMessage()); - } - - map.add(sqlStatMap); - } - } - } finally { - lock.readLock().unlock(); - } - - return map; - } - public void clearCache() { lock.writeLock().lock(); try { @@ -818,39 +803,67 @@ public abstract class WallProvider { } } + public WallProviderStatValue getStatValue(boolean reset) { + WallProviderStatValue statValue = new WallProviderStatValue(); + + statValue.setCheckCount(get(checkCount, reset)); + statValue.setHardCheckCount(get(hardCheckCount, reset)); + statValue.setViolationCount(get(violationCount, reset)); + statValue.setViolationEffectRowCount(get(violationEffectRowCount, reset)); + statValue.setBlackListHitCount(get(blackListHitCount, reset)); + statValue.setWhiteListHitCount(get(whiteListHitCount, reset)); + statValue.setSyntaxErrorCount(get(syntaxErrrorCount, reset)); + + for (Map.Entry entry : this.tableStats.entrySet()) { + String tableName = entry.getKey(); + WallTableStat tableStat = entry.getValue(); + + WallTableStatValue tableStatValue = tableStat.getStatValue(reset); + tableStatValue.setName(tableName); + + statValue.getTables().add(tableStatValue); + } + + for (Map.Entry entry : this.functionStats.entrySet()) { + String functionName = entry.getKey(); + WallFunctionStat functionStat = entry.getValue(); + + WallFunctionStatValue functionStatValue = functionStat.getStatValue(reset); + functionStatValue.setName(functionName); + + statValue.getFunctions().add(functionStatValue); + } + + final Lock lock = reset ? this.lock.writeLock() : this.lock.readLock(); + lock.lock(); + try { + if (this.whiteMergedList != null) { + for (Map.Entry entry : whiteMergedList.entrySet()) { + String sql = entry.getKey(); + WallSqlStat sqlStat = entry.getValue(); + WallSqlStatValue sqlStatValue = sqlStat.getStatValue(reset); + sqlStatValue.setSql(sql); + statValue.getWhiteList().add(sqlStatValue); + } + } + + if (this.blackMergedList != null) { + for (Map.Entry entry : blackMergedList.entrySet()) { + String sql = entry.getKey(); + WallSqlStat sqlStat = entry.getValue(); + WallSqlStatValue sqlStatValue = sqlStat.getStatValue(reset); + sqlStatValue.setSql(sql); + statValue.getBlackList().add(sqlStatValue); + } + } + } finally { + lock.unlock(); + } + + return statValue; + } + public Map getStatsMap() { - Map info = new LinkedHashMap(); - - info.put("checkCount", this.getCheckCount()); - info.put("hardCheckCount", this.getHardCheckCount()); - info.put("violationCount", this.getViolationCount()); - info.put("violationEffectRowCount", this.getViolationEffectRowCount()); - info.put("blackListHitCount", this.getBlackListHitCount()); - info.put("blackListSize", this.getBlackList().size()); - info.put("whiteListHitCount", this.getWhiteListHitCount()); - info.put("whiteListSize", this.getWhiteList().size()); - info.put("syntaxErrrorCount", this.getSyntaxErrorCount()); - - { - List> tables = new ArrayList>(); - for (Map.Entry entry : this.tableStats.entrySet()) { - Map statMap = entry.getValue().toMap(); - statMap.put("name", entry.getKey()); - tables.add(statMap); - } - info.put("tables", tables); - } - { - List> functions = new ArrayList>(); - for (Map.Entry entry : this.functionStats.entrySet()) { - Map statMap = entry.getValue().toMap(); - statMap.put("name", entry.getKey()); - functions.add(statMap); - } - info.put("functions", functions); - } - // info.put("whiteList", this.getWhiteList()); - info.put("blackList", this.getBlackListStat()); - return info; + return getStatValue(false).toMap(); } } diff --git a/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java b/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java new file mode 100644 index 0000000000..0d92f7e5d2 --- /dev/null +++ b/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java @@ -0,0 +1,160 @@ +/* + * 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.wall; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class WallProviderStatValue { + + private long checkCount; + private long hardCheckCount; + private long violationCount; + private long whiteListHitCount; + private long blackListHitCount; + private long syntaxErrroCount; + private long violationEffectRowCount; + + private final List tables = new ArrayList(); + private final List functions = new ArrayList(); + + private final List whiteList = new ArrayList(); + private final List blackList = new ArrayList(); + + public long getCheckCount() { + return checkCount; + } + + public void setCheckCount(long checkCount) { + this.checkCount = checkCount; + } + + public long getHardCheckCount() { + return hardCheckCount; + } + + public void setHardCheckCount(long hardCheckCount) { + this.hardCheckCount = hardCheckCount; + } + + public long getViolationCount() { + return violationCount; + } + + public void setViolationCount(long violationCount) { + this.violationCount = violationCount; + } + + public long getWhiteListHitCount() { + return whiteListHitCount; + } + + public void setWhiteListHitCount(long whiteListHitCount) { + this.whiteListHitCount = whiteListHitCount; + } + + public long getBlackListHitCount() { + return blackListHitCount; + } + + public void setBlackListHitCount(long blackListHitCount) { + this.blackListHitCount = blackListHitCount; + } + + public long getSyntaxErrorCount() { + return syntaxErrroCount; + } + + public void setSyntaxErrorCount(long syntaxErrroCount) { + this.syntaxErrroCount = syntaxErrroCount; + } + + public long getViolationEffectRowCount() { + return violationEffectRowCount; + } + + public void setViolationEffectRowCount(long violationEffectRowCount) { + this.violationEffectRowCount = violationEffectRowCount; + } + + public List getTables() { + return tables; + } + + public List getFunctions() { + return functions; + } + + public List getWhiteList() { + return whiteList; + } + + public List getBlackList() { + return blackList; + } + + public Map toMap() { + Map info = new LinkedHashMap(); + + info.put("checkCount", this.getCheckCount()); + info.put("hardCheckCount", this.getHardCheckCount()); + info.put("violationCount", this.getViolationCount()); + info.put("violationEffectRowCount", this.getViolationEffectRowCount()); + info.put("blackListHitCount", this.getBlackListHitCount()); + info.put("blackListSize", this.getBlackList().size()); + info.put("whiteListHitCount", this.getWhiteListHitCount()); + info.put("whiteListSize", this.getWhiteList().size()); + info.put("syntaxErrrorCount", this.getSyntaxErrorCount()); + + { + List> tables = new ArrayList>(this.tables.size()); + for (WallTableStatValue tableStatValue : this.tables) { + Map statMap = tableStatValue.toMap(); + tables.add(statMap); + } + info.put("tables", tables); + } + + { + List> functions = new ArrayList>(); + for (WallFunctionStatValue funStatValue : this.functions) { + Map statMap = funStatValue.toMap(); + functions.add(statMap); + } + info.put("functions", functions); + } + + { + List> blackList = new ArrayList>(this.blackList.size()); + for (WallSqlStatValue sqlStatValue : this.blackList) { + blackList.add(sqlStatValue.toMap()); + } + info.put("blackList", blackList); + } + + { + List> whiteList = new ArrayList>(this.whiteList.size()); + for (WallSqlStatValue sqlStatValue : this.whiteList) { + whiteList.add(sqlStatValue.toMap()); + } + info.put("whiteList", whiteList); + } + + return info; + } +} diff --git a/src/main/java/com/alibaba/druid/wall/WallSqlStat.java b/src/main/java/com/alibaba/druid/wall/WallSqlStat.java index 50a87fc44e..a16d6707c0 100644 --- a/src/main/java/com/alibaba/druid/wall/WallSqlStat.java +++ b/src/main/java/com/alibaba/druid/wall/WallSqlStat.java @@ -15,6 +15,8 @@ */ package com.alibaba.druid.wall; +import static com.alibaba.druid.util.JdbcSqlStatUtils.get; + import java.util.Collections; import java.util.List; import java.util.Map; @@ -29,7 +31,7 @@ public class WallSqlStat { "executeCount"); final static AtomicLongFieldUpdater effectRowCountUpdater = AtomicLongFieldUpdater.newUpdater(WallSqlStat.class, "effectRowCount"); - + private final Map tableStats; private final List violations; @@ -38,6 +40,8 @@ public class WallSqlStat { private final boolean syntaxError; + private String sample; + public WallSqlStat(Map tableStats, Map functionStats, boolean syntaxError){ this(tableStats, functionStats, Collections. emptyList(), syntaxError); @@ -51,6 +55,14 @@ public class WallSqlStat { this.syntaxError = syntaxError; } + public String getSample() { + return sample; + } + + public void setSample(String sample) { + this.sample = sample; + } + public long incrementAndGetExecuteCount() { return executeCountUpdater.incrementAndGet(this); } @@ -58,15 +70,15 @@ public class WallSqlStat { public long getExecuteCount() { return executeCount; } - + public long incrementAndGetEffectRowCount() { return effectRowCountUpdater.incrementAndGet(this); } - + public long addAndGetEffectRowCount(long delta) { return effectRowCountUpdater.addAndGet(this, delta); } - + public long getEffectRowCount() { return effectRowCount; } @@ -86,4 +98,19 @@ public class WallSqlStat { public boolean isSyntaxError() { return syntaxError; } + + public WallSqlStatValue getStatValue(boolean reset) { + final WallSqlStatValue statValue = new WallSqlStatValue(); + + statValue.setExecuteCount(get(this, executeCountUpdater, reset)); + statValue.setEffectRowCount(get(this, effectRowCountUpdater, reset)); + statValue.setSyntaxError(this.syntaxError); + statValue.setSqlSample(sample); + if (violations.size() > 0) { + String violationMessage = violations.get(0).getMessage(); + statValue.setViolationMessage(violationMessage); + } + + return statValue; + } } diff --git a/src/main/java/com/alibaba/druid/wall/WallSqlStatValue.java b/src/main/java/com/alibaba/druid/wall/WallSqlStatValue.java new file mode 100644 index 0000000000..ad0d62a33d --- /dev/null +++ b/src/main/java/com/alibaba/druid/wall/WallSqlStatValue.java @@ -0,0 +1,94 @@ +/* + * 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.wall; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class WallSqlStatValue { + + private String sql; + private String sqlSample; + private long executeCount; + private long effectRowCount; + private boolean syntaxError; + private String violationMessage; + + public WallSqlStatValue(){ + + } + + public String getSql() { + return sql; + } + + public void setSql(String sql) { + this.sql = sql; + } + + public String getSqlSample() { + return sqlSample; + } + + public void setSqlSample(String sqlSample) { + this.sqlSample = sqlSample; + } + + public long getExecuteCount() { + return executeCount; + } + + public void setExecuteCount(long executeCount) { + this.executeCount = executeCount; + } + + public long getEffectRowCount() { + return effectRowCount; + } + + public void setEffectRowCount(long effectRowCount) { + this.effectRowCount = effectRowCount; + } + + public boolean isSyntaxError() { + return syntaxError; + } + + public void setSyntaxError(boolean syntaxError) { + this.syntaxError = syntaxError; + } + + public String getViolationMessage() { + return violationMessage; + } + + public void setViolationMessage(String violationMessage) { + this.violationMessage = violationMessage; + } + + public Map toMap() { + Map sqlStatMap = new LinkedHashMap(); + sqlStatMap.put("sql", sql); + if (sql != sqlSample) { + sqlStatMap.put("sample", sqlSample); + } + sqlStatMap.put("executeCount", getExecuteCount()); + sqlStatMap.put("effectRowCount", getEffectRowCount()); + sqlStatMap.put("violationMessage", getViolationMessage()); + + return sqlStatMap; + } +} diff --git a/src/main/java/com/alibaba/druid/wall/WallTableStat.java b/src/main/java/com/alibaba/druid/wall/WallTableStat.java index e33f5df1f9..078c0f3e86 100644 --- a/src/main/java/com/alibaba/druid/wall/WallTableStat.java +++ b/src/main/java/com/alibaba/druid/wall/WallTableStat.java @@ -1,11 +1,3 @@ -package com.alibaba.druid.wall; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicLongFieldUpdater; - -import com.alibaba.druid.support.json.JSONUtils; - /* * Copyright 1999-2011 Alibaba Group Holding Ltd. * @@ -21,6 +13,16 @@ import com.alibaba.druid.support.json.JSONUtils; * See the License for the specific language governing permissions and * limitations under the License. */ +package com.alibaba.druid.wall; + +import static com.alibaba.druid.util.JdbcSqlStatUtils.get; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; + +import com.alibaba.druid.support.json.JSONUtils; + public class WallTableStat { private volatile long selectCount; @@ -124,7 +126,7 @@ public class WallTableStat { public long getUpdateDataCount() { return this.updateDataCount; } - + public long getInsertDataCount() { return this.insertDataCount; } @@ -132,7 +134,7 @@ public class WallTableStat { public void addInsertDataCount(long delta) { insertDataCountUpdater.addAndGet(this, delta); } - + public void addUpdateDataCount(long delta) { updateDataCountUpdater.addAndGet(this, delta); } @@ -220,43 +222,26 @@ public class WallTableStat { return toMap(map); } + public WallTableStatValue getStatValue(boolean reset) { + WallTableStatValue statValue = new WallTableStatValue(); + + statValue.setSelectCount(get(this, selectCountUpdater, reset)); + statValue.setDeleteCount(get(this, deleteCountUpdater, reset)); + statValue.setInsertCount(get(this, insertCountUpdater, reset)); + statValue.setUpdateCount(get(this, updateCountUpdater, reset)); + statValue.setAlterCount(get(this, alterCountUpdater, reset)); + statValue.setDropCount(get(this, dropCountUpdater, reset)); + statValue.setCreateCount(get(this, createCountUpdater, reset)); + statValue.setTruncateCount(get(this, truncateCountUpdater, reset)); + statValue.setReplaceCount(get(this, replaceCountUpdater, reset)); + statValue.setDeleteDataCount(get(this, deleteDataCountUpdater, reset)); + statValue.setFetchRowCount(get(this, fetchRowCountUpdater, reset)); + statValue.setUpdateDataCount(get(this, updateDataCountUpdater, reset)); + + return statValue; + } + public Map toMap(Map map) { - if (selectCount > 0) { - map.put("selectCount", selectCount); - } - if (deleteCount > 0) { - map.put("deleteCount", deleteCount); - } - if (insertCount > 0) { - map.put("insertCount", insertCount); - } - if (updateCount > 0) { - map.put("updateCount", updateCount); - } - if (alterCount > 0) { - map.put("alterCount", alterCount); - } - if (dropCount > 0) { - map.put("dropCount", dropCount); - } - if (createCount > 0) { - map.put("createCount", createCount); - } - if (truncateCount > 0) { - map.put("truncateCount", truncateCount); - } - if (replaceCount > 0) { - map.put("replaceCount", replaceCount); - } - if (deleteDataCount > 0) { - map.put("deleteDataCount", deleteDataCount); - } - if (fetchRowCount > 0) { - map.put("fetchRowCount", fetchRowCount); - } - if (updateDataCount > 0) { - map.put("updateDataCount", updateDataCount); - } - return map; + return getStatValue(false).toMap(map); } } diff --git a/src/main/java/com/alibaba/druid/wall/WallTableStatValue.java b/src/main/java/com/alibaba/druid/wall/WallTableStatValue.java new file mode 100644 index 0000000000..07bb7c5240 --- /dev/null +++ b/src/main/java/com/alibaba/druid/wall/WallTableStatValue.java @@ -0,0 +1,213 @@ +/* + * 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.wall; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class WallTableStatValue { + + private String name; + + private long selectCount; + private long selectIntoCount; + private long insertCount; + private long updateCount; + private long deleteCount; + private long truncateCount; + private long createCount; + private long alterCount; + private long dropCount; + private long replaceCount; + private long deleteDataCount; + private long updateDataCount; + private long insertDataCount; + private long fetchRowCount; + + public WallTableStatValue(){ + this(null); + } + + public WallTableStatValue(String name){ + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getSelectCount() { + return selectCount; + } + + public void setSelectCount(long selectCount) { + this.selectCount = selectCount; + } + + public long getSelectIntoCount() { + return selectIntoCount; + } + + public void setSelectIntoCount(long selectIntoCount) { + this.selectIntoCount = selectIntoCount; + } + + public long getInsertCount() { + return insertCount; + } + + public void setInsertCount(long insertCount) { + this.insertCount = insertCount; + } + + public long getUpdateCount() { + return updateCount; + } + + public void setUpdateCount(long updateCount) { + this.updateCount = updateCount; + } + + public long getDeleteCount() { + return deleteCount; + } + + public void setDeleteCount(long deleteCount) { + this.deleteCount = deleteCount; + } + + public long getTruncateCount() { + return truncateCount; + } + + public void setTruncateCount(long truncateCount) { + this.truncateCount = truncateCount; + } + + public long getCreateCount() { + return createCount; + } + + public void setCreateCount(long createCount) { + this.createCount = createCount; + } + + public long getAlterCount() { + return alterCount; + } + + public void setAlterCount(long alterCount) { + this.alterCount = alterCount; + } + + public long getDropCount() { + return dropCount; + } + + public void setDropCount(long dropCount) { + this.dropCount = dropCount; + } + + public long getReplaceCount() { + return replaceCount; + } + + public void setReplaceCount(long replaceCount) { + this.replaceCount = replaceCount; + } + + public long getDeleteDataCount() { + return deleteDataCount; + } + + public void setDeleteDataCount(long deleteDataCount) { + this.deleteDataCount = deleteDataCount; + } + + public long getUpdateDataCount() { + return updateDataCount; + } + + public void setUpdateDataCount(long updateDataCount) { + this.updateDataCount = updateDataCount; + } + + public long getInsertDataCount() { + return insertDataCount; + } + + public void setInsertDataCount(long insertDataCount) { + this.insertDataCount = insertDataCount; + } + + public long getFetchRowCount() { + return fetchRowCount; + } + + public void setFetchRowCount(long fetchRowCount) { + this.fetchRowCount = fetchRowCount; + } + + public Map toMap() { + Map map = new LinkedHashMap(); + toMap(map); + return map; + } + + public Map toMap(Map map) { + if (selectCount > 0) { + map.put("selectCount", selectCount); + } + if (deleteCount > 0) { + map.put("deleteCount", deleteCount); + } + if (insertCount > 0) { + map.put("insertCount", insertCount); + } + if (updateCount > 0) { + map.put("updateCount", updateCount); + } + if (alterCount > 0) { + map.put("alterCount", alterCount); + } + if (dropCount > 0) { + map.put("dropCount", dropCount); + } + if (createCount > 0) { + map.put("createCount", createCount); + } + if (truncateCount > 0) { + map.put("truncateCount", truncateCount); + } + if (replaceCount > 0) { + map.put("replaceCount", replaceCount); + } + if (deleteDataCount > 0) { + map.put("deleteDataCount", deleteDataCount); + } + if (fetchRowCount > 0) { + map.put("fetchRowCount", fetchRowCount); + } + if (updateDataCount > 0) { + map.put("updateDataCount", updateDataCount); + } + return map; + } +} diff --git a/src/main/java/com/alibaba/druid/wall/spi/WallStatLogger.java b/src/main/java/com/alibaba/druid/wall/spi/WallStatLogger.java new file mode 100644 index 0000000000..baefce5e36 --- /dev/null +++ b/src/main/java/com/alibaba/druid/wall/spi/WallStatLogger.java @@ -0,0 +1,20 @@ +/* + * 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.wall.spi; + +public interface WallStatLogger { + +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/WallStatTest_statMap.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/WallStatTest_statMap.java index ec064f70c9..f9962f2fe6 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/wall/WallStatTest_statMap.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/WallStatTest_statMap.java @@ -1,6 +1,5 @@ package com.alibaba.druid.bvt.filter.wall; -import java.util.Calendar; import java.util.Collection; import java.util.Map; @@ -24,25 +23,26 @@ public class WallStatTest_statMap extends TestCase { WallContext.clearContext(); } + @SuppressWarnings("unchecked") public void testMySql() throws Exception { WallProvider providerA = new MySqlWallProvider(); { - String sql = "select * from t where len(fname) = 1 OR 1 = 1"; + String sql = "select * from t where len(fname1) = 1 OR 1 = 1"; Assert.assertFalse(providerA.checkValid(sql)); providerA.addViolationEffectRowCount(10); } WallProvider providerB = new MySqlWallProvider(); { - String sql = "select * from t where len(fname) = 2 OR 1 = 1"; + String sql = "select * from t where len(fname2) = 2 OR 1 = 1"; Assert.assertFalse(providerB.checkValid(sql)); providerB.addViolationEffectRowCount(11); } WallProvider providerC = new MySqlWallProvider(); { - String sql = "select * from t where len(fname) = 2 OR 1 = 1"; + String sql = "select * from t where len(fname2) = 2 OR 1 = 1"; Assert.assertFalse(providerC.checkValid(sql)); providerC.addViolationEffectRowCount(12); }