Search in sources :

Example 91 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testRouteMultiTables.

public void testRouteMultiTables() throws Exception {
    // company is global table ,route to 3 datanode and ignored in route
    String sql = "select * from company,customer ,orders where customer.company_id=company.id and orders.customer_id=customer.id and company.name like 'aaa' limit 10";
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    Assert.assertEquals(true, rrs.isCacheAble());
    Assert.assertEquals(10, rrs.getLimitSize());
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    Assert.assertEquals("dn2", rrs.getNodes()[1].getName());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset)

Example 92 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testAggregateExpr.

/**
 * 测试函数COUNT
 *
 * @throws Exception
 */
@Test
public void testAggregateExpr() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select id, name, count(name) from employee group by name;";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertTrue(rrs.getMergeCols().containsKey("COUNT2"));
    sql = "select id, name, count(name) as c from employee group by name;";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertTrue(rrs.getMergeCols().containsKey("c"));
    sql = "select id, name, count(name) c from employee group by name;";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertTrue(rrs.getMergeCols().containsKey("c"));
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 93 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testBetweenExpr.

/**
 * 测试between语句的路由
 *
 * @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(new SystemConfig(), schema, ServerParse.SELECT, sql, null, 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(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 2);
    sql = "select * from customer where id between 2000001 and 3000001;";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, 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(new SystemConfig(), schema, ServerParse.SELECT, sql, null, 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(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 1);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn2"));
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 94 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testConfigSchema.

public void testConfigSchema() throws Exception {
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer where offer_id=1";
        routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
        Assert.assertFalse(true);
    } catch (Exception e) {
        Assert.assertEquals("route rule for table OFFER is required: select * from offer where offer_id=1", e.getMessage());
    }
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer where col11111=1";
        routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
        Assert.assertFalse(true);
    } catch (Exception e) {
    }
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer ";
        routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
        Assert.assertFalse(true);
    } catch (Exception e) {
    }
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) SQLNonTransientException(java.sql.SQLNonTransientException) NoSuchElementException(java.util.NoSuchElementException)

Example 95 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testSelectNoTable.

/**
 * select 1
 * select 1 union all select 2
 *
 * @throws Exception
 */
public void testSelectNoTable() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select 1";
    RouteResultset rrs = null;
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    sql = "select 1 union select 2";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

SystemConfig (io.mycat.config.model.SystemConfig)120 SchemaConfig (io.mycat.config.model.SchemaConfig)110 RouteResultset (io.mycat.route.RouteResultset)64 Test (org.junit.Test)60 RouteResultsetNode (io.mycat.route.RouteResultsetNode)14 CacheService (io.mycat.cache.CacheService)10 SQLNonTransientException (java.sql.SQLNonTransientException)10 NoSuchElementException (java.util.NoSuchElementException)10 RouteService (io.mycat.route.RouteService)8 ArrayList (java.util.ArrayList)8 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)6 TableConfig (io.mycat.config.model.TableConfig)6 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)2 DirectByteBufferPool (io.mycat.buffer.DirectByteBufferPool)2 NettyBufferPool (io.mycat.buffer.NettyBufferPool)2 MycatConfig (io.mycat.config.MycatConfig)2 ManagerConnectionFactory (io.mycat.manager.ManagerConnectionFactory)2 MyCatMemory (io.mycat.memory.MyCatMemory)2 AIOAcceptor (io.mycat.net.AIOAcceptor)2