mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser schema stat
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package com.alibaba.druid.sql.dialect.oracle.visitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLEvalVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
|
||||
|
||||
|
||||
public class OracleEvalVisitor extends OracleASTVisitorAdapter implements SQLEvalVisitor {
|
||||
private List<Object> parameters = new ArrayList<Object>();
|
||||
|
||||
private int variantIndex = -1;
|
||||
|
||||
private boolean markVariantIndex = true;
|
||||
|
||||
public OracleEvalVisitor(){
|
||||
this(new ArrayList<Object>(1));
|
||||
}
|
||||
|
||||
public OracleEvalVisitor(List<Object> parameters){
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public List<Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public void setParameters(List<Object> parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public boolean visit(SQLCharExpr x) {
|
||||
return SQLEvalVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
public int incrementAndGetVariantIndex() {
|
||||
return ++variantIndex;
|
||||
}
|
||||
|
||||
public int getVariantIndex() {
|
||||
return variantIndex;
|
||||
}
|
||||
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
return SQLEvalVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return SQLEvalVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
public boolean isMarkVariantIndex() {
|
||||
return markVariantIndex;
|
||||
}
|
||||
|
||||
public void setMarkVariantIndex(boolean markVariantIndex) {
|
||||
this.markVariantIndex = markVariantIndex;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLEvalVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
|
||||
|
||||
|
||||
public class PGEvalVisitor extends PGASTVisitorAdapter implements SQLEvalVisitor {
|
||||
private List<Object> parameters = new ArrayList<Object>();
|
||||
|
||||
private int variantIndex = -1;
|
||||
|
||||
private boolean markVariantIndex = true;
|
||||
|
||||
public PGEvalVisitor(){
|
||||
this(new ArrayList<Object>(1));
|
||||
}
|
||||
|
||||
public PGEvalVisitor(List<Object> parameters){
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public List<Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public void setParameters(List<Object> parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public boolean visit(SQLCharExpr x) {
|
||||
return SQLEvalVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
public int incrementAndGetVariantIndex() {
|
||||
return ++variantIndex;
|
||||
}
|
||||
|
||||
public int getVariantIndex() {
|
||||
return variantIndex;
|
||||
}
|
||||
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
return SQLEvalVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return SQLEvalVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
public boolean isMarkVariantIndex() {
|
||||
return markVariantIndex;
|
||||
}
|
||||
|
||||
public void setMarkVariantIndex(boolean markVariantIndex) {
|
||||
this.markVariantIndex = markVariantIndex;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,6 +22,8 @@ import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlEvalVisitorImpl;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleEvalVisitor;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGEvalVisitor;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class SQLEvalVisitorUtils {
|
||||
@@ -47,15 +49,19 @@ public class SQLEvalVisitorUtils {
|
||||
|
||||
return sqlObject.getAttributes().get(EVAL_VALUE);
|
||||
}
|
||||
|
||||
|
||||
public static Object eval(String dbType, SQLObject sqlObject, List<Object> parameters) {
|
||||
return eval(dbType, sqlObject, parameters, true);
|
||||
}
|
||||
|
||||
public static Object eval(String dbType, SQLObject sqlObject, List<Object> parameters, boolean throwError) {
|
||||
SQLEvalVisitor visitor = createEvalVisitor(dbType);
|
||||
visitor.setParameters(parameters);
|
||||
sqlObject.accept(visitor);
|
||||
|
||||
Object value = getValue(sqlObject);
|
||||
if (value == null) {
|
||||
if (!sqlObject.getAttributes().containsKey(EVAL_VALUE)) {
|
||||
if (throwError && !sqlObject.getAttributes().containsKey(EVAL_VALUE)) {
|
||||
throw new DruidRuntimeException("eval error : " + SQLUtils.toSQLString(sqlObject, dbType));
|
||||
}
|
||||
}
|
||||
@@ -67,6 +73,14 @@ public class SQLEvalVisitorUtils {
|
||||
if (JdbcUtils.MYSQL.equals(dbType)) {
|
||||
return new MySqlEvalVisitorImpl();
|
||||
}
|
||||
|
||||
if (JdbcUtils.ORACLE.equals(dbType)) {
|
||||
return new OracleEvalVisitor();
|
||||
}
|
||||
|
||||
if (JdbcUtils.POSTGRESQL.equals(dbType)) {
|
||||
return new PGEvalVisitor();
|
||||
}
|
||||
|
||||
return new SQLEvalVisitorImpl();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ 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.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
@@ -53,9 +54,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
protected final HashMap<TableStat.Name, TableStat> tableStats = new LinkedHashMap<TableStat.Name, TableStat>();
|
||||
protected final Set<Column> columns = new LinkedHashSet<Column>();
|
||||
protected final List<Condition> conditions = new ArrayList<Condition>();
|
||||
protected final List<Condition> conditions = new ArrayList<Condition>();
|
||||
protected final Set<Relationship> relationships = new LinkedHashSet<Relationship>();
|
||||
protected final Set<Column> orderByColumns = new LinkedHashSet<Column>();
|
||||
protected final List<Column> orderByColumns = new ArrayList<Column>();
|
||||
protected final Set<Column> groupByColumns = new LinkedHashSet<Column>();
|
||||
|
||||
protected final Map<String, SQLObject> subQueryMap = new LinkedHashMap<String, SQLObject>();
|
||||
@@ -192,6 +193,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
public OrderByStatVisitor(SQLOrderBy orderBy){
|
||||
this.orderBy = orderBy;
|
||||
for (SQLSelectOrderByItem item : orderBy.getItems()) {
|
||||
item.getExpr().setParent(item);
|
||||
}
|
||||
}
|
||||
|
||||
public SQLOrderBy getOrderBy() {
|
||||
@@ -206,9 +210,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
if (currentTable != null) {
|
||||
orderByColumns.add(new Column(currentTable, x.getName()));
|
||||
addOrderByColumn(currentTable, x.getName(), x);
|
||||
} else {
|
||||
orderByColumns.add(new Column("UNKOWN", x.getName()));
|
||||
addOrderByColumn("UNKOWN", x.getName(), x);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -224,12 +228,24 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
owner = aliasWrap(owner);
|
||||
|
||||
if (owner != null) {
|
||||
orderByColumns.add(new Column(owner, x.getName()));
|
||||
addOrderByColumn(owner, x.getName(), x);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addOrderByColumn(String table, String columnName, SQLObject expr) {
|
||||
Column column = new Column(table, columnName);
|
||||
|
||||
SQLObject parent = expr.getParent();
|
||||
if (parent instanceof SQLSelectOrderByItem) {
|
||||
SQLOrderingSpecification type = ((SQLSelectOrderByItem) parent).getType();
|
||||
column.getAttributes().put("orderBy.type", type);
|
||||
}
|
||||
|
||||
orderByColumns.add(column);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean visit(SQLOrderBy x) {
|
||||
@@ -258,7 +274,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
return relationships;
|
||||
}
|
||||
|
||||
public Set<Column> getOrderByColumns() {
|
||||
public List<Column> getOrderByColumns() {
|
||||
return orderByColumns;
|
||||
}
|
||||
|
||||
@@ -340,13 +356,13 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
condition.setOperator(operator);
|
||||
this.conditions.add(condition);
|
||||
}
|
||||
|
||||
|
||||
for (SQLExpr item : valueExprs) {
|
||||
Object value = SQLEvalVisitorUtils.eval(getDbType(), item, parameters);
|
||||
Object value = SQLEvalVisitorUtils.eval(getDbType(), item, parameters, false);
|
||||
condition.getValues().add(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getDbType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TableStat {
|
||||
|
||||
@@ -341,8 +343,10 @@ public class TableStat {
|
||||
|
||||
public static class Column {
|
||||
|
||||
private String table;
|
||||
private String name;
|
||||
private String table;
|
||||
private String name;
|
||||
|
||||
private Map<String, Object> attributes = new HashMap<String, Object>();
|
||||
|
||||
public Column(){
|
||||
|
||||
@@ -369,6 +373,14 @@ public class TableStat {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Map<String, Object> getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public void setAttributes(Map<String, Object> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int tableHashCode = table != null ? table.toLowerCase().hashCode() : 0;
|
||||
int nameHashCode = name != null ? name.toLowerCase().hashCode() : 0;
|
||||
|
||||
@@ -6,17 +6,19 @@ import java.util.List;
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.stat.TableStat.Column;
|
||||
import com.alibaba.druid.stat.TableStat.Condition;
|
||||
|
||||
public class OracleSchemaStatVisitorTest extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT id, name name from department d" + //
|
||||
" WHERE d.id = ?";
|
||||
" WHERE d.id = ? order by name desc";
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
@@ -49,5 +51,8 @@ public class OracleSchemaStatVisitorTest extends OracleTest {
|
||||
|
||||
Condition condition = visitor.getConditions().get(0);
|
||||
Assert.assertSame(parameters.get(0), condition.getValues().get(0));
|
||||
|
||||
Column orderByColumn = visitor.getOrderByColumns().iterator().next();
|
||||
Assert.assertEquals(SQLOrderingSpecification.DESC, orderByColumn.getAttributes().get("orderBy.type"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user