This commit is contained in:
温少
2012-05-06 22:20:18 +08:00
parent f184715d0e
commit b0f6002f8a
@@ -1,5 +1,6 @@
package com.alibaba.druid.bvt.mapping;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mapping.Entity;
@@ -11,8 +12,8 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
public class MySqlMappingShardingTest extends TestCase {
MappingEngine engine = new ShardingMappingEngine();
protected void setUp() throws Exception {
@@ -25,23 +26,28 @@ public class MySqlMappingShardingTest extends TestCase {
engine.addEntity(entity);
}
public void test_0 () throws Exception {
public void test_0() throws Exception {
String oql = "select * from 用户 u where u.名称 = 'a'";
String sql = engine.explainToSelectSQL(oql);
String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + //
"FROM user_a u\n" + //
"WHERE u.uid = 'a'";
System.out.println(sql);
Assert.assertEquals(expected, sql);
}
public static class ShardingMappingEngine extends MappingEngine {
public void afterResole(MappingVisitor visitor) {
String shardingTableName = null;
for (PropertyValue entry : visitor.getPropertyValues()) {
Entity entity = entry.getEntity();
Property property = entry.getProperty();
Object value = entry.getValue();
if ("用户".equals(entity.getName()) && "名称".equals(property.getName())) {
if ("a".equals(value)) {
shardingTableName = "user_a";
@@ -50,7 +56,7 @@ public class MySqlMappingShardingTest extends TestCase {
}
}
}
for (SQLTableSource tableSource : visitor.getTableSources().values()) {
Entity entity = (Entity) tableSource.getAttribute("mapping.entity");
if (entity != null && "用户".equals(entity.getName())) {