mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add mapping support
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.alibaba.druid.mapping;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlSelectParser;
|
||||
|
||||
public class MappingEngine {
|
||||
|
||||
private LinkedHashMap<String, Entity> entities = new LinkedHashMap<String, Entity>();
|
||||
private Integer maxLimit;
|
||||
|
||||
public Integer getMaxLimit() {
|
||||
return maxLimit;
|
||||
}
|
||||
|
||||
public void setMaxLimit(Integer maxLimit) {
|
||||
this.maxLimit = maxLimit;
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, Entity> getEntities() {
|
||||
return entities;
|
||||
}
|
||||
|
||||
public void addEntity(Entity entity) {
|
||||
this.entities.put(entity.getName(), entity);
|
||||
}
|
||||
|
||||
public EntityMapingMySqlVisitor createMappingVisitor() {
|
||||
return new EntityMapingMySqlVisitor(entities);
|
||||
}
|
||||
|
||||
public String explain(String sql) {
|
||||
MySqlSelectQueryBlock query = explainToSQLObject(sql);
|
||||
|
||||
if (this.maxLimit != null) {
|
||||
if (query.getLimit() == null) {
|
||||
Limit limit = new Limit();
|
||||
limit.setRowCount(new SQLNumberExpr(maxLimit));
|
||||
query.setLimit(limit);
|
||||
} else {
|
||||
SQLNumberExpr rowCountExpr = (SQLNumberExpr) query.getLimit().getRowCount();
|
||||
int rowCount = rowCountExpr.getNumber().intValue();
|
||||
if (rowCount > maxLimit.intValue()) {
|
||||
rowCountExpr.setNumber(maxLimit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
|
||||
return SQLUtils.toMySqlString(query);
|
||||
}
|
||||
|
||||
public MySqlSelectQueryBlock explainToSQLObject(String sql) {
|
||||
MySqlSelectParser selectParser = new MySqlSelectParser(sql);
|
||||
MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) selectParser.query();
|
||||
return query;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user