use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class DruidMysqlRouteStrategyTest method testAddLimitToSQL.
public void testAddLimitToSQL() throws Exception {
final SchemaConfig schema = schemaMap.get("TESTDB");
String sql = null;
RouteResultset rrs = null;
sql = "select * from orders";
rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
Assert.assertEquals(true, rrs.isCacheAble());
Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
NodeNameAsserter nameAsserter = new NodeNameAsserter("dn2", "dn1");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
Assert.assertEquals(schema.getDefaultMaxLimit(), rrs.getLimitSize());
// Assert.assertEquals("SELECT * FROM orders LIMIT 100", rrs.getNodes()[0].getStatement());
MySqlStatementParser parser = new MySqlStatementParser("SELECT * FROM orders LIMIT 100");
SQLStatement statement = parser.parseStatement();
Assert.assertEquals(statement.toString(), rrs.getNodes()[0].getStatement());
sql = "select * from goods";
rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
Assert.assertEquals(false, rrs.isCacheAble());
Assert.assertEquals(1, rrs.getNodes().length);
Assert.assertEquals(schema.getDefaultMaxLimit(), rrs.getLimitSize());
// Assert.assertEquals("select * from goods", rrs.getNodes()[0].getStatement());
parser = new MySqlStatementParser("SELECT * FROM goods LIMIT 100");
statement = parser.parseStatement();
Assert.assertEquals(statement.toString(), rrs.getNodes()[0].getStatement());
sql = "select * from goods limit 2 ,3";
rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
Assert.assertEquals(false, rrs.isCacheAble());
Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1, rrs.getLimitSize());
Assert.assertEquals("select * from goods limit 2 ,3", rrs.getNodes()[0].getStatement());
sql = "select * from notpartionTable limit 2 ,3";
rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
Assert.assertEquals(true, rrs.isCacheAble());
Assert.assertEquals(1, rrs.getNodes().length);
Assert.assertEquals(3, rrs.getLimitSize());
Assert.assertEquals("select * from notpartionTable limit 2 ,3", rrs.getNodes()[0].getStatement());
}
use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class DruidMysqlRouteStrategyTest method testERRouteMutiNode.
/**
* testERRouteMutiNode
*
* @throws Exception
*/
@Test
public void testERRouteMutiNode() throws Exception {
SchemaConfig schema = schemaMap.get("TESTDB");
String sql = "select * from orders where customer_id in(1,2000001);";
RouteResultset rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
Assert.assertTrue(rrs.getNodes().length == 2);
Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
}
use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class DruidMysqlRouteStrategyTest method testroute.
public void testroute() throws Exception {
SchemaConfig schema = schemaMap.get("cndb");
String sql = "select * from independent where member='abc'";
RouteResultset rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
Assert.assertEquals(true, rrs.isCacheAble());
Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 128);
IndexedNodeNameAsserter nameAsserter = new IndexedNodeNameAsserter("independent_dn", 0, 128);
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
for (int i = 0; i < 128; ++i) {
sqlAsserter.addExpectSQL(i, "select * from independent where member='abc'");
}
RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
// include database schema ,should remove
sql = "select * from cndb.independent A where a.member='abc'";
schema = schemaMap.get("cndb");
rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
Assert.assertEquals(true, rrs.isCacheAble());
nodeMap = getNodeMap(rrs, 128);
nameAsserter = new IndexedNodeNameAsserter("independent_dn", 0, 128);
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
sqlAsserter = new SimpleSQLAsserter();
for (int i = 0; i < 128; ++i) {
sqlAsserter.addExpectSQL(i, "select * from independent A where a.member='abc'");
}
asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
}
use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class DruidMysqlRouteStrategyTest method testInsertOnDuplicateKey.
/**
* insert ... on duplicate key ... update...
*
* @throws Exception
*/
public void testInsertOnDuplicateKey() throws Exception {
SchemaConfig schema = schemaMap.get("TESTDB");
String sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000) on duplicate key update name='nihao'";
RouteResultset rrs = null;
rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
Assert.assertEquals(1, rrs.getNodes().length);
Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
// insert ... on duplicate key ... update col1 = VALUES(col1),col2 = VALUES(col2)
sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000) " + "on duplicate key update name=VALUES(name),id = VALUES(id)";
rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
Assert.assertEquals(1, rrs.getNodes().length);
Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
// insert ... on duplicate key ,sharding key can't be updated
sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000) " + "on duplicate key update name=VALUES(name),id = VALUES(id),sharding_id = VALUES(sharding_id)";
try {
rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
} catch (Exception e) {
Assert.assertEquals("Sharding column can't be updated: EMPLOYEE -> SHARDING_ID", e.getMessage());
}
}
use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class DruidMysqlRouteStrategyTest method testIgnoreSchema.
public void testIgnoreSchema() throws Exception {
SchemaConfig schema = schemaMap.get("ignoreSchemaTest");
String sql = "select * from offer where offer_id=1";
RouteResultset rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
Assert.assertEquals(false, rrs.isCacheAble());
Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
sql = "select * from ignoreSchemaTest.offer1 where ignoreSchemaTest.offer1.offer_id=1";
rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
Assert.assertEquals(false, rrs.isCacheAble());
Assert.assertEquals("select * from offer1 where offer1.offer_id=1", rrs.getNodes()[0].getStatement());
sql = "select * from ignoreSchemaTest2.offer where ignoreSchemaTest2.offer.offer_id=1";
rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
Assert.assertEquals(false, rrs.isCacheAble());
Assert.assertEquals(sql, rrs.getNodes()[0].getStatement(), sql);
sql = "select * from ignoreSchemaTest2.offer a,offer b where ignoreSchemaTest2.offer.offer_id=1";
rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
Assert.assertEquals(false, rrs.isCacheAble());
Assert.assertEquals("select * from ignoreSchemaTest2.offer a,offer b where ignoreSchemaTest2.offer.offer_id=1", rrs.getNodes()[0].getStatement());
}
Aggregations