Search in sources :

Example 51 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testBetweenExpr.

/**
 * testBetweenExpr
 *
 * @throws Exception
 */
@Test
public void testBetweenExpr() throws Exception {
    // 0-200M=0
    // 200M1-400M=1
    // 400M1-600M=2
    // 600M1-800M=3
    // 800M1-1000M=4
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select * from customer where id between 1 and 5;";
    RouteResultset rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 1);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
    sql = "select * from customer where id between 1 and 2000001;";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 2);
    sql = "select * from customer where id between 2000001 and 3000001;";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 1);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn2"));
    sql = "delete from customer where id between 2000001 and 3000001;";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 1);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn2"));
    sql = "update customer set name='newName' where id between 2000001 and 3000001;";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 1);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn2"));
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Test(org.junit.Test)

Example 52 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testNonPartitionSQL.

public void testNonPartitionSQL() throws Exception {
    SchemaConfig schema = schemaMap.get("cndb");
    String sql = null;
    RouteResultset rrs = null;
    schema = schemaMap.get("dubbo2");
    sql = "SHOW TABLES from db_name like 'solo'";
    rrs = routeStrategy.route(schema, 9, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW TABLES like 'solo'", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("dubbo");
    sql = "SHOW TABLES from db_name like 'solo'";
    rrs = routeStrategy.route(schema, 9, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW TABLES like 'solo'", rrs.getNodes()[0].getStatement());
    sql = "desc cndb.offer";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc cndb.offer", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("cndb");
    sql = "SHOW fulL TaBLES from db_name like 'solo'";
    rrs = routeStrategy.route(schema, 9, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 3);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn0", "offer_dn0", "independent_dn0");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SHOW FULL TABLES like 'solo'").addExpectSQL(1, "SHOW FULL TABLES like 'solo'").addExpectSQL(2, "SHOW FULL TABLES like 'solo'").addExpectSQL(3, "SHOW FULL TABLES like 'solo'");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

Example 53 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testBatchInsert.

/**
 * support insert into ... values (),()...
 * not support insert into ... select...
 *
 * @throws Exception
 */
public void testBatchInsert() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = null;
    // not supprot childtable batch insert
    String sql = "insert into orders (id,name,customer_id) values(1,'testonly',1),(2,'testonly',2000001)";
    try {
        rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    } catch (Exception e) {
        Assert.assertEquals("ChildTable multi insert not provided", e.getMessage());
    }
    sql = "insert into employee (id,name,customer_id) select id,name,customer_id from customer";
    try {
        rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    } catch (Exception e) {
        Assert.assertEquals("TODO:insert into .... select .... not supported!", e.getMessage());
    }
    sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000),(2,'testonly',10010)";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    Assert.assertEquals("dn2", rrs.getNodes()[1].getName());
    String node1Sql = formatSql("insert into employee (id,name,sharding_id) values(1,'testonly',10000)");
    String node2Sql = formatSql("insert into employee (id,name,sharding_id) values(2,'testonly',10010)");
    RouteResultsetNode[] nodes = rrs.getNodes();
    Assert.assertEquals(node1Sql, nodes[0].getStatement());
    Assert.assertEquals(node2Sql, nodes[1].getStatement());
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLNonTransientException(java.sql.SQLNonTransientException)

Example 54 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testGlobalTableOr.

/**
 * testGlobalTableOr
 *
 * @throws Exception
 */
@Test
public void testGlobalTableOr() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select id from company where 1 = 1 and name ='company1' or name = 'company2'";
    for (int i = 0; i < 20; i++) {
        RouteResultset rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
        Assert.assertTrue(rrs.getNodes().length == 1);
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Test(org.junit.Test)

Example 55 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testAlias.

/**
 * testAlias
 *
 * @throws Exception
 */
public void testAlias() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = null;
    // update global table
    String sql = "update company a set name = '' where a.id = 1;";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(3, rrs.getNodes().length);
    // update with alias
    sql = "update travelrecord a set name = '' where a.id = 1;";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    // alias with lowercase
    sql = "select * from travelrecord A where a.id = 1;";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

Aggregations

SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)70 TableConfig (com.actiontech.dble.config.model.TableConfig)16 SQLNonTransientException (java.sql.SQLNonTransientException)16 Test (org.junit.Test)15 RouteResultset (com.actiontech.dble.route.RouteResultset)6 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)5 ServerConfig (com.actiontech.dble.config.ServerConfig)5 UserConfig (com.actiontech.dble.config.model.UserConfig)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (com.actiontech.dble.cache.CacheService)4 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)4 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)4 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)4 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)3 ERTable (com.actiontech.dble.config.model.ERTable)3 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)3