remove mapping

This commit is contained in:
wenshao
2012-11-25 21:23:49 +08:00
parent 0c0bdc5f55
commit 9361ff49f1
23 changed files with 5 additions and 2746 deletions
@@ -1,74 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.mapping;
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;
public class MappingVisitorOracleTest extends TestCase {
MappingEngine engine = new MappingEngine(new OracleMappingProvider());
protected void setUp() throws Exception {
Entity entity = new Entity();
entity.setName("设备");
entity.setDescription("Device");
entity.setTableName("device");
entity.addProperty(new Property("编号", "", "serviceTag"));
entity.addProperty(new Property("IP地址", "", "ip"));
engine.addEntity(entity);
}
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 *", 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'", 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'", context));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM device\nWHERE ip = '127.0.0.1'",
engine.explainToDeleteSQL("WHERE IP地址 = '127.0.0.1'"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE device\nSET serviceTag = ?\nWHERE ip = '127.0.0.1'",
engine.explainToUpdateSQL("SET 编号 = ? WHERE IP地址 = '127.0.0.1'"));
}
public void test_insert() throws Exception {
Assert.assertEquals("INSERT INTO device\n\t(serviceTag, ip)\nVALUES\n(?, ?)",
engine.explainToInsertSQL("(编号, IP地址) VALUES (?, ?)"));
}
}
@@ -1,72 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.mapping;
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;
public class MappingVisitorSQLServerTest extends TestCase {
MappingEngine engine = new MappingEngine(new MSSQLServerMappingProvider());
protected void setUp() throws Exception {
Entity entity = new Entity();
entity.setName("设备");
entity.setDescription("Device");
entity.setTableName("device");
entity.addProperty(new Property("编号", "", "serviceTag"));
entity.addProperty(new Property("IP地址", "", "ip"));
engine.addEntity(entity);
}
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 *", 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'", context));
}
public void test_1() throws Exception {
engine.setMaxLimit(10);
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'", 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'", context));
engine.setMaxLimit(null);
}
}
@@ -1,88 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.mapping;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.mapping.Entity;
import com.alibaba.druid.mapping.MappingEngine;
import com.alibaba.druid.mapping.Property;
public class MappingVisitorTest extends TestCase {
MappingEngine engine = new MappingEngine();
protected void setUp() throws Exception {
Entity entity = new Entity();
entity.setName("设备");
entity.setDescription("Device");
entity.setTableName("device");
entity.addProperty(new Property("编号", "", "serviceTag"));
entity.addProperty(new Property("IP地址", "", "ip"));
engine.addEntity(entity);
}
public void test_0() throws Exception {
Assert.assertEquals("SELECT *\nFROM device",
engine.explainToSelectSQL("select *"));
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 *\nFROM device\nWHERE ip = '127.0.0.1'",
engine.explainToSelectSQL("WHERE IP地址 = '127.0.0.1'"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM device\nWHERE ip = '127.0.0.1'",
engine.explainToDeleteSQL("WHERE IP地址 = '127.0.0.1'"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE device\nSET serviceTag = ?\nWHERE ip = '127.0.0.1'",
engine.explainToUpdateSQL("SET 编号 = ? WHERE IP地址 = '127.0.0.1'"));
}
public void test_insert() throws Exception {
Assert.assertEquals("INSERT INTO device (serviceTag, ip)\nVALUES (?, ?)",
engine.explainToInsertSQL("(编号, IP地址) VALUES (?, ?)"));
}
public void test_1() throws Exception {
engine.setMaxLimit(10);
Assert.assertEquals("SELECT *\nFROM device\nLIMIT 10",
engine.explainToSelectSQL("select *"));
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, 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,
engine.exportParameters(engine.explainToSelectSQLObject("select 编号, IP地址 where 1 = 0")).size());
Assert.assertEquals(1,
engine.exportParameters(engine.explainToSelectSQLObject("select 编号, IP地址 where 1 = 0 and IP地址 = '127.0.0.1'")).size());
Assert.assertEquals("127.0.0.1",
engine.exportParameters(engine.explainToSelectSQLObject("select 编号, IP地址 where 1 = 0 and IP地址 = '127.0.0.1'")).get(0));
engine.setMaxLimit(null);
}
}
@@ -1,82 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.mapping;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mapping.MappingEngine;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.JdbcUtils;
public class MySqlMappingEngineTest extends TestCase {
private DruidDataSource dataSource;
private MappingEngine mapping = new MappingEngine();
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setMinIdle(1);
dataSource.setUrl("jdbc:h2:mem:test;");
dataSource.setTestOnBorrow(false);
JdbcUtils.execute(dataSource, "CREATE TABLE user (id INT, name VARCHAR(40))");
mapping.setDataSource(dataSource);
}
protected void tearDown() throws Exception {
JdbcUtils.execute(dataSource, "DROP TABLE user");
JdbcUtils.close(dataSource);
}
public void test_curd() throws Exception {
{
List<Map<String, Object>> list = mapping.select("select * from user");
Assert.assertEquals(0, list.size());
}
mapping.insert("insert into user (id, name) values (?, ?)", 123, "wenshao");
{
List<Map<String, Object>> list = mapping.select("select * from user");
Assert.assertEquals(1, list.size());
Map<String, Object> data = list.get(0);
Assert.assertEquals(123, data.get("ID"));
Assert.assertEquals("wenshao", data.get("NAME"));
}
{
List<Map<String, Object>> list = mapping.select("select id \"id\", name \"name\" from user");
Assert.assertEquals(1, list.size());
Map<String, Object> data = list.get(0);
Assert.assertEquals(123, data.get("id"));
Assert.assertEquals("wenshao", data.get("name"));
}
mapping.delete("delete from user");
{
List<Map<String, Object>> list = mapping.select("select * from user");
Assert.assertEquals(0, list.size());
}
}
}
@@ -1,168 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.mapping;
import java.util.Collections;
import junit.framework.Assert;
import junit.framework.TestCase;
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.MappingVisitor;
import com.alibaba.druid.mapping.spi.PropertyValue;
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 {
{
Entity entity = new Entity();
entity.setName("用户");
entity.setTableName("user");
entity.addProperty(new Property("名称", "", "uid"));
entity.addProperty(new Property("昵称", "", "name"));
engine.addEntity(entity);
}
{
Entity entity = new Entity();
entity.setName("英雄");
entity.setTableName("hero");
entity.addProperty(new Property("用户名", "", "uid"));
entity.addProperty(new Property("称号", "", "name"));
engine.addEntity(entity);
}
}
public void test_0() throws Exception {
String oql = "select * from 用户 u where u.名称 = 'a'";
MappingContext context = new MappingContext();
context.setGenerateAlias(true);
context.setExplainAllColumnToList(true);
String sql = engine.explainToSelectSQL(oql, context);
String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + //
"FROM user_a u\n" + //
"WHERE u.uid = 'a'";
Assert.assertEquals(expected, sql);
}
public void test_1() throws Exception {
String oql = "select * from 用户 u where u.名称 = ?";
MappingContext context = new MappingContext(Collections.<Object> singletonList("a"));
context.setGenerateAlias(true);
String sql = engine.explainToSelectSQL(oql, context);
String expected = "SELECT *\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.名称 = ?";
MappingContext context = new MappingContext(Collections.<Object> singletonList("b"));
context.setExplainAllColumnToList(true);
String sql = engine.explainToSelectSQL(oql, context);
String expected = "SELECT uid, name\n" + //
"FROM user_x u\n" + //
"WHERE u.uid = ?";
Assert.assertEquals(expected, sql);
}
public void test_3() throws Exception {
String oql = "select h.用户名, u.昵称 from 用户 u left join 英雄 h on h.用户名 = u.名称 where u.名称 = 2";
String sql = engine.explainToSelectSQL(oql, Collections.<Object> singletonList("b"));
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";
System.out.println(sql);
Assert.assertEquals(expected, sql);
}
public void test_4() throws Exception {
String oql = "select * from 用户 u where 名称 = ?";
String sql = engine.explainToSelectSQL(oql, Collections.<Object> singletonList("b"));
String expected = "SELECT *\n" + //
"FROM user_x u\n" + //
"WHERE uid = ?";
Assert.assertEquals(expected, sql);
}
public void test_5() throws Exception {
String oql = "select h.用户名, u.昵称 from 用户 u left join 英雄 h on h.用户名 = u.名称 where 名称 = 2";
String sql = engine.explainToSelectSQL(oql, Collections.<Object> singletonList("b"));
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";
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";
} else {
shardingTableName = "user_x";
}
}
}
for (SQLTableSource tableSource : visitor.getTableSources().values()) {
Entity entity = (Entity) tableSource.getAttribute("mapping.entity");
if (entity != null && "用户".equals(entity.getName())) {
SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
exprTableSource.setExpr(new SQLIdentifierExpr(shardingTableName));
}
}
}
}
}
@@ -1,46 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.mapping;
import junit.framework.TestCase;
import com.alibaba.druid.mapping.Entity;
import com.alibaba.druid.mapping.MappingEngine;
import com.alibaba.druid.mapping.Property;
public class MySqlMappingTest extends TestCase {
MappingEngine engine = new MappingEngine();
protected void setUp() throws Exception {
Entity entity = new Entity();
entity.setName("用户");
entity.setTableName("user");
entity.addProperty(new Property("名称", "", "uid"));
entity.addProperty(new Property("昵称", "", "name"));
engine.addEntity(entity);
}
public void test_0 () throws Exception {
String oql = "select * from 用户 u where u.名称 = 'a'";
String sql = engine.explainToSelectSQL(oql);
System.out.println(sql);
}
}