From 1c23d323b7cea0023644772b14e4e0e0d5f7a259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E5=B0=91?= Date: Mon, 12 Mar 2012 16:47:22 +0800 Subject: [PATCH] refactor --- .../mapping/EntityMapingMySqlVisitor.java | 150 --------------- .../alibaba/druid/mapping/MappingEngine.java | 4 +- .../druid/mapping/MySqlMappingVisitor.java | 179 ++++++++++++++++++ 3 files changed, 181 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/com/alibaba/druid/mapping/EntityMapingMySqlVisitor.java create mode 100644 src/main/java/com/alibaba/druid/mapping/MySqlMappingVisitor.java diff --git a/src/main/java/com/alibaba/druid/mapping/EntityMapingMySqlVisitor.java b/src/main/java/com/alibaba/druid/mapping/EntityMapingMySqlVisitor.java deleted file mode 100644 index dcf6ac7aef..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/EntityMapingMySqlVisitor.java +++ /dev/null @@ -1,150 +0,0 @@ -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 LinkedHashMap entities; - - public EntityMapingMySqlVisitor(LinkedHashMap entities){ - this.entities = entities; - } - - public Map getEntities() { - return entities; - } - - public Entity getFirstEntity() { - for (Map.Entry 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 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; - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/MappingEngine.java b/src/main/java/com/alibaba/druid/mapping/MappingEngine.java index b136315ae2..b3567dc234 100644 --- a/src/main/java/com/alibaba/druid/mapping/MappingEngine.java +++ b/src/main/java/com/alibaba/druid/mapping/MappingEngine.java @@ -29,8 +29,8 @@ public class MappingEngine { this.entities.put(entity.getName(), entity); } - public EntityMapingMySqlVisitor createMappingVisitor() { - return new EntityMapingMySqlVisitor(entities); + public MySqlMappingVisitor createMappingVisitor() { + return new MySqlMappingVisitor(entities); } public String explain(String sql) { diff --git a/src/main/java/com/alibaba/druid/mapping/MySqlMappingVisitor.java b/src/main/java/com/alibaba/druid/mapping/MySqlMappingVisitor.java new file mode 100644 index 0000000000..a83762e9ef --- /dev/null +++ b/src/main/java/com/alibaba/druid/mapping/MySqlMappingVisitor.java @@ -0,0 +1,179 @@ +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.SQLJoinTableSource; +import com.alibaba.druid.sql.ast.statement.SQLSelectItem; +import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; +import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource; +import com.alibaba.druid.sql.ast.statement.SQLTableSource; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter; + +public class MySqlMappingVisitor extends MySqlASTVisitorAdapter { + + private final LinkedHashMap entities; + private final Map tableSources = new LinkedHashMap(); + + public MySqlMappingVisitor(LinkedHashMap entities) { + this.entities = entities; + } + + public Map getEntities() { + return entities; + } + + public Entity getFirstEntity() { + for (Map.Entry 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 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(SQLSubqueryTableSource x) { + if (x.getAlias() != null) { + tableSources.put(x.getAlias(), x); + } + + return true; + } + + public boolean visit(SQLJoinTableSource x) { + if (x.getAlias() != null) { + tableSources.put(x.getAlias(), x); + } + + return true; + } + + 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()); + } + + if (x.getAlias() != null) { + tableSources.put(x.getAlias(), x); + } + + return false; + } +}