mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve wall filter
This commit is contained in:
+26
-10
@@ -21,27 +21,43 @@ import java.util.List;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlReplaceStatement extends MySqlStatementImpl {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private boolean lowPriority = false;
|
||||
private boolean delayed = false;
|
||||
private boolean lowPriority = false;
|
||||
private boolean delayed = false;
|
||||
|
||||
private SQLName tableName;
|
||||
private final List<SQLExpr> columns = new ArrayList<SQLExpr>();
|
||||
private List<ValuesClause> valuesList = new ArrayList<ValuesClause>();
|
||||
private SQLQueryExpr query;
|
||||
private SQLExprTableSource tableSource;
|
||||
private final List<SQLExpr> columns = new ArrayList<SQLExpr>();
|
||||
private List<ValuesClause> valuesList = new ArrayList<ValuesClause>();
|
||||
private SQLQueryExpr query;
|
||||
|
||||
public SQLName getTableName() {
|
||||
return tableName;
|
||||
if (tableSource == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (SQLName) tableSource.getExpr();
|
||||
}
|
||||
|
||||
public void setTableName(SQLName tableName) {
|
||||
this.tableName = tableName;
|
||||
this.setTableSource(new SQLExprTableSource(tableName));
|
||||
}
|
||||
|
||||
public SQLExprTableSource getTableSource() {
|
||||
return tableSource;
|
||||
}
|
||||
|
||||
public void setTableSource(SQLExprTableSource tableSource) {
|
||||
if (tableSource != null) {
|
||||
tableSource.setParent(this);
|
||||
}
|
||||
this.tableSource = tableSource;
|
||||
}
|
||||
|
||||
public List<SQLExpr> getColumns() {
|
||||
@@ -79,7 +95,7 @@ public class MySqlReplaceStatement extends MySqlStatementImpl {
|
||||
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, tableName);
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, columns);
|
||||
acceptChild(visitor, valuesList);
|
||||
acceptChild(visitor, query);
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -30,6 +31,7 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import com.alibaba.druid.VERSION;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
|
||||
import com.alibaba.druid.support.http.stat.WebAppStatManager;
|
||||
import com.alibaba.druid.support.spring.stat.SpringStatManager;
|
||||
import com.alibaba.druid.util.DruidDataSourceUtils;
|
||||
@@ -195,6 +197,7 @@ public final class DruidStatManagerFacade {
|
||||
return mapA;
|
||||
}
|
||||
|
||||
Map<String, Object> newMap = new LinkedHashMap<String, Object>();
|
||||
for (Object item : mapB.entrySet()) {
|
||||
Map.Entry entry = (Map.Entry) item;
|
||||
String key = (String) entry.getKey();
|
||||
@@ -202,16 +205,26 @@ public final class DruidStatManagerFacade {
|
||||
Object valueA = mapA.get(key);
|
||||
|
||||
if (valueA == null) {
|
||||
mapA.put(key, valueB);
|
||||
newMap.put(key, valueB);
|
||||
} else if (valueB == null) {
|
||||
newMap.put(key, valueA);
|
||||
} else {
|
||||
if (valueA instanceof Map && valueB instanceof Map) {
|
||||
Object newValue = mergWallStat((Map)valueA, (Map)valueB);
|
||||
mapA.put(key, newValue);
|
||||
newMap.put(key, newValue);
|
||||
} else if (valueA instanceof Set && valueB instanceof Set) {
|
||||
Set<Object> set = new HashSet<Object>();
|
||||
set.addAll((Set) valueA);
|
||||
set.addAll((Set) valueB);
|
||||
newMap.put(key, set);
|
||||
} else {
|
||||
Object sum = SQLEvalVisitorUtils.add(valueA, valueB);
|
||||
newMap.put(key, sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return newMap;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList(Object datasource) {
|
||||
|
||||
@@ -26,6 +26,19 @@ public class WallSqlTableStat {
|
||||
private int createCount;
|
||||
private int alterCount;
|
||||
private int dropCount;
|
||||
private int replaceCount;
|
||||
|
||||
public int getReplaceCount() {
|
||||
return replaceCount;
|
||||
}
|
||||
|
||||
public int incrementReplaceCount() {
|
||||
return replaceCount++;
|
||||
}
|
||||
|
||||
public void addReplaceCount(int value) {
|
||||
this.replaceCount+= value;
|
||||
}
|
||||
|
||||
public int getSelectCount() {
|
||||
return selectCount;
|
||||
@@ -34,7 +47,7 @@ public class WallSqlTableStat {
|
||||
public void incrementSelectCount() {
|
||||
this.selectCount++;
|
||||
}
|
||||
|
||||
|
||||
public void addSelectCount(int value) {
|
||||
this.selectCount += value;
|
||||
}
|
||||
@@ -46,7 +59,7 @@ public class WallSqlTableStat {
|
||||
public void incrementSelectIntoCount() {
|
||||
this.selectIntoCount++;
|
||||
}
|
||||
|
||||
|
||||
public void addSelectIntoCount(int value) {
|
||||
this.selectIntoCount += value;
|
||||
}
|
||||
@@ -58,7 +71,7 @@ public class WallSqlTableStat {
|
||||
public void incrementInsertCount() {
|
||||
this.insertCount++;
|
||||
}
|
||||
|
||||
|
||||
public void addInsertCount(int value) {
|
||||
this.insertCount += value;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ public class WallTableStat {
|
||||
private volatile long createCount;
|
||||
private volatile long alterCount;
|
||||
private volatile long dropCount;
|
||||
private volatile long replaceCount;
|
||||
|
||||
final static AtomicLongFieldUpdater<WallTableStat> selectCountUpdater = AtomicLongFieldUpdater.newUpdater(WallTableStat.class,
|
||||
"selectCount");
|
||||
@@ -51,6 +52,8 @@ public class WallTableStat {
|
||||
"alterCount");
|
||||
final static AtomicLongFieldUpdater<WallTableStat> dropCountUpdater = AtomicLongFieldUpdater.newUpdater(WallTableStat.class,
|
||||
"dropCount");
|
||||
final static AtomicLongFieldUpdater<WallTableStat> replaceCountUpdater = AtomicLongFieldUpdater.newUpdater(WallTableStat.class,
|
||||
"replaceCount");
|
||||
|
||||
public WallTableStat(){
|
||||
|
||||
@@ -91,6 +94,10 @@ public class WallTableStat {
|
||||
public long getDropCount() {
|
||||
return dropCount;
|
||||
}
|
||||
|
||||
public long getReplaceCount() {
|
||||
return replaceCount;
|
||||
}
|
||||
|
||||
public void addSqlTableStat(WallSqlTableStat stat) {
|
||||
{
|
||||
@@ -147,6 +154,12 @@ public class WallTableStat {
|
||||
dropCountUpdater.addAndGet(this, val);
|
||||
}
|
||||
}
|
||||
{
|
||||
long val = stat.getReplaceCount();
|
||||
if (val > 0) {
|
||||
replaceCountUpdater.addAndGet(this, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
@@ -155,7 +168,7 @@ public class WallTableStat {
|
||||
|
||||
return JSONUtils.toJSONString(map);
|
||||
}
|
||||
|
||||
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
return toMap(map);
|
||||
@@ -186,6 +199,9 @@ public class WallTableStat {
|
||||
if (truncateCount > 0) {
|
||||
map.put("truncateCount", truncateCount);
|
||||
}
|
||||
if (replaceCount > 0) {
|
||||
map.put("replaceCount", replaceCount);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
|
||||
@@ -342,4 +343,9 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi
|
||||
public boolean visit(SQLSetStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlReplaceStatement x) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -872,6 +872,8 @@ public class WallVisitorUtils {
|
||||
tableStat.incrementDeleteCount();
|
||||
} else if (parent instanceof SQLUpdateStatement) {
|
||||
tableStat.incrementUpdateCount();
|
||||
} else if (parent instanceof MySqlReplaceStatement) {
|
||||
tableStat.incrementReplaceCount();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user