add mapping support

This commit is contained in:
温少
2012-03-12 16:31:58 +08:00
parent 6a493d1e3b
commit 05e310f36d
13 changed files with 449 additions and 42 deletions
@@ -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;
}
}