mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add test
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.alibaba.druid.bvt.mapping;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
@@ -11,8 +14,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 +28,52 @@ 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 void test_1() throws Exception {
|
||||
String oql = "select * from 用户 u where u.名称 = ?";
|
||||
|
||||
String sql = engine.explainToSelectSQL(oql, Collections.<Object>singletonList("a"));
|
||||
|
||||
String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + //
|
||||
"FROM user_a u\n" + //
|
||||
"WHERE u.uid = ?";
|
||||
|
||||
Assert.assertEquals(expected, sql);
|
||||
}
|
||||
|
||||
public void test_2() throws Exception {
|
||||
String oql = "select * from 用户 u where u.名称 = ?";
|
||||
|
||||
String sql = engine.explainToSelectSQL(oql, Collections.<Object>singletonList("b"));
|
||||
|
||||
String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + //
|
||||
"FROM user_x u\n" + //
|
||||
"WHERE u.uid = ?";
|
||||
|
||||
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 +82,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())) {
|
||||
|
||||
Reference in New Issue
Block a user