enhance schema stat

This commit is contained in:
温少
2012-02-24 09:42:39 +08:00
parent e200714281
commit 8c7352beca
53 changed files with 351 additions and 217 deletions
@@ -22,4 +22,7 @@ public class SQLDefaultExpr extends SQLLiteralExpr {
visitor.endVisit(this);
}
public String toString() {
return "DEFAULT";
}
}
@@ -91,6 +91,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
public boolean visit(MySqlSelectQueryBlock select) {
if (select.getOrderBy() != null) {
select.getOrderBy().setParent(select);
}
print("SELECT ");
if (SQLSetQuantifier.ALL == select.getDistionOption()) print("ALL ");
@@ -212,7 +212,7 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements
// table == null时是SubQuery
if (table != null) {
fields.add(new Column(table, x.getName()));
columns.add(new Column(table, x.getName()));
}
}
}
@@ -236,7 +236,7 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements
String currentTable = currentTableLocal.get();
if (currentTable != null) {
fields.add(new Column(currentTable, x.getName()));
columns.add(new Column(currentTable, x.getName()));
}
return false;
}
@@ -442,9 +442,8 @@ public class SQLExprParser extends SQLParser {
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Assignment, rightExp);
} else {
if (lexer.token() == Token.LPAREN) {
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
String method_name = identExpr.getName();
if (expr instanceof SQLIdentifierExpr || expr instanceof SQLDefaultExpr) {
String method_name = expr.toString();
lexer.nextToken();
if (isAggreateFunction(method_name)) {
@@ -463,7 +462,7 @@ public class SQLExprParser extends SQLParser {
return primaryRest(methodInvokeExpr);
}
throw new ParserException("not support token:");
throw new ParserException("not support token:" + lexer.token());
}
}
@@ -480,7 +479,7 @@ public class SQLExprParser extends SQLParser {
if (lexer.token() == Token.KEY) {
name = "KEY";
lexer.nextToken();
} else if (lexer.token() != Token.IDENTIFIER) {
} else if (lexer.token() != Token.IDENTIFIER && lexer.token() != Token.USER && lexer.token() != Token.PASSWORD) {
throw new ParserException("error : " + lexer.stringVal());
} else {
name = lexer.stringVal();
@@ -9,12 +9,14 @@ import java.util.Set;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
@@ -23,6 +25,8 @@ import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
@@ -36,13 +40,87 @@ import com.alibaba.druid.stat.TableStat.Mode;
public class SchemaStatVisitor extends SQLASTVisitorAdapter {
protected final HashMap<TableStat.Name, TableStat> tableStats = new HashMap<TableStat.Name, TableStat>();
protected final Set<Column> fields = new HashSet<Column>();
protected final Set<Column> columns = new HashSet<Column>();
protected final Set<Condition> conditions = new HashSet<Condition>();
protected final Set<Column> orderByColumns = new HashSet<Column>();
protected final Set<Column> groupByColumns = new HashSet<Column>();
protected final static ThreadLocal<Map<String, String>> aliasLocal = new ThreadLocal<Map<String, String>>();
protected final static ThreadLocal<String> currentTableLocal = new ThreadLocal<String>();
protected final static ThreadLocal<Mode> modeLocal = new ThreadLocal<Mode>();
public class OrderByStatVisitor extends SQLASTVisitorAdapter {
private final SQLOrderBy orderBy;
public OrderByStatVisitor(SQLOrderBy orderBy){
this.orderBy = orderBy;
}
public SQLOrderBy getOrderBy() {
return orderBy;
}
public boolean visit(SQLIdentifierExpr x) {
String currentTable = currentTableLocal.get();
if (currentTable != null) {
orderByColumns.add(new Column(currentTable, x.getName()));
}
return false;
}
public boolean visit(SQLPropertyExpr x) {
if (x.getOwner() instanceof SQLIdentifierExpr) {
String owner = ((SQLIdentifierExpr) x.getOwner()).getName();
if (owner != null) {
Map<String, String> aliasMap = aliasLocal.get();
if (aliasMap != null) {
String table = aliasMap.get(owner);
// table == null时是SubQuery
if (table != null) {
orderByColumns.add(new Column(table, x.getName()));
}
}
}
}
return false;
}
}
public boolean visit(SQLOrderBy x) {
OrderByStatVisitor orderByVisitor = new OrderByStatVisitor(x);
SQLSelectQueryBlock query = null;
if (x.getParent() instanceof SQLSelectQueryBlock) {
query = (SQLSelectQueryBlock) x.getParent();
}
if (query != null) {
for (SQLSelectOrderByItem item : x.getItems()) {
SQLExpr expr = item.getExpr();
if (expr instanceof SQLIntegerExpr) {
int intValue = ((SQLIntegerExpr) expr).getNumber().intValue() - 1;
if (intValue < query.getSelectList().size()) {
SQLSelectItem selectItem = query.getSelectList().get(intValue);
selectItem.getExpr().accept(orderByVisitor);
}
}
}
}
x.accept(orderByVisitor);
return true;
}
public Set<Column> getOrderByColumns() {
return orderByColumns;
}
public Set<Column> getGroupByColumns() {
return groupByColumns;
}
public Set<Condition> getConditions() {
return conditions;
}
@@ -278,7 +356,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
// table == null时是SubQuery
if (table != null) {
fields.add(new Column(table, x.getName()));
columns.add(new Column(table, x.getName()));
}
}
}
@@ -290,7 +368,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
String currentTable = currentTableLocal.get();
if (currentTable != null) {
fields.add(new Column(currentTable, x.getName()));
columns.add(new Column(currentTable, x.getName()));
}
return false;
}
@@ -299,7 +377,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
String currentTable = currentTableLocal.get();
if (currentTable != null) {
fields.add(new Column(currentTable, "*"));
columns.add(new Column(currentTable, "*"));
}
return false;
}
@@ -312,8 +390,8 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
return tableStats.containsKey(new TableStat.Name(tableName));
}
public Set<Column> getFields() {
return fields;
public Set<Column> getColumns() {
return columns;
}
public boolean visit(SQLSelectStatement x) {
@@ -387,6 +465,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
public boolean visit(SQLSelect x) {
if (x.getOrderBy() != null) {
x.getOrderBy().setParent(x);
}
accept(x.getQuery());
String originalTable = currentTableLocal.get();
@@ -453,14 +535,14 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
return false;
}
public boolean visit(SQLInListExpr x) {
if (x.isNot()) {
handleCondition(x.getExpr(), "NOT IN");
} else {
handleCondition(x.getExpr(), "IN");
}
return true;
}