mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
添加SQL统计功能,统计SQL中所使用的表和字段
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@274 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
+11
-7
@@ -26,7 +26,7 @@ import com.alibaba.druid.stat.TableStat.Mode;
|
||||
|
||||
public class MySqlSchemaStatVisitor extends MySqlASTVisitorAdapter {
|
||||
|
||||
private HashMap<String, TableStat> tableStats = new HashMap<String, TableStat>();
|
||||
private HashMap<TableStat.Name, TableStat> tableStats = new HashMap<TableStat.Name, TableStat>();
|
||||
private Set<Column> fields = new HashSet<Column>();
|
||||
|
||||
private final static ThreadLocal<Map<String, String>> aliasLocal = new ThreadLocal<Map<String, String>>();
|
||||
@@ -49,7 +49,7 @@ public class MySqlSchemaStatVisitor extends MySqlASTVisitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
|
||||
Mode mode = modeLocal.get();
|
||||
@@ -98,7 +98,7 @@ public class MySqlSchemaStatVisitor extends MySqlASTVisitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementUpdateCount();
|
||||
|
||||
@@ -133,7 +133,7 @@ public class MySqlSchemaStatVisitor extends MySqlASTVisitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementDeleteCount();
|
||||
}
|
||||
@@ -183,7 +183,7 @@ public class MySqlSchemaStatVisitor extends MySqlASTVisitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementInsertCount();
|
||||
|
||||
@@ -221,7 +221,7 @@ public class MySqlSchemaStatVisitor extends MySqlASTVisitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementInsertCount();
|
||||
|
||||
@@ -318,9 +318,13 @@ public class MySqlSchemaStatVisitor extends MySqlASTVisitorAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map<String, TableStat> getTables() {
|
||||
public Map<TableStat.Name, TableStat> getTables() {
|
||||
return tableStats;
|
||||
}
|
||||
|
||||
public boolean containsTable(String tableName) {
|
||||
return tableStats.containsKey(new TableStat.Name(tableName));
|
||||
}
|
||||
|
||||
public Set<Column> getFields() {
|
||||
return fields;
|
||||
|
||||
+13
-8
@@ -25,10 +25,11 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleUpdateStatement;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.stat.TableStat.Column;
|
||||
import com.alibaba.druid.stat.TableStat.Mode;
|
||||
import com.alibaba.druid.stat.TableStat.Name;
|
||||
|
||||
public class OracleSchemaStatVisitor extends OracleASTVIsitorAdapter {
|
||||
|
||||
private HashMap<String, TableStat> tableStats = new HashMap<String, TableStat>();
|
||||
private HashMap<TableStat.Name, TableStat> tableStats = new HashMap<TableStat.Name, TableStat>();
|
||||
private Set<Column> fields = new HashSet<Column>();
|
||||
|
||||
private final static ThreadLocal<Map<String, String>> aliasLocal = new ThreadLocal<Map<String, String>>();
|
||||
@@ -51,7 +52,7 @@ public class OracleSchemaStatVisitor extends OracleASTVIsitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
|
||||
Mode mode = modeLocal.get();
|
||||
@@ -90,7 +91,7 @@ public class OracleSchemaStatVisitor extends OracleASTVIsitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
|
||||
Mode mode = modeLocal.get();
|
||||
@@ -139,7 +140,7 @@ public class OracleSchemaStatVisitor extends OracleASTVIsitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementUpdateCount();
|
||||
|
||||
@@ -166,7 +167,7 @@ public class OracleSchemaStatVisitor extends OracleASTVIsitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementUpdateCount();
|
||||
|
||||
@@ -208,7 +209,7 @@ public class OracleSchemaStatVisitor extends OracleASTVIsitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementDeleteCount();
|
||||
|
||||
@@ -251,7 +252,7 @@ public class OracleSchemaStatVisitor extends OracleASTVIsitorAdapter {
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(ident, stat);
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementInsertCount();
|
||||
|
||||
@@ -365,9 +366,13 @@ public class OracleSchemaStatVisitor extends OracleASTVIsitorAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map<String, TableStat> getTables() {
|
||||
public Map<Name, TableStat> getTables() {
|
||||
return tableStats;
|
||||
}
|
||||
|
||||
public boolean containsTable(String tableName) {
|
||||
return tableStats.containsKey(new TableStat.Name(tableName));
|
||||
}
|
||||
|
||||
public Set<Column> getFields() {
|
||||
return fields;
|
||||
|
||||
@@ -22,7 +22,6 @@ public class TableStat {
|
||||
public int getUpdateCount() {
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
|
||||
public void incrementUpdateCount() {
|
||||
updateCount++;
|
||||
@@ -35,7 +34,7 @@ public class TableStat {
|
||||
public int getDeleteCount() {
|
||||
return deleteCount;
|
||||
}
|
||||
|
||||
|
||||
public void incrementDeleteCount() {
|
||||
this.deleteCount++;
|
||||
}
|
||||
@@ -43,7 +42,7 @@ public class TableStat {
|
||||
public void setDeleteCount(int deleteCount) {
|
||||
this.deleteCount = deleteCount;
|
||||
}
|
||||
|
||||
|
||||
public void incrementInsertCount() {
|
||||
this.insertCount++;
|
||||
}
|
||||
@@ -74,6 +73,37 @@ public class TableStat {
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static class Name {
|
||||
|
||||
private String name;
|
||||
|
||||
public Name(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.name.toLowerCase().hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof Name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Name other = (Name) o;
|
||||
|
||||
return this.name.equalsIgnoreCase(other.name);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Column {
|
||||
|
||||
private String table;
|
||||
|
||||
Reference in New Issue
Block a user