Search in sources :

Example 6 with SystemConfig

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

the class DruidDb2SqlParserTest method testLimitToDb2Page.

@Test
public void testLimitToDb2Page() throws SQLNonTransientException {
    String sql = "select * from offer order by id desc limit 5,10";
    SchemaConfig schema = schemaMap.get("db2db");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    Assert.assertEquals(5, rrs.getLimitStart());
    Assert.assertEquals(10, rrs.getLimitSize());
    Assert.assertEquals(0, rrs.getNodes()[0].getLimitStart());
    Assert.assertEquals(15, rrs.getNodes()[0].getLimitSize());
    Assert.assertEquals("db2_1", rrs.getNodes()[0].getName());
    Assert.assertEquals("db2_2", rrs.getNodes()[1].getName());
    sql = rrs.getNodes()[0].getStatement();
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(0, rrs.getNodes()[0].getLimitStart());
    Assert.assertEquals(15, rrs.getNodes()[0].getLimitSize());
    Assert.assertEquals(0, rrs.getLimitStart());
    Assert.assertEquals(15, rrs.getLimitSize());
    sql = "select * from offer1 order by id desc limit 5,10";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(5, rrs.getLimitStart());
    Assert.assertEquals(10, rrs.getLimitSize());
    Assert.assertEquals(5, rrs.getNodes()[0].getLimitStart());
    Assert.assertEquals(10, rrs.getNodes()[0].getLimitSize());
    Assert.assertEquals("db2_1", rrs.getNodes()[0].getName());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) Test(org.junit.Test)

Example 7 with SystemConfig

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

the class DruidMysqlCreateTableTest method testCreate.

@Test
public void testCreate() throws SQLNonTransientException {
    SchemaConfig schema = schemaMap.get("mysqldb");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, originSql1, null, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    String sql = rrs.getNodes()[0].getStatement();
    Assert.assertTrue(parseSql(sql));
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) Test(org.junit.Test)

Example 8 with SystemConfig

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

the class DruidMysqlHavingTest method testHaving.

@Test
public void testHaving() throws SQLNonTransientException {
    String sql = "select avg(offer_id) avgofferid, member_id from offer_detail group by member_id having avgofferid > 100";
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getSqlMerge().getHavingColsName().length);
    sql = "select avg(offer_id) avgofferid, member_id from offer_detail group by member_id having avg(offer_id) > 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getSqlMerge().getHavingColsName().length);
    sql = "select count(offer_id) countofferid, member_id from offer_detail group by member_id having countofferid > 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getSqlMerge().getHavingColsName().length);
    sql = "select count(offer_id) countofferid, member_id from offer_detail group by member_id having count(offer_id) > 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getSqlMerge().getHavingColsName().length);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) Test(org.junit.Test)

Example 9 with SystemConfig

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

the class DruidMysqlRouteStrategyTest method testGroupLimit.

public void testGroupLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = null;
    RouteResultset rrs = null;
    sql = "select count(*) from (select * from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select count(*) from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select * from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select * from (select count(*) from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 10 with SystemConfig

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

the class DDLRouteTest method testSpecialCharDDL.

@Test
public void testSpecialCharDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // alter table test
    String sql = " ALTER TABLE COMPANY\r\nADD COLUMN TEST  VARCHAR(255) NULL AFTER CREATE_DATE,\r\n CHARACTER SET = UTF8";
    sql = RouterUtil.getFixedSql(sql);
    List<String> dataNodes = new ArrayList<>();
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(sql, 0));
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Aggregations

SystemConfig (io.mycat.config.model.SystemConfig)59 SchemaConfig (io.mycat.config.model.SchemaConfig)54 RouteResultset (io.mycat.route.RouteResultset)32 Test (org.junit.Test)29 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 CacheService (io.mycat.cache.CacheService)5 SQLNonTransientException (java.sql.SQLNonTransientException)5 NoSuchElementException (java.util.NoSuchElementException)5 RouteService (io.mycat.route.RouteService)4 ArrayList (java.util.ArrayList)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 TableConfig (io.mycat.config.model.TableConfig)3 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)1 DirectByteBufferPool (io.mycat.buffer.DirectByteBufferPool)1 MycatConfig (io.mycat.config.MycatConfig)1 ManagerConnectionFactory (io.mycat.manager.ManagerConnectionFactory)1 MyCatMemory (io.mycat.memory.MyCatMemory)1 AIOAcceptor (io.mycat.net.AIOAcceptor)1 AIOConnector (io.mycat.net.AIOConnector)1