mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
enhance mapping
This commit is contained in:
@@ -7,7 +7,8 @@ public class MappingContext {
|
||||
|
||||
private List<Object> parameters;
|
||||
|
||||
private boolean generateAlias = false;
|
||||
private boolean generateAlias = false;
|
||||
private boolean explainAllColumnToList = false;
|
||||
|
||||
public MappingContext(){
|
||||
this(new ArrayList<Object>());
|
||||
@@ -33,4 +34,12 @@ public class MappingContext {
|
||||
this.generateAlias = generateAlias;
|
||||
}
|
||||
|
||||
public boolean isExplainAllColumnToList() {
|
||||
return explainAllColumnToList;
|
||||
}
|
||||
|
||||
public void setExplainAllColumnToList(boolean explainAllColumnToList) {
|
||||
this.explainAllColumnToList = explainAllColumnToList;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -100,6 +100,7 @@ public class MappingEngine {
|
||||
public String explainToSelectSQL(String sql, List<Object> parameters) {
|
||||
return explainToSelectSQL(sql, new MappingContext(parameters));
|
||||
}
|
||||
|
||||
public String explainToSelectSQL(String sql, MappingContext context) {
|
||||
SQLSelectQueryBlock query = explainToSelectSQLObject(sql);
|
||||
|
||||
|
||||
@@ -63,7 +63,12 @@ public class MappingVisitorUtils {
|
||||
Entity entity = visitor.getFirstEntity();
|
||||
|
||||
for (Property item : entity.getProperties().values()) {
|
||||
x.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr(item.getName()), '"' + item.getName() + '"'));
|
||||
String alias = null;
|
||||
|
||||
if (visitor.getContext().isGenerateAlias()) {
|
||||
alias = '"' + item.getName() + '"';
|
||||
}
|
||||
x.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr(item.getName()), alias));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +80,7 @@ public class MappingVisitorUtils {
|
||||
|
||||
Property property = null;
|
||||
Entity entity = visitor.getEntity(ownerName);
|
||||
|
||||
|
||||
if (entity == null) {
|
||||
visitor.getUnresolveList().add(x);
|
||||
return false;
|
||||
@@ -100,13 +105,13 @@ public class MappingVisitorUtils {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLIdentifierExpr x) {
|
||||
String propertyName = x.getName();
|
||||
|
||||
Property property = null;
|
||||
Entity propertyEntity = null;
|
||||
|
||||
|
||||
for (SQLTableSource tableSource : visitor.getTableSources().values()) {
|
||||
Entity entity = (Entity) tableSource.getAttribute("mapping.entity");
|
||||
if (entity != null) {
|
||||
@@ -125,7 +130,7 @@ public class MappingVisitorUtils {
|
||||
|
||||
String dbColumName = visitor.resovleColumnName(propertyEntity, property);
|
||||
x.setName(dbColumName);
|
||||
|
||||
|
||||
x.putAttribute("mapping.property", property);
|
||||
x.putAttribute("mapping.entity", propertyEntity);
|
||||
|
||||
@@ -138,55 +143,55 @@ public class MappingVisitorUtils {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLSelectItem x) {
|
||||
x.getExpr().setParent(x);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLBinaryOpExpr x) {
|
||||
if (x.getOperator() == SQLBinaryOperator.Equality) {
|
||||
if (x.getLeft() instanceof SQLIdentifierExpr && isSimpleValue(visitor, x.getRight())) {
|
||||
visit(visitor, (SQLIdentifierExpr) x.getLeft());
|
||||
x.getRight().accept(visitor);
|
||||
|
||||
|
||||
Entity entity = (Entity) x.getLeft().getAttribute("mapping.entity");
|
||||
Property property = (Property) x.getLeft().getAttribute("mapping.property");
|
||||
Object value = x.getRight().getAttribute("mapping.value");
|
||||
|
||||
|
||||
visitor.getPropertyValues().add(new PropertyValue(entity, property, value));
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (x.getLeft() instanceof SQLPropertyExpr && isSimpleValue(visitor, x.getRight())) {
|
||||
visit(visitor, (SQLPropertyExpr) x.getLeft());
|
||||
x.getRight().accept(visitor);
|
||||
|
||||
|
||||
Entity entity = (Entity) x.getLeft().getAttribute("mapping.entity");
|
||||
Property property = (Property) x.getLeft().getAttribute("mapping.property");
|
||||
Object value = x.getRight().getAttribute("mapping.value");
|
||||
|
||||
|
||||
visitor.getPropertyValues().add(new PropertyValue(entity, property, value));
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private static boolean isSimpleValue(MappingVisitor visitor, SQLExpr expr) {
|
||||
if (expr instanceof SQLNumericLiteralExpr) {
|
||||
expr.putAttribute("mapping.value", ((SQLNumericLiteralExpr) expr).getNumber());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if (expr instanceof SQLCharExpr) {
|
||||
expr.putAttribute("mapping.value", ((SQLCharExpr) expr).getText());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if (expr instanceof SQLVariantRefExpr) {
|
||||
Map<String, Object> attributes = expr.getAttributes();
|
||||
Integer varIndex = (Integer) attributes.get("mapping.varIndex");
|
||||
@@ -194,22 +199,22 @@ public class MappingVisitorUtils {
|
||||
varIndex = visitor.getAndIncrementVariantIndex();
|
||||
expr.putAttribute("mapping.varIndex", varIndex);
|
||||
}
|
||||
|
||||
|
||||
if (visitor.getParameters().size() > varIndex) {
|
||||
Object parameter = visitor.getParameters().get(varIndex);
|
||||
expr.putAttribute("mapping.value", parameter);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static void afterResolve(MappingVisitor visitor) {
|
||||
for (Iterator<SQLExpr> iter = visitor.getUnresolveList().iterator();iter.hasNext();) {
|
||||
for (Iterator<SQLExpr> iter = visitor.getUnresolveList().iterator(); iter.hasNext();) {
|
||||
SQLExpr expr = iter.next();
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
if (resolve(visitor, (SQLIdentifierExpr) expr)) {
|
||||
iter.remove();
|
||||
}
|
||||
@@ -220,10 +225,10 @@ public class MappingVisitorUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean resolve(MappingVisitor visitor, SQLIdentifierExpr x) {
|
||||
String propertyName = x.getName();
|
||||
|
||||
|
||||
for (SQLTableSource tableSource : visitor.getTableSources().values()) {
|
||||
Entity entity = (Entity) tableSource.getAttribute("mapping.entity");
|
||||
if (entity != null) {
|
||||
@@ -231,23 +236,23 @@ public class MappingVisitorUtils {
|
||||
if (property != null) {
|
||||
String columnName = visitor.resovleColumnName(entity, property);
|
||||
x.setName(columnName);
|
||||
|
||||
|
||||
x.putAttribute("mapping.entity", entity);
|
||||
x.putAttribute("mapping.property", property);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean resolve(MappingVisitor visitor, SQLPropertyExpr x) {
|
||||
if (x.getOwner() instanceof SQLIdentifierExpr) {
|
||||
String ownerName = ((SQLIdentifierExpr) x.getOwner()).getName();
|
||||
SQLTableSource tableSource = visitor.getTableSources().get(ownerName);
|
||||
Entity entity = (Entity) tableSource.getAttribute("mapping.entity");
|
||||
|
||||
|
||||
if (entity != null) {
|
||||
Property property = entity.getProperty(x.getName());
|
||||
if (property != null) {
|
||||
@@ -255,22 +260,22 @@ public class MappingVisitorUtils {
|
||||
x.setName(columnName);
|
||||
x.putAttribute("mapping.entity", entity);
|
||||
x.putAttribute("mapping.property", property);
|
||||
|
||||
|
||||
if (x.getParent() instanceof SQLSelectItem) {
|
||||
SQLSelectItem selectItem = (SQLSelectItem) x.getParent();
|
||||
if (selectItem.getAlias() == null) {
|
||||
selectItem.setAlias('"' + property.getName() + '"');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLVariantRefExpr x) {
|
||||
Map<String, Object> attributes = x.getAttributes();
|
||||
Integer varIndex = (Integer) attributes.get("mapping.varIndex");
|
||||
@@ -283,13 +288,15 @@ public class MappingVisitorUtils {
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLSelectQueryBlock x) {
|
||||
if (x.getSelectList().size() == 0) {
|
||||
fillSelectList(visitor, x);
|
||||
x.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
|
||||
}
|
||||
|
||||
if (x.getSelectList().size() == 1) {
|
||||
if (x.getSelectList().get(0).getExpr() instanceof SQLAllColumnExpr) {
|
||||
x.getSelectList().clear();
|
||||
fillSelectList(visitor, x);
|
||||
if (visitor.getContext().isExplainAllColumnToList()) {
|
||||
if (x.getSelectList().size() == 1) {
|
||||
if (x.getSelectList().get(0).getExpr() instanceof SQLAllColumnExpr) {
|
||||
x.getSelectList().clear();
|
||||
fillSelectList(visitor, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,10 @@ public class SQLSelectItem extends SQLObjectImpl {
|
||||
public SQLSelectItem(){
|
||||
|
||||
}
|
||||
|
||||
public SQLSelectItem(SQLExpr expr) {
|
||||
this(expr, null);
|
||||
}
|
||||
|
||||
public SQLSelectItem(SQLExpr expr, String alias){
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingContext;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.mapping.Property;
|
||||
import com.alibaba.druid.mapping.spi.MappingVisitor;
|
||||
@@ -44,7 +45,10 @@ public class MySqlMappingShardingTest extends TestCase {
|
||||
public void test_0() throws Exception {
|
||||
String oql = "select * from 用户 u where u.名称 = 'a'";
|
||||
|
||||
String sql = engine.explainToSelectSQL(oql);
|
||||
MappingContext context = new MappingContext();
|
||||
context.setGenerateAlias(true);
|
||||
context.setExplainAllColumnToList(true);
|
||||
String sql = engine.explainToSelectSQL(oql, context);
|
||||
|
||||
String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + //
|
||||
"FROM user_a u\n" + //
|
||||
@@ -56,9 +60,11 @@ public class MySqlMappingShardingTest extends TestCase {
|
||||
public void test_1() throws Exception {
|
||||
String oql = "select * from 用户 u where u.名称 = ?";
|
||||
|
||||
String sql = engine.explainToSelectSQL(oql, Collections.<Object> singletonList("a"));
|
||||
MappingContext context = new MappingContext(Collections.<Object> singletonList("a"));
|
||||
context.setGenerateAlias(true);
|
||||
String sql = engine.explainToSelectSQL(oql, context);
|
||||
|
||||
String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + //
|
||||
String expected = "SELECT *\n" + //
|
||||
"FROM user_a u\n" + //
|
||||
"WHERE u.uid = ?";
|
||||
|
||||
@@ -67,10 +73,12 @@ public class MySqlMappingShardingTest extends TestCase {
|
||||
|
||||
public void test_2() throws Exception {
|
||||
String oql = "select * from 用户 u where u.名称 = ?";
|
||||
MappingContext context = new MappingContext(Collections.<Object> singletonList("b"));
|
||||
context.setExplainAllColumnToList(true);
|
||||
|
||||
String sql = engine.explainToSelectSQL(oql, context);
|
||||
|
||||
String sql = engine.explainToSelectSQL(oql, Collections.<Object> singletonList("b"));
|
||||
|
||||
String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + //
|
||||
String expected = "SELECT uid, name\n" + //
|
||||
"FROM user_x u\n" + //
|
||||
"WHERE u.uid = ?";
|
||||
|
||||
@@ -95,7 +103,7 @@ public class MySqlMappingShardingTest extends TestCase {
|
||||
|
||||
String sql = engine.explainToSelectSQL(oql, Collections.<Object> singletonList("b"));
|
||||
|
||||
String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + //
|
||||
String expected = "SELECT *\n" + //
|
||||
"FROM user_x u\n" + //
|
||||
"WHERE uid = ?";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user