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,23 @@
|
||||
package com.alibaba.druid.mapping;
|
||||
|
||||
public class DruidMappingException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public DruidMappingException(){
|
||||
super();
|
||||
}
|
||||
|
||||
public DruidMappingException(String message, Throwable cause){
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public DruidMappingException(String message){
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DruidMappingException(Throwable cause){
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +1,24 @@
|
||||
package com.alibaba.druid.mapping;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Entity {
|
||||
|
||||
private String name;
|
||||
private String description;
|
||||
private String tableName;
|
||||
private String name;
|
||||
private String description;
|
||||
private String tableName;
|
||||
|
||||
private List<Property> properties = new ArrayList<Property>();
|
||||
private Map<String, Property> properties = new LinkedHashMap<String, Property>();
|
||||
|
||||
public Entity(){
|
||||
}
|
||||
|
||||
public Entity(String name, String description, String tableName){
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -35,11 +44,30 @@ public class Entity {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public List<Property> getProperties() {
|
||||
public void addProperty(Property property) {
|
||||
this.properties.put(property.getName(), property);
|
||||
}
|
||||
|
||||
public Property getProperty(String name) {
|
||||
Property prop = this.properties.get(name);
|
||||
|
||||
if (prop == null) {
|
||||
for (Map.Entry<String, Property> entry : properties.entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(name)) {
|
||||
prop = entry.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return prop;
|
||||
}
|
||||
|
||||
public Map<String, Property> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void setProperties(List<Property> properties) {
|
||||
public void setProperties(Map<String, Property> properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,150 @@
|
||||
package com.alibaba.druid.mapping;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
|
||||
|
||||
public class EntityMapingMySqlVisitor extends MySqlASTVisitorAdapter {
|
||||
|
||||
private final Entity entity;
|
||||
private final LinkedHashMap<String, Entity> entities;
|
||||
|
||||
public EntityMapingMySqlVisitor(Entity entity){
|
||||
super();
|
||||
this.entity = entity;
|
||||
public EntityMapingMySqlVisitor(LinkedHashMap<String, Entity> entities){
|
||||
this.entities = entities;
|
||||
}
|
||||
|
||||
public Entity getEntity() {
|
||||
public Map<String, Entity> getEntities() {
|
||||
return entities;
|
||||
}
|
||||
|
||||
public Entity getFirstEntity() {
|
||||
for (Map.Entry<String, Entity> entry : entities.entrySet()) {
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Entity getEntity(String name) {
|
||||
Entity entity = this.entities.get(name);
|
||||
|
||||
if (entity == null) {
|
||||
for (Map.Entry<String, Entity> entry : entities.entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(name)) {
|
||||
entity = entry.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectItem x) {
|
||||
x.getExpr().setParent(x);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(MySqlSelectQueryBlock x) {
|
||||
if (x.getSelectList().size() == 0) {
|
||||
fillSelectList(x);
|
||||
}
|
||||
|
||||
if (x.getSelectList().size() == 1) {
|
||||
if (x.getSelectList().get(0).getExpr() instanceof SQLAllColumnExpr) {
|
||||
x.getSelectList().clear();
|
||||
fillSelectList(x);
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getFrom() == null) {
|
||||
Entity firstEntity = this.getFirstEntity();
|
||||
SQLExprTableSource from = new SQLExprTableSource(new SQLIdentifierExpr(firstEntity.getName()));
|
||||
x.setFrom(from);
|
||||
}
|
||||
|
||||
for (SQLSelectItem item : x.getSelectList()) {
|
||||
item.setParent(x);
|
||||
}
|
||||
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectQueryBlock x) {
|
||||
if (x instanceof MySqlSelectQueryBlock) {
|
||||
return visit((MySqlSelectQueryBlock) x);
|
||||
}
|
||||
|
||||
if (x.getSelectList().size() == 0) {
|
||||
fillSelectList(x);
|
||||
}
|
||||
|
||||
for (SQLSelectItem item : x.getSelectList()) {
|
||||
item.setParent(x);
|
||||
}
|
||||
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
protected void fillSelectList(SQLSelectQueryBlock x) {
|
||||
Entity entity = getFirstEntity();
|
||||
|
||||
for (Property item : entity.getProperties().values()) {
|
||||
x.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr(item.getName()), '"' + item.getName() + '"'));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
String propertyName = x.getName();
|
||||
|
||||
Property property = null;
|
||||
for (Entity entity : this.entities.values()) {
|
||||
property = entity.getProperty(propertyName);
|
||||
if (property != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (property == null) {
|
||||
throw new DruidMappingException("property not found : " + propertyName);
|
||||
}
|
||||
|
||||
String dbColumName = property.getDbColumnName();
|
||||
x.setName(dbColumName);
|
||||
|
||||
if (x.getParent() instanceof SQLSelectItem) {
|
||||
SQLSelectItem selectItem = (SQLSelectItem) x.getParent();
|
||||
if (selectItem.getAlias() == null) {
|
||||
selectItem.setAlias('"' + property.getName() + '"');
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
SQLExpr expr = x.getExpr();
|
||||
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr tableExpr = (SQLIdentifierExpr) expr;
|
||||
String entityName = tableExpr.getName();
|
||||
|
||||
Entity entity = this.getEntity(entityName);
|
||||
|
||||
if (entity == null) {
|
||||
throw new DruidMappingException("entity not foudn : " + entityName);
|
||||
}
|
||||
|
||||
tableExpr.setName(entity.getTableName());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,18 @@ package com.alibaba.druid.mapping;
|
||||
public class Property {
|
||||
|
||||
private String name;
|
||||
private String desciption;
|
||||
private String dbColumnName;
|
||||
|
||||
public Property(){
|
||||
}
|
||||
|
||||
public Property(String name, String desciption, String dbColumnName){
|
||||
this.name = name;
|
||||
this.desciption = desciption;
|
||||
this.dbColumnName = dbColumnName;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@@ -21,4 +31,12 @@ public class Property {
|
||||
this.dbColumnName = dbColumnName;
|
||||
}
|
||||
|
||||
public String getDesciption() {
|
||||
return desciption;
|
||||
}
|
||||
|
||||
public void setDesciption(String desciption) {
|
||||
this.desciption = desciption;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user