bug fixed for mapping

This commit is contained in:
温少
2012-05-08 21:47:11 +08:00
parent df18f2a441
commit 57c2af1435
5 changed files with 46 additions and 26 deletions
@@ -42,6 +42,8 @@ public class MappingVisitorUtils {
x.putAttribute("mapping.entity", entity);
String tableName = visitor.resolveTableName(entity);
tableExpr.setName(tableName);
visitor.getTableSources().put(entityName, x);
}
if (x.getAlias() != null) {
@@ -55,7 +57,7 @@ public class MappingVisitorUtils {
if (x.getAlias() != null) {
visitor.getTableSources().put(x.getAlias(), x);
}
return true;
}
@@ -63,12 +65,10 @@ public class MappingVisitorUtils {
Entity entity = visitor.getFirstEntity();
for (Property item : entity.getProperties().values()) {
String alias = null;
SQLExpr expr = new SQLIdentifierExpr(item.getName());
SQLSelectItem selelctItem = new SQLSelectItem(expr);
if (visitor.getContext().isGenerateAlias()) {
alias = '"' + item.getName() + '"';
}
x.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr(item.getName()), alias));
x.getSelectList().add(selelctItem);
}
}
@@ -239,11 +239,19 @@ public class MappingVisitorUtils {
x.putAttribute("mapping.entity", entity);
x.putAttribute("mapping.property", property);
if (visitor.getContext().isGenerateAlias() && x.getParent() instanceof SQLSelectItem) {
SQLSelectItem selectItem = (SQLSelectItem) x.getParent();
if (selectItem.getAlias() == null) {
selectItem.setAlias('"' + property.getName() + '"');
}
}
return true;
}
}
}
return false;
}
@@ -261,7 +269,7 @@ public class MappingVisitorUtils {
x.putAttribute("mapping.entity", entity);
x.putAttribute("mapping.property", property);
if (x.getParent() instanceof SQLSelectItem) {
if (visitor.getContext().isGenerateAlias() && x.getParent() instanceof SQLSelectItem) {
SQLSelectItem selectItem = (SQLSelectItem) x.getParent();
if (selectItem.getAlias() == null) {
selectItem.setAlias('"' + property.getName() + '"');
@@ -5,6 +5,7 @@ import junit.framework.TestCase;
import org.junit.Assert;
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.OracleMappingProvider;
@@ -27,13 +28,16 @@ public class MappingVisitorOracleTest extends TestCase {
}
public void test_0() throws Exception {
MappingContext context = new MappingContext();
context.setGenerateAlias(true);
context.setExplainAllColumnToList(true);
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device",
engine.explainToSelectSQL("select *"));
engine.explainToSelectSQL("select *", context));
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'",
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'"));
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'", context));
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'",
engine.explainToSelectSQL("WHERE IP地址 = '127.0.0.1'"));
engine.explainToSelectSQL("WHERE IP地址 = '127.0.0.1'", context));
}
@@ -5,6 +5,7 @@ import junit.framework.TestCase;
import org.junit.Assert;
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.MSSQLServerMappingProvider;
@@ -26,22 +27,29 @@ public class MappingVisitorSQLServerTest extends TestCase {
}
public void test_0() throws Exception {
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", engine.explainToSelectSQL("select *"));
MappingContext context = new MappingContext();
context.setGenerateAlias(true);
context.setExplainAllColumnToList(true);
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", engine.explainToSelectSQL("select *", context));
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'",
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'"));
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'", context));
}
public void test_1() throws Exception {
engine.setMaxLimit(10);
Assert.assertEquals("SELECT TOP 10 serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", engine.explainToSelectSQL("select *"));
MappingContext context = new MappingContext();
context.setGenerateAlias(true);
context.setExplainAllColumnToList(true);
Assert.assertEquals("SELECT TOP 10 serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", engine.explainToSelectSQL("select *", context));
Assert.assertEquals("SELECT TOP 10 serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'",
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'"));
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'", context));
Assert.assertEquals("SELECT TOP 3 serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'",
engine.explainToSelectSQL("select top 3 编号, IP地址 WHERE IP地址 = '127.0.0.1'"));
engine.explainToSelectSQL("select top 3 编号, IP地址 WHERE IP地址 = '127.0.0.1'", context));
engine.setMaxLimit(null);
@@ -26,11 +26,11 @@ public class MappingVisitorTest extends TestCase {
public void test_0() throws Exception {
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device",
Assert.assertEquals("SELECT *\nFROM device",
engine.explainToSelectSQL("select *"));
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'",
Assert.assertEquals("SELECT serviceTag, ip\nFROM device\nWHERE ip = '127.0.0.1'",
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'"));
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'",
Assert.assertEquals("SELECT *\nFROM device\nWHERE ip = '127.0.0.1'",
engine.explainToSelectSQL("WHERE IP地址 = '127.0.0.1'"));
}
@@ -53,11 +53,11 @@ public class MappingVisitorTest extends TestCase {
public void test_1() throws Exception {
engine.setMaxLimit(10);
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nLIMIT 10",
Assert.assertEquals("SELECT *\nFROM device\nLIMIT 10",
engine.explainToSelectSQL("select *"));
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'\nLIMIT 10",
Assert.assertEquals("SELECT serviceTag, ip\nFROM device\nWHERE ip = '127.0.0.1'\nLIMIT 10",
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'"));
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'\nLIMIT 3",
Assert.assertEquals("SELECT serviceTag, ip\nFROM device\nWHERE ip = '127.0.0.1'\nLIMIT 3",
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1' limit 3"));
Assert.assertEquals(0, engine.exportParameters(engine.explainToSelectSQLObject("select 1")).size());
Assert.assertEquals(0,
@@ -90,7 +90,7 @@ public class MySqlMappingShardingTest extends TestCase {
String sql = engine.explainToSelectSQL(oql, Collections.<Object> singletonList("b"));
String expected = "SELECT h.uid AS \"用户名\", u.name AS \"昵称\"\n" + //
String expected = "SELECT h.uid, u.name\n" + //
"FROM user_x u LEFT JOIN hero h ON h.uid = u.uid\n" + //
"WHERE u.uid = 2";
@@ -115,7 +115,7 @@ public class MySqlMappingShardingTest extends TestCase {
String sql = engine.explainToSelectSQL(oql, Collections.<Object> singletonList("b"));
String expected = "SELECT h.uid AS \"用户名\", u.name AS \"昵称\"\n" + //
String expected = "SELECT h.uid, u.name\n" + //
"FROM user_x u LEFT JOIN hero h ON h.uid = u.uid\n" + //
"WHERE uid = 2";