enhance mapping

This commit is contained in:
温少
2012-05-10 11:15:21 +08:00
5 changed files with 28 additions and 17 deletions
@@ -38,7 +38,13 @@ public class MappingEngine {
this.provider = provider;
}
public Entity getFirstEntity() {
public Entity getFirstEntity(MappingContext context) {
Entity entity = context.getDefaultEntity();
if (entity != null) {
return entity;
}
for (Map.Entry<String, Entity> entry : entities.entrySet()) {
return entry.getValue();
}
@@ -110,6 +116,10 @@ public class MappingEngine {
return toSQL(stmt);
}
public SQLSelectQueryBlock explainToSelectSQLObject(String sql) {
return explainToSelectSQLObject(sql, new MappingContext());
}
public SQLSelectQueryBlock explainToSelectSQLObject(String sql, MappingContext context) {
return provider.explainToSelectSQLObject(this, sql, context);
@@ -64,7 +64,7 @@ public class MSSQLServerMappingProvider implements MappingProvider {
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLDeleteStatement stmt = parser.parseDeleteStatement();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}
@@ -73,7 +73,7 @@ public class MSSQLServerMappingProvider implements MappingProvider {
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLUpdateStatement stmt = parser.parseUpdateStatement();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}
@@ -82,7 +82,7 @@ public class MSSQLServerMappingProvider implements MappingProvider {
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLInsertStatement stmt = (SQLInsertStatement) parser.parseInsert();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}
@@ -5,6 +5,7 @@ import java.util.Map;
import com.alibaba.druid.mapping.DruidMappingException;
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.sql.ast.SQLExpr;
@@ -410,7 +411,7 @@ public class MappingVisitorUtils {
}
if (x.getFrom() == null) {
Entity firstEntity = visitor.getEngine().getFirstEntity();
Entity firstEntity = visitor.getEngine().getFirstEntity(visitor.getContext());
SQLExprTableSource from = new SQLExprTableSource(new SQLIdentifierExpr(firstEntity.getName()));
from.putAttribute(MAPPING_ENTITY, firstEntity);
x.setFrom(from);
@@ -470,23 +471,23 @@ public class MappingVisitorUtils {
return entity;
}
public static void setTableSource(MappingEngine engine, SQLDeleteStatement stmt) {
public static void setTableSource(MappingEngine engine, SQLDeleteStatement stmt, MappingContext context) {
if (stmt.getExprTableSource() == null) {
Entity entity = engine.getFirstEntity();
Entity entity = engine.getFirstEntity(context);
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
}
}
public static void setTableSource(MappingEngine engine, SQLUpdateStatement stmt) {
public static void setTableSource(MappingEngine engine, SQLUpdateStatement stmt, MappingContext context) {
if (stmt.getTableSource() == null) {
Entity entity = engine.getFirstEntity();
Entity entity = engine.getFirstEntity(context);
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
}
}
public static void setTableSource(MappingEngine engine, SQLInsertStatement stmt) {
public static void setTableSource(MappingEngine engine, SQLInsertStatement stmt, MappingContext context) {
if (stmt.getTableSource() == null) {
Entity entity = engine.getFirstEntity();
Entity entity = engine.getFirstEntity(context);
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
}
}
@@ -64,7 +64,7 @@ public class MySqlMappingProvider implements MappingProvider {
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlDeleteStatement stmt = parser.parseDeleteStatement();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}
@@ -73,7 +73,7 @@ public class MySqlMappingProvider implements MappingProvider {
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLUpdateStatement stmt = parser.parseUpdateStatement();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}
@@ -82,7 +82,7 @@ public class MySqlMappingProvider implements MappingProvider {
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLInsertStatement stmt = (SQLInsertStatement) parser.parseInsert();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}
@@ -45,7 +45,7 @@ public class OracleMappingProvider implements MappingProvider {
OracleStatementParser parser = new OracleStatementParser(sql);
OracleDeleteStatement stmt = parser.parseDeleteStatement();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}
@@ -54,7 +54,7 @@ public class OracleMappingProvider implements MappingProvider {
OracleStatementParser parser = new OracleStatementParser(sql);
SQLUpdateStatement stmt = parser.parseUpdateStatement();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}
@@ -62,7 +62,7 @@ public class OracleMappingProvider implements MappingProvider {
public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql, MappingContext context) {
OracleStatementParser parser = new OracleStatementParser(sql);
SQLInsertStatement stmt = (SQLInsertStatement) parser.parseInsert();
MappingVisitorUtils.setTableSource(engine, stmt);
MappingVisitorUtils.setTableSource(engine, stmt, context);
return stmt;
}